Annotation of 43BSDReno/lib/libc/net/getservent.c, revision 1.1.1.1

1.1       root        1: /*
                      2:  * Copyright (c) 1983 Regents of the University of California.
                      3:  * All rights reserved.
                      4:  *
                      5:  * Redistribution and use in source and binary forms are permitted
                      6:  * provided that: (1) source distributions retain this entire copyright
                      7:  * notice and comment, and (2) distributions including binaries display
                      8:  * the following acknowledgement:  ``This product includes software
                      9:  * developed by the University of California, Berkeley and its contributors''
                     10:  * in the documentation or other materials provided with the distribution
                     11:  * and in all advertising materials mentioning features or use of this
                     12:  * software. Neither the name of the University nor the names of its
                     13:  * contributors may be used to endorse or promote products derived
                     14:  * from this software without specific prior written permission.
                     15:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
                     16:  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
                     17:  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
                     18:  */
                     19: 
                     20: #if defined(LIBC_SCCS) && !defined(lint)
                     21: static char sccsid[] = "@(#)getservent.c       5.8 (Berkeley) 6/1/90";
                     22: #endif /* LIBC_SCCS and not lint */
                     23: 
                     24: #include <stdio.h>
                     25: #include <sys/param.h>
                     26: #include <sys/types.h>
                     27: #include <sys/socket.h>
                     28: #include <netdb.h>
                     29: #include <ctype.h>
                     30: 
                     31: #define        MAXALIASES      35
                     32: 
                     33: static FILE *servf = NULL;
                     34: static char line[BUFSIZ+1];
                     35: static struct servent serv;
                     36: static char *serv_aliases[MAXALIASES];
                     37: static char *any();
                     38: int _serv_stayopen;
                     39: 
                     40: setservent(f)
                     41:        int f;
                     42: {
                     43:        if (servf == NULL)
                     44:                servf = fopen(_PATH_SERVICES, "r" );
                     45:        else
                     46:                rewind(servf);
                     47:        _serv_stayopen |= f;
                     48: }
                     49: 
                     50: endservent()
                     51: {
                     52:        if (servf) {
                     53:                fclose(servf);
                     54:                servf = NULL;
                     55:        }
                     56:        _serv_stayopen = 0;
                     57: }
                     58: 
                     59: struct servent *
                     60: getservent()
                     61: {
                     62:        char *p;
                     63:        register char *cp, **q;
                     64: 
                     65:        if (servf == NULL && (servf = fopen(_PATH_SERVICES, "r" )) == NULL)
                     66:                return (NULL);
                     67: again:
                     68:        if ((p = fgets(line, BUFSIZ, servf)) == NULL)
                     69:                return (NULL);
                     70:        if (*p == '#')
                     71:                goto again;
                     72:        cp = any(p, "#\n");
                     73:        if (cp == NULL)
                     74:                goto again;
                     75:        *cp = '\0';
                     76:        serv.s_name = p;
                     77:        p = any(p, " \t");
                     78:        if (p == NULL)
                     79:                goto again;
                     80:        *p++ = '\0';
                     81:        while (*p == ' ' || *p == '\t')
                     82:                p++;
                     83:        cp = any(p, ",/");
                     84:        if (cp == NULL)
                     85:                goto again;
                     86:        *cp++ = '\0';
                     87:        serv.s_port = htons((u_short)atoi(p));
                     88:        serv.s_proto = cp;
                     89:        q = serv.s_aliases = serv_aliases;
                     90:        cp = any(cp, " \t");
                     91:        if (cp != NULL)
                     92:                *cp++ = '\0';
                     93:        while (cp && *cp) {
                     94:                if (*cp == ' ' || *cp == '\t') {
                     95:                        cp++;
                     96:                        continue;
                     97:                }
                     98:                if (q < &serv_aliases[MAXALIASES - 1])
                     99:                        *q++ = cp;
                    100:                cp = any(cp, " \t");
                    101:                if (cp != NULL)
                    102:                        *cp++ = '\0';
                    103:        }
                    104:        *q = NULL;
                    105:        return (&serv);
                    106: }
                    107: 
                    108: static char *
                    109: any(cp, match)
                    110:        register char *cp;
                    111:        char *match;
                    112: {
                    113:        register char *mp, c;
                    114: 
                    115:        while (c = *cp) {
                    116:                for (mp = match; *mp; mp++)
                    117:                        if (*mp == c)
                    118:                                return (cp);
                    119:                cp++;
                    120:        }
                    121:        return ((char *)0);
                    122: }

unix.superglobalmegacorp.com

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