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