1 /* getusershell.c -- Return names of valid user shells.
2 Copyright (C) 1991, 1997 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> */
25 /* File containing a list of nonrestricted shells, one per line. */
26 # define SHELLS_FILE "/etc/shells"
32 #if defined (STDC_HEADERS) || (!defined (isascii) && !defined (HAVE_ISASCII))
33 # define IN_CTYPE_DOMAIN(c) 1
35 # define IN_CTYPE_DOMAIN(c) isascii(c)
38 #define ISSPACE(c) (IN_CTYPE_DOMAIN (c) && isspace (c))
49 static int readname ();
51 /* List of shells to use if the shells file is missing. */
52 static char const* const default_shells
[] =
54 "/bin/sh", "/bin/csh", "/usr/bin/sh", "/usr/bin/csh", NULL
57 /* Index of the next shell in `default_shells' to return.
58 0 means we are not using `default_shells'. */
59 static int default_index
= 0;
61 /* Input stream from the shells file. */
62 static FILE *shellstream
= NULL
;
64 /* Line of input from the shells file. */
65 static char *line
= NULL
;
67 /* Number of bytes allocated for `line'. */
68 static int line_size
= 0;
70 /* Return an entry from the shells file, ignoring comment lines.
71 If the file doesn't exist, use the list in DEFAULT_SHELLS (above).
72 In any case, the returned string is in memory allocated through malloc.
73 Return NULL if there are no more entries. */
78 if (default_index
> 0)
80 if (default_shells
[default_index
])
81 /* Not at the end of the list yet. */
82 return xstrdup (default_shells
[default_index
++]);
86 if (shellstream
== NULL
)
88 shellstream
= fopen (SHELLS_FILE
, "r");
89 if (shellstream
== NULL
)
91 /* No shells file. Use the default list. */
93 return xstrdup (default_shells
[0]);
97 while (readname (&line
, &line_size
, shellstream
))
102 return NULL
; /* End of file. */
105 /* Rewind the shells file. */
111 if (shellstream
== NULL
)
112 shellstream
= fopen (SHELLS_FILE
, "r");
114 fseek (shellstream
, 0L, 0);
117 /* Close the shells file. */
124 fclose (shellstream
);
129 /* Allocate N bytes of memory dynamically, with error checking. */
140 fprintf (stderr
, "virtual memory exhausted\n");
146 /* Reallocate space P to size N, with error checking. */
156 fprintf (stderr
, "virtual memory exhausted\n");
162 /* Read a line from STREAM, removing any newline at the end.
163 Place the result in *NAME, which is malloc'd
164 and/or realloc'd as necessary and can start out NULL,
165 and whose size is passed and returned in *SIZE.
167 Return the number of characters placed in *NAME
168 if some nonempty sequence was found, otherwise 0. */
171 readname (name
, size
, stream
)
182 *name
= (char *) xmalloc (*size
);
185 /* Skip blank space. */
186 while ((c
= getc (stream
)) != EOF
&& ISSPACE (c
))
189 while (c
!= EOF
&& !ISSPACE (c
))
191 (*name
)[name_index
++] = c
;
192 while (name_index
>= *size
)
195 *name
= (char *) xrealloc (*name
, *size
);
199 (*name
)[name_index
] = '\0';
208 while (s
= getusershell ())