source: trunk/minix/lib/ip/getprotoent.c@ 12

Last change on this file since 12 was 9, checked in by Mattia Monga, 14 years ago

Minix 3.1.2a

File size: 2.7 KB
Line 
1/*
2 * Copyright (c) 1983 Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms are permitted
6 * provided that: (1) source distributions retain this entire copyright
7 * notice and comment, and (2) distributions including binaries display
8 * the following acknowledgement: ``This product includes software
9 * developed by the University of California, Berkeley and its contributors''
10 * in the documentation or other materials provided with the distribution
11 * and in all advertising materials mentioning features or use of this
12 * software. Neither the name of the University nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
18 */
19
20#if defined(LIBC_SCCS) && !defined(lint)
21static char sccsid[] = "@(#)getprotoent.c 5.7 (Berkeley) 6/1/90";
22#endif /* LIBC_SCCS and not lint */
23
24#include <ctype.h>
25#include <stdio.h>
26#include <stdlib.h>
27
28#ifdef _MINIX
29#include <net/gen/netdb.h>
30
31static char *any _ARGS(( char *cp, char *match ));
32#endif
33
34#define MAXALIASES 35
35
36static FILE *protof = NULL;
37static char line[BUFSIZ+1];
38static struct protoent proto;
39static char *proto_aliases[MAXALIASES];
40int _proto_stayopen;
41
42void
43setprotoent(f)
44 int f;
45{
46 if (protof == NULL)
47 protof = fopen(_PATH_PROTOCOLS, "r" );
48 else
49 rewind(protof);
50 _proto_stayopen |= f;
51}
52
53void
54endprotoent()
55{
56 if (protof) {
57 fclose(protof);
58 protof = NULL;
59 }
60 _proto_stayopen = 0;
61}
62
63struct protoent *
64getprotoent()
65{
66 char *p;
67 register char *cp, **q;
68
69 if (protof == NULL && (protof = fopen(_PATH_PROTOCOLS, "r" )) == NULL)
70 return (NULL);
71again:
72 if ((p = fgets(line, BUFSIZ, protof)) == NULL)
73 return (NULL);
74 if (*p == '#')
75 goto again;
76 cp = any(p, "#\n");
77 if (cp == NULL)
78 goto again;
79 *cp = '\0';
80 proto.p_name = p;
81 cp = any(p, " \t");
82 if (cp == NULL)
83 goto again;
84 *cp++ = '\0';
85 while (*cp == ' ' || *cp == '\t')
86 cp++;
87 p = any(cp, " \t");
88 if (p != NULL)
89 *p++ = '\0';
90 proto.p_proto = atoi(cp);
91 q = proto.p_aliases = proto_aliases;
92 if (p != NULL) {
93 cp = p;
94 while (cp && *cp) {
95 if (*cp == ' ' || *cp == '\t') {
96 cp++;
97 continue;
98 }
99 if (q < &proto_aliases[MAXALIASES - 1])
100 *q++ = cp;
101 cp = any(cp, " \t");
102 if (cp != NULL)
103 *cp++ = '\0';
104 }
105 }
106 *q = NULL;
107 return (&proto);
108}
109
110static char *
111any(cp, match)
112 register char *cp;
113 char *match;
114{
115 register char *mp, c;
116
117 while (c = *cp) {
118 for (mp = match; *mp; mp++)
119 if (*mp == c)
120 return (cp);
121 cp++;
122 }
123 return ((char *)0);
124}
Note: See TracBrowser for help on using the repository browser.