/* 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>
_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
i4btrace_open,
csids_open,
vms_open,
- dbs_etherwatch_open,
cosine_open,
erf_open,
hcidump_open,
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)) {
/*
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) {
/* 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);
}
if (!(wth->fh = filed_open(wth->fd, "rb"))) {
*err = errno;
+ close(wth->fd);
g_free(wth);
return NULL;
}
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,
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;