1*21377Sdist /* 2*21377Sdist * Copyright (c) 1983 Regents of the University of California. 3*21377Sdist * All rights reserved. The Berkeley software License Agreement 4*21377Sdist * specifies the terms and conditions for redistribution. 5*21377Sdist */ 67896Ssam 7*21377Sdist #ifndef lint 8*21377Sdist static char sccsid[] = "@(#)getnetent.c 5.1 (Berkeley) 05/30/85"; 9*21377Sdist #endif not lint 10*21377Sdist 117896Ssam #include <stdio.h> 1212691Ssam #include <sys/types.h> 137896Ssam #include <sys/socket.h> 147896Ssam #include <netdb.h> 157896Ssam #include <ctype.h> 167896Ssam 177896Ssam #define MAXALIASES 35 187896Ssam 199808Ssam static char NETDB[] = "/etc/networks"; 207896Ssam static FILE *netf = NULL; 217896Ssam static char line[BUFSIZ+1]; 227896Ssam static struct netent net; 237896Ssam static char *net_aliases[MAXALIASES]; 247896Ssam static int stayopen = 0; 257896Ssam static char *any(); 267896Ssam 277896Ssam setnetent(f) 287896Ssam int f; 297896Ssam { 307896Ssam if (netf == NULL) 317896Ssam netf = fopen(NETDB, "r" ); 327896Ssam else 337896Ssam rewind(netf); 347896Ssam stayopen |= f; 357896Ssam } 367896Ssam 377896Ssam endnetent() 387896Ssam { 397896Ssam if (netf && !stayopen) { 407896Ssam fclose(netf); 417896Ssam netf = NULL; 427896Ssam } 437896Ssam } 447896Ssam 457896Ssam struct netent * 467896Ssam getnetent() 477896Ssam { 487896Ssam char *p; 497896Ssam register char *cp, **q; 507896Ssam 517896Ssam if (netf == NULL && (netf = fopen(NETDB, "r" )) == NULL) 527896Ssam return (NULL); 537896Ssam again: 547896Ssam p = fgets(line, BUFSIZ, netf); 557896Ssam if (p == NULL) 567896Ssam return (NULL); 577896Ssam if (*p == '#') 587896Ssam goto again; 597896Ssam cp = any(p, "#\n"); 607896Ssam if (cp == NULL) 617896Ssam goto again; 627896Ssam *cp = '\0'; 637896Ssam net.n_name = p; 647896Ssam cp = any(p, " \t"); 657896Ssam if (cp == NULL) 667896Ssam goto again; 677896Ssam *cp++ = '\0'; 687896Ssam while (*cp == ' ' || *cp == '\t') 697896Ssam cp++; 707896Ssam p = any(cp, " \t"); 717896Ssam if (p != NULL) 727896Ssam *p++ = '\0'; 738364Ssam net.n_net = inet_network(cp); 748342Ssam net.n_addrtype = AF_INET; 759190Ssam q = net.n_aliases = net_aliases; 7610087Ssam if (p != NULL) 779190Ssam cp = p; 7810087Ssam while (cp && *cp) { 7910087Ssam if (*cp == ' ' || *cp == '\t') { 8010087Ssam cp++; 8110087Ssam continue; 827896Ssam } 8310087Ssam if (q < &net_aliases[MAXALIASES - 1]) 8410087Ssam *q++ = cp; 8510087Ssam cp = any(cp, " \t"); 8610087Ssam if (cp != NULL) 8710087Ssam *cp++ = '\0'; 887896Ssam } 897896Ssam *q = NULL; 907896Ssam return (&net); 917896Ssam } 927896Ssam 937896Ssam static char * 947896Ssam any(cp, match) 957896Ssam register char *cp; 967896Ssam char *match; 977896Ssam { 987896Ssam register char *mp, c; 997896Ssam 1007896Ssam while (c = *cp) { 1017896Ssam for (mp = match; *mp; mp++) 1027896Ssam if (*mp == c) 1037896Ssam return (cp); 1047896Ssam cp++; 1057896Ssam } 1067896Ssam return ((char *)0); 1077896Ssam } 108