defs.c revision 36465
1193323Sed/*-
2193323Sed * Copyright (c) 1997 Brian Somers <brian@Awfulhak.org>
3193323Sed * All rights reserved.
4193323Sed *
5193323Sed * Redistribution and use in source and binary forms, with or without
6193323Sed * modification, are permitted provided that the following conditions
7193323Sed * are met:
8193323Sed * 1. Redistributions of source code must retain the above copyright
9193323Sed *    notice, this list of conditions and the following disclaimer.
10193323Sed * 2. Redistributions in binary form must reproduce the above copyright
11193323Sed *    notice, this list of conditions and the following disclaimer in the
12193323Sed *    documentation and/or other materials provided with the distribution.
13193323Sed *
14193323Sed * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15193323Sed * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16193323Sed * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17193323Sed * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18193323Sed * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19193323Sed * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20193323Sed * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21193323Sed * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22193323Sed * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23193323Sed * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24193323Sed * SUCH DAMAGE.
25193323Sed *
26193323Sed *	$Id: defs.c,v 1.14 1998/05/29 00:03:00 brian Exp $
27193323Sed */
28193323Sed
29193323Sed
30193323Sed#include <stdlib.h>
31193323Sed#include <string.h>
32193323Sed#include <sys/errno.h>
33193323Sed#include <time.h>
34193323Sed#include <unistd.h>
35193323Sed
36193323Sed#include "defs.h"
37193323Sed
38193323Sedvoid
39193323Sedrandinit()
40193323Sed{
41193323Sed#if __FreeBSD__ >= 3
42198892Srdivacky  static int initdone;
43198892Srdivacky
44198892Srdivacky  if (!initdone) {
45198892Srdivacky    initdone = 1;
46198892Srdivacky    srandomdev();
47198892Srdivacky  }
48193323Sed#else
49193323Sed  srandom((time(NULL)^getpid())+random());
50198892Srdivacky#endif
51193323Sed}
52198892Srdivacky
53198892Srdivackyssize_t
54198892Srdivackyfullread(int fd, void *v, size_t n)
55198892Srdivacky{
56198892Srdivacky  size_t got, total;
57193323Sed
58193323Sed  for (total = 0; total < n; total += got)
59193323Sed    switch ((got = read(fd, (char *)v + total, n - total))) {
60193323Sed      case 0:
61193323Sed        return total;
62193323Sed      case -1:
63193323Sed        if (errno == EINTR)
64193323Sed          got = 0;
65193323Sed        else
66193323Sed          return -1;
67193323Sed    }
68193323Sed  return total;
69193323Sed}
70193323Sed
71193323Sedstatic struct {
72193323Sed  int mode;
73193323Sed  const char *name;
74193323Sed} modes[] = {
75198892Srdivacky  { PHYS_INTERACTIVE, "interactive" },
76193323Sed  { PHYS_AUTO, "auto" },
77193323Sed  { PHYS_DIRECT, "direct" },
78193323Sed  { PHYS_DEDICATED, "dedicated" },
79193323Sed  { PHYS_DDIAL, "ddial" },
80193323Sed  { PHYS_BACKGROUND, "background" },
81193323Sed  { PHYS_ALL, "*" },
82193323Sed  { 0, 0 }
83193323Sed};
84193323Sed
85193323Sedconst char *
86193323Sedmode2Nam(int mode)
87193323Sed{
88193323Sed  int m;
89193323Sed
90193323Sed  for (m = 0; modes[m].mode; m++)
91193323Sed    if (modes[m].mode == mode)
92193323Sed      return modes[m].name;
93193323Sed
94193323Sed  return "unknown";
95193323Sed}
96193323Sed
97193323Sedint
98193323SedNam2mode(const char *name)
99193323Sed{
100193323Sed  int m, got, len;
101193323Sed
102193323Sed  len = strlen(name);
103193323Sed  got = -1;
104193323Sed  for (m = 0; modes[m].mode; m++)
105193323Sed    if (!strncasecmp(name, modes[m].name, len)) {
106193323Sed      if (modes[m].name[len] == '\0')
107193323Sed	return modes[m].mode;
108193323Sed      if (got != -1)
109193323Sed        return 0;
110193323Sed      got = m;
111193323Sed    }
112193323Sed
113193323Sed  return got == -1 ? 0 : modes[got].mode;
114193323Sed}
115193323Sed