getnetent.c 3.0 KB

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