getrpcent.c 5.1 KB

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