home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 9 / FreshFishVol9-CD2.bin / bbs / comm / amitcp-sdk-4.0.lha / AmiTCP-SDK / src / rpclib / getrpcent.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-30  |  4.3 KB  |  245 lines

  1. /*
  2.  *      $Id: getrpcent.c,v 4.2 1994/09/29 23:48:50 jraja Exp $
  3.  *
  4.  *      Copyright © 1994 AmiTCP/IP Group,
  5.  *                       Network Solutions Development Inc.
  6.  *                       All rights reserved. 
  7.  */
  8.  
  9. /* @(#)getrpcent.c    2.2 88/07/29 4.0 RPCSRC */
  10. #if !defined(lint) && defined(SCCSIDS)
  11. static  char sccsid[] = "@(#)getrpcent.c 1.9 87/08/11  Copyr 1984 Sun Micro";
  12. #endif
  13.  
  14. /*
  15.  * Copyright (c) 1985 by Sun Microsystems, Inc.
  16.  */
  17.  
  18. #include <sys/param.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <sys/types.h>
  22. #include <rpc/rpc.h>
  23. #include <netdb.h>
  24. #include <sys/socket.h>
  25.  
  26. /*
  27.  * Internet version.
  28.  */
  29. struct rpcdata {
  30. #ifdef USE_DOSIO /* AMIGA specific */
  31.         BPTR    rpcf;
  32. #else
  33.     FILE    *rpcf;
  34. #endif
  35. #ifndef AMIGA
  36.     char    *current;
  37.     int    currentlen;
  38. #endif
  39.     int    stayopen;
  40. #define    MAXALIASES    35
  41.     char    *rpc_aliases[MAXALIASES];
  42.     struct    rpcent rpc;
  43.     char    line[BUFSIZ+1];
  44.     char    *domain;
  45. } *rpcdata;
  46.  
  47. static struct rpcdata *_rpcdata(void);
  48.  
  49. /*
  50.  * The ent-functions have been made static, since the database
  51.  * implementation should be hidden from the applications (J.R.)
  52.  */
  53. static void setrpcent(int f);
  54. static void endrpcent(void);
  55. static struct rpcent * getrpcent(void);
  56. static struct rpcent * interpret(char *val, size_t len);
  57.  
  58. #ifndef AMIGA
  59. static    char *index();
  60. #endif
  61.  
  62. #if defined(AMITCP)
  63. static char RPCDB[] = "AmiTCP:db/rpc";
  64. #else
  65. static char RPCDB[] = "/etc/rpc";
  66. #endif
  67.  
  68. static struct rpcdata *
  69. _rpcdata(void)
  70. {
  71.     register struct rpcdata *d = rpcdata;
  72.  
  73.     if (d == 0) {
  74.         d = (struct rpcdata *)mem_calloc(1, sizeof (struct rpcdata));
  75.         rpcdata = d;
  76.     }
  77.     return (d);
  78. }
  79.  
  80. struct rpcent *
  81. getrpcbynumber(number)
  82.     register int number;
  83. {
  84.     register struct rpcdata *d = _rpcdata();
  85.     register struct rpcent *p;
  86.  
  87.     if (d == 0)
  88.         return (0);
  89.     setrpcent(0);
  90.     while (p = getrpcent()) {
  91.         if (p->r_number == number)
  92.             break;
  93.     }
  94.     endrpcent();
  95.     return (p);
  96. }
  97.  
  98. struct rpcent *
  99. getrpcbyname(name)
  100.     char *name;
  101. {
  102.     struct rpcent *rpc;
  103.     char **rp;
  104.  
  105.     setrpcent(0);
  106.     while(rpc = getrpcent()) {
  107.         if (strcmp(rpc->r_name, name) == 0)
  108.             return (rpc);
  109.         for (rp = rpc->r_aliases; *rp != NULL; rp++) {
  110.             if (strcmp(*rp, name) == 0)
  111.                 return (rpc);
  112.         }
  113.     }
  114.     endrpcent();
  115.     return (NULL);
  116. }
  117.  
  118. static void
  119. setrpcent(int f)
  120. {
  121.     register struct rpcdata *d = _rpcdata();
  122.  
  123.     if (d == 0)
  124.         return;
  125.     if (d->rpcf == NULL)
  126. #ifdef USE_DOSIO
  127.         d->rpcf = Open((STRPTR)RPCDB, MODE_OLDFILE);
  128. #else
  129.         d->rpcf = fopen(RPCDB, "r");
  130. #endif
  131.     else
  132.         rewind(d->rpcf);
  133. #ifndef AMIGA
  134.     if (d->current)
  135.         mem_free(d->current, d->currentlen);
  136.     d->current = NULL;
  137. #endif
  138.     d->stayopen |= f;
  139. }
  140.  
  141. static void
  142. endrpcent(void)
  143. {
  144.     register struct rpcdata *d = _rpcdata();
  145.  
  146.     if (d == 0)
  147.         return;
  148. #ifndef AMIGA
  149.     if (d->current && !d->stayopen) {
  150.         mem_free(d->current, d->currentlen);
  151.         d->current = NULL;
  152.     }
  153. #endif
  154.     if (d->rpcf && !d->stayopen) {
  155.         fclose(d->rpcf);
  156.         d->rpcf = NULL;
  157.     }
  158. }
  159.  
  160. static struct rpcent *
  161. getrpcent(void)
  162. {
  163.     register struct rpcdata *d = _rpcdata();
  164.  
  165.     if (d == 0)
  166.         return(NULL);
  167.     if (d->rpcf == NULL &&
  168. #ifdef USE_DOSIO
  169.         (d->rpcf = Open((STRPTR)RPCDB, MODE_OLDFILE))
  170. #else
  171.         (d->rpcf = fopen(RPCDB, "r"))
  172. #endif
  173.         == NULL)
  174.         return (NULL);
  175.     if (fgets(d->line, BUFSIZ, d->rpcf) == NULL)
  176.         return (NULL);
  177.     return interpret(d->line, strlen(d->line));
  178. }
  179.  
  180. static struct rpcent *
  181. interpret(char *val, size_t len)
  182. {
  183.     register struct rpcdata *d = _rpcdata();
  184.     char *p;
  185.     register char *cp, **q;
  186.  
  187.     if (d == 0)
  188.         return NULL;
  189.     strncpy(d->line, val, len);
  190.     p = d->line;
  191.     d->line[len] = '\n';
  192.     if (*p == '#')
  193.         return (getrpcent());
  194.     cp = index(p, '#');
  195.     if (cp == NULL)
  196.     {
  197.         cp = index(p, '\n');
  198.         if (cp == NULL)
  199.             return (getrpcent());
  200.     }
  201.     *cp = '\0';
  202.     cp = index(p, ' ');
  203.     if (cp == NULL)
  204.     {
  205.         cp = index(p, '\t');
  206.         if (cp == NULL)
  207.             return (getrpcent());
  208.     }
  209.     *cp++ = '\0';
  210.     /* THIS STUFF IS INTERNET SPECIFIC */
  211.     d->rpc.r_name = d->line;
  212.     while (*cp == ' ' || *cp == '\t')
  213.         cp++;
  214.     d->rpc.r_number = atoi(cp);
  215.     q = d->rpc.r_aliases = d->rpc_aliases;
  216.     cp = index(p, ' ');
  217.     if (cp != NULL)
  218.         *cp++ = '\0';
  219.     else
  220.     {
  221.         cp = index(p, '\t');
  222.         if (cp != NULL)
  223.             *cp++ = '\0';
  224.     }
  225.     while (cp && *cp) {
  226.         if (*cp == ' ' || *cp == '\t') {
  227.             cp++;
  228.             continue;
  229.         }
  230.         if (q < &(d->rpc_aliases[MAXALIASES - 1]))
  231.             *q++ = cp;
  232.         cp = index(p, ' ');
  233.         if (cp != NULL)
  234.             *cp++ = '\0';
  235.         else
  236.         {
  237.             cp = index(p, '\t');
  238.             if (cp != NULL)
  239.                 *cp++ = '\0';
  240.         }
  241.     }
  242.     *q = NULL;
  243.     return (&d->rpc);
  244. }
  245.