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