getrpcent.c 4.9 KB

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