getrpcent.c 5.2 KB

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