home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / dev / gcc / ixemulsrc.lha / ixemul / ixnet / getservent.c < prev    next >
C/C++ Source or Header  |  1996-12-11  |  5KB  |  158 lines

  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, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *      This product includes software developed by the University of
  16.  *      California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #if defined(LIBC_SCCS) && !defined(lint)
  35. static char sccsid[] = "@(#)getservent.c        5.9 (Berkeley) 2/24/91";
  36. #endif /* LIBC_SCCS and not lint */
  37.  
  38. #define _KERNEL
  39. #include "ixnet.h"
  40. #include <sys/types.h>
  41. #include <sys/socket.h>
  42. #include <netdb.h>
  43. #include <stdio.h>
  44. #include <string.h>
  45. #include <stdlib.h>
  46.  
  47. #define MAXALIASES      35
  48.  
  49. void
  50. setservent(int f) 
  51. {
  52.     register struct ixnet *p = (struct ixnet *)u.u_ixnet;
  53.     register int network_protocol = p->u_networkprotocol;
  54.  
  55.     if (network_protocol == IX_NETWORK_AS225) {
  56.         SOCK_setservent(f);
  57.     }
  58.     else /* if (network_protocol == IX_NETWORK_AMITCP) */ {
  59.         if (u.u_serv_fp == NULL) {
  60.             u.u_serv_fp = fopen(_TCP_PATH_SERVICES, "r" );
  61.             if (!u.u_serv_fp)
  62.                 return;
  63.         }
  64.         else
  65.             rewind(u.u_serv_fp);
  66.         u.u_serv_stayopen |= f;
  67.     }
  68. }
  69.  
  70. void
  71. endservent(void)
  72. {
  73.     register struct ixnet *p = (struct ixnet *)u.u_ixnet;
  74.     if (p->u_networkprotocol == IX_NETWORK_AS225) {
  75.         SOCK_endservent();
  76.     }
  77.     else {
  78.         if (u.u_serv_fp) {
  79.             fclose(u.u_serv_fp);
  80.             u.u_serv_fp = NULL;
  81.         }
  82.         u.u_serv_stayopen = 0;
  83.     }
  84. }
  85.  
  86. struct servent *
  87. getservent(void)
  88. {
  89.         char *s;
  90.         register char *cp, **q;
  91.         register struct ixnet *p = (struct ixnet *)u.u_ixnet;
  92.         register int network_protocol = p->u_networkprotocol;
  93.  
  94.         if (network_protocol == IX_NETWORK_AS225) {
  95.             return SOCK_getservent();
  96.         }
  97.         else /*if (network_protocol == IX_NETWORK_AMITCP)*/ {
  98.             if (u.u_serv_line == NULL)
  99.               u.u_serv_line = malloc(BUFSIZ + 1);
  100.             if (u.u_serv_aliases == NULL)
  101.               u.u_serv_aliases = malloc(MAXALIASES * sizeof(char *));
  102.             if (u.u_serv_line == NULL || u.u_serv_aliases == NULL)
  103.               {
  104.                 errno = ENOMEM;
  105.                 return NULL;
  106.               }
  107.             if (u.u_serv_fp == NULL && (u.u_serv_fp = fopen(_TCP_PATH_SERVICES, "r" )) == NULL)
  108.                 return (NULL);
  109. again:
  110.             if ((s = fgets(u.u_serv_line, BUFSIZ, u.u_serv_fp)) == NULL)
  111.                 return (NULL);
  112.  
  113.             if (*s == '#')
  114.                 goto again;
  115.  
  116.             cp = strpbrk(s, "#\n");
  117.             if (cp == NULL)
  118.                 goto again;
  119.  
  120.             *cp = '\0';
  121.             u.u_serv.s_name = s;
  122.             s = strpbrk(s, " \t");
  123.             if (s == NULL)
  124.                 goto again;
  125.  
  126.             *s++ = '\0';
  127.             while (*s == ' ' || *s == '\t')
  128.                 s++;
  129.  
  130.             cp = strpbrk(s, ",/");
  131.             if (cp == NULL)
  132.                 goto again;
  133.  
  134.             *cp++ = '\0';
  135.             u.u_serv.s_port = htons((u_short)atoi(s));
  136.             u.u_serv.s_proto = cp;
  137.             q = u.u_serv.s_aliases = u.u_serv_aliases;
  138.             cp = strpbrk(cp, " \t");
  139.             if (cp != NULL)
  140.                 *cp++ = '\0';
  141.  
  142.             while (cp && *cp) {
  143.                 if (*cp == ' ' || *cp == '\t') {
  144.                     cp++;
  145.                 continue;
  146.             }
  147.             if (q < &u.u_serv_aliases[MAXALIASES - 1])
  148.                 *q++ = cp;
  149.  
  150.             cp = strpbrk(cp, " \t");
  151.             if (cp != NULL)
  152.                 *cp++ = '\0';
  153.         }
  154.         *q = NULL;
  155.         return (&u.u_serv);
  156.     }
  157. }
  158.