1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30: #if defined(LIBC_SCCS) && !defined(lint)
31: static char sccsid[] = "@(#)getusershell.c 8.1 (Berkeley) 6/4/93";
32: #endif
33:
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>
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
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;
60:
61:
62:
63:
64: char *
65: getusershell()
66: {
67: char *ret;
68:
69: if (curshell == NULL)
70: curshell = initshells();
71: ret = *curshell;
72: if (ret != NULL)
73: curshell++;
74: return (ret);
75: }
76:
77: void
78: endusershell()
79: {
80:
81: free(shells);
82: shells = NULL;
83: free(strings);
84: strings = NULL;
85: curshell = NULL;
86: }
87:
88: void
89: setusershell()
90: {
91:
92: curshell = initshells();
93: }
94:
95: static char **
96: initshells()
97: {
98: register char **sp, *cp;
99: register FILE *fp;
100: struct stat64 statb;
101: size_t flen;
102:
103: free(shells);
104: shells = NULL;
105: free(strings);
106: strings = NULL;
107: if ((fp = fopen(_PATH_SHELLS, "rc")) == 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;
116: }
117: if (statb.st_size > ~(size_t)0 / sizeof (char *) * 3)
118: goto init_okshells;
119: if ((strings = malloc(statb.st_size + 2)) == NULL)
120: goto init_okshells;
121: shells = malloc(statb.st_size / 3 * sizeof (char *));
122: if (shells == NULL) {
123: free(strings);
124: strings = NULL;
125: goto init_okshells;
126: }
127: sp = shells;
128: cp = strings;
129: flen = statb.st_size + 2;
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';
139: }
140: *sp = NULL;
141: (void)fclose(fp);
142: return (shells);
143: }