home *** CD-ROM | disk | FTP | other *** search
/ Serving the Web / ServingTheWeb1995.disc1of1.iso / linux / slacksrce / d / libc / libc-4.6 / libc-4 / libc-linux / rpc / getrpcent.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-17  |  8.8 KB  |  462 lines

  1. /* @(#)getrpcent.c    2.2 88/07/29 4.0 RPCSRC */
  2. #if !defined(lint) && defined(SCCSIDS)
  3. static  char sccsid[] = "@(#)getrpcent.c 1.9 87/08/11  Copyr 1984 Sun Micro";
  4. #endif
  5.  
  6. /*
  7.  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  8.  * unrestricted use provided that this legend is included on all tape
  9.  * media and as a part of the software program in whole or part.  Users
  10.  * may copy or modify Sun RPC without charge, but are not authorized
  11.  * to license or distribute it to anyone else except as part of a product or
  12.  * program developed by the user.
  13.  * 
  14.  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  15.  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  16.  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  17.  * 
  18.  * Sun RPC is provided with no support and without any obligation on the
  19.  * part of Sun Microsystems, Inc. to assist in its use, correction,
  20.  * modification or enhancement.
  21.  * 
  22.  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  23.  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  24.  * OR ANY PART THEREOF.
  25.  * 
  26.  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  27.  * or profits or other special, indirect and consequential damages, even if
  28.  * Sun has been advised of the possibility of such damages.
  29.  * 
  30.  * Sun Microsystems, Inc.
  31.  * 2550 Garcia Avenue
  32.  * Mountain View, California  94043
  33.  */
  34.  
  35. /*
  36.  * Copyright (c) 1985 by Sun Microsystems, Inc.
  37.  */
  38.  
  39. #include <stdio.h>
  40. #include <sys/types.h>
  41. #include <rpc/rpc.h>
  42. #include <netdb.h>
  43. #include <sys/socket.h>
  44.  
  45. #ifdef YP
  46. #include <rpcsvc/yp_prot.h>
  47. #include <rpcsvc/ypclnt.h>
  48. #endif
  49.  
  50. #ifdef __STDC__
  51. #include <stdlib.h>
  52. #endif
  53.  
  54. /*
  55.  * Internet version.
  56.  */
  57. static
  58. struct rpcdata {
  59.     FILE    *rpcf;
  60.     char    *current;
  61.     int    currentlen;
  62.     int    stayopen;
  63. #define    MAXALIASES    35
  64.     char    *rpc_aliases[MAXALIASES];
  65.     struct    rpcent rpc;
  66.     char    line[BUFSIZ+1];
  67.     char    *domain;
  68. } *rpcdata;
  69.  
  70. #ifdef __linux__
  71. #include <string.h>
  72. #include <arpa/inet.h>
  73. static    struct rpcent *interpret(char *, int);
  74. static  struct rpcent *_fgetrpcent(void);
  75. #else
  76. static    struct rpcent *interpret();
  77. static  struct rpcent *_fgetrpcent();
  78. char    *inet_ntoa();
  79. struct    hostent *gethostent();
  80. static    char *index();
  81. static char RPCDB[] = _PATH_RPC; /* Changed from "/etc/rpc" by roland@gnu */
  82. #endif
  83.  
  84. #ifdef YP
  85. static struct rpcent * _nis_getrpcent(int);
  86. static struct rpcent * _nis_getrpcbynumber(int);
  87. static int ypmode = 0;
  88. static int inkeylen;
  89. static char *inkey = NULL;
  90. #endif
  91.  
  92. static struct rpcdata *
  93. #ifdef __linux__
  94. _rpcdata(void)
  95. #else
  96. _rpcdata()
  97. #endif
  98. {
  99.     register struct rpcdata *d = rpcdata;
  100.  
  101.     if (d == 0) {
  102.         d = (struct rpcdata *)calloc(1, sizeof (struct rpcdata));
  103.         rpcdata = d;
  104.     }
  105.     return (d);
  106. }
  107.  
  108. struct rpcent *
  109. #ifdef __linux__
  110. getrpcbynumber(int number)
  111. #else
  112. getrpcbynumber(number)
  113.     register int number;
  114. #endif
  115. {
  116.     register struct rpcdata *d = _rpcdata();
  117.     register struct rpcent *p;
  118.  
  119.     if (d == 0)
  120.         return (0);
  121.     setrpcent(0);
  122.     while (NULL != (p = getrpcent())) {
  123.         if (p->r_number == number)
  124.             break;
  125.     }
  126.     endrpcent();
  127. #ifdef YP
  128.     if(NULL == p)
  129.       p = _nis_getrpcbynumber(number);
  130. #endif
  131.     return (p);
  132. }
  133.  
  134. struct rpcent *
  135. #ifdef __linux__
  136. getrpcbyname(const char *name)
  137. #else
  138. getrpcbyname(name)
  139.     char *name;
  140. #endif
  141. {
  142.     struct rpcent *rpc;
  143.     char **rp;
  144.  
  145.     setrpcent(0);
  146.     while(NULL != (rpc = getrpcent())) {
  147. #ifdef __linux__
  148.         if (strcmp(rpc->r_name, name) == 0) {
  149.             endrpcent();
  150.             return (rpc);
  151.         }
  152. #else
  153.         if (strcmp(rpc->r_name, name) == 0)
  154.             return (rpc);
  155. #endif
  156.         for (rp = rpc->r_aliases; *rp != NULL; rp++) {
  157. #ifdef __linux__
  158.             if (strcmp(*rp, name) == 0) {
  159.                 endrpcent();
  160.                 return (rpc);
  161.             }
  162. #else
  163.             if (strcmp(*rp, name) == 0)
  164.                 return (rpc);
  165. #endif
  166.         }
  167.     }
  168.     endrpcent();
  169.     return (NULL);
  170. }
  171.  
  172. #ifdef __linux__
  173. void
  174. #endif
  175. setrpcent(f)
  176.     int f;
  177. {
  178.     register struct rpcdata *d = _rpcdata();
  179.  
  180.     if (d == 0)
  181.         return;
  182.     if (d->rpcf == NULL)
  183. #ifdef __linux__
  184.         d->rpcf = fopen(_PATH_RPC, "r");
  185. #else
  186.         d->rpcf = fopen(RPCDB, "r");
  187. #endif
  188.     else
  189.         rewind(d->rpcf);
  190.     if (d->current)
  191.         free(d->current);
  192.     d->current = NULL;
  193.     d->stayopen |= f;
  194. #ifdef YP
  195.     ypmode = 0;
  196.     if(NULL != inkey)
  197.       {
  198.         free (inkey);
  199.         inkey = NULL;
  200.       }
  201. #endif    
  202. }
  203.  
  204. #ifdef __linux__
  205. void
  206. #endif
  207. endrpcent()
  208. {
  209.     register struct rpcdata *d = _rpcdata();
  210.  
  211.     if (d == 0)
  212.         return;
  213.     if (d->current && !d->stayopen) {
  214.         free(d->current);
  215.         d->current = NULL;
  216.     }
  217.     if (d->rpcf && !d->stayopen) {
  218.         fclose(d->rpcf);
  219.         d->rpcf = NULL;
  220.     }
  221. #ifdef YP
  222.     ypmode = 0;
  223.     if(NULL != inkey)
  224.       {
  225.         free (inkey);
  226.         inkey = NULL;
  227.       }
  228. #endif    
  229. }
  230.  
  231. struct rpcent *
  232. getrpcent(void)
  233. {
  234. #ifdef YP
  235.   register struct rpcent *entry;
  236.   if(0 == ypmode)
  237.     {
  238.       entry = _fgetrpcent();
  239.       if (NULL == entry)
  240.         {
  241.           ypmode = 1;
  242.           entry = _nis_getrpcent(1);
  243.         }
  244.     }
  245.   else
  246.     {
  247.       entry = _nis_getrpcent(0);
  248.     }
  249.   return entry;
  250. #else
  251.   return (_fgetrpcent());
  252. #endif
  253. }
  254.  
  255.  
  256. static struct rpcent *
  257. _fgetrpcent(void)
  258. {
  259.     register struct rpcdata *d = _rpcdata();
  260.  
  261.     if (d == 0)
  262.         return(NULL);
  263. #ifdef __linux__
  264.     if (d->rpcf == NULL && (d->rpcf = fopen(_PATH_RPC, "r")) == NULL)
  265. #else
  266.     if (d->rpcf == NULL && (d->rpcf = fopen(RPCDB, "r")) == NULL)
  267. #endif
  268.         return (NULL);
  269.     if (fgets(d->line, BUFSIZ, d->rpcf) == NULL)
  270.         return (NULL);
  271.     return interpret(d->line, strlen(d->line));
  272. }
  273.  
  274. #ifdef __linux__
  275. static char *
  276. firstwhite(char *s)
  277. {
  278.     char *s1, *s2;
  279.  
  280.     s1 = index(s, ' ');
  281.     s2 = index(s, '\t');
  282.     if (s1) {
  283.         if (s2)
  284.             return (s1 < s2) ? s1 : s2;
  285.         else
  286.             return s1;
  287.     }
  288.     else
  289.         return s2;
  290. }
  291. #endif
  292.  
  293. static struct rpcent *
  294. #ifdef __linux__
  295. interpret(char *val, int len)
  296. #else
  297. interpret(val, len)
  298.     char *val;
  299.     int len;
  300. #endif
  301. {
  302.     register struct rpcdata *d = _rpcdata();
  303.     char *p;
  304.     register char *cp, **q;
  305.  
  306.     if (d == 0)
  307.         return NULL;
  308.     strncpy(d->line, val, len);
  309.     p = d->line;
  310.     d->line[len] = '\n';
  311.     if (*p == '#')
  312.         return (getrpcent());
  313.     cp = index(p, '#');
  314.     if (cp == NULL)
  315.     {
  316.         cp = index(p, '\n');
  317.         if (cp == NULL)
  318.             return (getrpcent());
  319.     }
  320.     *cp = '\0';
  321. #ifdef __linux__
  322.     if ((cp = firstwhite(p)))
  323.         *cp++ = 0;
  324.     else
  325.         return (getrpcent());
  326. #else
  327.     cp = index(p, ' ');
  328.     if (cp == NULL)
  329.     {
  330.         cp = index(p, '\t');
  331.         if (cp == NULL)
  332.             return (getrpcent());
  333.     }
  334.     *cp++ = '\0';
  335. #endif
  336.     /* THIS STUFF IS INTERNET SPECIFIC */
  337.     d->rpc.r_name = d->line;
  338.     while (*cp == ' ' || *cp == '\t')
  339.         cp++;
  340.     d->rpc.r_number = atoi(cp);
  341.     q = d->rpc.r_aliases = d->rpc_aliases;
  342. #ifdef __linux__
  343.     if ((cp = firstwhite(cp)))
  344.         *cp++ = '\0';
  345. #else
  346.     cp = index(p, ' ');
  347.     if (cp != NULL)
  348.         *cp++ = '\0';
  349.     else
  350.     {
  351.         cp = index(p, '\t');
  352.         if (cp != NULL)
  353.             *cp++ = '\0';
  354.     }
  355. #endif
  356.     while (cp && *cp) {
  357.         if (*cp == ' ' || *cp == '\t') {
  358.             cp++;
  359.             continue;
  360.         }
  361.         if (q < &(d->rpc_aliases[MAXALIASES - 1]))
  362.             *q++ = cp;
  363. #ifdef __linux__
  364.         if ((cp = firstwhite(cp)))
  365.             *cp++ = '\0';
  366. #else
  367.         cp = index(p, ' ');
  368.         if (cp != NULL)
  369.             *cp++ = '\0';
  370.         else
  371.         {
  372.             cp = index(p, '\t');
  373.             if (cp != NULL)
  374.                 *cp++ = '\0';
  375.         }
  376. #endif
  377.     }
  378.     *q = NULL;
  379.     return (&d->rpc);
  380. }
  381.  
  382. #ifdef    YP
  383. static struct rpcent *
  384. _nis_getrpcent(int first)
  385. {
  386.   static char *nisdomain = NULL;
  387.   char *outkey, *outval;
  388.   int outkeylen, outvallen, status;
  389.   struct rpcent *rptr;
  390.   
  391.   if (1 == __yp_check(NULL))
  392.     {
  393.       if (NULL == nisdomain)
  394.         yp_get_default_domain(&nisdomain);
  395.       if (1 == first)
  396.         {
  397.           status = yp_first(nisdomain, "rpc.bynumber",
  398.                             &outkey, &outkeylen,
  399.                             &outval, &outvallen);
  400.           if (0 != status) 
  401.             {
  402.               if (NULL != outval) free(outval) ;
  403.               if (NULL != outkey) free(outkey) ;
  404.               return NULL; 
  405.             }
  406.           inkey = outkey;
  407.           inkeylen = outkeylen;
  408.           rptr = interpret(outval, outvallen);
  409.           free (outval);
  410.           return (rptr);
  411.         }
  412.       else
  413.         {
  414.           status = yp_next(nisdomain, "rpc.bynumber",
  415.                            inkey, inkeylen,
  416.                            &outkey, &outkeylen,
  417.                            &outval, &outvallen);
  418.           if (0 != status)
  419.             {
  420.               free(inkey);
  421.               inkey = NULL;
  422.               return NULL;
  423.             }
  424.           free (inkey);
  425.           inkey = outkey;
  426.           inkeylen = outkeylen;
  427.           rptr = interpret(outval, outvallen);
  428.           free (outval);
  429.           return (rptr);
  430.         }
  431.     }
  432.   return NULL;
  433.  
  434. }
  435.  
  436. static struct rpcent *
  437. _nis_getrpcbynumber(int number)
  438. {
  439.   register struct rpcdata *d = _rpcdata();
  440.   register struct rpcent *p;
  441.   int reason;
  442.   char adrstr[16];
  443.   
  444.   if (__yp_check(NULL))
  445.     {
  446.       if (NULL == d->domain)
  447.         yp_get_default_domain(&d->domain);
  448.       sprintf(adrstr, "%d", number);
  449.       reason = yp_match(d->domain, "rpc.bynumber",
  450.                         adrstr, strlen(adrstr),
  451.                         &d->current, &d->currentlen);
  452.       if (0 != reason)
  453.         return(0);
  454.       p = interpret(d->current, d->currentlen);
  455.       free (d->current);
  456.       return p;
  457.     }
  458.   return(NULL);
  459. }
  460.  
  461. #endif
  462.