getnetent.c 2.3 KB

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