Annotation of 42BSD/lib/libc/net/getnetent.c, revision 1.1.1.1

1.1       root        1: /*     getnetent.c     4.8     83/05/23        */
                      2: 
                      3: #include <stdio.h>
                      4: #include <sys/types.h>
                      5: #include <sys/socket.h>
                      6: #include <netdb.h>
                      7: #include <ctype.h>
                      8: 
                      9: #define        MAXALIASES      35
                     10: 
                     11: static char NETDB[] = "/etc/networks";
                     12: static FILE *netf = NULL;
                     13: static char line[BUFSIZ+1];
                     14: static struct netent net;
                     15: static char *net_aliases[MAXALIASES];
                     16: static int stayopen = 0;
                     17: static char *any();
                     18: 
                     19: setnetent(f)
                     20:        int f;
                     21: {
                     22:        if (netf == NULL)
                     23:                netf = fopen(NETDB, "r" );
                     24:        else
                     25:                rewind(netf);
                     26:        stayopen |= f;
                     27: }
                     28: 
                     29: endnetent()
                     30: {
                     31:        if (netf && !stayopen) {
                     32:                fclose(netf);
                     33:                netf = NULL;
                     34:        }
                     35: }
                     36: 
                     37: struct netent *
                     38: getnetent()
                     39: {
                     40:        char *p;
                     41:        register char *cp, **q;
                     42: 
                     43:        if (netf == NULL && (netf = fopen(NETDB, "r" )) == NULL)
                     44:                return (NULL);
                     45: again:
                     46:        p = fgets(line, BUFSIZ, netf);
                     47:        if (p == NULL)
                     48:                return (NULL);
                     49:        if (*p == '#')
                     50:                goto again;
                     51:        cp = any(p, "#\n");
                     52:        if (cp == NULL)
                     53:                goto again;
                     54:        *cp = '\0';
                     55:        net.n_name = p;
                     56:        cp = any(p, " \t");
                     57:        if (cp == NULL)
                     58:                goto again;
                     59:        *cp++ = '\0';
                     60:        while (*cp == ' ' || *cp == '\t')
                     61:                cp++;
                     62:        p = any(cp, " \t");
                     63:        if (p != NULL)
                     64:                *p++ = '\0';
                     65:        net.n_net = inet_network(cp);
                     66:        net.n_addrtype = AF_INET;
                     67:        q = net.n_aliases = net_aliases;
                     68:        if (p != NULL) 
                     69:                cp = p;
                     70:        while (cp && *cp) {
                     71:                if (*cp == ' ' || *cp == '\t') {
                     72:                        cp++;
                     73:                        continue;
                     74:                }
                     75:                if (q < &net_aliases[MAXALIASES - 1])
                     76:                        *q++ = cp;
                     77:                cp = any(cp, " \t");
                     78:                if (cp != NULL)
                     79:                        *cp++ = '\0';
                     80:        }
                     81:        *q = NULL;
                     82:        return (&net);
                     83: }
                     84: 
                     85: static char *
                     86: any(cp, match)
                     87:        register char *cp;
                     88:        char *match;
                     89: {
                     90:        register char *mp, c;
                     91: 
                     92:        while (c = *cp) {
                     93:                for (mp = match; *mp; mp++)
                     94:                        if (*mp == c)
                     95:                                return (cp);
                     96:                cp++;
                     97:        }
                     98:        return ((char *)0);
                     99: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.