libc/locale: Fix type breakage in __collate_range_cmp().
[freebsd-src.git] / usr.sbin / ppp / defs.c
blob856c200e02d7d9d9e05aa8a934296bb11e0726ec
1 /*-
2 * Copyright (c) 1997 Brian Somers <brian@Awfulhak.org>
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
26 * $FreeBSD$
30 #include <sys/param.h>
31 #include <netdb.h>
32 #include <netinet/in.h>
33 #include <arpa/inet.h>
34 #include <sys/socket.h>
36 #include <ctype.h>
37 #include <errno.h>
38 #include <stdarg.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #if defined(__FreeBSD__) && !defined(NOKLDLOAD)
43 #include <sys/module.h>
44 #endif
45 #include <termios.h>
46 #ifndef __FreeBSD__
47 #include <time.h>
48 #endif
49 #include <unistd.h>
51 #if defined(__FreeBSD__) && !defined(NOKLDLOAD)
52 #include "id.h"
53 #include "log.h"
54 #endif
55 #include "defs.h"
57 #define issep(c) ((c) == '\t' || (c) == ' ')
59 #ifdef __NetBSD__
60 void
61 randinit()
63 srandom((time(NULL)^getpid())+random());
65 #endif
67 ssize_t
68 fullread(int fd, void *v, size_t n)
70 size_t got, total;
72 for (total = 0; total < n; total += got)
73 switch ((got = read(fd, (char *)v + total, n - total))) {
74 case 0:
75 return total;
76 case -1:
77 if (errno == EINTR)
78 got = 0;
79 else
80 return -1;
82 return total;
85 static struct {
86 int mode;
87 const char *name;
88 } modes[] = {
89 { PHYS_INTERACTIVE, "interactive" },
90 { PHYS_AUTO, "auto" },
91 { PHYS_DIRECT, "direct" },
92 { PHYS_DEDICATED, "dedicated" },
93 { PHYS_DDIAL, "ddial" },
94 { PHYS_BACKGROUND, "background" },
95 { PHYS_FOREGROUND, "foreground" },
96 { PHYS_ALL, "*" },
97 { 0, 0 }
100 const char *
101 mode2Nam(int mode)
103 int m;
105 for (m = 0; modes[m].mode; m++)
106 if (modes[m].mode == mode)
107 return modes[m].name;
109 return "unknown";
113 Nam2mode(const char *name)
115 int m, got, len;
117 len = strlen(name);
118 got = -1;
119 for (m = 0; modes[m].mode; m++)
120 if (!strncasecmp(name, modes[m].name, len)) {
121 if (modes[m].name[len] == '\0')
122 return modes[m].mode;
123 if (got != -1)
124 return 0;
125 got = m;
128 return got == -1 ? 0 : modes[got].mode;
131 struct in_addr
132 GetIpAddr(const char *cp)
134 struct in_addr ipaddr;
136 if (!strcasecmp(cp, "default"))
137 ipaddr.s_addr = INADDR_ANY;
138 else if (inet_aton(cp, &ipaddr) == 0) {
139 const char *ptr;
141 /* Any illegal characters ? */
142 for (ptr = cp; *ptr != '\0'; ptr++)
143 if (!isalnum(*ptr) && strchr("-.", *ptr) == NULL)
144 break;
146 if (*ptr == '\0') {
147 struct hostent *hp;
149 hp = gethostbyname(cp);
150 if (hp && hp->h_addrtype == AF_INET)
151 memcpy(&ipaddr, hp->h_addr, hp->h_length);
152 else
153 ipaddr.s_addr = INADDR_NONE;
154 } else
155 ipaddr.s_addr = INADDR_NONE;
158 return ipaddr;
161 static const struct speeds {
162 unsigned nspeed;
163 speed_t speed;
164 } speeds[] = {
165 #ifdef B50
166 { 50, B50, },
167 #endif
168 #ifdef B75
169 { 75, B75, },
170 #endif
171 #ifdef B110
172 { 110, B110, },
173 #endif
174 #ifdef B134
175 { 134, B134, },
176 #endif
177 #ifdef B150
178 { 150, B150, },
179 #endif
180 #ifdef B200
181 { 200, B200, },
182 #endif
183 #ifdef B300
184 { 300, B300, },
185 #endif
186 #ifdef B600
187 { 600, B600, },
188 #endif
189 #ifdef B1200
190 { 1200, B1200, },
191 #endif
192 #ifdef B1800
193 { 1800, B1800, },
194 #endif
195 #ifdef B2400
196 { 2400, B2400, },
197 #endif
198 #ifdef B4800
199 { 4800, B4800, },
200 #endif
201 #ifdef B9600
202 { 9600, B9600, },
203 #endif
204 #ifdef B19200
205 { 19200, B19200, },
206 #endif
207 #ifdef B38400
208 { 38400, B38400, },
209 #endif
210 #ifndef _POSIX_SOURCE
211 #ifdef B7200
212 { 7200, B7200, },
213 #endif
214 #ifdef B14400
215 { 14400, B14400, },
216 #endif
217 #ifdef B28800
218 { 28800, B28800, },
219 #endif
220 #ifdef B57600
221 { 57600, B57600, },
222 #endif
223 #ifdef B76800
224 { 76800, B76800, },
225 #endif
226 #ifdef B115200
227 { 115200, B115200, },
228 #endif
229 #ifdef B230400
230 { 230400, B230400, },
231 #endif
232 #ifdef B460800
233 { 460800, B460800, },
234 #endif
235 #ifdef B921600
236 { 921600, B921600, },
237 #endif
238 #ifdef EXTA
239 { 19200, EXTA, },
240 #endif
241 #ifdef EXTB
242 { 38400, EXTB, },
243 #endif
244 #endif /* _POSIX_SOURCE */
245 { 0, 0 }
248 unsigned
249 SpeedToUnsigned(speed_t speed)
251 const struct speeds *sp;
253 for (sp = speeds; sp->nspeed; sp++) {
254 if (sp->speed == speed) {
255 return sp->nspeed;
258 return 0;
261 speed_t
262 UnsignedToSpeed(unsigned nspeed)
264 const struct speeds *sp;
266 for (sp = speeds; sp->nspeed; sp++) {
267 if (sp->nspeed == nspeed) {
268 return sp->speed;
271 return B0;
274 char *
275 findblank(char *p, int flags)
277 int instring;
279 instring = 0;
280 while (*p) {
281 if (*p == '\\') {
282 if (flags & PARSE_REDUCE) {
283 memmove(p, p + 1, strlen(p));
284 if (!*p)
285 break;
286 } else
287 p++;
288 } else if (*p == '"') {
289 memmove(p, p + 1, strlen(p));
290 instring = !instring;
291 continue;
292 } else if (!instring && (issep(*p) ||
293 (*p == '#' && !(flags & PARSE_NOHASH))))
294 return p;
295 p++;
298 return instring ? NULL : p;
302 MakeArgs(char *script, char **pvect, int maxargs, int flags)
304 int nargs;
306 nargs = 0;
307 while (*script) {
308 script += strspn(script, " \t");
309 if (*script == '#' && !(flags & PARSE_NOHASH)) {
310 *script = '\0';
311 break;
313 if (*script) {
314 if (nargs >= maxargs - 1)
315 break;
316 *pvect++ = script;
317 nargs++;
318 script = findblank(script, flags);
319 if (script == NULL)
320 return -1;
321 else if (!(flags & PARSE_NOHASH) && *script == '#')
322 *script = '\0';
323 else if (*script)
324 *script++ = '\0';
327 *pvect = NULL;
328 return nargs;
331 const char *
332 NumStr(long val, char *buf, size_t sz)
334 static char result[23]; /* handles 64 bit numbers */
336 if (buf == NULL || sz == 0) {
337 buf = result;
338 sz = sizeof result;
340 snprintf(buf, sz, "<%ld>", val);
341 return buf;
344 const char *
345 HexStr(long val, char *buf, size_t sz)
347 static char result[21]; /* handles 64 bit numbers */
349 if (buf == NULL || sz == 0) {
350 buf = result;
351 sz = sizeof result;
353 snprintf(buf, sz, "<0x%lx>", val);
354 return buf;
357 const char *
358 ex_desc(int ex)
360 static char num[12]; /* Used immediately if returned */
361 static const char * const desc[] = {
362 "normal", "start", "sock", "modem", "dial", "dead", "done",
363 "reboot", "errdead", "hangup", "term", "nodial", "nologin",
364 "redial", "reconnect"
367 if (ex >= 0 && ex < (int)(sizeof desc / sizeof *desc))
368 return desc[ex];
369 snprintf(num, sizeof num, "%d", ex);
370 return num;
373 void
374 SetTitle(const char *title)
376 if (title == NULL)
377 setproctitle(NULL);
378 else if (title[0] == '-' && title[1] != '\0')
379 setproctitle("-%s", title + 1);
380 else
381 setproctitle("%s", title);
384 fd_set *
385 mkfdset()
387 return (fd_set *)malloc(howmany(getdtablesize(), NFDBITS) * sizeof (fd_mask));
390 void
391 zerofdset(fd_set *s)
393 memset(s, '\0', howmany(getdtablesize(), NFDBITS) * sizeof (fd_mask));
396 void
397 Concatinate(char *buf, size_t sz, int argc, const char *const *argv)
399 int i, n;
400 unsigned pos;
402 *buf = '\0';
403 for (pos = i = 0; i < argc; i++) {
404 n = snprintf(buf + pos, sz - pos, "%s%s", i ? " " : "", argv[i]);
405 if (n < 0) {
406 buf[pos] = '\0';
407 break;
409 if ((pos += n) >= sz)
410 break;
414 #if defined(__FreeBSD__) && !defined(NOKLDLOAD)
416 loadmodules(int how, const char *module, ...)
418 int loaded = 0;
419 va_list ap;
421 va_start(ap, module);
422 while (module != NULL) {
423 if (modfind(module) == -1) {
424 if (ID0kldload(module) == -1) {
425 if (how == LOAD_VERBOSLY)
426 log_Printf(LogWARN, "%s: Cannot load module\n", module);
427 } else
428 loaded++;
430 module = va_arg(ap, const char *);
432 va_end(ap);
433 return loaded;
435 #else
437 loadmodules(int how __unused, const char *module __unused, ...)
439 return 0;
441 #endif