1 /* getusershell.c -- Return names of valid user shells.
3 Copyright (C) 1991, 1997, 2000, 2001, 2003, 2004, 2005, 2006 Free
4 Software Foundation, Inc.
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.
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.
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/>. */
19 /* Written by David MacKenzie <djm@gnu.ai.mit.edu> */
25 /* File containing a list of nonrestricted shells, one per line. */
26 # define SHELLS_FILE "/etc/shells"
28 /* This is a horrible kludge. Isn't there a better way? */
29 # define SHELLS_FILE "/dev/env/DJDIR/etc/shells"
40 # include "unlocked-io.h"
43 static size_t readname (char **, size_t *, FILE *);
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",
49 # define ADDITIONAL_DEFAULT_SHELLS /* empty */
52 /* List of shells to use if the shells file is missing. */
53 static char const* const default_shells[] =
55 ADDITIONAL_DEFAULT_SHELLS
56 "/bin/sh", "/bin/csh", "/usr/bin/sh", "/usr/bin/csh", NULL
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;
63 /* Input stream from the shells file. */
64 static FILE *shellstream = NULL;
66 /* Line of input from the shells file. */
67 static char *line = NULL;
69 /* Number of bytes allocated for `line'. */
70 static size_t line_size = 0;
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. */
80 if (default_index > 0)
82 if (default_shells[default_index])
83 /* Not at the end of the list yet. */
84 return xstrdup (default_shells[default_index++]);
88 if (shellstream == NULL)
90 shellstream = fopen (SHELLS_FILE, "r");
91 if (shellstream == NULL)
93 /* No shells file. Use the default list. */
95 return xstrdup (default_shells[0]);
99 while (readname (&line, &line_size, shellstream))
104 return NULL; /* End of file. */
107 /* Rewind the shells file. */
114 rewind (shellstream);
117 /* Close the shells file. */
124 fclose (shellstream);
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.
134 Return the number of bytes placed in *NAME
135 if some nonempty sequence was found, otherwise 0. */
138 readname (char **name, size_t *size, FILE *stream)
141 size_t name_index = 0;
143 /* Skip blank space. */
144 while ((c = getc (stream)) != EOF && isspace (c))
149 if (*size <= name_index)
150 *name = x2nrealloc (*name, size, sizeof **name);
151 if (c == EOF || isspace (c))
153 (*name)[name_index++] = c;
156 (*name)[name_index] = '\0';
166 while (s = getusershell ())