Set LC_ALL=C before sed.
[glibc.git] / misc / getusershell.c
blob2e8d97e65c77ec4a9f59ead566166d542b488ace
1 /*
2 * Copyright (c) 1985, 1993
3 * The Regents of the University of California. All rights reserved.
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.
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.
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 */
34 #include <sys/param.h>
35 #include <sys/file.h>
36 #include <sys/stat.h>
37 #include <stdio.h>
38 #include <stdio_ext.h>
39 #include <ctype.h>
40 #include <stdlib.h>
41 #include <unistd.h>
42 #include <paths.h>
45 * Local shells should NOT be added here. They should be added in
46 * /etc/shells.
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
54 static const char *const okshells[] = { _PATH_BSHELL, _PATH_CSHELL, NULL };
55 #else
56 static const char *okshells[3];
57 #endif
58 static char **curshell, **shells, *strings;
59 static char **initshells (void) __THROW;
62 * Get a list of shells from _PATH_SHELLS, if it exists.
64 char *
65 getusershell()
67 char *ret;
69 if (curshell == NULL)
70 curshell = initshells();
71 ret = *curshell;
72 if (ret != NULL)
73 curshell++;
74 return (ret);
77 void
78 endusershell()
81 free(shells);
82 shells = NULL;
83 free(strings);
84 strings = NULL;
85 curshell = NULL;
88 void
89 setusershell()
92 curshell = initshells();
95 static char **
96 initshells()
98 register char **sp, *cp;
99 register FILE *fp;
100 struct stat64 statb;
101 size_t flen;
103 free(shells);
104 shells = NULL;
105 free(strings);
106 strings = NULL;
107 if ((fp = fopen(_PATH_SHELLS, "rce")) == NULL)
108 goto init_okshells_noclose;
109 if (fstat64(fileno(fp), &statb) == -1) {
110 init_okshells:
111 (void)fclose(fp);
112 init_okshells_noclose:
113 okshells[0] = _PATH_BSHELL;
114 okshells[1] = _PATH_CSHELL;
115 return (char **) okshells;
117 if (statb.st_size > ~(size_t)0 / sizeof (char *) * 3)
118 goto init_okshells;
119 flen = statb.st_size + 3;
120 if ((strings = malloc(flen)) == NULL)
121 goto init_okshells;
122 shells = malloc(statb.st_size / 3 * sizeof (char *));
123 if (shells == NULL) {
124 free(strings);
125 strings = NULL;
126 goto init_okshells;
128 sp = shells;
129 cp = strings;
130 while (fgets_unlocked(cp, flen - (cp - strings), fp) != NULL) {
131 while (*cp != '#' && *cp != '/' && *cp != '\0')
132 cp++;
133 if (*cp == '#' || *cp == '\0' || cp[1] == '\0')
134 continue;
135 *sp++ = cp;
136 while (!isspace(*cp) && *cp != '#' && *cp != '\0')
137 cp++;
138 *cp++ = '\0';
140 *sp = NULL;
141 (void)fclose(fp);
142 return (shells);