Commit | Line | Data |
---|---|---|
4a76e37c KM |
1 | /* |
2 | * Copyright (c) 1985 Regents of the University of California. | |
73da8e88 KB |
3 | * All rights reserved. |
4 | * | |
269a7923 | 5 | * %sccs.include.redist.c% |
4a76e37c KM |
6 | */ |
7 | ||
2ce81398 | 8 | #if defined(LIBC_SCCS) && !defined(lint) |
870586f0 | 9 | static char sccsid[] = "@(#)getusershell.c 5.8 (Berkeley) %G%"; |
73da8e88 | 10 | #endif /* LIBC_SCCS and not lint */ |
4a76e37c KM |
11 | |
12 | #include <sys/param.h> | |
13 | #include <sys/file.h> | |
14 | #include <sys/stat.h> | |
4a76e37c | 15 | #include <stdio.h> |
870586f0 | 16 | #include <ctype.h> |
c5980113 DS |
17 | #include <stdlib.h> |
18 | #include <unistd.h> | |
870586f0 | 19 | #include <paths.h> |
4a76e37c KM |
20 | |
21 | /* | |
870586f0 KB |
22 | * Local shells should NOT be added here. They should be added in |
23 | * /etc/shells. | |
4a76e37c | 24 | */ |
4a76e37c | 25 | |
870586f0 KB |
26 | static char *okshells[] = { _PATH_BSHELL, _PATH_CSHELL, NULL }; |
27 | static char **curshell, **shells, *strings; | |
28 | static char **initshells __P((void)); | |
4a76e37c KM |
29 | |
30 | /* | |
870586f0 | 31 | * Get a list of shells from _PATH_SHELLS, if it exists. |
4a76e37c KM |
32 | */ |
33 | char * | |
34 | getusershell() | |
35 | { | |
36 | char *ret; | |
4a76e37c | 37 | |
25848be5 JB |
38 | if (curshell == NULL) |
39 | curshell = initshells(); | |
40 | ret = *curshell; | |
41 | if (ret != NULL) | |
42 | curshell++; | |
4a76e37c KM |
43 | return (ret); |
44 | } | |
45 | ||
c5980113 | 46 | void |
4a76e37c KM |
47 | endusershell() |
48 | { | |
49 | ||
50 | if (shells != NULL) | |
870586f0 | 51 | free(shells); |
4a76e37c KM |
52 | shells = NULL; |
53 | if (strings != NULL) | |
54 | free(strings); | |
55 | strings = NULL; | |
25848be5 | 56 | curshell = NULL; |
4a76e37c KM |
57 | } |
58 | ||
c5980113 | 59 | void |
4a76e37c KM |
60 | setusershell() |
61 | { | |
62 | ||
25848be5 | 63 | curshell = initshells(); |
4a76e37c KM |
64 | } |
65 | ||
66 | static char ** | |
67 | initshells() | |
68 | { | |
69 | register char **sp, *cp; | |
70 | register FILE *fp; | |
71 | struct stat statb; | |
4a76e37c | 72 | |
4a76e37c | 73 | if (shells != NULL) |
870586f0 | 74 | free(shells); |
4a76e37c KM |
75 | shells = NULL; |
76 | if (strings != NULL) | |
77 | free(strings); | |
78 | strings = NULL; | |
870586f0 KB |
79 | if ((fp = fopen(_PATH_SHELLS, "r")) == NULL) |
80 | return (okshells); | |
4a76e37c KM |
81 | if (fstat(fileno(fp), &statb) == -1) { |
82 | (void)fclose(fp); | |
870586f0 | 83 | return (okshells); |
4a76e37c | 84 | } |
870586f0 | 85 | if ((strings = malloc((u_int)statb.st_size)) == NULL) { |
4a76e37c | 86 | (void)fclose(fp); |
870586f0 | 87 | return (okshells); |
4a76e37c | 88 | } |
870586f0 | 89 | shells = calloc((unsigned)statb.st_size / 3, sizeof (char *)); |
4a76e37c KM |
90 | if (shells == NULL) { |
91 | (void)fclose(fp); | |
92 | free(strings); | |
93 | strings = NULL; | |
870586f0 | 94 | return (okshells); |
4a76e37c KM |
95 | } |
96 | sp = shells; | |
97 | cp = strings; | |
98 | while (fgets(cp, MAXPATHLEN + 1, fp) != NULL) { | |
25848be5 | 99 | while (*cp != '#' && *cp != '/' && *cp != '\0') |
4a76e37c | 100 | cp++; |
25848be5 | 101 | if (*cp == '#' || *cp == '\0') |
4a76e37c KM |
102 | continue; |
103 | *sp++ = cp; | |
104 | while (!isspace(*cp) && *cp != '#' && *cp != '\0') | |
105 | cp++; | |
106 | *cp++ = '\0'; | |
107 | } | |
870586f0 | 108 | *sp = NULL; |
4a76e37c KM |
109 | (void)fclose(fp); |
110 | return (shells); | |
111 | } |