121377Sdist /* 221377Sdist * Copyright (c) 1983 Regents of the University of California. 3*33679Sbostic * All rights reserved. 4*33679Sbostic * 5*33679Sbostic * Redistribution and use in source and binary forms are permitted 6*33679Sbostic * provided that this notice is preserved and that due credit is given 7*33679Sbostic * to the University of California at Berkeley. The name of the University 8*33679Sbostic * may not be used to endorse or promote products derived from this 9*33679Sbostic * software without specific prior written permission. This software 10*33679Sbostic * is provided ``as is'' without express or implied warranty. 1121377Sdist */ 127896Ssam 1326618Sdonn #if defined(LIBC_SCCS) && !defined(lint) 14*33679Sbostic static char sccsid[] = "@(#)getnetent.c 5.4 (Berkeley) 03/07/88"; 15*33679Sbostic #endif /* LIBC_SCCS and not lint */ 1621377Sdist 177896Ssam #include <stdio.h> 1812691Ssam #include <sys/types.h> 197896Ssam #include <sys/socket.h> 207896Ssam #include <netdb.h> 217896Ssam #include <ctype.h> 227896Ssam 237896Ssam #define MAXALIASES 35 247896Ssam 259808Ssam static char NETDB[] = "/etc/networks"; 267896Ssam static FILE *netf = NULL; 277896Ssam static char line[BUFSIZ+1]; 287896Ssam static struct netent net; 297896Ssam static char *net_aliases[MAXALIASES]; 3028280Slepreau int _net_stayopen; 317896Ssam static char *any(); 327896Ssam 337896Ssam setnetent(f) 347896Ssam int f; 357896Ssam { 367896Ssam if (netf == NULL) 377896Ssam netf = fopen(NETDB, "r" ); 387896Ssam else 397896Ssam rewind(netf); 4028280Slepreau _net_stayopen |= f; 417896Ssam } 427896Ssam 437896Ssam endnetent() 447896Ssam { 4528280Slepreau if (netf) { 467896Ssam fclose(netf); 477896Ssam netf = NULL; 487896Ssam } 4928280Slepreau _net_stayopen = 0; 507896Ssam } 517896Ssam 527896Ssam struct netent * 537896Ssam getnetent() 547896Ssam { 557896Ssam char *p; 567896Ssam register char *cp, **q; 577896Ssam 587896Ssam if (netf == NULL && (netf = fopen(NETDB, "r" )) == NULL) 597896Ssam return (NULL); 607896Ssam again: 617896Ssam p = fgets(line, BUFSIZ, netf); 627896Ssam if (p == NULL) 637896Ssam return (NULL); 647896Ssam if (*p == '#') 657896Ssam goto again; 667896Ssam cp = any(p, "#\n"); 677896Ssam if (cp == NULL) 687896Ssam goto again; 697896Ssam *cp = '\0'; 707896Ssam net.n_name = p; 717896Ssam cp = any(p, " \t"); 727896Ssam if (cp == NULL) 737896Ssam goto again; 747896Ssam *cp++ = '\0'; 757896Ssam while (*cp == ' ' || *cp == '\t') 767896Ssam cp++; 777896Ssam p = any(cp, " \t"); 787896Ssam if (p != NULL) 797896Ssam *p++ = '\0'; 808364Ssam net.n_net = inet_network(cp); 818342Ssam net.n_addrtype = AF_INET; 829190Ssam q = net.n_aliases = net_aliases; 8310087Ssam if (p != NULL) 849190Ssam cp = p; 8510087Ssam while (cp && *cp) { 8610087Ssam if (*cp == ' ' || *cp == '\t') { 8710087Ssam cp++; 8810087Ssam continue; 897896Ssam } 9010087Ssam if (q < &net_aliases[MAXALIASES - 1]) 9110087Ssam *q++ = cp; 9210087Ssam cp = any(cp, " \t"); 9310087Ssam if (cp != NULL) 9410087Ssam *cp++ = '\0'; 957896Ssam } 967896Ssam *q = NULL; 977896Ssam return (&net); 987896Ssam } 997896Ssam 1007896Ssam static char * 1017896Ssam any(cp, match) 1027896Ssam register char *cp; 1037896Ssam char *match; 1047896Ssam { 1057896Ssam register char *mp, c; 1067896Ssam 1077896Ssam while (c = *cp) { 1087896Ssam for (mp = match; *mp; mp++) 1097896Ssam if (*mp == c) 1107896Ssam return (cp); 1117896Ssam cp++; 1127896Ssam } 1137896Ssam return ((char *)0); 1147896Ssam } 115