1 /* getusershell.c -- Return names of valid user shells.
2 Copyright (C) 1991, 1997, 2000, 2001, 2003 Free Software Foundation, Inc.
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)
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.
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. */
18 /* Written by David MacKenzie <djm@gnu.ai.mit.edu> */
26 /* File containing a list of nonrestricted shells, one per line. */
27 # define SHELLS_FILE "/etc/shells"
29 /* This is a horrible kludge. Isn't there a better way? */
30 # define SHELLS_FILE "/dev/env/DJDIR/etc/shells"
37 #include "unlocked-io.h"
40 #if defined (STDC_HEADERS) || (!defined (isascii) && !defined (HAVE_ISASCII))
41 # define IN_CTYPE_DOMAIN(c) 1
43 # define IN_CTYPE_DOMAIN(c) isascii(c)
46 #define ISSPACE(c) (IN_CTYPE_DOMAIN (c) && isspace (c))
48 static int readname (char **, int *, FILE *);
50 #if ! defined ADDITIONAL_DEFAULT_SHELLS && defined __MSDOS__
51 # define ADDITIONAL_DEFAULT_SHELLS \
52 "c:/dos/command.com", "c:/windows/command.com", "c:/command.com",
54 # define ADDITIONAL_DEFAULT_SHELLS /* empty */
57 /* List of shells to use if the shells file is missing. */
58 static char const* const default_shells[] =
60 ADDITIONAL_DEFAULT_SHELLS
61 "/bin/sh", "/bin/csh", "/usr/bin/sh", "/usr/bin/csh", NULL
64 /* Index of the next shell in `default_shells' to return.
65 0 means we are not using `default_shells'. */
66 static int default_index = 0;
68 /* Input stream from the shells file. */
69 static FILE *shellstream = NULL;
71 /* Line of input from the shells file. */
72 static char *line = NULL;
74 /* Number of bytes allocated for `line'. */
75 static int line_size = 0;
77 /* Return an entry from the shells file, ignoring comment lines.
78 If the file doesn't exist, use the list in DEFAULT_SHELLS (above).
79 In any case, the returned string is in memory allocated through malloc.
80 Return NULL if there are no more entries. */
85 if (default_index > 0)
87 if (default_shells[default_index])
88 /* Not at the end of the list yet. */
89 return xstrdup (default_shells[default_index++]);
93 if (shellstream == NULL)
95 shellstream = fopen (SHELLS_FILE, "r");
96 if (shellstream == NULL)
98 /* No shells file. Use the default list. */
100 return xstrdup (default_shells[0]);
104 while (readname (&line, &line_size, shellstream))
109 return NULL; /* End of file. */
112 /* Rewind the shells file. */
119 rewind (shellstream);
122 /* Close the shells file. */
129 fclose (shellstream);
134 /* Read a line from STREAM, removing any newline at the end.
135 Place the result in *NAME, which is malloc'd
136 and/or realloc'd as necessary and can start out NULL,
137 and whose size is passed and returned in *SIZE.
139 Return the number of characters placed in *NAME
140 if some nonempty sequence was found, otherwise 0. */
143 readname (char **name, int *size, FILE *stream)
151 *name = xmalloc (*size);
154 /* Skip blank space. */
155 while ((c = getc (stream)) != EOF && ISSPACE (c))
158 while (c != EOF && !ISSPACE (c))
160 (*name)[name_index++] = c;
161 while (name_index >= *size)
164 *name = xrealloc (*name, *size);
168 (*name)[name_index] = '\0';
178 while (s = getusershell ())