]> arthur.barton.de Git - netatalk.git/blob - libatalk/compat/getusershell.c
implemented config.h
[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 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #if defined(LIBC_SCCS) && !defined(lint)
25 static char sccsid[] = "@(#)getusershell.c      5.6 (Berkeley) 6/1/90";
26 #endif /* LIBC_SCCS and not lint */
27
28 static int      _getusershell_dummy;
29
30 #if defined(ultrix) || defined(_IBMR2) || defined(NEED_GETUSERSHELL)
31
32 #include <sys/param.h>
33 #include <sys/file.h>
34 #include <sys/stat.h>
35 #include <ctype.h>
36 #include <stdio.h>
37
38 #define SHELLS "/etc/shells"
39
40 /*
41  * Do not add local shells here.  They should be added in /etc/shells
42  */
43 static char *okshells[] = {
44     "/bin/sh", "/bin/csh",
45 #ifdef _IBMR2
46     "/bin/ksh",
47 #endif _IBMR2
48     0
49 };
50
51 static char **shells, *strings;
52 static char **curshell = NULL;
53 extern char **initshells();
54
55 /*
56  * Get a list of shells from SHELLS, if it exists.
57  */
58 char *
59 getusershell()
60 {
61         char *ret;
62
63         if (curshell == NULL)
64                 curshell = initshells();
65         ret = *curshell;
66         if (ret != NULL)
67                 curshell++;
68         return (ret);
69 }
70
71 endusershell()
72 {
73         
74         if (shells != NULL)
75                 free((char *)shells);
76         shells = NULL;
77         if (strings != NULL)
78                 free(strings);
79         strings = NULL;
80         curshell = NULL;
81 }
82
83 setusershell()
84 {
85
86         curshell = initshells();
87 }
88
89 static char **
90 initshells()
91 {
92         register char **sp, *cp;
93         register FILE *fp;
94         struct stat statb;
95         extern char *malloc(), *calloc();
96
97         if (shells != NULL)
98                 free((char *)shells);
99         shells = NULL;
100         if (strings != NULL)
101                 free(strings);
102         strings = NULL;
103         if ((fp = fopen(SHELLS, "r")) == (FILE *)0)
104                 return(okshells);
105         if (fstat(fileno(fp), &statb) == -1) {
106                 (void)fclose(fp);
107                 return(okshells);
108         }
109         if ((strings = malloc((unsigned)statb.st_size)) == NULL) {
110                 (void)fclose(fp);
111                 return(okshells);
112         }
113         shells = (char **)calloc((unsigned)statb.st_size / 3, sizeof (char *));
114         if (shells == NULL) {
115                 (void)fclose(fp);
116                 free(strings);
117                 strings = NULL;
118                 return(okshells);
119         }
120         sp = shells;
121         cp = strings;
122         while (fgets(cp, MAXPATHLEN + 1, fp) != NULL) {
123                 while (*cp != '#' && *cp != '/' && *cp != '\0')
124                         cp++;
125                 if (*cp == '#' || *cp == '\0')
126                         continue;
127                 *sp++ = cp;
128                 while (!isspace(*cp) && *cp != '#' && *cp != '\0')
129                         cp++;
130                 *cp++ = '\0';
131         }
132         *sp = (char *)0;
133         (void)fclose(fp);
134         return (shells);
135 }
136
137 # endif ultrix