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"
46 static int readname ();
48 /* List of shells to use if the shells file is missing. */
49 static char const* const default_shells
[] =
51 "/bin/sh", "/bin/csh", "/usr/bin/sh", "/usr/bin/csh", NULL
54 /* Index of the next shell in `default_shells' to return.
55 0 means we are not using `default_shells'. */
56 static int default_index
= 0;
58 /* Input stream from the shells file. */
59 static FILE *shellstream
= NULL
;
61 /* Line of input from the shells file. */
62 static char *line
= NULL
;
64 /* Number of bytes allocated for `line'. */
65 static int line_size
= 0;
67 /* Return an entry from the shells file, ignoring comment lines.
68 Return NULL if there are no more entries. */
73 if (default_index
> 0)
75 if (default_shells
[default_index
])
76 /* Not at the end of the list yet. */
77 return default_shells
[default_index
++];
81 if (shellstream
== NULL
)
83 shellstream
= fopen (SHELLS_FILE
, "r");
84 if (shellstream
== NULL
)
86 /* No shells file. Use the default list. */
88 return default_shells
[0];
92 while (readname (&line
, &line_size
, shellstream
))
97 return NULL
; /* End of file. */
100 /* Rewind the shells file. */
106 if (shellstream
== NULL
)
107 shellstream
= fopen (SHELLS_FILE
, "r");
109 fseek (shellstream
, 0L, 0);
112 /* Close the shells file. */
119 fclose (shellstream
);
124 /* Allocate N bytes of memory dynamically, with error checking. */
135 fprintf (stderr
, "virtual memory exhausted\n");
141 /* Reallocate space P to size N, with error checking. */
151 fprintf (stderr
, "virtual memory exhausted\n");
157 /* Read a line from STREAM, removing any newline at the end.
158 Place the result in *NAME, which is malloc'd
159 and/or realloc'd as necessary and can start out NULL,
160 and whose size is passed and returned in *SIZE.
162 Return the number of characters placed in *NAME
163 if some nonempty sequence was found, otherwise 0. */
166 readname (name
, size
, stream
)
177 *name
= (char *) xmalloc (*size
);
180 /* Skip blank space. */
181 while ((c
= getc (stream
)) != EOF
&& isspace (c
))
184 while (c
!= EOF
&& !isspace (c
))
186 (*name
)[name_index
++] = c
;
187 while (name_index
>= *size
)
190 *name
= (char *) xrealloc (*name
, *size
);
194 (*name
)[name_index
] = '\0';
203 while (s
= getusershell ())