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