121383Sdist /* 221383Sdist * Copyright (c) 1983 Regents of the University of California. 321383Sdist * All rights reserved. The Berkeley software License Agreement 421383Sdist * specifies the terms and conditions for redistribution. 521383Sdist */ 67901Ssam 726624Sdonn #if defined(LIBC_SCCS) && !defined(lint) 8*32104Smckusick static char sccsid[] = "@(#)getservent.c 5.4 (Berkeley) 09/04/87"; 926624Sdonn #endif LIBC_SCCS and not lint 1021383Sdist 117901Ssam #include <stdio.h> 12*32104Smckusick #include <sys/param.h> 139808Ssam #include <sys/types.h> 147901Ssam #include <sys/socket.h> 157901Ssam #include <netdb.h> 167901Ssam #include <ctype.h> 177901Ssam 187901Ssam #define MAXALIASES 35 197901Ssam 209808Ssam static char SERVDB[] = "/etc/services"; 217901Ssam static FILE *servf = NULL; 227901Ssam static char line[BUFSIZ+1]; 237901Ssam static struct servent serv; 247901Ssam static char *serv_aliases[MAXALIASES]; 257901Ssam static char *any(); 2628280Slepreau int _serv_stayopen; 277901Ssam 287901Ssam setservent(f) 297901Ssam int f; 307901Ssam { 317901Ssam if (servf == NULL) 327901Ssam servf = fopen(SERVDB, "r" ); 337901Ssam else 347901Ssam rewind(servf); 3528280Slepreau _serv_stayopen |= f; 367901Ssam } 377901Ssam 387901Ssam endservent() 397901Ssam { 4028280Slepreau if (servf) { 417901Ssam fclose(servf); 427901Ssam servf = NULL; 437901Ssam } 4428280Slepreau _serv_stayopen = 0; 457901Ssam } 467901Ssam 477901Ssam struct servent * 487901Ssam getservent() 497901Ssam { 507901Ssam char *p; 517901Ssam register char *cp, **q; 527901Ssam 537901Ssam if (servf == NULL && (servf = fopen(SERVDB, "r" )) == NULL) 547901Ssam return (NULL); 557901Ssam again: 567901Ssam if ((p = fgets(line, BUFSIZ, servf)) == NULL) 577901Ssam return (NULL); 587901Ssam if (*p == '#') 597901Ssam goto again; 607901Ssam cp = any(p, "#\n"); 617901Ssam if (cp == NULL) 627901Ssam goto again; 637901Ssam *cp = '\0'; 647901Ssam serv.s_name = p; 657901Ssam p = any(p, " \t"); 667901Ssam if (p == NULL) 677901Ssam goto again; 687901Ssam *p++ = '\0'; 697901Ssam while (*p == ' ' || *p == '\t') 707901Ssam p++; 717901Ssam cp = any(p, ",/"); 727901Ssam if (cp == NULL) 737901Ssam goto again; 747901Ssam *cp++ = '\0'; 759808Ssam serv.s_port = htons((u_short)atoi(p)); 767901Ssam serv.s_proto = cp; 779190Ssam q = serv.s_aliases = serv_aliases; 787901Ssam cp = any(cp, " \t"); 7910087Ssam if (cp != NULL) 807901Ssam *cp++ = '\0'; 8110087Ssam while (cp && *cp) { 8210087Ssam if (*cp == ' ' || *cp == '\t') { 8310087Ssam cp++; 8410087Ssam continue; 857901Ssam } 8610087Ssam if (q < &serv_aliases[MAXALIASES - 1]) 8710087Ssam *q++ = cp; 8810087Ssam cp = any(cp, " \t"); 8910087Ssam if (cp != NULL) 9010087Ssam *cp++ = '\0'; 917901Ssam } 927901Ssam *q = NULL; 937901Ssam return (&serv); 947901Ssam } 957901Ssam 967901Ssam static char * 977901Ssam any(cp, match) 987901Ssam register char *cp; 997901Ssam char *match; 1007901Ssam { 1017901Ssam register char *mp, c; 1027901Ssam 1037901Ssam while (c = *cp) { 1047901Ssam for (mp = match; *mp; mp++) 1057901Ssam if (*mp == c) 1067901Ssam return (cp); 1077901Ssam cp++; 1087901Ssam } 1097901Ssam return ((char *)0); 1107901Ssam } 111