]> arthur.barton.de Git - netatalk.git/blob - libatalk/compat/getusershell.c
Initial revision
[netatalk.git] / libatalk / compat / getusershell.c
1 /*
2  * Copyright (c) 1985 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that: (1) source distributions retain this entire copyright
7  * notice and comment, and (2) distributions including binaries display
8  * the following acknowledgement:  ``This product includes software
9  * developed by the University of California, Berkeley and its contributors''
10  * in the documentation or other materials provided with the distribution
11  * and in all advertising materials mentioning features or use of this
12  * software. Neither the name of the University nor the names of its
13  * contributors may be used to endorse or promote products derived
14  * from this software without specific prior written permission.
15  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
18  */
19
20 #if defined(LIBC_SCCS) && !defined(lint)
21 static char sccsid[] = "@(#)getusershell.c      5.6 (Berkeley) 6/1/90";
22 #endif /* LIBC_SCCS and not lint */
23
24 static int      _getusershell_dummy;
25
26 #if defined(ultrix) || defined(_IBMR2) || defined(NEED_GETUSERSHELL)
27
28 #include <sys/param.h>
29 #include <sys/file.h>
30 #include <sys/stat.h>
31 #include <ctype.h>
32 #include <stdio.h>
33
34 #define SHELLS "/etc/shells"
35
36 /*
37  * Do not add local shells here.  They should be added in /etc/shells
38  */
39 static char *okshells[] = {
40     "/bin/sh", "/bin/csh",
41 #ifdef _IBMR2
42     "/bin/ksh",
43 #endif _IBMR2
44     0
45 };
46
47 static char **shells, *strings;
48 static char **curshell = NULL;
49 extern char **initshells();
50
51 /*
52  * Get a list of shells from SHELLS, if it exists.
53  */
54 char *
55 getusershell()
56 {
57         char *ret;
58
59         if (curshell == NULL)
60                 curshell = initshells();
61         ret = *curshell;
62         if (ret != NULL)
63                 curshell++;
64         return (ret);
65 }
66
67 endusershell()
68 {
69         
70         if (shells != NULL)
71                 free((char *)shells);
72         shells = NULL;
73         if (strings != NULL)
74                 free(strings);
75         strings = NULL;
76         curshell = NULL;
77 }
78
79 setusershell()
80 {
81
82         curshell = initshells();
83 }
84
85 static char **
86 initshells()
87 {
88         register char **sp, *cp;
89         register FILE *fp;
90         struct stat statb;
91         extern char *malloc(), *calloc();
92
93         if (shells != NULL)
94                 free((char *)shells);
95         shells = NULL;
96         if (strings != NULL)
97                 free(strings);
98         strings = NULL;
99         if ((fp = fopen(SHELLS, "r")) == (FILE *)0)
100                 return(okshells);
101         if (fstat(fileno(fp), &statb) == -1) {
102                 (void)fclose(fp);
103                 return(okshells);
104         }
105         if ((strings = malloc((unsigned)statb.st_size)) == NULL) {
106                 (void)fclose(fp);
107                 return(okshells);
108         }
109         shells = (char **)calloc((unsigned)statb.st_size / 3, sizeof (char *));
110         if (shells == NULL) {
111                 (void)fclose(fp);
112                 free(strings);
113                 strings = NULL;
114                 return(okshells);
115         }
116         sp = shells;
117         cp = strings;
118         while (fgets(cp, MAXPATHLEN + 1, fp) != NULL) {
119                 while (*cp != '#' && *cp != '/' && *cp != '\0')
120                         cp++;
121                 if (*cp == '#' || *cp == '\0')
122                         continue;
123                 *sp++ = cp;
124                 while (!isspace(*cp) && *cp != '#' && *cp != '\0')
125                         cp++;
126                 *cp++ = '\0';
127         }
128         *sp = (char *)0;
129         (void)fclose(fp);
130         return (shells);
131 }
132
133 # endif ultrix