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