getnetent.c 2.4 KB

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