home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1995 April / Internet Tools.iso / osi / isode / vmsisode / vmsisode80_tar.Z / vmsisode80_tar / sockit / source / dns / getservent.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-03-15  |  2.8 KB  |  125 lines

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