getnetent.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. # define LOCK __pthread_mutex_lock(&mylock)
  27. # define UNLOCK __pthread_mutex_unlock(&mylock);
  28. #else
  29. # define LOCK
  30. # define UNLOCK
  31. #endif
  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. int _net_stayopen;
  39. void setnetent(int f)
  40. {
  41. LOCK;
  42. if (netf == NULL)
  43. netf = fopen(NETDB, "r" );
  44. else
  45. rewind(netf);
  46. _net_stayopen |= f;
  47. UNLOCK;
  48. return;
  49. }
  50. void endnetent(void)
  51. {
  52. LOCK;
  53. if (netf) {
  54. fclose(netf);
  55. netf = NULL;
  56. }
  57. _net_stayopen = 0;
  58. UNLOCK;
  59. }
  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 * 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. }