getusershell.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright (c) 1985, 1993
  3. * The Regents of the University of California. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 4. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. #if defined(LIBC_SCCS) && !defined(lint)
  30. static char sccsid[] = "@(#)getusershell.c 8.1 (Berkeley) 6/4/93";
  31. #endif /* LIBC_SCCS and not lint */
  32. #include <sys/param.h>
  33. #include <sys/file.h>
  34. #include <sys/stat.h>
  35. #include <stdio.h>
  36. #include <stdio_ext.h>
  37. #include <ctype.h>
  38. #include <stdlib.h>
  39. #include <unistd.h>
  40. #include <paths.h>
  41. /*
  42. * Local shells should NOT be added here. They should be added in
  43. * /etc/shells.
  44. */
  45. /* NB: we do not initialize okshells here. The initialization needs
  46. relocations. These interfaces are used so rarely that this is not
  47. justified. Instead explicitly initialize the array when it is
  48. used. */
  49. #if 0
  50. static const char *const okshells[] = { _PATH_BSHELL, _PATH_CSHELL, NULL };
  51. #else
  52. static const char *okshells[3];
  53. #endif
  54. static char **curshell, **shells, *strings;
  55. static char **initshells (void) __THROW;
  56. /*
  57. * Get a list of shells from _PATH_SHELLS, if it exists.
  58. */
  59. char *
  60. getusershell (void)
  61. {
  62. char *ret;
  63. if (curshell == NULL)
  64. curshell = initshells();
  65. ret = *curshell;
  66. if (ret != NULL)
  67. curshell++;
  68. return (ret);
  69. }
  70. void
  71. endusershell (void)
  72. {
  73. free(shells);
  74. shells = NULL;
  75. free(strings);
  76. strings = NULL;
  77. curshell = NULL;
  78. }
  79. void
  80. setusershell (void)
  81. {
  82. curshell = initshells();
  83. }
  84. static char **
  85. initshells (void)
  86. {
  87. char **sp, *cp;
  88. FILE *fp;
  89. struct stat64 statb;
  90. size_t flen;
  91. free(shells);
  92. shells = NULL;
  93. free(strings);
  94. strings = NULL;
  95. if ((fp = fopen(_PATH_SHELLS, "rce")) == NULL)
  96. goto init_okshells_noclose;
  97. if (fstat64(fileno(fp), &statb) == -1) {
  98. init_okshells:
  99. (void)fclose(fp);
  100. init_okshells_noclose:
  101. okshells[0] = _PATH_BSHELL;
  102. okshells[1] = _PATH_CSHELL;
  103. return (char **) okshells;
  104. }
  105. if (statb.st_size > ~(size_t)0 / sizeof (char *) * 3)
  106. goto init_okshells;
  107. flen = statb.st_size + 3;
  108. if ((strings = malloc(flen)) == NULL)
  109. goto init_okshells;
  110. shells = malloc(statb.st_size / 3 * sizeof (char *));
  111. if (shells == NULL) {
  112. free(strings);
  113. strings = NULL;
  114. goto init_okshells;
  115. }
  116. sp = shells;
  117. cp = strings;
  118. while (fgets_unlocked(cp, flen - (cp - strings), fp) != NULL) {
  119. while (*cp != '#' && *cp != '/' && *cp != '\0')
  120. cp++;
  121. if (*cp == '#' || *cp == '\0' || cp[1] == '\0')
  122. continue;
  123. *sp++ = cp;
  124. while (!isspace(*cp) && *cp != '#' && *cp != '\0')
  125. cp++;
  126. *cp++ = '\0';
  127. }
  128. *sp = NULL;
  129. (void)fclose(fp);
  130. return (shells);
  131. }