getrpcent.c 7.4 KB

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