getrpcent.c 7.4 KB

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