getnetent.c 2.8 KB

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