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 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. 11 */ 12 13 #if defined(LIBC_SCCS) && !defined(lint) 14 static char sccsid[] = "@(#)getnetent.c 5.4 (Berkeley) 03/07/88"; 15 #endif /* LIBC_SCCS and not lint */ 16 17 #include <stdio.h> 18 #include <sys/types.h> 19 #include <sys/socket.h> 20 #include <netdb.h> 21 #include <ctype.h> 22 23 #define MAXALIASES 35 24 25 static char NETDB[] = "/etc/networks"; 26 static FILE *netf = NULL; 27 static char line[BUFSIZ+1]; 28 static struct netent net; 29 static char *net_aliases[MAXALIASES]; 30 int _net_stayopen; 31 static char *any(); 32 33 setnetent(f) 34 int f; 35 { 36 if (netf == NULL) 37 netf = fopen(NETDB, "r" ); 38 else 39 rewind(netf); 40 _net_stayopen |= f; 41 } 42 43 endnetent() 44 { 45 if (netf) { 46 fclose(netf); 47 netf = NULL; 48 } 49 _net_stayopen = 0; 50 } 51 52 struct netent * 53 getnetent() 54 { 55 char *p; 56 register char *cp, **q; 57 58 if (netf == NULL && (netf = fopen(NETDB, "r" )) == NULL) 59 return (NULL); 60 again: 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'; 80 net.n_net = inet_network(cp); 81 net.n_addrtype = AF_INET; 82 q = net.n_aliases = net_aliases; 83 if (p != NULL) 84 cp = p; 85 while (cp && *cp) { 86 if (*cp == ' ' || *cp == '\t') { 87 cp++; 88 continue; 89 } 90 if (q < &net_aliases[MAXALIASES - 1]) 91 *q++ = cp; 92 cp = any(cp, " \t"); 93 if (cp != NULL) 94 *cp++ = '\0'; 95 } 96 *q = NULL; 97 return (&net); 98 } 99 100 static char * 101 any(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 } 115