xref: /titanic_52/usr/src/lib/libbc/libc/gen/common/getusershell.c (revision bdfc6d18da790deeec2e0eb09c625902defe2498)
1 #pragma ident	"%Z%%M%	%I%	%E% SMI"
2 	  /* from UCB 5.4 7/25/86 */
3 /*
4  * Copyright (c) 1985 Regents of the University of California.
5  * All rights reserved.  The Berkeley software License Agreement
6  * specifies the terms and conditions for redistribution.
7  */
8 
9 #include <sys/param.h>
10 #include <sys/file.h>
11 #include <sys/stat.h>
12 #include <ctype.h>
13 #include <stdio.h>
14 
15 #define SHELLS "/etc/shells"
16 
17 /*
18  * Do not add local shells here.  They should be added in /etc/shells
19  */
20 static char *okshells[] =
21     { "/bin/sh", "/bin/csh", "/usr/bin/sh", "/usr/bin/csh", 0 };
22 
23 static char **shells, *strings;
24 static char **curshell;
25 extern char **initshells();
26 
27 /*
28  * Get a list of shells from SHELLS, if it exists.
29  */
30 char *
31 getusershell()
32 {
33 	char *ret;
34 
35 	if (curshell == NULL)
36 		curshell = initshells();
37 	ret = *curshell;
38 	if (ret != NULL)
39 		curshell++;
40 	return (ret);
41 }
42 
43 endusershell()
44 {
45 
46 	if (shells != NULL)
47 		free((char *)shells);
48 	shells = NULL;
49 	if (strings != NULL)
50 		free(strings);
51 	strings = NULL;
52 	curshell = NULL;
53 }
54 
55 setusershell()
56 {
57 
58 	curshell = initshells();
59 }
60 
61 static char **
62 initshells()
63 {
64 	register char **sp, *cp;
65 	register FILE *fp;
66 	struct stat statb;
67 	extern char *malloc(), *calloc();
68 
69 	if (shells != NULL)
70 		free((char *)shells);
71 	shells = NULL;
72 	if (strings != NULL)
73 		free(strings);
74 	strings = NULL;
75 	if ((fp = fopen(SHELLS, "r")) == (FILE *)0)
76 		return(okshells);
77 	if (fstat(fileno(fp), &statb) == -1) {
78 		(void)fclose(fp);
79 		return(okshells);
80 	}
81 	if ((strings = malloc((unsigned)statb.st_size + 1)) == NULL) {
82 		(void)fclose(fp);
83 		return(okshells);
84 	}
85 	shells = (char **)calloc((unsigned)statb.st_size / 3, sizeof (char *));
86 	if (shells == NULL) {
87 		(void)fclose(fp);
88 		free(strings);
89 		strings = NULL;
90 		return(okshells);
91 	}
92 	sp = shells;
93 	cp = strings;
94 	while (fgets(cp, MAXPATHLEN + 1, fp) != NULL) {
95 		while (*cp != '#' && *cp != '/' && *cp != '\0')
96 			cp++;
97 		if (*cp == '#' || *cp == '\0')
98 			continue;
99 		*sp++ = cp;
100 		while (!isspace(*cp) && *cp != '#' && *cp != '\0')
101 			cp++;
102 		*cp++ = '\0';
103 	}
104 	*sp = (char *)0;
105 	(void)fclose(fp);
106 	return (shells);
107 }
108