]>
Commit | Line | Data |
---|---|---|
28f540f4 RM |
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. | |
28f540f4 RM |
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 | ||
30 | #if defined(LIBC_SCCS) && !defined(lint) | |
31 | static char sccsid[] = "@(#)getusershell.c 8.1 (Berkeley) 6/4/93"; | |
32 | #endif /* LIBC_SCCS and not lint */ | |
33 | ||
34 | #include <sys/param.h> | |
35 | #include <sys/file.h> | |
36 | #include <sys/stat.h> | |
37 | #include <stdio.h> | |
2706ee38 | 38 | #include <stdio_ext.h> |
28f540f4 RM |
39 | #include <ctype.h> |
40 | #include <stdlib.h> | |
41 | #include <unistd.h> | |
42 | #include <paths.h> | |
43 | ||
44 | /* | |
45 | * Local shells should NOT be added here. They should be added in | |
46 | * /etc/shells. | |
47 | */ | |
48 | ||
02264020 UD |
49 | /* NB: we do not initialize okshells here. The initialization needs |
50 | relocations. These interfaces are used so rarely that this is not | |
51 | justified. Instead explicitly initialize the array when it is | |
52 | used. */ | |
53 | #if 0 | |
970362dc | 54 | static const char *const okshells[] = { _PATH_BSHELL, _PATH_CSHELL, NULL }; |
02264020 UD |
55 | #else |
56 | static const char *okshells[3]; | |
57 | #endif | |
05d4c982 | 58 | static char **curshell, **shells, *strings; |
79937577 | 59 | static char **initshells (void) __THROW; |
28f540f4 RM |
60 | |
61 | /* | |
62 | * Get a list of shells from _PATH_SHELLS, if it exists. | |
63 | */ | |
64 | char * | |
60d2f8f3 | 65 | getusershell (void) |
28f540f4 | 66 | { |
05d4c982 | 67 | char *ret; |
28f540f4 RM |
68 | |
69 | if (curshell == NULL) | |
70 | curshell = initshells(); | |
71 | ret = *curshell; | |
72 | if (ret != NULL) | |
73 | curshell++; | |
05d4c982 | 74 | return (ret); |
28f540f4 RM |
75 | } |
76 | ||
77 | void | |
60d2f8f3 | 78 | endusershell (void) |
28f540f4 | 79 | { |
05d4c982 | 80 | |
970362dc | 81 | free(shells); |
28f540f4 | 82 | shells = NULL; |
970362dc | 83 | free(strings); |
28f540f4 RM |
84 | strings = NULL; |
85 | curshell = NULL; | |
86 | } | |
87 | ||
88 | void | |
60d2f8f3 | 89 | setusershell (void) |
28f540f4 RM |
90 | { |
91 | ||
92 | curshell = initshells(); | |
93 | } | |
94 | ||
05d4c982 | 95 | static char ** |
60d2f8f3 | 96 | initshells (void) |
28f540f4 | 97 | { |
2e09a79a JM |
98 | char **sp, *cp; |
99 | FILE *fp; | |
8edf6e0d | 100 | struct stat64 statb; |
66f17705 | 101 | size_t flen; |
28f540f4 | 102 | |
970362dc | 103 | free(shells); |
28f540f4 | 104 | shells = NULL; |
970362dc | 105 | free(strings); |
28f540f4 | 106 | strings = NULL; |
312be3f9 | 107 | if ((fp = fopen(_PATH_SHELLS, "rce")) == NULL) |
02264020 | 108 | goto init_okshells_noclose; |
8edf6e0d | 109 | if (fstat64(fileno(fp), &statb) == -1) { |
02264020 | 110 | init_okshells: |
28f540f4 | 111 | (void)fclose(fp); |
02264020 UD |
112 | init_okshells_noclose: |
113 | okshells[0] = _PATH_BSHELL; | |
114 | okshells[1] = _PATH_CSHELL; | |
49f3a758 | 115 | return (char **) okshells; |
28f540f4 | 116 | } |
66f17705 | 117 | if (statb.st_size > ~(size_t)0 / sizeof (char *) * 3) |
02264020 | 118 | goto init_okshells; |
caa6e772 UD |
119 | flen = statb.st_size + 3; |
120 | if ((strings = malloc(flen)) == NULL) | |
66f17705 UD |
121 | goto init_okshells; |
122 | shells = malloc(statb.st_size / 3 * sizeof (char *)); | |
28f540f4 | 123 | if (shells == NULL) { |
28f540f4 RM |
124 | free(strings); |
125 | strings = NULL; | |
02264020 | 126 | goto init_okshells; |
28f540f4 | 127 | } |
05d4c982 RM |
128 | sp = shells; |
129 | cp = strings; | |
71412a8c | 130 | while (fgets_unlocked(cp, flen - (cp - strings), fp) != NULL) { |
28f540f4 RM |
131 | while (*cp != '#' && *cp != '/' && *cp != '\0') |
132 | cp++; | |
66f17705 | 133 | if (*cp == '#' || *cp == '\0' || cp[1] == '\0') |
28f540f4 RM |
134 | continue; |
135 | *sp++ = cp; | |
136 | while (!isspace(*cp) && *cp != '#' && *cp != '\0') | |
137 | cp++; | |
138 | *cp++ = '\0'; | |
139 | } | |
140 | *sp = NULL; | |
141 | (void)fclose(fp); | |
142 | return (shells); | |
143 | } |