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