getnetent.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Copyright (c) 1983 Regents of the University of California.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms are permitted
  6. * provided that the above copyright notice and this paragraph are
  7. * duplicated in all such forms and that any documentation,
  8. * advertising materials, and other materials related to such
  9. * distribution and use acknowledge that the software was developed
  10. * by the University of California, Berkeley. The name of the
  11. * University may not be used to endorse or promote products derived
  12. * from this software without specific prior written permission.
  13. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  14. * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  15. * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  16. */
  17. #define __FORCE_GLIBC
  18. #include <features.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <malloc.h>
  22. #include <netdb.h>
  23. #include <arpa/inet.h>
  24. libc_hidden_proto(fopen)
  25. libc_hidden_proto(fclose)
  26. libc_hidden_proto(inet_network)
  27. libc_hidden_proto(rewind)
  28. libc_hidden_proto(fgets)
  29. libc_hidden_proto(abort)
  30. #include <bits/uClibc_mutex.h>
  31. __UCLIBC_MUTEX_STATIC(mylock, PTHREAD_MUTEX_INITIALIZER);
  32. static const char NETDB[] = _PATH_NETWORKS;
  33. static FILE *netf = NULL;
  34. int _net_stayopen attribute_hidden;
  35. libc_hidden_proto(setnetent)
  36. void setnetent(int f)
  37. {
  38. __UCLIBC_MUTEX_LOCK(mylock);
  39. if (netf == NULL)
  40. netf = fopen(NETDB, "r" );
  41. else
  42. rewind(netf);
  43. _net_stayopen |= f;
  44. __UCLIBC_MUTEX_UNLOCK(mylock);
  45. return;
  46. }
  47. libc_hidden_def(setnetent)
  48. libc_hidden_proto(endnetent)
  49. void endnetent(void)
  50. {
  51. __UCLIBC_MUTEX_LOCK(mylock);
  52. if (netf) {
  53. fclose(netf);
  54. netf = NULL;
  55. }
  56. _net_stayopen = 0;
  57. __UCLIBC_MUTEX_UNLOCK(mylock);
  58. }
  59. libc_hidden_def(endnetent)
  60. static char * any(register char *cp, char *match)
  61. {
  62. register char *mp, c;
  63. while ((c = *cp)) {
  64. for (mp = match; *mp; mp++)
  65. if (*mp == c)
  66. return (cp);
  67. cp++;
  68. }
  69. return ((char *)0);
  70. }
  71. #define MAXALIASES 35
  72. static struct {
  73. char *line;
  74. struct netent net;
  75. char *net_aliases[MAXALIASES];
  76. } *sp;
  77. #define line (sp->line)
  78. #define net (sp->net)
  79. #define net_aliases (sp->net_aliases)
  80. #define INIT_SP() { \
  81. if (!sp) { \
  82. sp = __uc_malloc(sizeof(*sp)); \
  83. line = NULL; \
  84. } \
  85. }
  86. libc_hidden_proto(getnetent)
  87. struct netent *getnetent(void)
  88. {
  89. char *p;
  90. register char *cp, **q;
  91. struct netent *rv = NULL;
  92. INIT_SP();
  93. __UCLIBC_MUTEX_LOCK(mylock);
  94. if (netf == NULL && (netf = fopen(NETDB, "r" )) == NULL) {
  95. goto DONE;
  96. }
  97. again:
  98. if (!line) {
  99. line = malloc(BUFSIZ + 1);
  100. if (!line)
  101. abort();
  102. }
  103. p = fgets(line, BUFSIZ, netf);
  104. if (p == NULL) {
  105. goto DONE;
  106. }
  107. if (*p == '#')
  108. goto again;
  109. cp = any(p, "#\n");
  110. if (cp == NULL)
  111. goto again;
  112. *cp = '\0';
  113. net.n_name = p;
  114. cp = any(p, " \t");
  115. if (cp == NULL)
  116. goto again;
  117. *cp++ = '\0';
  118. while (*cp == ' ' || *cp == '\t')
  119. cp++;
  120. p = any(cp, " \t");
  121. if (p != NULL)
  122. *p++ = '\0';
  123. net.n_net = inet_network(cp);
  124. net.n_addrtype = AF_INET;
  125. q = net.n_aliases = net_aliases;
  126. if (p != NULL)
  127. cp = p;
  128. while (cp && *cp) {
  129. if (*cp == ' ' || *cp == '\t') {
  130. cp++;
  131. continue;
  132. }
  133. if (q < &net_aliases[MAXALIASES - 1])
  134. *q++ = cp;
  135. cp = any(cp, " \t");
  136. if (cp != NULL)
  137. *cp++ = '\0';
  138. }
  139. *q = NULL;
  140. rv = &net;
  141. DONE:
  142. __UCLIBC_MUTEX_UNLOCK(mylock);
  143. return rv;
  144. }
  145. libc_hidden_def(getnetent)