home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / dev / gcc / ixemulsrc.lha / ixemul / ixnet / getprotoent.c < prev    next >
C/C++ Source or Header  |  1996-12-11  |  5KB  |  155 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[] = "@(#)getprotoent.c       5.8 (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 <stdlib.h>
  45. #include <string.h>
  46.  
  47. #define MAXALIASES      35
  48.  
  49. void
  50. setprotoent(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_AMITCP) {
  56.         if (u.u_proto_fp == NULL)
  57.             u.u_proto_fp = fopen(_TCP_PATH_PROTOCOLS, "r" );
  58.         else
  59.             rewind(u.u_proto_fp);
  60.         u.u_proto_stayopen |= f;
  61.     }
  62.     else /*if (network_protocol == IX_NETWORK_AS225) */ {
  63.         SOCK_setprotoent(f);
  64.         return;
  65.     }
  66. }
  67.  
  68. void
  69. endprotoent(void)
  70. {
  71.     register struct ixnet *p = (struct ixnet *)u.u_ixnet;
  72.  
  73.     if (p->u_networkprotocol == IX_NETWORK_AS225) {
  74.         SOCK_endprotoent();
  75.         return;
  76.     }
  77.     else {
  78.         if (u.u_proto_fp) {
  79.             fclose(u.u_proto_fp);
  80.             u.u_proto_fp = NULL;
  81.         }
  82.         u.u_proto_stayopen = 0;
  83.     }
  84. }
  85.  
  86. struct protoent *
  87. getprotoent(void)
  88. {
  89.     char *s;
  90.     register char *cp, **q;
  91.     register struct ixnet *p = (struct ixnet *)u.u_ixnet;
  92.  
  93.     if (p->u_networkprotocol == IX_NETWORK_AS225) {
  94.         return SOCK_getprotoent();
  95.     }
  96.     else/* if (p->u_networkprotocol == IX_NETWORK_AMITCP)*/ {
  97.         if (u.u_proto_line == NULL)
  98.           u.u_proto_line = malloc(BUFSIZ + 1);
  99.         if (u.u_proto_aliases == NULL)
  100.           u.u_proto_aliases = malloc(MAXALIASES * sizeof(char *));
  101.         if (u.u_proto_line == NULL || u.u_proto_aliases == NULL)
  102.           {
  103.             errno = ENOMEM;
  104.             return NULL;
  105.           }
  106.         if (u.u_proto_fp == NULL && (u.u_proto_fp = fopen(_TCP_PATH_PROTOCOLS, "r" )) == NULL)
  107.         return (NULL);
  108. again:
  109.  
  110.         if ((s = fgets(u.u_proto_line, BUFSIZ, u.u_proto_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_proto.p_name = s;
  122.         cp = strpbrk(s, " \t");
  123.         if (cp == NULL)
  124.             goto again;
  125.  
  126.         *cp++ = '\0';
  127.         while (*cp == ' ' || *cp == '\t')
  128.             cp++;
  129.  
  130.         s = strpbrk(cp, " \t");
  131.         if (s != NULL)
  132.             *s++ = '\0';
  133.  
  134.         u.u_proto.p_proto = atoi(cp);
  135.         q = u.u_proto.p_aliases = u.u_proto_aliases;
  136.         if (s != NULL) {
  137.             cp = s;
  138.             while (cp && *cp) {
  139.                 if (*cp == ' ' || *cp == '\t') {
  140.                     cp++;
  141.                     continue;
  142.                 }
  143.                 if (q < &u.u_proto_aliases[MAXALIASES - 1])
  144.                     *q++ = cp;
  145.  
  146.                 cp = strpbrk(cp, " \t");
  147.                 if (cp != NULL)
  148.                     *cp++ = '\0';
  149.             }
  150.         }
  151.         *q = NULL;
  152.         return (&u.u_proto);
  153.     }
  154. }
  155.