getnetent.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. #include <unistd.h>
  25. libc_hidden_proto(fopen)
  26. libc_hidden_proto(fclose)
  27. libc_hidden_proto(inet_network)
  28. libc_hidden_proto(rewind)
  29. libc_hidden_proto(fgets)
  30. libc_hidden_proto(abort)
  31. libc_hidden_proto(__uc_malloc)
  32. #include <bits/uClibc_mutex.h>
  33. __UCLIBC_MUTEX_STATIC(mylock, PTHREAD_MUTEX_INITIALIZER);
  34. static const char NETDB[] = _PATH_NETWORKS;
  35. static FILE *netf = NULL;
  36. smallint _net_stayopen attribute_hidden;
  37. libc_hidden_proto(setnetent)
  38. void setnetent(int f)
  39. {
  40. __UCLIBC_MUTEX_LOCK(mylock);
  41. if (netf == NULL)
  42. netf = fopen(NETDB, "r" );
  43. else
  44. rewind(netf);
  45. if (f) _net_stayopen = 1;
  46. __UCLIBC_MUTEX_UNLOCK(mylock);
  47. return;
  48. }
  49. libc_hidden_def(setnetent)
  50. libc_hidden_proto(endnetent)
  51. void endnetent(void)
  52. {
  53. __UCLIBC_MUTEX_LOCK(mylock);
  54. if (netf) {
  55. fclose(netf);
  56. netf = NULL;
  57. }
  58. _net_stayopen = 0;
  59. __UCLIBC_MUTEX_UNLOCK(mylock);
  60. }
  61. libc_hidden_def(endnetent)
  62. static char * any(register char *cp, char *match)
  63. {
  64. register char *mp, c;
  65. while ((c = *cp)) {
  66. for (mp = match; *mp; mp++)
  67. if (*mp == c)
  68. return (cp);
  69. cp++;
  70. }
  71. return ((char *)0);
  72. }
  73. #define MAXALIASES 35
  74. static struct {
  75. char *line;
  76. struct netent net;
  77. char *net_aliases[MAXALIASES];
  78. } *sp;
  79. #define line (sp->line)
  80. #define net (sp->net)
  81. #define net_aliases (sp->net_aliases)
  82. #define INIT_SP() { \
  83. if (!sp) { \
  84. sp = __uc_malloc(sizeof(*sp)); \
  85. line = NULL; \
  86. } \
  87. }
  88. libc_hidden_proto(getnetent)
  89. struct netent *getnetent(void)
  90. {
  91. char *p;
  92. register char *cp, **q;
  93. struct netent *rv = NULL;
  94. INIT_SP();
  95. __UCLIBC_MUTEX_LOCK(mylock);
  96. if (netf == NULL && (netf = fopen(NETDB, "r" )) == NULL) {
  97. goto DONE;
  98. }
  99. again:
  100. if (!line) {
  101. line = __uc_malloc(BUFSIZ + 1);
  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)