Sync from coreutils.
[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 Free Software
4    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 2, or (at your option)
9    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, write to the Free Software Foundation,
18    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
19
20 /* Written by David MacKenzie <djm@gnu.ai.mit.edu> */
21
22 #ifdef HAVE_CONFIG_H
23 # include <config.h>
24 #endif
25
26 #ifndef SHELLS_FILE
27 # ifndef __DJGPP__
28 /* File containing a list of nonrestricted shells, one per line. */
29 #  define SHELLS_FILE "/etc/shells"
30 # else
31 /* This is a horrible kludge.  Isn't there a better way?  */
32 #  define SHELLS_FILE "/dev/env/DJDIR/etc/shells"
33 # endif
34 #endif
35
36 #include <stdlib.h>
37 #include <ctype.h>
38
39 #include "stdio--.h"
40 #include "xalloc.h"
41
42 #if USE_UNLOCKED_IO
43 # include "unlocked-io.h"
44 #endif
45
46 #if defined (STDC_HEADERS) || (!defined (isascii) && !defined (HAVE_ISASCII))
47 # define IN_CTYPE_DOMAIN(c) 1
48 #else
49 # define IN_CTYPE_DOMAIN(c) isascii(c)
50 #endif
51
52 #define ISSPACE(c) (IN_CTYPE_DOMAIN (c) && isspace (c))
53
54 static size_t readname (char **, size_t *, FILE *);
55
56 #if ! defined ADDITIONAL_DEFAULT_SHELLS && defined __MSDOS__
57 # define ADDITIONAL_DEFAULT_SHELLS \
58   "c:/dos/command.com", "c:/windows/command.com", "c:/command.com",
59 #else
60 # define ADDITIONAL_DEFAULT_SHELLS /* empty */
61 #endif
62
63 /* List of shells to use if the shells file is missing. */
64 static char const* const default_shells[] =
65 {
66   ADDITIONAL_DEFAULT_SHELLS
67   "/bin/sh", "/bin/csh", "/usr/bin/sh", "/usr/bin/csh", NULL
68 };
69
70 /* Index of the next shell in `default_shells' to return.
71    0 means we are not using `default_shells'. */
72 static size_t default_index = 0;
73
74 /* Input stream from the shells file. */
75 static FILE *shellstream = NULL;
76
77 /* Line of input from the shells file. */
78 static char *line = NULL;
79
80 /* Number of bytes allocated for `line'. */
81 static size_t line_size = 0;
82 \f
83 /* Return an entry from the shells file, ignoring comment lines.
84    If the file doesn't exist, use the list in DEFAULT_SHELLS (above).
85    In any case, the returned string is in memory allocated through malloc.
86    Return NULL if there are no more entries.  */
87
88 char *
89 getusershell (void)
90 {
91   if (default_index > 0)
92     {
93       if (default_shells[default_index])
94         /* Not at the end of the list yet.  */
95         return xstrdup (default_shells[default_index++]);
96       return NULL;
97     }
98
99   if (shellstream == NULL)
100     {
101       shellstream = fopen (SHELLS_FILE, "r");
102       if (shellstream == NULL)
103         {
104           /* No shells file.  Use the default list.  */
105           default_index = 1;
106           return xstrdup (default_shells[0]);
107         }
108     }
109
110   while (readname (&line, &line_size, shellstream))
111     {
112       if (*line != '#')
113         return line;
114     }
115   return NULL;                  /* End of file. */
116 }
117
118 /* Rewind the shells file. */
119
120 void
121 setusershell (void)
122 {
123   default_index = 0;
124   if (shellstream)
125     rewind (shellstream);
126 }
127
128 /* Close the shells file. */
129
130 void
131 endusershell (void)
132 {
133   if (shellstream)
134     {
135       fclose (shellstream);
136       shellstream = NULL;
137     }
138 }
139
140 /* Read a line from STREAM, removing any newline at the end.
141    Place the result in *NAME, which is malloc'd
142    and/or realloc'd as necessary and can start out NULL,
143    and whose size is passed and returned in *SIZE.
144
145    Return the number of bytes placed in *NAME
146    if some nonempty sequence was found, otherwise 0.  */
147
148 static size_t
149 readname (char **name, size_t *size, FILE *stream)
150 {
151   int c;
152   size_t name_index = 0;
153
154   /* Skip blank space.  */
155   while ((c = getc (stream)) != EOF && ISSPACE (c))
156     /* Do nothing. */ ;
157
158   for (;;)
159     {
160       if (*size <= name_index)
161         *name = x2nrealloc (*name, size, sizeof **name);
162       if (c == EOF || ISSPACE (c))
163         break;
164       (*name)[name_index++] = c;
165       c = getc (stream);
166     }
167   (*name)[name_index] = '\0';
168   return name_index;
169 }
170
171 #ifdef TEST
172 int
173 main (void)
174 {
175   char *s;
176
177   while (s = getusershell ())
178     puts (s);
179   exit (0);
180 }
181 #endif