getrpcent.c 7.4 KB

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