+ info(1);
+ }
+ if (signo)
+ exit(0);
+}
+
+static void
+info(register int verbose)
+{
+ struct pcap_stat stat;
+
+ if (pcap_stats(pd, &stat) < 0) {
+ (void)fprintf(stderr, "pcap_stats: %s\n", pcap_geterr(pd));
+ return;
+ }
+
+ if (!verbose)
+ fprintf(stderr, "%s: ", program_name);
+
+ (void)fprintf(stderr, "%d packets received by filter", stat.ps_recv);
+ if (!verbose)
+ fputs(", ", stderr);
+ else
+ putc('\n', stderr);
+ (void)fprintf(stderr, "%d packets dropped by kernel\n", stat.ps_drop);
+ infoprint = 0;
+}
+
+static void
+reverse(char *s)
+{
+ int i, j, c;
+
+ for (i = 0, j = strlen(s) - 1; i < j; i++, j--) {
+ c = s[i];
+ s[i] = s[j];
+ s[j] = c;
+ }
+}
+
+
+static void
+swebitoa(unsigned int n, char *s)
+{
+ unsigned int i;
+
+ i = 0;
+ do {
+ s[i++] = n % 10 + '0';
+ } while ((n /= 10) > 0);
+
+ s[i] = '\0';
+ reverse(s);
+}
+
+static void
+dump_packet_and_trunc(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
+{
+ struct dump_info *dump_info;
+ static uint cnt = 2;
+ char *name;
+
+ ++infodelay;
+
+ dump_info = (struct dump_info *)user;
+
+ /*
+ * XXX - this won't prevent capture files from getting
+ * larger than Cflag - the last packet written to the
+ * file could put it over Cflag.
+ */
+ if (ftell((FILE *)dump_info->p) > Cflag) {
+ name = (char *) malloc(strlen(dump_info->WFileName) + 4);
+ if (name == NULL)
+ error("dump_packet_and_trunc: malloc");
+ strcpy(name, dump_info->WFileName);
+ swebitoa(cnt, name + strlen(dump_info->WFileName));
+ cnt++;
+ pcap_dump_close(dump_info->p);
+ dump_info->p = pcap_dump_open(dump_info->pd, name);
+ free(name);
+ if (dump_info->p == NULL)
+ error("%s", pcap_geterr(pd));
+ }
+
+ pcap_dump((u_char *)dump_info->p, h, sp);
+
+ --infodelay;
+ if (infoprint)
+ info(0);
+}
+
+static void
+dump_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
+{
+ ++infodelay;
+
+ pcap_dump(user, h, sp);
+
+ --infodelay;
+ if (infoprint)
+ info(0);
+}
+
+static void
+print_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
+{
+ struct print_info *print_info;
+ u_int hdrlen;
+
+ ++infodelay;
+ ts_print(&h->ts);
+
+ print_info = (struct print_info *)user;
+
+ /*
+ * Some printers want to check that they're not walking off the
+ * end of the packet.
+ * Rather than pass it all the way down, we set this global.
+ */
+ snapend = sp + h->caplen;
+
+ hdrlen = (*print_info->printer)(h, sp);
+ if (xflag) {
+ /*
+ * Print the raw packet data.
+ */
+ if (xflag > 1) {
+ /*
+ * Include the link-layer header.
+ */
+ default_print_unaligned(sp, h->caplen);
+ } else {
+ /*
+ * Don't include the link-layer header - and if
+ * we have nothing past the link-layer header,
+ * print nothing.
+ */
+ if (h->caplen > hdrlen)
+ default_print_unaligned(sp + hdrlen,
+ h->caplen - hdrlen);