1 /* 2 * Copyright (c) 1983, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #if defined(LIBC_SCCS) && !defined(lint) 9 static char sccsid[] = "@(#)getprotoent.c 8.1 (Berkeley) 06/04/93"; 10 #endif /* LIBC_SCCS and not lint */ 11 12 #include <sys/types.h> 13 #include <sys/socket.h> 14 #include <netdb.h> 15 #include <stdio.h> 16 #include <stdlib.h> 17 #include <string.h> 18 19 #define MAXALIASES 35 20 21 static FILE *protof = NULL; 22 static char line[BUFSIZ+1]; 23 static struct protoent proto; 24 static char *proto_aliases[MAXALIASES]; 25 int _proto_stayopen; 26 27 void 28 setprotoent(f) 29 int f; 30 { 31 if (protof == NULL) 32 protof = fopen(_PATH_PROTOCOLS, "r" ); 33 else 34 rewind(protof); 35 _proto_stayopen |= f; 36 } 37 38 void 39 endprotoent() 40 { 41 if (protof) { 42 fclose(protof); 43 protof = NULL; 44 } 45 _proto_stayopen = 0; 46 } 47 48 struct protoent * 49 getprotoent() 50 { 51 char *p; 52 register char *cp, **q; 53 54 if (protof == NULL && (protof = fopen(_PATH_PROTOCOLS, "r" )) == NULL) 55 return (NULL); 56 again: 57 if ((p = fgets(line, BUFSIZ, protof)) == NULL) 58 return (NULL); 59 if (*p == '#') 60 goto again; 61 cp = strpbrk(p, "#\n"); 62 if (cp == NULL) 63 goto again; 64 *cp = '\0'; 65 proto.p_name = p; 66 cp = strpbrk(p, " \t"); 67 if (cp == NULL) 68 goto again; 69 *cp++ = '\0'; 70 while (*cp == ' ' || *cp == '\t') 71 cp++; 72 p = strpbrk(cp, " \t"); 73 if (p != NULL) 74 *p++ = '\0'; 75 proto.p_proto = atoi(cp); 76 q = proto.p_aliases = proto_aliases; 77 if (p != NULL) { 78 cp = p; 79 while (cp && *cp) { 80 if (*cp == ' ' || *cp == '\t') { 81 cp++; 82 continue; 83 } 84 if (q < &proto_aliases[MAXALIASES - 1]) 85 *q++ = cp; 86 cp = strpbrk(cp, " \t"); 87 if (cp != NULL) 88 *cp++ = '\0'; 89 } 90 } 91 *q = NULL; 92 return (&proto); 93 } 94