1 /* 2 * Copyright (c) 1983 Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #if defined(LIBC_SCCS) && !defined(lint) 9 static char sccsid[] = "@(#)getnetent.c 5.7 (Berkeley) 06/01/90"; 10 #endif /* LIBC_SCCS and not lint */ 11 12 #include <stdio.h> 13 #include <sys/types.h> 14 #include <sys/socket.h> 15 #include <netdb.h> 16 #include <ctype.h> 17 18 #define MAXALIASES 35 19 20 static FILE *netf = NULL; 21 static char line[BUFSIZ+1]; 22 static struct netent net; 23 static char *net_aliases[MAXALIASES]; 24 int _net_stayopen; 25 static char *any(); 26 27 setnetent(f) 28 int f; 29 { 30 if (netf == NULL) 31 netf = fopen(_PATH_NETWORKS, "r" ); 32 else 33 rewind(netf); 34 _net_stayopen |= f; 35 } 36 37 endnetent() 38 { 39 if (netf) { 40 fclose(netf); 41 netf = NULL; 42 } 43 _net_stayopen = 0; 44 } 45 46 struct netent * 47 getnetent() 48 { 49 char *p; 50 register char *cp, **q; 51 52 if (netf == NULL && (netf = fopen(_PATH_NETWORKS, "r" )) == NULL) 53 return (NULL); 54 again: 55 p = fgets(line, BUFSIZ, netf); 56 if (p == NULL) 57 return (NULL); 58 if (*p == '#') 59 goto again; 60 cp = any(p, "#\n"); 61 if (cp == NULL) 62 goto again; 63 *cp = '\0'; 64 net.n_name = p; 65 cp = any(p, " \t"); 66 if (cp == NULL) 67 goto again; 68 *cp++ = '\0'; 69 while (*cp == ' ' || *cp == '\t') 70 cp++; 71 p = any(cp, " \t"); 72 if (p != NULL) 73 *p++ = '\0'; 74 net.n_net = inet_network(cp); 75 net.n_addrtype = AF_INET; 76 q = net.n_aliases = net_aliases; 77 if (p != NULL) 78 cp = p; 79 while (cp && *cp) { 80 if (*cp == ' ' || *cp == '\t') { 81 cp++; 82 continue; 83 } 84 if (q < &net_aliases[MAXALIASES - 1]) 85 *q++ = cp; 86 cp = any(cp, " \t"); 87 if (cp != NULL) 88 *cp++ = '\0'; 89 } 90 *q = NULL; 91 return (&net); 92 } 93 94 static char * 95 any(cp, match) 96 register char *cp; 97 char *match; 98 { 99 register char *mp, c; 100 101 while (c = *cp) { 102 for (mp = match; *mp; mp++) 103 if (*mp == c) 104 return (cp); 105 cp++; 106 } 107 return ((char *)0); 108 } 109