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. libc_hidden_proto(__uc_malloc)
  31. #include <bits/uClibc_mutex.h>
  32. __UCLIBC_MUTEX_STATIC(mylock, PTHREAD_MUTEX_INITIALIZER);
  33. static const char NETDB[] = _PATH_NETWORKS;
  34. static FILE *netf = NULL;
  35. int _net_stayopen attribute_hidden;
  36. libc_hidden_proto(setnetent)
  37. void setnetent(int f)
  38. {
  39. __UCLIBC_MUTEX_LOCK(mylock);
  40. if (netf == NULL)
  41. netf = fopen(NETDB, "r" );
  42. else
  43. rewind(netf);
  44. _net_stayopen |= f;
  45. __UCLIBC_MUTEX_UNLOCK(mylock);
  46. return;
  47. }
  48. libc_hidden_def(setnetent)
  49. libc_hidden_proto(endnetent)
  50. void endnetent(void)
  51. {
  52. __UCLIBC_MUTEX_LOCK(mylock);
  53. if (netf) {
  54. fclose(netf);
  55. netf = NULL;
  56. }
  57. _net_stayopen = 0;
  58. __UCLIBC_MUTEX_UNLOCK(mylock);
  59. }
  60. libc_hidden_def(endnetent)
  61. static char * any(register char *cp, char *match)
  62. {
  63. register char *mp, c;
  64. while ((c = *cp)) {
  65. for (mp = match; *mp; mp++)
  66. if (*mp == c)
  67. return (cp);
  68. cp++;
  69. }
  70. return ((char *)0);
  71. }
  72. #define MAXALIASES 35
  73. static struct {
  74. char *line;
  75. struct netent net;
  76. char *net_aliases[MAXALIASES];
  77. } *sp;
  78. #define line (sp->line)
  79. #define net (sp->net)
  80. #define net_aliases (sp->net_aliases)
  81. #define INIT_SP() { \
  82. if (!sp) { \
  83. sp = __uc_malloc(sizeof(*sp)); \
  84. line = NULL; \
  85. } \
  86. }
  87. libc_hidden_proto(getnetent)
  88. struct netent *getnetent(void)
  89. {
  90. char *p;
  91. register char *cp, **q;
  92. struct netent *rv = NULL;
  93. INIT_SP();
  94. __UCLIBC_MUTEX_LOCK(mylock);
  95. if (netf == NULL && (netf = fopen(NETDB, "r" )) == NULL) {
  96. goto DONE;
  97. }
  98. again:
  99. if (!line) {
  100. line = __uc_malloc(BUFSIZ + 1);
  101. }
  102. p = fgets(line, BUFSIZ, netf);
  103. if (p == NULL) {
  104. goto DONE;
  105. }
  106. if (*p == '#')
  107. goto again;
  108. cp = any(p, "#\n");
  109. if (cp == NULL)
  110. goto again;
  111. *cp = '\0';
  112. net.n_name = p;
  113. cp = any(p, " \t");
  114. if (cp == NULL)
  115. goto again;
  116. *cp++ = '\0';
  117. while (*cp == ' ' || *cp == '\t')
  118. cp++;
  119. p = any(cp, " \t");
  120. if (p != NULL)
  121. *p++ = '\0';
  122. net.n_net = inet_network(cp);
  123. net.n_addrtype = AF_INET;
  124. q = net.n_aliases = net_aliases;
  125. if (p != NULL)
  126. cp = p;
  127. while (cp && *cp) {
  128. if (*cp == ' ' || *cp == '\t') {
  129. cp++;
  130. continue;
  131. }
  132. if (q < &net_aliases[MAXALIASES - 1])
  133. *q++ = cp;
  134. cp = any(cp, " \t");
  135. if (cp != NULL)
  136. *cp++ = '\0';
  137. }
  138. *q = NULL;
  139. rv = &net;
  140. DONE:
  141. __UCLIBC_MUTEX_UNLOCK(mylock);
  142. return rv;
  143. }
  144. libc_hidden_def(getnetent)