getnetent.c 3.3 KB

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