getnetent.c 3.0 KB

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