defs.c revision 46686
155211Smsmith/*- 255211Smsmith * Copyright (c) 1997 Brian Somers <brian@Awfulhak.org> 355211Smsmith * All rights reserved. 455211Smsmith * 555211Smsmith * Redistribution and use in source and binary forms, with or without 655211Smsmith * modification, are permitted provided that the following conditions 755211Smsmith * are met: 855211Smsmith * 1. Redistributions of source code must retain the above copyright 955211Smsmith * notice, this list of conditions and the following disclaimer. 1055211Smsmith * 2. Redistributions in binary form must reproduce the above copyright 1155211Smsmith * notice, this list of conditions and the following disclaimer in the 1255211Smsmith * documentation and/or other materials provided with the distribution. 1355211Smsmith * 1455211Smsmith * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1555211Smsmith * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1655211Smsmith * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1755211Smsmith * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1855211Smsmith * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1955211Smsmith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2055211Smsmith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2155211Smsmith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2255211Smsmith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2355211Smsmith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2455211Smsmith * SUCH DAMAGE. 2539441Smsmith * 2639441Smsmith * $Id: defs.c,v 1.19 1999/04/26 08:54:24 brian Exp $ 27119482Sobrien */ 28119482Sobrien 29119482Sobrien 3039441Smsmith#include <sys/types.h> 3139441Smsmith#include <netdb.h> 3239441Smsmith#include <netinet/in.h> 3339441Smsmith#include <arpa/inet.h> 34173118Sjhb#include <sys/socket.h> 3564187Sjhb 3639441Smsmith#include <ctype.h> 3739441Smsmith#include <errno.h> 38200219Sjhb#include <stdlib.h> 39200219Sjhb#include <string.h> 4039441Smsmith#include <termios.h> 41173118Sjhb#if !defined(__FreeBSD__) || __FreeBSD__ < 3 4239441Smsmith#include <time.h> 43200219Sjhb#endif 44200219Sjhb#include <unistd.h> 45200219Sjhb 46200219Sjhb#include "defs.h" 47200219Sjhb 4855211Smsmith#define issep(c) ((c) == '\t' || (c) == ' ') 4955211Smsmith 5039441Smsmithvoid 51200219Sjhbrandinit() 5239441Smsmith{ 5355211Smsmith#if __FreeBSD__ >= 3 5455211Smsmith static int initdone; /* srandomdev() call is only required once */ 5555211Smsmith 5655211Smsmith if (!initdone) { 5755211Smsmith initdone = 1; 5855211Smsmith srandomdev(); 59173118Sjhb } 60173118Sjhb#else 6155211Smsmith srandom((time(NULL)^getpid())+random()); 6255211Smsmith#endif 6355211Smsmith} 64226746Sjhb 6555211Smsmithssize_t 6655211Smsmithfullread(int fd, void *v, size_t n) 67173118Sjhb{ 68173118Sjhb size_t got, total; 6955211Smsmith 7055211Smsmith for (total = 0; total < n; total += got) 71173118Sjhb switch ((got = read(fd, (char *)v + total, n - total))) { 7255211Smsmith case 0: 7355211Smsmith return total; 74200219Sjhb case -1: 75200219Sjhb if (errno == EINTR) 76200219Sjhb got = 0; 77200219Sjhb else 78200219Sjhb return -1; 79200219Sjhb } 80200219Sjhb return total; 81200219Sjhb} 82200219Sjhb 83200219Sjhbstatic struct { 84200219Sjhb int mode; 85200219Sjhb const char *name; 86200219Sjhb} modes[] = { 87200219Sjhb { PHYS_INTERACTIVE, "interactive" }, 88200219Sjhb { PHYS_AUTO, "auto" }, 89200219Sjhb { PHYS_DIRECT, "direct" }, 90200219Sjhb { PHYS_DEDICATED, "dedicated" }, 91200219Sjhb { PHYS_DDIAL, "ddial" }, 92200219Sjhb { PHYS_BACKGROUND, "background" }, 93200219Sjhb { PHYS_ALL, "*" }, 9455211Smsmith { 0, 0 } 9539441Smsmith}; 9655211Smsmith 9755211Smsmithconst char * 9855211Smsmithmode2Nam(int mode) 9955211Smsmith{ 10055211Smsmith int m; 10155211Smsmith 10255211Smsmith for (m = 0; modes[m].mode; m++) 10355211Smsmith if (modes[m].mode == mode) 10455211Smsmith return modes[m].name; 10555211Smsmith 10655211Smsmith return "unknown"; 10755211Smsmith} 10855211Smsmith 10955211Smsmithint 11055211SmsmithNam2mode(const char *name) 111226746Sjhb{ 11255211Smsmith int m, got, len; 11355211Smsmith 11455211Smsmith len = strlen(name); 11555211Smsmith got = -1; 11655211Smsmith for (m = 0; modes[m].mode; m++) 11755211Smsmith if (!strncasecmp(name, modes[m].name, len)) { 11855211Smsmith if (modes[m].name[len] == '\0') 11955211Smsmith return modes[m].mode; 12055211Smsmith if (got != -1) 12155211Smsmith return 0; 12255211Smsmith got = m; 12355211Smsmith } 124153589Ssobomax 12555211Smsmith return got == -1 ? 0 : modes[got].mode; 126200219Sjhb} 127200219Sjhb 128200219Sjhbstruct in_addr 129200219SjhbGetIpAddr(const char *cp) 130200219Sjhb{ 131200219Sjhb struct in_addr ipaddr; 132200219Sjhb 133200219Sjhb if (!strcasecmp(cp, "default")) 134200219Sjhb ipaddr.s_addr = INADDR_ANY; 13555211Smsmith else if (inet_aton(cp, &ipaddr) == 0) { 136 const char *ptr; 137 138 /* Any illegal characters ? */ 139 for (ptr = cp; *ptr != '\0'; ptr++) 140 if (!isalnum(*ptr) && strchr("-.", *ptr) == NULL) 141 break; 142 143 if (*ptr == '\0') { 144 struct hostent *hp; 145 146 hp = gethostbyname(cp); 147 if (hp && hp->h_addrtype == AF_INET) 148 memcpy(&ipaddr, hp->h_addr, hp->h_length); 149 else 150 ipaddr.s_addr = INADDR_NONE; 151 } else 152 ipaddr.s_addr = INADDR_NONE; 153 } 154 155 return ipaddr; 156} 157 158static const struct speeds { 159 int nspeed; 160 speed_t speed; 161} speeds[] = { 162#ifdef B50 163 { 50, B50, }, 164#endif 165#ifdef B75 166 { 75, B75, }, 167#endif 168#ifdef B110 169 { 110, B110, }, 170#endif 171#ifdef B134 172 { 134, B134, }, 173#endif 174#ifdef B150 175 { 150, B150, }, 176#endif 177#ifdef B200 178 { 200, B200, }, 179#endif 180#ifdef B300 181 { 300, B300, }, 182#endif 183#ifdef B600 184 { 600, B600, }, 185#endif 186#ifdef B1200 187 { 1200, B1200, }, 188#endif 189#ifdef B1800 190 { 1800, B1800, }, 191#endif 192#ifdef B2400 193 { 2400, B2400, }, 194#endif 195#ifdef B4800 196 { 4800, B4800, }, 197#endif 198#ifdef B9600 199 { 9600, B9600, }, 200#endif 201#ifdef B19200 202 { 19200, B19200, }, 203#endif 204#ifdef B38400 205 { 38400, B38400, }, 206#endif 207#ifndef _POSIX_SOURCE 208#ifdef B7200 209 { 7200, B7200, }, 210#endif 211#ifdef B14400 212 { 14400, B14400, }, 213#endif 214#ifdef B28800 215 { 28800, B28800, }, 216#endif 217#ifdef B57600 218 { 57600, B57600, }, 219#endif 220#ifdef B76800 221 { 76800, B76800, }, 222#endif 223#ifdef B115200 224 { 115200, B115200, }, 225#endif 226#ifdef B230400 227 { 230400, B230400, }, 228#endif 229#ifdef EXTA 230 { 19200, EXTA, }, 231#endif 232#ifdef EXTB 233 { 38400, EXTB, }, 234#endif 235#endif /* _POSIX_SOURCE */ 236 { 0, 0 } 237}; 238 239int 240SpeedToInt(speed_t speed) 241{ 242 const struct speeds *sp; 243 244 for (sp = speeds; sp->nspeed; sp++) { 245 if (sp->speed == speed) { 246 return sp->nspeed; 247 } 248 } 249 return 0; 250} 251 252speed_t 253IntToSpeed(int nspeed) 254{ 255 const struct speeds *sp; 256 257 for (sp = speeds; sp->nspeed; sp++) { 258 if (sp->nspeed == nspeed) { 259 return sp->speed; 260 } 261 } 262 return B0; 263} 264 265static char * 266findblank(char *p, int instring) 267{ 268 if (instring) { 269 while (*p) { 270 if (*p == '\\') { 271 memmove(p, p + 1, strlen(p + 1)); 272 if (!*p) 273 break; 274 } else if (*p == '"') 275 return (p); 276 p++; 277 } 278 } else { 279 while (*p) { 280 if (issep(*p)) 281 return (p); 282 p++; 283 } 284 } 285 286 return p; 287} 288 289int 290MakeArgs(char *script, char **pvect, int maxargs) 291{ 292 int nargs, nb; 293 int instring; 294 295 nargs = 0; 296 while (*script) { 297 nb = strspn(script, " \t"); 298 script += nb; 299 if (*script) { 300 if (*script == '"') { 301 instring = 1; 302 script++; 303 if (*script == '\0') 304 break; /* Shouldn't return here. Need to null 305 * terminate below */ 306 } else 307 instring = 0; 308 if (nargs >= maxargs - 1) 309 break; 310 *pvect++ = script; 311 nargs++; 312 script = findblank(script, instring); 313 if (*script) 314 *script++ = '\0'; 315 } 316 } 317 *pvect = NULL; 318 return nargs; 319} 320