getnetent.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 <malloc.h>
  22. #include <netdb.h>
  23. #include <arpa/inet.h>
  24. #include <unistd.h>
  25. libc_hidden_proto(fopen)
  26. libc_hidden_proto(fclose)
  27. libc_hidden_proto(inet_network)
  28. libc_hidden_proto(rewind)
  29. libc_hidden_proto(fgets)
  30. libc_hidden_proto(abort)
  31. libc_hidden_proto(__uc_malloc)
  32. #include <bits/uClibc_mutex.h>
  33. __UCLIBC_MUTEX_STATIC(mylock, PTHREAD_MUTEX_INITIALIZER);
  34. #define MAXALIASES 35
  35. static const char NETDB[] = _PATH_NETWORKS;
  36. static FILE *netf = NULL;
  37. static char *line = NULL;
  38. static struct netent net;
  39. static char *net_aliases[MAXALIASES];
  40. smallint _net_stayopen attribute_hidden;
  41. libc_hidden_proto(setnetent)
  42. void setnetent(int f)
  43. {
  44. __UCLIBC_MUTEX_LOCK(mylock);
  45. if (netf == NULL)
  46. netf = fopen(NETDB, "r" );
  47. else
  48. rewind(netf);
  49. if (f) _net_stayopen = 1;
  50. __UCLIBC_MUTEX_UNLOCK(mylock);
  51. return;
  52. }
  53. libc_hidden_def(setnetent)
  54. libc_hidden_proto(endnetent)
  55. void endnetent(void)
  56. {
  57. __UCLIBC_MUTEX_LOCK(mylock);
  58. if (netf) {
  59. fclose(netf);
  60. netf = NULL;
  61. }
  62. _net_stayopen = 0;
  63. __UCLIBC_MUTEX_UNLOCK(mylock);
  64. }
  65. libc_hidden_def(endnetent)
  66. static char * any(register char *cp, char *match)
  67. {
  68. register char *mp, c;
  69. while ((c = *cp)) {
  70. for (mp = match; *mp; mp++)
  71. if (*mp == c)
  72. return (cp);
  73. cp++;
  74. }
  75. return ((char *)0);
  76. }
  77. libc_hidden_proto(getnetent)
  78. struct netent *getnetent(void)
  79. {
  80. char *p;
  81. register char *cp, **q;
  82. struct netent *rv = NULL;
  83. __UCLIBC_MUTEX_LOCK(mylock);
  84. if (netf == NULL && (netf = fopen(NETDB, "r" )) == NULL) {
  85. goto DONE;
  86. }
  87. again:
  88. if (!line) {
  89. line = __uc_malloc(BUFSIZ + 1);
  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)