1: /*
   2:  * Copyright (c) 1983 Regents of the University of California.
   3:  * All rights reserved.  The Berkeley software License Agreement
   4:  * specifies the terms and conditions for redistribution.
   5:  */
   6: 
   7: #if defined(LIBC_SCCS) && !defined(lint)
   8: static char sccsid[] = "@(#)getservent.c	5.3 (Berkeley) 5/19/86";
   9: #endif LIBC_SCCS and not lint
  10: 
  11: #include <stdio.h>
  12: #include <sys/types.h>
  13: #include <sys/socket.h>
  14: #include <netdb.h>
  15: #include <ctype.h>
  16: 
  17: #define MAXALIASES  35
  18: 
  19: static char SERVDB[] = "/etc/services";
  20: static FILE *servf = NULL;
  21: static char line[BUFSIZ+1];
  22: static struct servent serv;
  23: static char *serv_aliases[MAXALIASES];
  24: static char *any();
  25: int _serv_stayopen;
  26: 
  27: setservent(f)
  28:     int f;
  29: {
  30:     if (servf == NULL)
  31:         servf = fopen(SERVDB, "r" );
  32:     else
  33:         rewind(servf);
  34:     _serv_stayopen |= f;
  35: }
  36: 
  37: endservent()
  38: {
  39:     if (servf) {
  40:         fclose(servf);
  41:         servf = NULL;
  42:     }
  43:     _serv_stayopen = 0;
  44: }
  45: 
  46: struct servent *
  47: getservent()
  48: {
  49:     char *p;
  50:     register char *cp, **q;
  51: 
  52:     if (servf == NULL && (servf = fopen(SERVDB, "r" )) == NULL)
  53:         return (NULL);
  54: again:
  55:     if ((p = fgets(line, BUFSIZ, servf)) == NULL)
  56:         return (NULL);
  57:     if (*p == '#')
  58:         goto again;
  59:     cp = any(p, "#\n");
  60:     if (cp == NULL)
  61:         goto again;
  62:     *cp = '\0';
  63:     serv.s_name = p;
  64:     p = any(p, " \t");
  65:     if (p == NULL)
  66:         goto again;
  67:     *p++ = '\0';
  68:     while (*p == ' ' || *p == '\t')
  69:         p++;
  70:     cp = any(p, ",/");
  71:     if (cp == NULL)
  72:         goto again;
  73:     *cp++ = '\0';
  74:     serv.s_port = htons((u_short)atoi(p));
  75:     serv.s_proto = cp;
  76:     q = serv.s_aliases = serv_aliases;
  77:     cp = any(cp, " \t");
  78:     if (cp != NULL)
  79:         *cp++ = '\0';
  80:     while (cp && *cp) {
  81:         if (*cp == ' ' || *cp == '\t') {
  82:             cp++;
  83:             continue;
  84:         }
  85:         if (q < &serv_aliases[MAXALIASES - 1])
  86:             *q++ = cp;
  87:         cp = any(cp, " \t");
  88:         if (cp != NULL)
  89:             *cp++ = '\0';
  90:     }
  91:     *q = NULL;
  92:     return (&serv);
  93: }
  94: 
  95: static char *
  96: any(cp, match)
  97:     register char *cp;
  98:     char *match;
  99: {
 100:     register char *mp, c;
 101: 
 102:     while (c = *cp) {
 103:         for (mp = match; *mp; mp++)
 104:             if (*mp == c)
 105:                 return (cp);
 106:         cp++;
 107:     }
 108:     return ((char *)0);
 109: }

Defined functions

any defined in line 95; used 6 times

Defined variables

SERVDB defined in line 19; used 2 times
line defined in line 21; used 1 times
  • in line 55
sccsid defined in line 8; never used
serv defined in line 22; used 5 times
serv_aliases defined in line 23; used 2 times

Defined macros

MAXALIASES defined in line 17; used 2 times
Last modified: 1986-05-19
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 1003
Valid CSS Valid XHTML 1.0 Strict