always pad with spaces on the right
[unix-history] / usr / src / lib / libc / net / getnetent.c
CommitLineData
8ea4199d
DF
1/*
2 * Copyright (c) 1983 Regents of the University of California.
6b2f9dd0
KB
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms are permitted
6 * provided that this notice is preserved and that due credit is given
7 * to the University of California at Berkeley. The name of the University
8 * may not be used to endorse or promote products derived from this
9 * software without specific prior written permission. This software
10 * is provided ``as is'' without express or implied warranty.
8ea4199d
DF
11 */
12
2ce81398 13#if defined(LIBC_SCCS) && !defined(lint)
6b2f9dd0
KB
14static char sccsid[] = "@(#)getnetent.c 5.4 (Berkeley) %G%";
15#endif /* LIBC_SCCS and not lint */
b754e80b
SL
16
17#include <stdio.h>
1a58f155 18#include <sys/types.h>
b754e80b
SL
19#include <sys/socket.h>
20#include <netdb.h>
21#include <ctype.h>
22
23#define MAXALIASES 35
24
62fc8b9c 25static char NETDB[] = "/etc/networks";
b754e80b
SL
26static FILE *netf = NULL;
27static char line[BUFSIZ+1];
28static struct netent net;
29static char *net_aliases[MAXALIASES];
5442f2c7 30int _net_stayopen;
b754e80b
SL
31static char *any();
32
33setnetent(f)
34 int f;
35{
36 if (netf == NULL)
37 netf = fopen(NETDB, "r" );
38 else
39 rewind(netf);
5442f2c7 40 _net_stayopen |= f;
b754e80b
SL
41}
42
43endnetent()
44{
5442f2c7 45 if (netf) {
b754e80b
SL
46 fclose(netf);
47 netf = NULL;
48 }
5442f2c7 49 _net_stayopen = 0;
b754e80b
SL
50}
51
52struct netent *
53getnetent()
54{
55 char *p;
56 register char *cp, **q;
57
58 if (netf == NULL && (netf = fopen(NETDB, "r" )) == NULL)
59 return (NULL);
60again:
61 p = fgets(line, BUFSIZ, netf);
62 if (p == NULL)
63 return (NULL);
64 if (*p == '#')
65 goto again;
66 cp = any(p, "#\n");
67 if (cp == NULL)
68 goto again;
69 *cp = '\0';
70 net.n_name = p;
71 cp = any(p, " \t");
72 if (cp == NULL)
73 goto again;
74 *cp++ = '\0';
75 while (*cp == ' ' || *cp == '\t')
76 cp++;
77 p = any(cp, " \t");
78 if (p != NULL)
79 *p++ = '\0';
047cb5c4 80 net.n_net = inet_network(cp);
86755108 81 net.n_addrtype = AF_INET;
9243aabf 82 q = net.n_aliases = net_aliases;
463dee20 83 if (p != NULL)
9243aabf 84 cp = p;
463dee20
SL
85 while (cp && *cp) {
86 if (*cp == ' ' || *cp == '\t') {
87 cp++;
88 continue;
b754e80b 89 }
463dee20
SL
90 if (q < &net_aliases[MAXALIASES - 1])
91 *q++ = cp;
92 cp = any(cp, " \t");
93 if (cp != NULL)
94 *cp++ = '\0';
b754e80b
SL
95 }
96 *q = NULL;
97 return (&net);
98}
99
b754e80b
SL
100static char *
101any(cp, match)
102 register char *cp;
103 char *match;
104{
105 register char *mp, c;
106
107 while (c = *cp) {
108 for (mp = match; *mp; mp++)
109 if (*mp == c)
110 return (cp);
111 cp++;
112 }
113 return ((char *)0);
114}