diff options
author | Guy Harris <guy@alum.mit.edu> | 2005-01-28 11:31:19 +0000 |
---|---|---|
committer | Guy Harris <guy@alum.mit.edu> | 2005-01-28 11:31:19 +0000 |
commit | c29e427533609aaabab1853056cf324eceadcb23 (patch) | |
tree | e87ec0bffa9b93da7a64f73ee00a10bee631b347 /wiretap | |
parent | d90ba1a144f8f56cacf1ae9ae22f0f054cd42e1d (diff) | |
download | wireshark-c29e427533609aaabab1853056cf324eceadcb23.tar.gz wireshark-c29e427533609aaabab1853056cf324eceadcb23.tar.bz2 wireshark-c29e427533609aaabab1853056cf324eceadcb23.zip |
From Yoshihiro Oyama: support "-" as a file name, referring to the
standard input. Opening it for random access isn't supported; we add a
new error for that.
svn path=/trunk/; revision=13189
Diffstat (limited to 'wiretap')
-rw-r--r-- | wiretap/AUTHORS | 1 | ||||
-rw-r--r-- | wiretap/file_access.c | 44 | ||||
-rw-r--r-- | wiretap/wtap.c | 1 | ||||
-rw-r--r-- | wiretap/wtap.h | 2 |
4 files changed, 44 insertions, 4 deletions
diff --git a/wiretap/AUTHORS b/wiretap/AUTHORS index 142488b6c0..e684a11c20 100644 --- a/wiretap/AUTHORS +++ b/wiretap/AUTHORS @@ -27,3 +27,4 @@ Niels Koot <Niels.Koot[AT]logicacmg.com> Rolf Fiedler <Rolf.Fiedler[AT]Innoventif.com> James Fields <jvfields [AT] tds.net> Kevin Johnson <kjohnson [AT] secureideas.net> +Yoshihiro Oyama <y.oyama [AT] netagent.co.jp> diff --git a/wiretap/file_access.c b/wiretap/file_access.c index f94add63d5..6812938ddb 100644 --- a/wiretap/file_access.c +++ b/wiretap/file_access.c @@ -164,11 +164,23 @@ wtap* wtap_open_offline(const char *filename, int *err, char **err_info, struct stat statb; wtap *wth; unsigned int i; + gboolean use_stdin = FALSE; + + /* open standard input if filename is '-' */ + if (strcmp(filename, "-") == 0) + use_stdin = TRUE; /* First, make sure the file is valid */ - if (stat(filename, &statb) < 0) { - *err = errno; - return NULL; + if (use_stdin) { + if (fstat(0, &statb) < 0) { + *err = errno; + return NULL; + } + } else { + if (stat(filename, &statb) < 0) { + *err = errno; + return NULL; + } } if (S_ISFIFO(statb.st_mode)) { /* @@ -200,6 +212,18 @@ wtap* wtap_open_offline(const char *filename, int *err, char **err_info, return NULL; } + /* + * We need two independent descriptors for random access, so + * they have different file positions. If we're opening the + * standard input, we can only dup it to get additional + * descriptors, so we can't have two independent descriptors, + * and thus can't do random access. + */ + if (use_stdin && do_random) { + *err = WTAP_ERR_RANDOM_OPEN_STDIN; + return NULL; + } + errno = ENOMEM; wth = g_malloc(sizeof(wtap)); if (wth == NULL) { @@ -214,7 +238,18 @@ wtap* wtap_open_offline(const char *filename, int *err, char **err_info, /* Open the file */ errno = WTAP_ERR_CANT_OPEN; - wth->fd = open(filename, O_RDONLY|O_BINARY); + if (use_stdin) { + /* + * We dup FD 0, so that we don't have to worry about + * an fclose or gzclose of wth->fh closing the standard + * input of the process. + */ + wth->fd = dup(0); +#ifdef _WIN32 + _setmode(wth->fd, O_BINARY); +#endif + } else + wth->fd = open(filename, O_RDONLY|O_BINARY); if (wth->fd < 0) { *err = errno; g_free(wth); @@ -222,6 +257,7 @@ wtap* wtap_open_offline(const char *filename, int *err, char **err_info, } if (!(wth->fh = filed_open(wth->fd, "rb"))) { *err = errno; + close(wth->fd); g_free(wth); return NULL; } diff --git a/wiretap/wtap.c b/wiretap/wtap.c index d2841a4ef9..c203d28687 100644 --- a/wiretap/wtap.c +++ b/wiretap/wtap.c @@ -320,6 +320,7 @@ static const char *wtap_errlist[] = { "Uncompression error: data oddly truncated", "Uncompression error: data would overflow buffer", "Uncompression error: bad LZ77 offset", + "The standard input cannot be opened for random access", }; #define WTAP_ERRLIST_SIZE (sizeof wtap_errlist / sizeof wtap_errlist[0]) diff --git a/wiretap/wtap.h b/wiretap/wtap.h index 6952ba1edc..b551d51b2a 100644 --- a/wiretap/wtap.h +++ b/wiretap/wtap.h @@ -545,6 +545,8 @@ void wtap_set_bytes_dumped(wtap_dumper *wdh, long bytes_dumped); /* Uncompressing Sniffer data would overflow buffer */ #define WTAP_ERR_UNC_BAD_OFFSET -17 /* LZ77 compressed data has bad offset to string */ +#define WTAP_ERR_RANDOM_OPEN_STDIN -18 + /* We're trying to open the standard input for random access */ /* Errors from zlib; zlib error Z_xxx turns into Wiretap error WTAP_ERR_ZLIB + Z_xxx. |