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