getnetent.c 3.2 KB

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