121377Sdist /* 221377Sdist * Copyright (c) 1983 Regents of the University of California. 333679Sbostic * All rights reserved. 433679Sbostic * 542625Sbostic * %sccs.include.redist.c% 621377Sdist */ 77896Ssam 826618Sdonn #if defined(LIBC_SCCS) && !defined(lint) 9*46604Sbostic static char sccsid[] = "@(#)getnetent.c 5.8 (Berkeley) 02/24/91"; 1033679Sbostic #endif /* LIBC_SCCS and not lint */ 1121377Sdist 1212691Ssam #include <sys/types.h> 137896Ssam #include <sys/socket.h> 14*46604Sbostic #include <netinet/in.h> 15*46604Sbostic #include <arpa/inet.h> 167896Ssam #include <netdb.h> 17*46604Sbostic #include <stdio.h> 18*46604Sbostic #include <string.h> 197896Ssam 207896Ssam #define MAXALIASES 35 217896Ssam 22*46604Sbostic static FILE *netf; 237896Ssam static char line[BUFSIZ+1]; 247896Ssam static struct netent net; 257896Ssam static char *net_aliases[MAXALIASES]; 2628280Slepreau int _net_stayopen; 277896Ssam 28*46604Sbostic void 297896Ssam setnetent(f) 307896Ssam int f; 317896Ssam { 327896Ssam if (netf == NULL) 3342266Sbostic netf = fopen(_PATH_NETWORKS, "r" ); 347896Ssam else 357896Ssam rewind(netf); 3628280Slepreau _net_stayopen |= f; 377896Ssam } 387896Ssam 39*46604Sbostic void 407896Ssam endnetent() 417896Ssam { 4228280Slepreau if (netf) { 437896Ssam fclose(netf); 447896Ssam netf = NULL; 457896Ssam } 4628280Slepreau _net_stayopen = 0; 477896Ssam } 487896Ssam 497896Ssam struct netent * 507896Ssam getnetent() 517896Ssam { 527896Ssam char *p; 537896Ssam register char *cp, **q; 547896Ssam 5542266Sbostic if (netf == NULL && (netf = fopen(_PATH_NETWORKS, "r" )) == NULL) 567896Ssam return (NULL); 577896Ssam again: 587896Ssam p = fgets(line, BUFSIZ, netf); 597896Ssam if (p == NULL) 607896Ssam return (NULL); 617896Ssam if (*p == '#') 627896Ssam goto again; 63*46604Sbostic cp = strpbrk(p, "#\n"); 647896Ssam if (cp == NULL) 657896Ssam goto again; 667896Ssam *cp = '\0'; 677896Ssam net.n_name = p; 68*46604Sbostic cp = strpbrk(p, " \t"); 697896Ssam if (cp == NULL) 707896Ssam goto again; 717896Ssam *cp++ = '\0'; 727896Ssam while (*cp == ' ' || *cp == '\t') 737896Ssam cp++; 74*46604Sbostic p = strpbrk(cp, " \t"); 757896Ssam if (p != NULL) 767896Ssam *p++ = '\0'; 778364Ssam net.n_net = inet_network(cp); 788342Ssam net.n_addrtype = AF_INET; 799190Ssam q = net.n_aliases = net_aliases; 8010087Ssam if (p != NULL) 819190Ssam cp = p; 8210087Ssam while (cp && *cp) { 8310087Ssam if (*cp == ' ' || *cp == '\t') { 8410087Ssam cp++; 8510087Ssam continue; 867896Ssam } 8710087Ssam if (q < &net_aliases[MAXALIASES - 1]) 8810087Ssam *q++ = cp; 89*46604Sbostic cp = strpbrk(cp, " \t"); 9010087Ssam if (cp != NULL) 9110087Ssam *cp++ = '\0'; 927896Ssam } 937896Ssam *q = NULL; 947896Ssam return (&net); 957896Ssam } 96