121383Sdist /* 221383Sdist * Copyright (c) 1983 Regents of the University of California. 3*33679Sbostic * All rights reserved. 4*33679Sbostic * 5*33679Sbostic * Redistribution and use in source and binary forms are permitted 6*33679Sbostic * provided that this notice is preserved and that due credit is given 7*33679Sbostic * to the University of California at Berkeley. The name of the University 8*33679Sbostic * may not be used to endorse or promote products derived from this 9*33679Sbostic * software without specific prior written permission. This software 10*33679Sbostic * is provided ``as is'' without express or implied warranty. 1121383Sdist */ 127901Ssam 1326624Sdonn #if defined(LIBC_SCCS) && !defined(lint) 14*33679Sbostic static char sccsid[] = "@(#)getservent.c 5.5 (Berkeley) 03/07/88"; 15*33679Sbostic #endif /* LIBC_SCCS and not lint */ 1621383Sdist 177901Ssam #include <stdio.h> 1832104Smckusick #include <sys/param.h> 199808Ssam #include <sys/types.h> 207901Ssam #include <sys/socket.h> 217901Ssam #include <netdb.h> 227901Ssam #include <ctype.h> 237901Ssam 247901Ssam #define MAXALIASES 35 257901Ssam 269808Ssam static char SERVDB[] = "/etc/services"; 277901Ssam static FILE *servf = NULL; 287901Ssam static char line[BUFSIZ+1]; 297901Ssam static struct servent serv; 307901Ssam static char *serv_aliases[MAXALIASES]; 317901Ssam static char *any(); 3228280Slepreau int _serv_stayopen; 337901Ssam 347901Ssam setservent(f) 357901Ssam int f; 367901Ssam { 377901Ssam if (servf == NULL) 387901Ssam servf = fopen(SERVDB, "r" ); 397901Ssam else 407901Ssam rewind(servf); 4128280Slepreau _serv_stayopen |= f; 427901Ssam } 437901Ssam 447901Ssam endservent() 457901Ssam { 4628280Slepreau if (servf) { 477901Ssam fclose(servf); 487901Ssam servf = NULL; 497901Ssam } 5028280Slepreau _serv_stayopen = 0; 517901Ssam } 527901Ssam 537901Ssam struct servent * 547901Ssam getservent() 557901Ssam { 567901Ssam char *p; 577901Ssam register char *cp, **q; 587901Ssam 597901Ssam if (servf == NULL && (servf = fopen(SERVDB, "r" )) == NULL) 607901Ssam return (NULL); 617901Ssam again: 627901Ssam if ((p = fgets(line, BUFSIZ, servf)) == NULL) 637901Ssam return (NULL); 647901Ssam if (*p == '#') 657901Ssam goto again; 667901Ssam cp = any(p, "#\n"); 677901Ssam if (cp == NULL) 687901Ssam goto again; 697901Ssam *cp = '\0'; 707901Ssam serv.s_name = p; 717901Ssam p = any(p, " \t"); 727901Ssam if (p == NULL) 737901Ssam goto again; 747901Ssam *p++ = '\0'; 757901Ssam while (*p == ' ' || *p == '\t') 767901Ssam p++; 777901Ssam cp = any(p, ",/"); 787901Ssam if (cp == NULL) 797901Ssam goto again; 807901Ssam *cp++ = '\0'; 819808Ssam serv.s_port = htons((u_short)atoi(p)); 827901Ssam serv.s_proto = cp; 839190Ssam q = serv.s_aliases = serv_aliases; 847901Ssam cp = any(cp, " \t"); 8510087Ssam if (cp != NULL) 867901Ssam *cp++ = '\0'; 8710087Ssam while (cp && *cp) { 8810087Ssam if (*cp == ' ' || *cp == '\t') { 8910087Ssam cp++; 9010087Ssam continue; 917901Ssam } 9210087Ssam if (q < &serv_aliases[MAXALIASES - 1]) 9310087Ssam *q++ = cp; 9410087Ssam cp = any(cp, " \t"); 9510087Ssam if (cp != NULL) 9610087Ssam *cp++ = '\0'; 977901Ssam } 987901Ssam *q = NULL; 997901Ssam return (&serv); 1007901Ssam } 1017901Ssam 1027901Ssam static char * 1037901Ssam any(cp, match) 1047901Ssam register char *cp; 1057901Ssam char *match; 1067901Ssam { 1077901Ssam register char *mp, c; 1087901Ssam 1097901Ssam while (c = *cp) { 1107901Ssam for (mp = match; *mp; mp++) 1117901Ssam if (*mp == c) 1127901Ssam return (cp); 1137901Ssam cp++; 1147901Ssam } 1157901Ssam return ((char *)0); 1167901Ssam } 117