1 /* getprotoent.c 4.4 82/12/17 */ 2 3 #include <stdio.h> 4 #include <sys/socket.h> 5 #include <netdb.h> 6 #include <ctype.h> 7 8 #define MAXALIASES 35 9 10 static char PROTODB[] = "/etc/protocols"; 11 static FILE *protof = NULL; 12 static char line[BUFSIZ+1]; 13 static struct protoent proto; 14 static char *proto_aliases[MAXALIASES]; 15 static int stayopen = 0; 16 static char *any(); 17 18 setprotoent(f) 19 int f; 20 { 21 if (protof == NULL) 22 protof = fopen(PROTODB, "r" ); 23 else 24 rewind(protof); 25 stayopen |= f; 26 } 27 28 endprotoent() 29 { 30 if (protof && !stayopen) { 31 fclose(protof); 32 protof = NULL; 33 } 34 } 35 36 struct protoent * 37 getprotoent() 38 { 39 char *p; 40 register char *cp, **q; 41 42 if (protof == NULL && (protof = fopen(PROTODB, "r" )) == NULL) 43 return (NULL); 44 again: 45 if ((p = fgets(line, BUFSIZ, protof)) == NULL) 46 return (NULL); 47 if (*p == '#') 48 goto again; 49 cp = any(p, "#\n"); 50 if (cp == NULL) 51 goto again; 52 *cp = '\0'; 53 proto.p_name = p; 54 cp = any(p, " \t"); 55 if (cp == NULL) 56 goto again; 57 *cp++ = '\0'; 58 while (*cp == ' ' || *cp == '\t') 59 cp++; 60 p = any(cp, " \t"); 61 if (p != NULL) 62 *p++ = '\0'; 63 proto.p_proto = atoi(cp); 64 q = proto.p_aliases = proto_aliases; 65 if (p != NULL) { 66 cp = p; 67 while (*cp) { 68 if (*cp == ' ' || *cp == '\t') { 69 cp++; 70 continue; 71 } 72 if (q < &proto_aliases[MAXALIASES - 1]) 73 *q++ = cp; 74 cp = any(cp, " \t"); 75 if (*cp != NULL) 76 *cp++ = '\0'; 77 } 78 } 79 *q = NULL; 80 return (&proto); 81 } 82 83 static char * 84 any(cp, match) 85 register char *cp; 86 char *match; 87 { 88 register char *mp, c; 89 90 while (c = *cp) { 91 for (mp = match; *mp; mp++) 92 if (*mp == c) 93 return (cp); 94 cp++; 95 } 96 return ((char *)0); 97 } 98