2 * Copyright (c) 1997 Brian Somers <brian@Awfulhak.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
30 #include <sys/types.h>
32 #include <netinet/in.h>
33 #include <arpa/inet.h>
34 #include <sys/socket.h>
42 #if !defined(__FreeBSD__) || __FreeBSD__ < 3
49 #define issep(c) ((c) == '\t' || (c) == ' ')
55 static int initdone; /* srandomdev() call is only required once */
62 srandom((time(NULL)^getpid())+random());
67 fullread(int fd, void *v, size_t n)
71 for (total = 0; total < n; total += got)
72 switch ((got = read(fd, (char *)v + total, n - total))) {
88 { PHYS_INTERACTIVE, "interactive" },
89 { PHYS_AUTO, "auto" },
90 { PHYS_DIRECT, "direct" },
91 { PHYS_DEDICATED, "dedicated" },
92 { PHYS_DDIAL, "ddial" },
93 { PHYS_BACKGROUND, "background" },
94 { PHYS_FOREGROUND, "foreground" },
104 for (m = 0; modes[m].mode; m++)
105 if (modes[m].mode == mode)
106 return modes[m].name;
112 Nam2mode(const char *name)
118 for (m = 0; modes[m].mode; m++)
119 if (!strncasecmp(name, modes[m].name, len)) {
120 if (modes[m].name[len] == '\0')
121 return modes[m].mode;
127 return got == -1 ? 0 : modes[got].mode;
131 GetIpAddr(const char *cp)
133 struct in_addr ipaddr;
135 if (!strcasecmp(cp, "default"))
136 ipaddr.s_addr = INADDR_ANY;
137 else if (inet_aton(cp, &ipaddr) == 0) {
140 /* Any illegal characters ? */
141 for (ptr = cp; *ptr != '\0'; ptr++)
142 if (!isalnum(*ptr) && strchr("-.", *ptr) == NULL)
148 hp = gethostbyname(cp);
149 if (hp && hp->h_addrtype == AF_INET)
150 memcpy(&ipaddr, hp->h_addr, hp->h_length);
152 ipaddr.s_addr = INADDR_NONE;
154 ipaddr.s_addr = INADDR_NONE;
160 static const struct speeds {
209 #ifndef _POSIX_SOURCE
226 { 115200, B115200, },
229 { 230400, B230400, },
237 #endif /* _POSIX_SOURCE */
242 SpeedToInt(speed_t speed)
244 const struct speeds *sp;
246 for (sp = speeds; sp->nspeed; sp++) {
247 if (sp->speed == speed) {
255 IntToSpeed(int nspeed)
257 const struct speeds *sp;
259 for (sp = speeds; sp->nspeed; sp++) {
260 if (sp->nspeed == nspeed) {
268 findblank(char *p, int flags)
275 if (flags & PARSE_REDUCE) {
276 memmove(p, p + 1, strlen(p));
281 } else if (*p == '"') {
282 memmove(p, p + 1, strlen(p));
283 instring = !instring;
285 } else if (!instring && (issep(*p) ||
286 (*p == '#' && !(flags & PARSE_NOHASH))))
291 return instring ? NULL : p;
295 MakeArgs(char *script, char **pvect, int maxargs, int flags)
300 while (*script && (*script != '#' || (flags & PARSE_NOHASH))) {
301 script += strspn(script, " \t");
303 if (nargs >= maxargs - 1)
307 script = findblank(script, flags);
310 else if (!(flags & PARSE_NOHASH) && *script == '#')
321 NumStr(long val, char *buf, size_t sz)
323 static char result[23]; /* handles 64 bit numbers */
325 if (buf == NULL || sz == 0) {
329 snprintf(buf, sz, "<%ld>", val);
334 HexStr(long val, char *buf, size_t sz)
336 static char result[21]; /* handles 64 bit numbers */
338 if (buf == NULL || sz == 0) {
342 snprintf(buf, sz, "<0x%lx>", val);
349 static char num[12]; /* Used immediately if returned */
350 static const char * const desc[] = {
351 "normal", "start", "sock", "modem", "dial", "dead", "done",
352 "reboot", "errdead", "hangup", "term", "nodial", "nologin",
353 "redial", "reconnect"
356 if (ex >= 0 && ex < sizeof desc / sizeof *desc)
358 snprintf(num, sizeof num, "%d", ex);