getrpcent.c 5.3 KB

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