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 /* File containing a list of nonrestricted shells, one per line. */
22 #define SHELLS_FILE "/etc/shells"
35 static int readname ();
37 /* List of shells to use if the shells file is missing. */
38 static char *default_shells[] =
40 "/bin/sh", "/bin/csh", "/usr/bin/sh", "/usr/bin/csh", NULL
43 /* Index of the next shell in `default_shells' to return.
44 0 means we are not using `default_shells'. */
45 static int default_index = 0;
47 /* Input stream from the shells file. */
48 static FILE *shellstream = NULL;
50 /* Line of input from the shells file. */
51 static char *line = NULL;
53 /* Number of bytes allocated for `line'. */
54 static int line_size = 0;
56 /* Return an entry from the shells file, ignoring comment lines.
57 Return NULL if there are no more entries. */
62 if (default_index > 0)
64 if (default_shells[default_index])
65 /* Not at the end of the list yet. */
66 return default_shells[default_index++];
70 if (shellstream == NULL)
72 shellstream = fopen (SHELLS_FILE, "r");
73 if (shellstream == NULL)
75 /* No shells file. Use the default list. */
77 return default_shells[0];
81 while (readname (&line, &line_size, shellstream))
86 return NULL; /* End of file. */
89 /* Rewind the shells file. */
95 if (shellstream == NULL)
96 shellstream = fopen (SHELLS_FILE, "r");
98 fseek (shellstream, 0L, 0);
101 /* Close the shells file. */
108 fclose (shellstream);
113 /* Allocate N bytes of memory dynamically, with error checking. */
124 fprintf (stderr, "virtual memory exhausted\n");
130 /* Reallocate space P to size N, with error checking. */
140 fprintf (stderr, "virtual memory exhausted\n");
146 /* Read a line from STREAM, removing any newline at the end.
147 Place the result in *NAME, which is malloc'd
148 and/or realloc'd as necessary and can start out NULL,
149 and whose size is passed and returned in *SIZE.
151 Return the number of characters placed in *NAME
152 if some nonempty sequence was found, otherwise 0. */
155 readname (name, size, stream)
166 *name = (char *) xmalloc (*size);
169 /* Skip blank space. */
170 while ((c = getc (stream)) != EOF && isspace (c))
173 while (c != EOF && !isspace (c))
175 (*name)[name_index++] = c;
176 while (name_index >= *size)
179 *name = (char *) xrealloc (*name, *size);
183 (*name)[name_index] = '\0';
192 while (s = getusershell ())