xref: /csrg-svn/lib/libc/gen/getusershell.c (revision 61111)
125429Smckusick /*
2*61111Sbostic  * Copyright (c) 1985, 1993
3*61111Sbostic  *	The Regents of the University of California.  All rights reserved.
435109Sbostic  *
542625Sbostic  * %sccs.include.redist.c%
625429Smckusick  */
725429Smckusick 
826564Sdonn #if defined(LIBC_SCCS) && !defined(lint)
9*61111Sbostic static char sccsid[] = "@(#)getusershell.c	8.1 (Berkeley) 06/04/93";
1035109Sbostic #endif /* LIBC_SCCS and not lint */
1125429Smckusick 
1225429Smckusick #include <sys/param.h>
1325429Smckusick #include <sys/file.h>
1425429Smckusick #include <sys/stat.h>
1553280Sbostic #include <stdio.h>
1625429Smckusick #include <ctype.h>
1746597Sdonn #include <stdlib.h>
1846597Sdonn #include <unistd.h>
1953280Sbostic #include <paths.h>
2025429Smckusick 
2125429Smckusick /*
2253280Sbostic  * Local shells should NOT be added here.  They should be added in
2353280Sbostic  * /etc/shells.
2425429Smckusick  */
2525429Smckusick 
2653280Sbostic static char *okshells[] = { _PATH_BSHELL, _PATH_CSHELL, NULL };
2753280Sbostic static char **curshell, **shells, *strings;
2853280Sbostic static char **initshells __P((void));
2925429Smckusick 
3025429Smckusick /*
3153280Sbostic  * Get a list of shells from _PATH_SHELLS, if it exists.
3225429Smckusick  */
3325429Smckusick char *
getusershell()3425429Smckusick getusershell()
3525429Smckusick {
3625429Smckusick 	char *ret;
3725429Smckusick 
3829662Sbloom 	if (curshell == NULL)
3929662Sbloom 		curshell = initshells();
4029662Sbloom 	ret = *curshell;
4129662Sbloom 	if (ret != NULL)
4229662Sbloom 		curshell++;
4325429Smckusick 	return (ret);
4425429Smckusick }
4525429Smckusick 
4646597Sdonn void
endusershell()4725429Smckusick endusershell()
4825429Smckusick {
4925429Smckusick 
5025429Smckusick 	if (shells != NULL)
5153280Sbostic 		free(shells);
5225429Smckusick 	shells = NULL;
5325429Smckusick 	if (strings != NULL)
5425429Smckusick 		free(strings);
5525429Smckusick 	strings = NULL;
5629662Sbloom 	curshell = NULL;
5725429Smckusick }
5825429Smckusick 
5946597Sdonn void
setusershell()6025429Smckusick setusershell()
6125429Smckusick {
6225429Smckusick 
6329662Sbloom 	curshell = initshells();
6425429Smckusick }
6525429Smckusick 
6625429Smckusick static char **
initshells()6725429Smckusick initshells()
6825429Smckusick {
6925429Smckusick 	register char **sp, *cp;
7025429Smckusick 	register FILE *fp;
7125429Smckusick 	struct stat statb;
7225429Smckusick 
7325429Smckusick 	if (shells != NULL)
7453280Sbostic 		free(shells);
7525429Smckusick 	shells = NULL;
7625429Smckusick 	if (strings != NULL)
7725429Smckusick 		free(strings);
7825429Smckusick 	strings = NULL;
7953280Sbostic 	if ((fp = fopen(_PATH_SHELLS, "r")) == NULL)
8053280Sbostic 		return (okshells);
8125429Smckusick 	if (fstat(fileno(fp), &statb) == -1) {
8225429Smckusick 		(void)fclose(fp);
8353280Sbostic 		return (okshells);
8425429Smckusick 	}
8553280Sbostic 	if ((strings = malloc((u_int)statb.st_size)) == NULL) {
8625429Smckusick 		(void)fclose(fp);
8753280Sbostic 		return (okshells);
8825429Smckusick 	}
8953280Sbostic 	shells = calloc((unsigned)statb.st_size / 3, sizeof (char *));
9025429Smckusick 	if (shells == NULL) {
9125429Smckusick 		(void)fclose(fp);
9225429Smckusick 		free(strings);
9325429Smckusick 		strings = NULL;
9453280Sbostic 		return (okshells);
9525429Smckusick 	}
9625429Smckusick 	sp = shells;
9725429Smckusick 	cp = strings;
9825429Smckusick 	while (fgets(cp, MAXPATHLEN + 1, fp) != NULL) {
9929662Sbloom 		while (*cp != '#' && *cp != '/' && *cp != '\0')
10025429Smckusick 			cp++;
10129662Sbloom 		if (*cp == '#' || *cp == '\0')
10225429Smckusick 			continue;
10325429Smckusick 		*sp++ = cp;
10425429Smckusick 		while (!isspace(*cp) && *cp != '#' && *cp != '\0')
10525429Smckusick 			cp++;
10625429Smckusick 		*cp++ = '\0';
10725429Smckusick 	}
10853280Sbostic 	*sp = NULL;
10925429Smckusick 	(void)fclose(fp);
11025429Smckusick 	return (shells);
11125429Smckusick }
112