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