getnetent.c 3.0 KB

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