1 /* getusershell.c -- Return names of valid user shells.
2 Copyright (C) 1991 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
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
18 /* Written by David MacKenzie <djm@gnu.ai.mit.edu> */
21 #if defined (CONFIG_BROKETS)
22 /* We use <config.h> instead of "config.h" so that a compilation
23 using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
24 (which it would do because it found this file in $srcdir). */
32 /* File containing a list of nonrestricted shells, one per line. */
33 #define SHELLS_FILE "/etc/shells"
48 static int readname ();
50 /* List of shells to use if the shells file is missing. */
51 static char const* const default_shells[] =
53 "/bin/sh", "/bin/csh", "/usr/bin/sh", "/usr/bin/csh", NULL
56 /* Index of the next shell in `default_shells' to return.
57 0 means we are not using `default_shells'. */
58 static int default_index = 0;
60 /* Input stream from the shells file. */
61 static FILE *shellstream = NULL;
63 /* Line of input from the shells file. */
64 static char *line = NULL;
66 /* Number of bytes allocated for `line'. */
67 static int line_size = 0;
69 /* Return an entry from the shells file, ignoring comment lines.
70 If the file doesn't exist, use the list in DEFAULT_SHELLS (above).
71 In any case, the returned string is in memory allocated through malloc.
72 Return NULL if there are no more entries. */
77 if (default_index > 0)
79 if (default_shells[default_index])
80 /* Not at the end of the list yet. */
81 return xstrdup (default_shells[default_index++]);
85 if (shellstream == NULL)
87 shellstream = fopen (SHELLS_FILE, "r");
88 if (shellstream == NULL)
90 /* No shells file. Use the default list. */
92 return xstrdup (default_shells[0]);
96 while (readname (&line, &line_size, shellstream))
101 return NULL; /* End of file. */
104 /* Rewind the shells file. */
110 if (shellstream == NULL)
111 shellstream = fopen (SHELLS_FILE, "r");
113 fseek (shellstream, 0L, 0);
116 /* Close the shells file. */
123 fclose (shellstream);
128 /* Allocate N bytes of memory dynamically, with error checking. */
139 fprintf (stderr, "virtual memory exhausted\n");
145 /* Reallocate space P to size N, with error checking. */
155 fprintf (stderr, "virtual memory exhausted\n");
161 /* Read a line from STREAM, removing any newline at the end.
162 Place the result in *NAME, which is malloc'd
163 and/or realloc'd as necessary and can start out NULL,
164 and whose size is passed and returned in *SIZE.
166 Return the number of characters placed in *NAME
167 if some nonempty sequence was found, otherwise 0. */
170 readname (name, size, stream)
181 *name = (char *) xmalloc (*size);
184 /* Skip blank space. */
185 while ((c = getc (stream)) != EOF && isspace (c))
188 while (c != EOF && !isspace (c))
190 (*name)[name_index++] = c;
191 while (name_index >= *size)
194 *name = (char *) xrealloc (*name, *size);
198 (*name)[name_index] = '\0';
207 while (s = getusershell ())