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 this notice is preserved and that due credit is given 7 * to the University of California at Berkeley. The name of the University 8 * may not be used to endorse or promote products derived from this 9 * software without specific prior written permission. This software 10 * is provided ``as is'' without express or implied warranty. 11 */ 12 13 #if defined(LIBC_SCCS) && !defined(lint) 14 static char sccsid[] = "@(#)getprotoent.c 5.4 (Berkeley) 03/07/88"; 15 #endif /* LIBC_SCCS and not lint */ 16 17 #include <stdio.h> 18 #include <sys/types.h> 19 #include <sys/socket.h> 20 #include <netdb.h> 21 #include <ctype.h> 22 23 #define MAXALIASES 35 24 25 static char PROTODB[] = "/etc/protocols"; 26 static FILE *protof = NULL; 27 static char line[BUFSIZ+1]; 28 static struct protoent proto; 29 static char *proto_aliases[MAXALIASES]; 30 static char *any(); 31 int _proto_stayopen; 32 33 setprotoent(f) 34 int f; 35 { 36 if (protof == NULL) 37 protof = fopen(PROTODB, "r" ); 38 else 39 rewind(protof); 40 _proto_stayopen |= f; 41 } 42 43 endprotoent() 44 { 45 if (protof) { 46 fclose(protof); 47 protof = NULL; 48 } 49 _proto_stayopen = 0; 50 } 51 52 struct protoent * 53 getprotoent() 54 { 55 char *p; 56 register char *cp, **q; 57 58 if (protof == NULL && (protof = fopen(PROTODB, "r" )) == NULL) 59 return (NULL); 60 again: 61 if ((p = fgets(line, BUFSIZ, protof)) == NULL) 62 return (NULL); 63 if (*p == '#') 64 goto again; 65 cp = any(p, "#\n"); 66 if (cp == NULL) 67 goto again; 68 *cp = '\0'; 69 proto.p_name = p; 70 cp = any(p, " \t"); 71 if (cp == NULL) 72 goto again; 73 *cp++ = '\0'; 74 while (*cp == ' ' || *cp == '\t') 75 cp++; 76 p = any(cp, " \t"); 77 if (p != NULL) 78 *p++ = '\0'; 79 proto.p_proto = atoi(cp); 80 q = proto.p_aliases = proto_aliases; 81 if (p != NULL) { 82 cp = p; 83 while (cp && *cp) { 84 if (*cp == ' ' || *cp == '\t') { 85 cp++; 86 continue; 87 } 88 if (q < &proto_aliases[MAXALIASES - 1]) 89 *q++ = cp; 90 cp = any(cp, " \t"); 91 if (cp != NULL) 92 *cp++ = '\0'; 93 } 94 } 95 *q = NULL; 96 return (&proto); 97 } 98 99 static char * 100 any(cp, match) 101 register char *cp; 102 char *match; 103 { 104 register char *mp, c; 105 106 while (c = *cp) { 107 for (mp = match; *mp; mp++) 108 if (*mp == c) 109 return (cp); 110 cp++; 111 } 112 return ((char *)0); 113 } 114