]> The Tcpdump Group git mirrors - tcpdump/blob - util.c
Squelch a Coverity warning.
[tcpdump] / util.c
1 /*
2 * Copyright (c) 1990, 1991, 1993, 1994, 1995, 1996, 1997
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code distributions
7 * retain the above copyright notice and this paragraph in its entirety, (2)
8 * distributions including binary code include the above copyright notice and
9 * this paragraph in its entirety in the documentation or other materials
10 * provided with the distribution, and (3) all advertising materials mentioning
11 * features or use of this software display the following acknowledgement:
12 * ``This product includes software developed by the University of California,
13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 * the University nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior
16 * written permission.
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 */
21
22 /*
23 * txtproto_print() derived from original code by Hannes Gredler
24 * (hannes@juniper.net):
25 *
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that: (1) source code
28 * distributions retain the above copyright notice and this paragraph
29 * in its entirety, and (2) distributions including binary code include
30 * the above copyright notice and this paragraph in its entirety in
31 * the documentation or other materials provided with the distribution.
32 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND
33 * WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT
34 * LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
35 * FOR A PARTICULAR PURPOSE.
36 */
37
38 #ifdef HAVE_CONFIG_H
39 #include "config.h"
40 #endif
41
42 #include <netdissect-stdinc.h>
43
44 #include <sys/stat.h>
45
46 #ifdef HAVE_FCNTL_H
47 #include <fcntl.h>
48 #endif
49 #include <stdio.h>
50 #include <stdarg.h>
51 #include <stdlib.h>
52 #include <string.h>
53
54 #include "interface.h"
55
56 /* VARARGS */
57 void
58 error(const char *fmt, ...)
59 {
60 va_list ap;
61
62 (void)fprintf(stderr, "%s: ", program_name);
63 va_start(ap, fmt);
64 (void)vfprintf(stderr, fmt, ap);
65 va_end(ap);
66 if (*fmt) {
67 fmt += strlen(fmt);
68 if (fmt[-1] != '\n')
69 (void)fputc('\n', stderr);
70 }
71 exit(1);
72 /* NOTREACHED */
73 }
74
75 /* VARARGS */
76 void
77 warning(const char *fmt, ...)
78 {
79 va_list ap;
80
81 (void)fprintf(stderr, "%s: WARNING: ", program_name);
82 va_start(ap, fmt);
83 (void)vfprintf(stderr, fmt, ap);
84 va_end(ap);
85 if (*fmt) {
86 fmt += strlen(fmt);
87 if (fmt[-1] != '\n')
88 (void)fputc('\n', stderr);
89 }
90 }
91
92 /*
93 * Copy arg vector into a new buffer, concatenating arguments with spaces.
94 */
95 char *
96 copy_argv(register char **argv)
97 {
98 register char **p;
99 register u_int len = 0;
100 char *buf;
101 char *src, *dst;
102
103 p = argv;
104 if (*p == 0)
105 return 0;
106
107 while (*p)
108 len += strlen(*p++) + 1;
109
110 buf = (char *)malloc(len);
111 if (buf == NULL)
112 error("copy_argv: malloc");
113
114 p = argv;
115 dst = buf;
116 while ((src = *p++) != NULL) {
117 while ((*dst++ = *src++) != '\0')
118 ;
119 dst[-1] = ' ';
120 }
121 dst[-1] = '\0';
122
123 return buf;
124 }
125
126 /*
127 * On Windows, we need to open the file in binary mode, so that
128 * we get all the bytes specified by the size we get from "fstat()".
129 * On UNIX, that's not necessary. O_BINARY is defined on Windows;
130 * we define it as 0 if it's not defined, so it does nothing.
131 */
132 #ifndef O_BINARY
133 #define O_BINARY 0
134 #endif
135
136 char *
137 read_infile(char *fname)
138 {
139 register int i, fd, cc;
140 register char *cp;
141 struct stat buf;
142
143 fd = open(fname, O_RDONLY|O_BINARY);
144 if (fd < 0)
145 error("can't open %s: %s", fname, pcap_strerror(errno));
146
147 if (fstat(fd, &buf) < 0)
148 error("can't stat %s: %s", fname, pcap_strerror(errno));
149
150 cp = malloc((u_int)buf.st_size + 1);
151 if (cp == NULL)
152 error("malloc(%d) for %s: %s", (u_int)buf.st_size + 1,
153 fname, pcap_strerror(errno));
154 cc = read(fd, cp, (u_int)buf.st_size);
155 if (cc < 0)
156 error("read %s: %s", fname, pcap_strerror(errno));
157 if (cc != buf.st_size)
158 error("short read %s (%d != %d)", fname, cc, (int)buf.st_size);
159
160 close(fd);
161 /* replace "# comment" with spaces */
162 for (i = 0; i < cc; i++) {
163 if (cp[i] == '#')
164 while (i < cc && cp[i] != '\n')
165 cp[i++] = ' ';
166 }
167 cp[cc] = '\0';
168 return (cp);
169 }