getnetent.c 3.1 KB

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