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. void setnetent(int f)
  43. {
  44. LOCK;
  45. if (netf == NULL)
  46. netf = fopen(NETDB, "r" );
  47. else
  48. rewind(netf);
  49. _net_stayopen |= f;
  50. UNLOCK;
  51. return;
  52. }
  53. libc_hidden_proto(setnetent)
  54. libc_hidden_def(setnetent)
  55. void endnetent(void)
  56. {
  57. LOCK;
  58. if (netf) {
  59. fclose(netf);
  60. netf = NULL;
  61. }
  62. _net_stayopen = 0;
  63. UNLOCK;
  64. }
  65. libc_hidden_proto(endnetent)
  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. struct netent *getnetent(void)
  79. {
  80. char *p;
  81. register char *cp, **q;
  82. LOCK;
  83. if (netf == NULL && (netf = fopen(NETDB, "r" )) == NULL) {
  84. UNLOCK;
  85. return (NULL);
  86. }
  87. again:
  88. if (!line) {
  89. line = malloc(BUFSIZ + 1);
  90. if (!line)
  91. abort();
  92. }
  93. p = fgets(line, BUFSIZ, netf);
  94. if (p == NULL) {
  95. UNLOCK;
  96. return (NULL);
  97. }
  98. if (*p == '#')
  99. goto again;
  100. cp = any(p, "#\n");
  101. if (cp == NULL)
  102. goto again;
  103. *cp = '\0';
  104. net.n_name = p;
  105. cp = any(p, " \t");
  106. if (cp == NULL)
  107. goto again;
  108. *cp++ = '\0';
  109. while (*cp == ' ' || *cp == '\t')
  110. cp++;
  111. p = any(cp, " \t");
  112. if (p != NULL)
  113. *p++ = '\0';
  114. net.n_net = inet_network(cp);
  115. net.n_addrtype = AF_INET;
  116. q = net.n_aliases = net_aliases;
  117. if (p != NULL)
  118. cp = p;
  119. while (cp && *cp) {
  120. if (*cp == ' ' || *cp == '\t') {
  121. cp++;
  122. continue;
  123. }
  124. if (q < &net_aliases[MAXALIASES - 1])
  125. *q++ = cp;
  126. cp = any(cp, " \t");
  127. if (cp != NULL)
  128. *cp++ = '\0';
  129. }
  130. *q = NULL;
  131. UNLOCK;
  132. return (&net);
  133. }
  134. libc_hidden_proto(getnetent)
  135. libc_hidden_def(getnetent)