[9] | 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: (1) source distributions retain this entire copyright
|
---|
| 7 | * notice and comment, and (2) distributions including binaries display
|
---|
| 8 | * the following acknowledgement: ``This product includes software
|
---|
| 9 | * developed by the University of California, Berkeley and its contributors''
|
---|
| 10 | * in the documentation or other materials provided with the distribution
|
---|
| 11 | * and in all advertising materials mentioning features or use of this
|
---|
| 12 | * software. Neither the name of the University nor the names of its
|
---|
| 13 | * contributors may be used to endorse or promote products derived
|
---|
| 14 | * from this software without specific prior written permission.
|
---|
| 15 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
---|
| 16 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
---|
| 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
---|
| 18 | */
|
---|
| 19 |
|
---|
| 20 | #if defined(LIBC_SCCS) && !defined(lint)
|
---|
| 21 | static char sccsid[] = "@(#)getservent.c 5.8 (Berkeley) 6/1/90";
|
---|
| 22 | #endif /* LIBC_SCCS and not lint */
|
---|
| 23 |
|
---|
| 24 | #include <sys/types.h>
|
---|
| 25 | #include <ctype.h>
|
---|
| 26 | #include <stdio.h>
|
---|
| 27 | #include <stdlib.h>
|
---|
| 28 |
|
---|
| 29 | #include <net/hton.h>
|
---|
| 30 | #include <net/gen/netdb.h>
|
---|
| 31 |
|
---|
| 32 | #define MAXALIASES 35
|
---|
| 33 |
|
---|
| 34 | static FILE *servf = NULL;
|
---|
| 35 | static char line[BUFSIZ+1];
|
---|
| 36 | static struct servent serv;
|
---|
| 37 | static char *serv_aliases[MAXALIASES];
|
---|
| 38 | int _serv_stayopen;
|
---|
| 39 |
|
---|
| 40 | static char *any _ARGS(( char *cp, char *match ));
|
---|
| 41 |
|
---|
| 42 | void
|
---|
| 43 | setservent(f)
|
---|
| 44 | int f;
|
---|
| 45 | {
|
---|
| 46 | if (servf == NULL)
|
---|
| 47 | servf = fopen(_PATH_SERVICES, "r" );
|
---|
| 48 | else
|
---|
| 49 | rewind(servf);
|
---|
| 50 | _serv_stayopen |= f;
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | void
|
---|
| 54 | endservent()
|
---|
| 55 | {
|
---|
| 56 | if (servf) {
|
---|
| 57 | fclose(servf);
|
---|
| 58 | servf = NULL;
|
---|
| 59 | }
|
---|
| 60 | _serv_stayopen = 0;
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | struct servent *
|
---|
| 64 | getservent()
|
---|
| 65 | {
|
---|
| 66 | char *p;
|
---|
| 67 | register char *cp, **q;
|
---|
| 68 |
|
---|
| 69 | if (servf == NULL && (servf = fopen(_PATH_SERVICES, "r" )) == NULL)
|
---|
| 70 | return (NULL);
|
---|
| 71 | again:
|
---|
| 72 | if ((p = fgets(line, BUFSIZ, servf)) == NULL)
|
---|
| 73 | return (NULL);
|
---|
| 74 | if (*p == '#')
|
---|
| 75 | goto again;
|
---|
| 76 | cp = any(p, "#\n");
|
---|
| 77 | if (cp == NULL)
|
---|
| 78 | goto again;
|
---|
| 79 | *cp = '\0';
|
---|
| 80 | serv.s_name = p;
|
---|
| 81 | p = any(p, " \t");
|
---|
| 82 | if (p == NULL)
|
---|
| 83 | goto again;
|
---|
| 84 | *p++ = '\0';
|
---|
| 85 | while (*p == ' ' || *p == '\t')
|
---|
| 86 | p++;
|
---|
| 87 | cp = any(p, ",/");
|
---|
| 88 | if (cp == NULL)
|
---|
| 89 | goto again;
|
---|
| 90 | *cp++ = '\0';
|
---|
| 91 | serv.s_port = htons((u16_t)atoi(p));
|
---|
| 92 | serv.s_proto = cp;
|
---|
| 93 | q = serv.s_aliases = serv_aliases;
|
---|
| 94 | cp = any(cp, " \t");
|
---|
| 95 | if (cp != NULL)
|
---|
| 96 | *cp++ = '\0';
|
---|
| 97 | while (cp && *cp) {
|
---|
| 98 | if (*cp == ' ' || *cp == '\t') {
|
---|
| 99 | cp++;
|
---|
| 100 | continue;
|
---|
| 101 | }
|
---|
| 102 | if (q < &serv_aliases[MAXALIASES - 1])
|
---|
| 103 | *q++ = cp;
|
---|
| 104 | cp = any(cp, " \t");
|
---|
| 105 | if (cp != NULL)
|
---|
| 106 | *cp++ = '\0';
|
---|
| 107 | }
|
---|
| 108 | *q = NULL;
|
---|
| 109 | return (&serv);
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | static char *
|
---|
| 113 | any(cp, match)
|
---|
| 114 | register char *cp;
|
---|
| 115 | char *match;
|
---|
| 116 | {
|
---|
| 117 | register char *mp, c;
|
---|
| 118 |
|
---|
| 119 | while (c = *cp) {
|
---|
| 120 | for (mp = match; *mp; mp++)
|
---|
| 121 | if (*mp == c)
|
---|
| 122 | return (cp);
|
---|
| 123 | cp++;
|
---|
| 124 | }
|
---|
| 125 | return ((char *)0);
|
---|
| 126 | }
|
---|