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