getnetent.c 3.0 KB

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