getrpcent.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. #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 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 attribute_hidden __endrpcent(void)
  68. {
  69. register struct rpcdata *d = _rpcdata();
  70. if (d == NULL)
  71. return;
  72. if (d->stayopen)
  73. return;
  74. if (d->current) {
  75. free(d->current);
  76. d->current = NULL;
  77. }
  78. if (d->rpcf) {
  79. fclose(d->rpcf);
  80. d->rpcf = NULL;
  81. }
  82. }
  83. strong_alias(__endrpcent,endrpcent)
  84. void attribute_hidden __setrpcent(int f)
  85. {
  86. register struct rpcdata *d = _rpcdata();
  87. if (d == NULL)
  88. return;
  89. if (d->rpcf == NULL)
  90. d->rpcf = fopen(RPCDB, "r");
  91. else
  92. rewind(d->rpcf);
  93. if (d->current)
  94. free(d->current);
  95. d->current = NULL;
  96. d->stayopen |= f;
  97. }
  98. strong_alias(__setrpcent,setrpcent)
  99. static struct rpcent *interpret(struct rpcdata *);
  100. static struct rpcent *__get_next_rpcent(struct rpcdata *d)
  101. {
  102. if (fgets(d->line, BUFSIZ, d->rpcf) == NULL)
  103. return NULL;
  104. return interpret(d);
  105. }
  106. struct rpcent attribute_hidden *__getrpcent(void)
  107. {
  108. register struct rpcdata *d = _rpcdata();
  109. if (d == NULL)
  110. return NULL;
  111. if (d->rpcf == NULL && (d->rpcf = fopen(RPCDB, "r")) == NULL)
  112. return NULL;
  113. return __get_next_rpcent(d);
  114. }
  115. strong_alias(__getrpcent,getrpcent)
  116. struct rpcent attribute_hidden *__getrpcbynumber(register int number)
  117. {
  118. register struct rpcdata *d = _rpcdata();
  119. register struct rpcent *rpc;
  120. if (d == NULL)
  121. return NULL;
  122. __setrpcent(0);
  123. while ((rpc = __getrpcent())) {
  124. if (rpc->r_number == number)
  125. break;
  126. }
  127. __endrpcent();
  128. return rpc;
  129. }
  130. strong_alias(__getrpcbynumber,getrpcbynumber)
  131. struct rpcent attribute_hidden *__getrpcbyname(const char *name)
  132. {
  133. struct rpcent *rpc;
  134. char **rp;
  135. __setrpcent(0);
  136. while ((rpc = __getrpcent())) {
  137. if (__strcmp(rpc->r_name, name) == 0)
  138. return rpc;
  139. for (rp = rpc->r_aliases; *rp != NULL; rp++) {
  140. if (__strcmp(*rp, name) == 0)
  141. return rpc;
  142. }
  143. }
  144. __endrpcent();
  145. return NULL;
  146. }
  147. strong_alias(__getrpcbyname,getrpcbyname)
  148. #ifdef __linux__
  149. static char *firstwhite(char *s)
  150. {
  151. char *s1, *s2;
  152. s1 = __strchr(s, ' ');
  153. s2 = __strchr(s, '\t');
  154. if (s1) {
  155. if (s2)
  156. return (s1 < s2) ? s1 : s2;
  157. else
  158. return s1;
  159. } else
  160. return s2;
  161. }
  162. #endif
  163. static struct rpcent *interpret(register struct rpcdata *d)
  164. {
  165. char *p;
  166. register char *cp, **q;
  167. p = d->line;
  168. d->line[__strlen(p)-1] = '\n';
  169. if (*p == '#')
  170. return __get_next_rpcent(d);
  171. cp = __strchr(p, '#');
  172. if (cp == NULL) {
  173. cp = __strchr(p, '\n');
  174. if (cp == NULL)
  175. return __get_next_rpcent(d);
  176. }
  177. *cp = '\0';
  178. #ifdef __linux__
  179. if ((cp = firstwhite(p)))
  180. *cp++ = 0;
  181. else
  182. return __get_next_rpcent(d);
  183. #else
  184. cp = __strchr(p, ' ');
  185. if (cp == NULL) {
  186. cp = __strchr(p, '\t');
  187. if (cp == NULL)
  188. return __get_next_rpcent(d);
  189. }
  190. *cp++ = '\0';
  191. #endif
  192. /* THIS STUFF IS INTERNET SPECIFIC */
  193. d->rpc.r_name = d->line;
  194. while (*cp == ' ' || *cp == '\t')
  195. cp++;
  196. d->rpc.r_number = atoi(cp);
  197. q = d->rpc.r_aliases = d->rpc_aliases;
  198. #ifdef __linux__
  199. if ((cp = firstwhite(cp)))
  200. *cp++ = '\0';
  201. #else
  202. cp = __strchr(p, ' ');
  203. if (cp != NULL)
  204. *cp++ = '\0';
  205. else {
  206. cp = __strchr(p, '\t');
  207. if (cp != NULL)
  208. *cp++ = '\0';
  209. }
  210. #endif
  211. while (cp && *cp) {
  212. if (*cp == ' ' || *cp == '\t') {
  213. cp++;
  214. continue;
  215. }
  216. if (q < &(d->rpc_aliases[MAXALIASES - 1]))
  217. *q++ = cp;
  218. #ifdef __linux__
  219. if ((cp = firstwhite(cp)))
  220. *cp++ = '\0';
  221. #else
  222. cp = __strchr(p, ' ');
  223. if (cp != NULL)
  224. *cp++ = '\0';
  225. else {
  226. cp = __strchr(p, '\t');
  227. if (cp != NULL)
  228. *cp++ = '\0';
  229. }
  230. #endif
  231. }
  232. *q = NULL;
  233. return &d->rpc;
  234. }
  235. #if defined(__UCLIBC_HAS_REENTRANT_RPC__)
  236. #if defined(__UCLIBC_HAS_THREADS__)
  237. # include <pthread.h>
  238. static pthread_mutex_t rpcdata_lock = PTHREAD_MUTEX_INITIALIZER;
  239. #endif
  240. #define LOCK __pthread_mutex_lock(&rpcdata_lock)
  241. #define UNLOCK __pthread_mutex_unlock(&rpcdata_lock)
  242. static int __copy_rpcent(struct rpcent *r, struct rpcent *result_buf, char *buffer,
  243. size_t buflen, struct rpcent **result)
  244. {
  245. size_t i, s;
  246. *result = NULL;
  247. if (!r)
  248. return ENOENT;
  249. /* copy the struct from the shared mem */
  250. __memset(result_buf, 0x00, sizeof(*result_buf));
  251. __memset(buffer, 0x00, buflen);
  252. result_buf->r_number = r->r_number;
  253. /* copy the aliases ... need to not only copy the alias strings,
  254. * but the array of pointers to the alias strings */
  255. i = 0;
  256. while (r->r_aliases[i++]) ;
  257. s = i-- * sizeof(char*);
  258. if (buflen < s)
  259. goto err_out;
  260. result_buf->r_aliases = (char**)buffer;
  261. buffer += s;
  262. buflen -= s;
  263. while (i-- > 0) {
  264. s = __strlen(r->r_aliases[i]) + 1;
  265. if (buflen < s)
  266. goto err_out;
  267. result_buf->r_aliases[i] = buffer;
  268. buffer += s;
  269. buflen -= s;
  270. __memcpy(result_buf->r_aliases[i], r->r_aliases[i], s);
  271. }
  272. /* copy the name */
  273. i = __strlen(r->r_name);
  274. if (buflen <= i)
  275. goto err_out;
  276. result_buf->r_name = buffer;
  277. __memcpy(result_buf->r_name, r->r_name, i);
  278. /* that was a hoot eh ? */
  279. *result = result_buf;
  280. return 0;
  281. err_out:
  282. return ERANGE;
  283. }
  284. int getrpcbynumber_r(int number, struct rpcent *result_buf, char *buffer,
  285. size_t buflen, struct rpcent **result)
  286. {
  287. int ret;
  288. LOCK;
  289. ret = __copy_rpcent(__getrpcbynumber(number), result_buf, buffer, buflen, result);
  290. UNLOCK;
  291. return ret;
  292. }
  293. int getrpcbyname_r(const char *name, struct rpcent *result_buf, char *buffer,
  294. size_t buflen, struct rpcent **result)
  295. {
  296. int ret;
  297. LOCK;
  298. ret = __copy_rpcent(__getrpcbyname(name), result_buf, buffer, buflen, result);
  299. UNLOCK;
  300. return ret;
  301. }
  302. int getrpcent_r(struct rpcent *result_buf, char *buffer,
  303. size_t buflen, struct rpcent **result)
  304. {
  305. int ret;
  306. LOCK;
  307. ret = __copy_rpcent(__getrpcent(), result_buf, buffer, buflen, result);
  308. UNLOCK;
  309. return ret;
  310. }
  311. #endif /* __UCLIBC_HAS_REENTRANT_RPC__ */