The DBS Etherwatch file handler does look for a magic number (the word
[obnox/wireshark/wip.git] / wiretap / file_access.c
index 31118a229d82006211b8e99357eb528875b89ebb..26f782e593d1281168977440ba3cf46b7c6276a4 100644 (file)
@@ -1,6 +1,6 @@
 /* file_access.c
  *
- * $Id: file_access.c,v 1.11 2004/02/11 20:05:16 guy Exp $
+ * $Id$
  *
  * Wiretap Library
  * Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.edu>
@@ -109,6 +109,7 @@ static int (*const open_routines[])(wtap *, int *, char **) = {
        _5views_open,
        network_instruments_open,
        airopeek9_open,
+       dbs_etherwatch_open,
 
        /* Files that don't have magic bytes at a fixed location,
         * but that instead require a heuristic of some sort to
@@ -124,7 +125,6 @@ static int (*const open_routines[])(wtap *, int *, char **) = {
        i4btrace_open,
        csids_open,
        vms_open,
-       dbs_etherwatch_open,
        cosine_open,
        erf_open,
        hcidump_open,
@@ -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;
        }
@@ -394,8 +430,8 @@ static const struct file_type_info {
          NULL, NULL },
 
        /* WTAP_FILE_NETTL */
-       { "HP-UX nettl trace", NULL,
-         NULL, NULL },
+       { "HP-UX nettl trace", "nettl",
+         nettl_dump_can_write_encap, nettl_dump_open },
 
        /* WTAP_FILE_TOSHIBA */
        { "Toshiba Compact ISDN Router snoop trace", NULL,
@@ -537,9 +573,12 @@ wtap_dumper* wtap_dump_open(const char *filename, int filetype, int encap,
                return NULL;    /* couldn't allocate it */
 
        /* Empty filename means stdout */
-       if (*filename == '\0')
+       if (*filename == '\0') {
+#ifdef _WIN32
+               setmode(fileno(stdout), O_BINARY);
+#endif
                wdh->fh = stdout;
-       else {
+       else {
                /* In case "fopen()" fails but doesn't set "errno", set "errno"
                   to a generic "the open failed" error. */
                errno = WTAP_ERR_CANT_OPEN;