Annotation of 42BSD/ingres/source/iutil/getservent.c, revision 1.1.1.1

1.1       root        1: /*     getservent.c    4.5     83/01/02        */
                      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 SERVDB[] = "/etc/services";
                     12: static FILE *servf = NULL;
                     13: static char line[BUFSIZ+1];
                     14: static struct servent serv;
                     15: static char *serv_aliases[MAXALIASES];
                     16: static int stayopen = 0;
                     17: static char *any();
                     18: 
                     19: setservent(f)
                     20:        int f;
                     21: {
                     22:        if (servf == NULL)
                     23:                servf = fopen(SERVDB, "r" );
                     24:        else
                     25:                rewind(servf);
                     26:        stayopen |= f;
                     27: }
                     28: 
                     29: endservent()
                     30: {
                     31:        if (servf && !stayopen) {
                     32:                fclose(servf);
                     33:                servf = NULL;
                     34:        }
                     35: }
                     36: 
                     37: struct servent *
                     38: getservent()
                     39: {
                     40:        char *p;
                     41:        register char *cp, **q;
                     42: 
                     43: 
                     44:        if (servf == NULL && (servf = fopen(SERVDB, "r" )) == NULL)
                     45:                return (NULL);
                     46: again:
                     47:        if ((p = fgets(line, BUFSIZ, servf)) == 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:        serv.s_name = p;
                     56:        p = any(p, " \t");
                     57:        if (p == NULL)
                     58:                goto again;
                     59:        *p++ = '\0';
                     60:        while (*p == ' ' || *p == '\t')
                     61:                p++;
                     62:        cp = any(p, ",/");
                     63:        if (cp == NULL)
                     64:                goto again;
                     65:        *cp++ = '\0';
                     66:        serv.s_port = htons((u_short)myatoi(p));
                     67:        serv.s_proto = cp;
                     68:        q = serv.s_aliases = serv_aliases;
                     69:        cp = any(cp, " \t");
                     70:        if (cp != NULL)
                     71:                *cp++ = '\0';
                     72:        while (cp && *cp) {
                     73:                if (*cp == ' ' || *cp == '\t') {
                     74:                        cp++;
                     75:                        continue;
                     76:                }
                     77:                if (q < &serv_aliases[MAXALIASES - 1])
                     78:                        *q++ = cp;
                     79:                cp = any(cp, " \t");
                     80:                if (cp != NULL)
                     81:                        *cp++ = '\0';
                     82:        }
                     83:        *q = NULL;
                     84:        return (&serv);
                     85: }
                     86: 
                     87: static char *
                     88: any(cp, match)
                     89:        register char *cp;
                     90:        char *match;
                     91: {
                     92:        register char *mp, c;
                     93: 
                     94:        while (c = *cp) {
                     95:                for (mp = match; *mp; mp++)
                     96:                        if (*mp == c)
                     97:                                return (cp);
                     98:                cp++;
                     99:        }
                    100:        return ((char *)0);
                    101: }
                    102: 
                    103: static int
                    104: myatoi(p)
                    105: register char *p;
                    106: {
                    107:        register int n;
                    108:        register int f;
                    109: 
                    110:        n = 0;
                    111:        f = 0;
                    112:        for(;;p++) {
                    113:                switch(*p) {
                    114:                case ' ':
                    115:                case '\t':
                    116:                        continue;
                    117:                case '-':
                    118:                        f++;
                    119:                case '+':
                    120:                        p++;
                    121:                }
                    122:                break;
                    123:        }
                    124:        while(*p >= '0' && *p <= '9')
                    125:                n = n*10 + *p++ - '0';
                    126:        return(f? -n: n);
                    127: }

unix.superglobalmegacorp.com

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