getrpcent.c 7.1 KB

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