getnetent.c 3.2 KB

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