getrpcent.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /* @(#)getrpcent.c 2.2 88/07/29 4.0 RPCSRC */
  2. /*
  3. * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  4. * unrestricted use provided that this legend is included on all tape
  5. * media and as a part of the software program in whole or part. Users
  6. * may copy or modify Sun RPC without charge, but are not authorized
  7. * to license or distribute it to anyone else except as part of a product or
  8. * program developed by the user.
  9. *
  10. * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  11. * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  12. * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  13. *
  14. * Sun RPC is provided with no support and without any obligation on the
  15. * part of Sun Microsystems, Inc. to assist in its use, correction,
  16. * modification or enhancement.
  17. *
  18. * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  19. * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  20. * OR ANY PART THEREOF.
  21. *
  22. * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  23. * or profits or other special, indirect and consequential damages, even if
  24. * Sun has been advised of the possibility of such damages.
  25. *
  26. * Sun Microsystems, Inc.
  27. * 2550 Garcia Avenue
  28. * Mountain View, California 94043
  29. */
  30. /*
  31. * Copyright (c) 1985 by Sun Microsystems, Inc.
  32. */
  33. #define __FORCE_GLIBC
  34. #include <features.h>
  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. #include <errno.h>
  43. /*
  44. * Internet version.
  45. */
  46. static struct rpcdata {
  47. FILE *rpcf;
  48. char *current;
  49. int currentlen;
  50. int stayopen;
  51. #define MAXALIASES 35
  52. char *rpc_aliases[MAXALIASES];
  53. struct rpcent rpc;
  54. char line[BUFSIZ + 1];
  55. char *domain;
  56. } *rpcdata;
  57. static const char RPCDB[] = "/etc/rpc";
  58. static struct rpcdata *_rpcdata(void)
  59. {
  60. register struct rpcdata *d = rpcdata;
  61. if (d == NULL) {
  62. d = (struct rpcdata *) calloc(1, sizeof(struct rpcdata));
  63. rpcdata = d;
  64. }
  65. return d;
  66. }
  67. void endrpcent(void)
  68. {
  69. register struct rpcdata *d = _rpcdata();
  70. if (d == NULL)
  71. return;
  72. if (d->stayopen)
  73. return;
  74. free(d->current);
  75. d->current = NULL;
  76. if (d->rpcf) {
  77. fclose(d->rpcf);
  78. d->rpcf = NULL;
  79. }
  80. }
  81. libc_hidden_def(endrpcent)
  82. void setrpcent(int f)
  83. {
  84. register struct rpcdata *d = _rpcdata();
  85. if (d == NULL)
  86. return;
  87. if (d->rpcf == NULL)
  88. d->rpcf = fopen(RPCDB, "r");
  89. else
  90. rewind(d->rpcf);
  91. free(d->current);
  92. d->current = NULL;
  93. d->stayopen |= f;
  94. }
  95. libc_hidden_def(setrpcent)
  96. static struct rpcent *interpret(struct rpcdata *);
  97. static struct rpcent *__get_next_rpcent(struct rpcdata *d)
  98. {
  99. if (fgets(d->line, BUFSIZ, d->rpcf) == NULL)
  100. return NULL;
  101. return interpret(d);
  102. }
  103. struct rpcent *getrpcent(void)
  104. {
  105. register struct rpcdata *d = _rpcdata();
  106. if (d == NULL)
  107. return NULL;
  108. if (d->rpcf == NULL && (d->rpcf = fopen(RPCDB, "r")) == NULL)
  109. return NULL;
  110. return __get_next_rpcent(d);
  111. }
  112. libc_hidden_def(getrpcent)
  113. struct rpcent *getrpcbynumber(register int number)
  114. {
  115. register struct rpcdata *d = _rpcdata();
  116. register struct rpcent *rpc;
  117. if (d == NULL)
  118. return NULL;
  119. setrpcent(0);
  120. while ((rpc = getrpcent())) {
  121. if (rpc->r_number == number)
  122. break;
  123. }
  124. endrpcent();
  125. return rpc;
  126. }
  127. libc_hidden_def(getrpcbynumber)
  128. struct rpcent *getrpcbyname(const char *name)
  129. {
  130. struct rpcent *rpc;
  131. char **rp;
  132. setrpcent(0);
  133. while ((rpc = getrpcent())) {
  134. if (strcmp(rpc->r_name, name) == 0)
  135. return rpc;
  136. for (rp = rpc->r_aliases; *rp != NULL; rp++) {
  137. if (strcmp(*rp, name) == 0)
  138. return rpc;
  139. }
  140. }
  141. endrpcent();
  142. return NULL;
  143. }
  144. libc_hidden_def(getrpcbyname)
  145. #ifdef __linux__
  146. static char *firstwhite(char *s)
  147. {
  148. char *s1, *s2;
  149. s1 = strchr(s, ' ');
  150. s2 = strchr(s, '\t');
  151. if (s1) {
  152. if (s2)
  153. return (s1 < s2) ? s1 : s2;
  154. else
  155. return s1;
  156. } else
  157. return s2;
  158. }
  159. #endif
  160. static struct rpcent *interpret(register struct rpcdata *d)
  161. {
  162. char *p;
  163. register char *cp, **q;
  164. p = d->line;
  165. d->line[strlen(p)-1] = '\n';
  166. if (*p == '#')
  167. return __get_next_rpcent(d);
  168. cp = strchr(p, '#');
  169. if (cp == NULL) {
  170. cp = strchr(p, '\n');
  171. if (cp == NULL)
  172. return __get_next_rpcent(d);
  173. }
  174. *cp = '\0';
  175. #ifdef __linux__
  176. if ((cp = firstwhite(p)))
  177. *cp++ = 0;
  178. else
  179. return __get_next_rpcent(d);
  180. #else
  181. cp = strchr(p, ' ');
  182. if (cp == NULL) {
  183. cp = strchr(p, '\t');
  184. if (cp == NULL)
  185. return __get_next_rpcent(d);
  186. }
  187. *cp++ = '\0';
  188. #endif
  189. /* THIS STUFF IS INTERNET SPECIFIC */
  190. d->rpc.r_name = d->line;
  191. while (*cp == ' ' || *cp == '\t')
  192. cp++;
  193. d->rpc.r_number = atoi(cp);
  194. q = d->rpc.r_aliases = d->rpc_aliases;
  195. #ifdef __linux__
  196. if ((cp = firstwhite(cp)))
  197. *cp++ = '\0';
  198. #else
  199. cp = strchr(p, ' ');
  200. if (cp != NULL)
  201. *cp++ = '\0';
  202. else {
  203. cp = strchr(p, '\t');
  204. if (cp != NULL)
  205. *cp++ = '\0';
  206. }
  207. #endif
  208. while (cp && *cp) {
  209. if (*cp == ' ' || *cp == '\t') {
  210. cp++;
  211. continue;
  212. }
  213. if (q < &(d->rpc_aliases[MAXALIASES - 1]))
  214. *q++ = cp;
  215. #ifdef __linux__
  216. if ((cp = firstwhite(cp)))
  217. *cp++ = '\0';
  218. #else
  219. cp = strchr(p, ' ');
  220. if (cp != NULL)
  221. *cp++ = '\0';
  222. else {
  223. cp = strchr(p, '\t');
  224. if (cp != NULL)
  225. *cp++ = '\0';
  226. }
  227. #endif
  228. }
  229. *q = NULL;
  230. return &d->rpc;
  231. }
  232. #if defined(__UCLIBC_HAS_REENTRANT_RPC__)
  233. #include <bits/uClibc_mutex.h>
  234. __UCLIBC_MUTEX_STATIC(mylock, PTHREAD_MUTEX_INITIALIZER);
  235. static int __copy_rpcent(struct rpcent *r, struct rpcent *result_buf, char *buffer,
  236. size_t buflen, struct rpcent **result)
  237. {
  238. size_t i, s;
  239. *result = NULL;
  240. if (!r)
  241. return ENOENT;
  242. /* copy the struct from the shared mem */
  243. memset(result_buf, 0x00, sizeof(*result_buf));
  244. memset(buffer, 0x00, buflen);
  245. result_buf->r_number = r->r_number;
  246. /* copy the aliases ... need to not only copy the alias strings,
  247. * but the array of pointers to the alias strings */
  248. i = 0;
  249. while (r->r_aliases[i++]) ;
  250. s = i-- * sizeof(char*);
  251. if (buflen < s)
  252. goto err_out;
  253. result_buf->r_aliases = (char**)buffer;
  254. buffer += s;
  255. buflen -= s;
  256. while (i-- > 0) {
  257. s = strlen(r->r_aliases[i]) + 1;
  258. if (buflen < s)
  259. goto err_out;
  260. result_buf->r_aliases[i] = buffer;
  261. buffer += s;
  262. buflen -= s;
  263. memcpy(result_buf->r_aliases[i], r->r_aliases[i], s);
  264. }
  265. /* copy the name */
  266. i = strlen(r->r_name);
  267. if (buflen <= i)
  268. goto err_out;
  269. result_buf->r_name = buffer;
  270. memcpy(result_buf->r_name, r->r_name, i);
  271. /* that was a hoot eh ? */
  272. *result = result_buf;
  273. return 0;
  274. err_out:
  275. return ERANGE;
  276. }
  277. int getrpcbynumber_r(int number, struct rpcent *result_buf, char *buffer,
  278. size_t buflen, struct rpcent **result)
  279. {
  280. int ret;
  281. __UCLIBC_MUTEX_LOCK(mylock);
  282. ret = __copy_rpcent(getrpcbynumber(number), result_buf, buffer, buflen, result);
  283. __UCLIBC_MUTEX_UNLOCK(mylock);
  284. return ret;
  285. }
  286. int getrpcbyname_r(const char *name, struct rpcent *result_buf, char *buffer,
  287. size_t buflen, struct rpcent **result)
  288. {
  289. int ret;
  290. __UCLIBC_MUTEX_LOCK(mylock);
  291. ret = __copy_rpcent(getrpcbyname(name), result_buf, buffer, buflen, result);
  292. __UCLIBC_MUTEX_UNLOCK(mylock);
  293. return ret;
  294. }
  295. int getrpcent_r(struct rpcent *result_buf, char *buffer,
  296. size_t buflen, struct rpcent **result)
  297. {
  298. int ret;
  299. __UCLIBC_MUTEX_LOCK(mylock);
  300. ret = __copy_rpcent(getrpcent(), result_buf, buffer, buflen, result);
  301. __UCLIBC_MUTEX_UNLOCK(mylock);
  302. return ret;
  303. }
  304. #endif /* __UCLIBC_HAS_REENTRANT_RPC__ */