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) 05/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 } 110