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