Add support for Solaris 10 ACLs. Also, ACLs are Gnulib, not Autoconf.
[gnulib.git] / lib / getusershell.c
1 /* getusershell.c -- Return names of valid user shells.
2
3    Copyright (C) 1991, 1997, 2000, 2001, 2003, 2004, 2005, 2006 Free
4    Software Foundation, Inc.
5
6    This program is free software: you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
18
19 /* Written by David MacKenzie <djm@gnu.ai.mit.edu> */
20
21 #include <config.h>
22
23 #ifndef SHELLS_FILE
24 # ifndef __DJGPP__
25 /* File containing a list of nonrestricted shells, one per line. */
26 #  define SHELLS_FILE "/etc/shells"
27 # else
28 /* This is a horrible kludge.  Isn't there a better way?  */
29 #  define SHELLS_FILE "/dev/env/DJDIR/etc/shells"
30 # endif
31 #endif
32
33 #include <stdlib.h>
34 #include <ctype.h>
35
36 #include "stdio--.h"
37 #include "xalloc.h"
38
39 #if USE_UNLOCKED_IO
40 # include "unlocked-io.h"
41 #endif
42
43 static size_t readname (char **, size_t *, FILE *);
44
45 #if ! defined ADDITIONAL_DEFAULT_SHELLS && defined __MSDOS__
46 # define ADDITIONAL_DEFAULT_SHELLS \
47   "c:/dos/command.com", "c:/windows/command.com", "c:/command.com",
48 #else
49 # define ADDITIONAL_DEFAULT_SHELLS /* empty */
50 #endif
51
52 /* List of shells to use if the shells file is missing. */
53 static char const* const default_shells[] =
54 {
55   ADDITIONAL_DEFAULT_SHELLS
56   "/bin/sh", "/bin/csh", "/usr/bin/sh", "/usr/bin/csh", NULL
57 };
58
59 /* Index of the next shell in `default_shells' to return.
60    0 means we are not using `default_shells'. */
61 static size_t default_index = 0;
62
63 /* Input stream from the shells file. */
64 static FILE *shellstream = NULL;
65
66 /* Line of input from the shells file. */
67 static char *line = NULL;
68
69 /* Number of bytes allocated for `line'. */
70 static size_t line_size = 0;
71 \f
72 /* Return an entry from the shells file, ignoring comment lines.
73    If the file doesn't exist, use the list in DEFAULT_SHELLS (above).
74    In any case, the returned string is in memory allocated through malloc.
75    Return NULL if there are no more entries.  */
76
77 char *
78 getusershell (void)
79 {
80   if (default_index > 0)
81     {
82       if (default_shells[default_index])
83         /* Not at the end of the list yet.  */
84         return xstrdup (default_shells[default_index++]);
85       return NULL;
86     }
87
88   if (shellstream == NULL)
89     {
90       shellstream = fopen (SHELLS_FILE, "r");
91       if (shellstream == NULL)
92         {
93           /* No shells file.  Use the default list.  */
94           default_index = 1;
95           return xstrdup (default_shells[0]);
96         }
97     }
98
99   while (readname (&line, &line_size, shellstream))
100     {
101       if (*line != '#')
102         return line;
103     }
104   return NULL;                  /* End of file. */
105 }
106
107 /* Rewind the shells file. */
108
109 void
110 setusershell (void)
111 {
112   default_index = 0;
113   if (shellstream)
114     rewind (shellstream);
115 }
116
117 /* Close the shells file. */
118
119 void
120 endusershell (void)
121 {
122   if (shellstream)
123     {
124       fclose (shellstream);
125       shellstream = NULL;
126     }
127 }
128
129 /* Read a line from STREAM, removing any newline at the end.
130    Place the result in *NAME, which is malloc'd
131    and/or realloc'd as necessary and can start out NULL,
132    and whose size is passed and returned in *SIZE.
133
134    Return the number of bytes placed in *NAME
135    if some nonempty sequence was found, otherwise 0.  */
136
137 static size_t
138 readname (char **name, size_t *size, FILE *stream)
139 {
140   int c;
141   size_t name_index = 0;
142
143   /* Skip blank space.  */
144   while ((c = getc (stream)) != EOF && isspace (c))
145     /* Do nothing. */ ;
146
147   for (;;)
148     {
149       if (*size <= name_index)
150         *name = x2nrealloc (*name, size, sizeof **name);
151       if (c == EOF || isspace (c))
152         break;
153       (*name)[name_index++] = c;
154       c = getc (stream);
155     }
156   (*name)[name_index] = '\0';
157   return name_index;
158 }
159
160 #ifdef TEST
161 int
162 main (void)
163 {
164   char *s;
165
166   while (s = getusershell ())
167     puts (s);
168   exit (0);
169 }
170 #endif