getrpcent.c 5.3 KB

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