123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- #define __FORCE_GLIBC__
- #include <features.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netdb.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define MAXALIASES 35
- static FILE *protof = NULL;
- static char line[BUFSIZ+1];
- static struct protoent proto;
- static char *proto_aliases[MAXALIASES];
- static int proto_stayopen;
- void setprotoent(int f)
- {
- if (protof == NULL)
- protof = fopen(_PATH_PROTOCOLS, "r" );
- else
- rewind(protof);
- proto_stayopen |= f;
- }
- void endprotoent(void)
- {
- if (protof) {
- fclose(protof);
- protof = NULL;
- }
- proto_stayopen = 0;
- }
- struct protoent * getprotoent(void)
- {
- char *p;
- register char *cp, **q;
- if (protof == NULL && (protof = fopen(_PATH_PROTOCOLS, "r" )) == NULL)
- return (NULL);
- again:
- if ((p = fgets(line, BUFSIZ, protof)) == NULL)
- return (NULL);
- if (*p == '#')
- goto again;
- cp = strpbrk(p, "#\n");
- if (cp == NULL)
- goto again;
- *cp = '\0';
- proto.p_name = p;
- cp = strpbrk(p, " \t");
- if (cp == NULL)
- goto again;
- *cp++ = '\0';
- while (*cp == ' ' || *cp == '\t')
- cp++;
- p = strpbrk(cp, " \t");
- if (p != NULL)
- *p++ = '\0';
- proto.p_proto = atoi(cp);
- q = proto.p_aliases = proto_aliases;
- if (p != NULL) {
- cp = p;
- while (cp && *cp) {
- if (*cp == ' ' || *cp == '\t') {
- cp++;
- continue;
- }
- if (q < &proto_aliases[MAXALIASES - 1])
- *q++ = cp;
- cp = strpbrk(cp, " \t");
- if (cp != NULL)
- *cp++ = '\0';
- }
- }
- *q = NULL;
- return (&proto);
- }
- struct protoent * getprotobyname(const char *name)
- {
- register struct protoent *p;
- register char **cp;
- setprotoent(proto_stayopen);
- while ((p = getprotoent()) != NULL) {
- if (strcmp(p->p_name, name) == 0)
- break;
- for (cp = p->p_aliases; *cp != 0; cp++)
- if (strcmp(*cp, name) == 0)
- goto found;
- }
- found:
- if (!proto_stayopen)
- endprotoent();
- return (p);
- }
- struct protoent * getprotobynumber(int proto)
- {
- register struct protoent *p;
- setprotoent(proto_stayopen);
- while ((p = getprotoent()) != NULL)
- if (p->p_proto == proto)
- break;
- if (!proto_stayopen)
- endprotoent();
- return (p);
- }
|