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[] = "@(#)getprotoent.c 5.6 (Berkeley) 05/21/90"; 20 #endif /* LIBC_SCCS and not lint */ 21 22 #include <stdio.h> 23 #include <sys/types.h> 24 #include <sys/socket.h> 25 #include <netdb.h> 26 #include <ctype.h> 27 28 #define MAXALIASES 35 29 30 static FILE *protof = NULL; 31 static char line[BUFSIZ+1]; 32 static struct protoent proto; 33 static char *proto_aliases[MAXALIASES]; 34 static char *any(); 35 int _proto_stayopen; 36 37 setprotoent(f) 38 int f; 39 { 40 if (protof == NULL) 41 protof = fopen(_PATH_PROTOCOLS, "r" ); 42 else 43 rewind(protof); 44 _proto_stayopen |= f; 45 } 46 47 endprotoent() 48 { 49 if (protof) { 50 fclose(protof); 51 protof = NULL; 52 } 53 _proto_stayopen = 0; 54 } 55 56 struct protoent * 57 getprotoent() 58 { 59 char *p; 60 register char *cp, **q; 61 62 if (protof == NULL && (protof = fopen(_PATH_PROTOCOLS, "r" )) == NULL) 63 return (NULL); 64 again: 65 if ((p = fgets(line, BUFSIZ, protof)) == NULL) 66 return (NULL); 67 if (*p == '#') 68 goto again; 69 cp = any(p, "#\n"); 70 if (cp == NULL) 71 goto again; 72 *cp = '\0'; 73 proto.p_name = p; 74 cp = any(p, " \t"); 75 if (cp == NULL) 76 goto again; 77 *cp++ = '\0'; 78 while (*cp == ' ' || *cp == '\t') 79 cp++; 80 p = any(cp, " \t"); 81 if (p != NULL) 82 *p++ = '\0'; 83 proto.p_proto = atoi(cp); 84 q = proto.p_aliases = proto_aliases; 85 if (p != NULL) { 86 cp = p; 87 while (cp && *cp) { 88 if (*cp == ' ' || *cp == '\t') { 89 cp++; 90 continue; 91 } 92 if (q < &proto_aliases[MAXALIASES - 1]) 93 *q++ = cp; 94 cp = any(cp, " \t"); 95 if (cp != NULL) 96 *cp++ = '\0'; 97 } 98 } 99 *q = NULL; 100 return (&proto); 101 } 102 103 static char * 104 any(cp, match) 105 register char *cp; 106 char *match; 107 { 108 register char *mp, c; 109 110 while (c = *cp) { 111 for (mp = match; *mp; mp++) 112 if (*mp == c) 113 return (cp); 114 cp++; 115 } 116 return ((char *)0); 117 } 118