I feel it's high time we cleanup some of the sources. I'd start with savefile.c. Currently it doesn't work for offline data from stdin.
--gv
--- libpcap-2004.05.20/savefile.c Tue Mar 23 21:18:08 2004 +++ savefile.c Wed Mar 24 16:29:06 2004 @@ -52,6 +52,12 @@ #define TCPDUMP_MAGIC 0xa1b2c3d4 #define PATCHED_TCPDUMP_MAGIC 0xa1b2cd34 +#if defined(WIN32) || defined(MSDOS) +#define SETMODE(file,mode) setmode(file,mode) +#else +#define SETMODE(file,mode) ((void)0) +#endif + /* * We use the "receiver-makes-right" approach to byte order, * because time is at a premium when we are writing the file. @@ -587,6 +593,7 @@ { if (p->sf.rfile != stdin) (void)fclose(p->sf.rfile); + else SETMODE (fileno(stdin),O_TEXT); if (p->sf.base != NULL) free(p->sf.base); } @@ -607,15 +614,12 @@ } memset((char *)p, 0, sizeof(*p)); - - if (fname[0] == '-' && fname[1] == '\0') + if (fname[0] == '-' && fname[1] == '\0') { fp = stdin; + SETMODE(fileno(fp), O_BINARY); + } else { -#ifndef WIN32 - fp = fopen(fname, "r"); -#else fp = fopen(fname, "rb"); -#endif if (fp == NULL) { snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: %s", fname, pcap_strerror(errno)); @@ -726,13 +730,15 @@ break; } -#ifndef WIN32 +#if !defined(WIN32) && !defined(MSDOS) /* * You can do "select()" and "poll()" on plain files on most * platforms, and should be able to do so on pipes. * * You can't do "select()" on anything other than sockets in * Windows, so, on Win32 systems, we don't have "selectable_fd". + * But one could use 'WaitForSingleObject()' on HANDLE obtained + * from '_get_osfhandle(p->selectable_fd)'. */ p->selectable_fd = fileno(fp); #endif @@ -748,8 +754,10 @@ return (p); bad: - if(fp) + if(fp && fp != stdin) fclose(fp); + if (fp == stdin) + SETMODE (fileno(stdin),O_TEXT); free(p); return (NULL); } @@ -973,6 +981,7 @@ pcap_dump_open(pcap_t *p, const char *fname) { FILE *f; + pcap_dumper_t *pd; int linktype; linktype = dlt_to_linktype(p->linktype); @@ -985,26 +994,23 @@ if (fname[0] == '-' && fname[1] == '\0') { f = stdout; -#ifdef WIN32 - _setmode(_fileno(f), _O_BINARY); -#endif + SETMODE(fileno(f), O_BINARY); } else { -#ifndef WIN32 - f = fopen(fname, "w"); -#else f = fopen(fname, "wb"); -#endif - if (f == NULL) { + setbuf(f, NULL); /* XXX - why? */ + } + + pd = (pcap_dumper_t*)f; + + if (!pd || sf_write_header(f, linktype, p->tzoff, p->snapshot) < 0) { snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "%s: %s", fname, pcap_strerror(errno)); - return (NULL); + if (pd) + pcap_dump_close(pd); + pd = NULL; + f = NULL; } -#ifdef WIN32 - setbuf(f, NULL); /* XXX - why? */ -#endif - } - (void)sf_write_header(f, linktype, p->tzoff, p->snapshot); - return ((pcap_dumper_t *)f); + return (pd); } FILE * @@ -1026,11 +1032,15 @@ void pcap_dump_close(pcap_dumper_t *p) { + FILE *fil = (FILE*)p; #ifdef notyet - if (ferror((FILE *)p)) + if (ferror(fil)) return-an-error; /* XXX should check return from fclose() too */ #endif - (void)fclose((FILE *)p); + if (fil == stdin || fil == stdout) + SETMODE (fileno(fil),O_TEXT); + else + fclose (fil); }
- This is the tcpdump-workers list. Visit https://lists.sandelman.ca/ to unsubscribe.