getrpcent.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. /*
  42. * Internet version.
  43. */
  44. struct rpcdata {
  45. FILE *rpcf;
  46. char *current;
  47. int currentlen;
  48. int stayopen;
  49. #define MAXALIASES 35
  50. char *rpc_aliases[MAXALIASES];
  51. struct rpcent rpc;
  52. char line[BUFSIZ + 1];
  53. char *domain;
  54. } *rpcdata;
  55. static struct rpcent *interpret(const char *val, int len);
  56. struct hostent *gethostent();
  57. char *inet_ntoa();
  58. static char RPCDB[] = "/etc/rpc";
  59. static struct rpcdata *_rpcdata()
  60. {
  61. register struct rpcdata *d = rpcdata;
  62. if (d == 0) {
  63. d = (struct rpcdata *) calloc(1, sizeof(struct rpcdata));
  64. rpcdata = d;
  65. }
  66. return (d);
  67. }
  68. struct rpcent *getrpcbynumber(number)
  69. register int number;
  70. {
  71. register struct rpcdata *d = _rpcdata();
  72. register struct rpcent *p;
  73. if (d == 0)
  74. return (0);
  75. setrpcent(0);
  76. while ((p = getrpcent())) {
  77. if (p->r_number == number)
  78. break;
  79. }
  80. endrpcent();
  81. return (p);
  82. }
  83. struct rpcent *
  84. #ifdef __linux__
  85. getrpcbyname(const char *name)
  86. #else
  87. getrpcbyname(name)
  88. char *name;
  89. #endif
  90. {
  91. struct rpcent *rpc;
  92. char **rp;
  93. setrpcent(0);
  94. while ((rpc = getrpcent())) {
  95. if (strcmp(rpc->r_name, name) == 0)
  96. return (rpc);
  97. for (rp = rpc->r_aliases; *rp != NULL; rp++) {
  98. if (strcmp(*rp, name) == 0)
  99. return (rpc);
  100. }
  101. }
  102. endrpcent();
  103. return (NULL);
  104. }
  105. #ifdef __linux__
  106. void
  107. #endif
  108. setrpcent(f)
  109. int f;
  110. {
  111. register struct rpcdata *d = _rpcdata();
  112. if (d == 0)
  113. return;
  114. if (d->rpcf == NULL)
  115. d->rpcf = fopen(RPCDB, "r");
  116. else
  117. rewind(d->rpcf);
  118. if (d->current)
  119. free(d->current);
  120. d->current = NULL;
  121. d->stayopen |= f;
  122. }
  123. #ifdef __linux__
  124. void
  125. #endif
  126. endrpcent()
  127. {
  128. register struct rpcdata *d = _rpcdata();
  129. if (d == 0)
  130. return;
  131. if (d->current && !d->stayopen) {
  132. free(d->current);
  133. d->current = NULL;
  134. }
  135. if (d->rpcf && !d->stayopen) {
  136. fclose(d->rpcf);
  137. d->rpcf = NULL;
  138. }
  139. }
  140. struct rpcent *getrpcent()
  141. {
  142. register struct rpcdata *d = _rpcdata();
  143. if (d == 0)
  144. return (NULL);
  145. if (d->rpcf == NULL && (d->rpcf = fopen(RPCDB, "r")) == NULL)
  146. return (NULL);
  147. if (fgets(d->line, BUFSIZ, d->rpcf) == NULL)
  148. return (NULL);
  149. return interpret(d->line, strlen(d->line));
  150. }
  151. #ifdef __linux__
  152. static char *firstwhite(s)
  153. char *s;
  154. {
  155. char *s1, *s2;
  156. s1 = index(s, ' ');
  157. s2 = index(s, '\t');
  158. if (s1) {
  159. if (s2)
  160. return (s1 < s2) ? s1 : s2;
  161. else
  162. return s1;
  163. } else
  164. return s2;
  165. }
  166. #endif
  167. static struct rpcent *interpret(const char *val, int len)
  168. {
  169. register struct rpcdata *d = _rpcdata();
  170. char *p;
  171. register char *cp, **q;
  172. if (d == 0)
  173. return NULL;
  174. strncpy(d->line, val, len);
  175. p = d->line;
  176. d->line[len] = '\n';
  177. if (*p == '#')
  178. return (getrpcent());
  179. cp = index(p, '#');
  180. if (cp == NULL) {
  181. cp = index(p, '\n');
  182. if (cp == NULL)
  183. return (getrpcent());
  184. }
  185. *cp = '\0';
  186. #ifdef __linux__
  187. if ((cp = firstwhite(p)))
  188. *cp++ = 0;
  189. else
  190. return (getrpcent());
  191. #else
  192. cp = index(p, ' ');
  193. if (cp == NULL) {
  194. cp = index(p, '\t');
  195. if (cp == NULL)
  196. return (getrpcent());
  197. }
  198. *cp++ = '\0';
  199. #endif
  200. /* THIS STUFF IS INTERNET SPECIFIC */
  201. d->rpc.r_name = d->line;
  202. while (*cp == ' ' || *cp == '\t')
  203. cp++;
  204. d->rpc.r_number = atoi(cp);
  205. q = d->rpc.r_aliases = d->rpc_aliases;
  206. #ifdef __linux__
  207. if ((cp = firstwhite(cp)))
  208. *cp++ = '\0';
  209. #else
  210. cp = index(p, ' ');
  211. if (cp != NULL)
  212. *cp++ = '\0';
  213. else {
  214. cp = index(p, '\t');
  215. if (cp != NULL)
  216. *cp++ = '\0';
  217. }
  218. #endif
  219. while (cp && *cp) {
  220. if (*cp == ' ' || *cp == '\t') {
  221. cp++;
  222. continue;
  223. }
  224. if (q < &(d->rpc_aliases[MAXALIASES - 1]))
  225. *q++ = cp;
  226. #ifdef __linux__
  227. if ((cp = firstwhite(cp)))
  228. *cp++ = '\0';
  229. #else
  230. cp = index(p, ' ');
  231. if (cp != NULL)
  232. *cp++ = '\0';
  233. else {
  234. cp = index(p, '\t');
  235. if (cp != NULL)
  236. *cp++ = '\0';
  237. }
  238. #endif
  239. }
  240. *q = NULL;
  241. return (&d->rpc);
  242. }