1 /* getprotoent.c 4.1 82/08/25 */ 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 = "/usr/lib/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 unsigned long value(); 17 static char *any(); 18 19 setprotoent(f) 20 int f; 21 { 22 if (protof == NULL) 23 protof = fopen(PROTODB, "r" ); 24 else 25 rewind(protof); 26 stayopen |= f; 27 } 28 29 endprotoent() 30 { 31 if (protof && !stayopen) { 32 fclose(protof); 33 protof = NULL; 34 } 35 } 36 37 struct protoent * 38 getprotoent() 39 { 40 char *p; 41 register char *cp, **q; 42 43 if (protof == NULL && (protof = fopen(PROTODB, "r" )) == NULL) 44 return (NULL); 45 again: 46 if ((p = fgets(line, BUFSIZ, protof)) == NULL) 47 return (NULL); 48 if (*p == '#') 49 goto again; 50 cp = any(p, "#\n"); 51 if (cp == NULL) 52 goto again; 53 *cp = '\0'; 54 proto.p_name = p; 55 cp = any(p, " \t"); 56 if (cp == NULL) 57 goto again; 58 *cp++ = '\0'; 59 while (*cp == ' ' || *cp == '\t') 60 cp++; 61 p = any(cp, " \t"); 62 if (p != NULL) 63 *p++ = '\0'; 64 proto.p_proto = atoi(cp); 65 proto.p_aliases = proto_aliases; 66 cp = p; 67 q = proto_aliases; 68 while (*cp) { 69 if (*cp == ' ' || *cp == '\t') { 70 cp++; 71 continue; 72 } 73 if (q < &proto_aliases[MAXALIASES - 1]) 74 *q++ = cp; 75 cp = any(cp, " \t"); 76 if (*cp != NULL) 77 *cp++ = '\0'; 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