getrpcent.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /* @(#)getrpcent.c 2.2 88/07/29 4.0 RPCSRC */
  2. #define __FORCE_GLIBC
  3. #include <features.h>
  4. /*
  5. * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  6. * unrestricted use provided that this legend is included on all tape
  7. * media and as a part of the software program in whole or part. Users
  8. * may copy or modify Sun RPC without charge, but are not authorized
  9. * to license or distribute it to anyone else except as part of a product or
  10. * program developed by the user.
  11. *
  12. * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  13. * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  14. * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  15. *
  16. * Sun RPC is provided with no support and without any obligation on the
  17. * part of Sun Microsystems, Inc. to assist in its use, correction,
  18. * modification or enhancement.
  19. *
  20. * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  21. * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  22. * OR ANY PART THEREOF.
  23. *
  24. * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  25. * or profits or other special, indirect and consequential damages, even if
  26. * Sun has been advised of the possibility of such damages.
  27. *
  28. * Sun Microsystems, Inc.
  29. * 2550 Garcia Avenue
  30. * Mountain View, California 94043
  31. */
  32. /*
  33. * Copyright (c) 1985 by Sun Microsystems, Inc.
  34. */
  35. #include <stdio.h>
  36. #include <string.h>
  37. #include <sys/types.h>
  38. #include <rpc/rpc.h>
  39. #include <netdb.h>
  40. #include <sys/socket.h>
  41. #include <arpa/inet.h>
  42. /*
  43. * Internet version.
  44. */
  45. static struct rpcdata {
  46. FILE *rpcf;
  47. char *current;
  48. int currentlen;
  49. int stayopen;
  50. #define MAXALIASES 35
  51. char *rpc_aliases[MAXALIASES];
  52. struct rpcent rpc;
  53. char line[BUFSIZ + 1];
  54. char *domain;
  55. } *rpcdata;
  56. static struct rpcent *interpret(const char *val, int len);
  57. static char RPCDB[] = "/etc/rpc";
  58. static struct rpcdata *_rpcdata(void)
  59. {
  60. register struct rpcdata *d = rpcdata;
  61. if (d == 0) {
  62. d = (struct rpcdata *) calloc(1, sizeof(struct rpcdata));
  63. rpcdata = d;
  64. }
  65. return (d);
  66. }
  67. struct rpcent *getrpcbynumber(number)
  68. register int number;
  69. {
  70. register struct rpcdata *d = _rpcdata();
  71. register struct rpcent *p;
  72. if (d == 0)
  73. return (0);
  74. setrpcent(0);
  75. while ((p = getrpcent())) {
  76. if (p->r_number == number)
  77. break;
  78. }
  79. endrpcent();
  80. return (p);
  81. }
  82. struct rpcent *
  83. #ifdef __linux__
  84. getrpcbyname(const char *name)
  85. #else
  86. getrpcbyname(name)
  87. char *name;
  88. #endif
  89. {
  90. struct rpcent *rpc;
  91. char **rp;
  92. setrpcent(0);
  93. while ((rpc = getrpcent())) {
  94. if (strcmp(rpc->r_name, name) == 0)
  95. return (rpc);
  96. for (rp = rpc->r_aliases; *rp != NULL; rp++) {
  97. if (strcmp(*rp, name) == 0)
  98. return (rpc);
  99. }
  100. }
  101. endrpcent();
  102. return (NULL);
  103. }
  104. #ifdef __linux__
  105. void
  106. #endif
  107. setrpcent(f)
  108. int f;
  109. {
  110. register struct rpcdata *d = _rpcdata();
  111. if (d == 0)
  112. return;
  113. if (d->rpcf == NULL)
  114. d->rpcf = fopen(RPCDB, "r");
  115. else
  116. rewind(d->rpcf);
  117. if (d->current)
  118. free(d->current);
  119. d->current = NULL;
  120. d->stayopen |= f;
  121. }
  122. #ifdef __linux__
  123. void
  124. #endif
  125. endrpcent()
  126. {
  127. register struct rpcdata *d = _rpcdata();
  128. if (d == 0)
  129. return;
  130. if (d->current && !d->stayopen) {
  131. free(d->current);
  132. d->current = NULL;
  133. }
  134. if (d->rpcf && !d->stayopen) {
  135. fclose(d->rpcf);
  136. d->rpcf = NULL;
  137. }
  138. }
  139. struct rpcent *getrpcent()
  140. {
  141. register struct rpcdata *d = _rpcdata();
  142. if (d == 0)
  143. return (NULL);
  144. if (d->rpcf == NULL && (d->rpcf = fopen(RPCDB, "r")) == NULL)
  145. return (NULL);
  146. if (fgets(d->line, BUFSIZ, d->rpcf) == NULL)
  147. return (NULL);
  148. return interpret(d->line, strlen(d->line));
  149. }
  150. #ifdef __linux__
  151. static char *firstwhite(char *s)
  152. {
  153. char *s1, *s2;
  154. s1 = index(s, ' ');
  155. s2 = index(s, '\t');
  156. if (s1) {
  157. if (s2)
  158. return (s1 < s2) ? s1 : s2;
  159. else
  160. return s1;
  161. } else
  162. return s2;
  163. }
  164. #endif
  165. static struct rpcent *interpret(const char *val, int len)
  166. {
  167. register struct rpcdata *d = _rpcdata();
  168. char *p;
  169. register char *cp, **q;
  170. if (d == 0)
  171. return NULL;
  172. strncpy(d->line, val, len);
  173. p = d->line;
  174. d->line[len] = '\n';
  175. if (*p == '#')
  176. return (getrpcent());
  177. cp = index(p, '#');
  178. if (cp == NULL) {
  179. cp = index(p, '\n');
  180. if (cp == NULL)
  181. return (getrpcent());
  182. }
  183. *cp = '\0';
  184. #ifdef __linux__
  185. if ((cp = firstwhite(p)))
  186. *cp++ = 0;
  187. else
  188. return (getrpcent());
  189. #else
  190. cp = index(p, ' ');
  191. if (cp == NULL) {
  192. cp = index(p, '\t');
  193. if (cp == NULL)
  194. return (getrpcent());
  195. }
  196. *cp++ = '\0';
  197. #endif
  198. /* THIS STUFF IS INTERNET SPECIFIC */
  199. d->rpc.r_name = d->line;
  200. while (*cp == ' ' || *cp == '\t')
  201. cp++;
  202. d->rpc.r_number = atoi(cp);
  203. q = d->rpc.r_aliases = d->rpc_aliases;
  204. #ifdef __linux__
  205. if ((cp = firstwhite(cp)))
  206. *cp++ = '\0';
  207. #else
  208. cp = index(p, ' ');
  209. if (cp != NULL)
  210. *cp++ = '\0';
  211. else {
  212. cp = index(p, '\t');
  213. if (cp != NULL)
  214. *cp++ = '\0';
  215. }
  216. #endif
  217. while (cp && *cp) {
  218. if (*cp == ' ' || *cp == '\t') {
  219. cp++;
  220. continue;
  221. }
  222. if (q < &(d->rpc_aliases[MAXALIASES - 1]))
  223. *q++ = cp;
  224. #ifdef __linux__
  225. if ((cp = firstwhite(cp)))
  226. *cp++ = '\0';
  227. #else
  228. cp = index(p, ' ');
  229. if (cp != NULL)
  230. *cp++ = '\0';
  231. else {
  232. cp = index(p, '\t');
  233. if (cp != NULL)
  234. *cp++ = '\0';
  235. }
  236. #endif
  237. }
  238. *q = NULL;
  239. return (&d->rpc);
  240. }