add copyright/no-warranty comment
[coreutils.git] / lib / getusershell.c
blob730d1b5c04bc19ba8d226d4a4aefc5c92feb3eea
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)
7 any later version.
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> */
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
24 #ifndef SHELLS_FILE
25 /* File containing a list of nonrestricted shells, one per line. */
26 # define SHELLS_FILE "/etc/shells"
27 #endif
29 #include <stdio.h>
30 #include <ctype.h>
32 #if defined (STDC_HEADERS) || (!defined (isascii) && !defined (HAVE_ISASCII))
33 # define IN_CTYPE_DOMAIN(c) 1
34 #else
35 # define IN_CTYPE_DOMAIN(c) isascii(c)
36 #endif
38 #define ISSPACE(c) (IN_CTYPE_DOMAIN (c) && isspace (c))
40 #ifdef STDC_HEADERS
41 # include <stdlib.h>
42 #else
43 char *malloc ();
44 char *realloc ();
45 #endif
47 char *xstrdup ();
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. */
75 char *
76 getusershell ()
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++]);
83 return NULL;
86 if (shellstream == NULL)
88 shellstream = fopen (SHELLS_FILE, "r");
89 if (shellstream == NULL)
91 /* No shells file. Use the default list. */
92 default_index = 1;
93 return xstrdup (default_shells[0]);
97 while (readname (&line, &line_size, shellstream))
99 if (*line != '#')
100 return line;
102 return NULL; /* End of file. */
105 /* Rewind the shells file. */
107 void
108 setusershell ()
110 default_index = 0;
111 if (shellstream == NULL)
112 shellstream = fopen (SHELLS_FILE, "r");
113 else
114 fseek (shellstream, 0L, 0);
117 /* Close the shells file. */
119 void
120 endusershell ()
122 if (shellstream)
124 fclose (shellstream);
125 shellstream = NULL;
129 /* Allocate N bytes of memory dynamically, with error checking. */
131 static char *
132 xmalloc (n)
133 unsigned n;
135 char *p;
137 p = malloc (n);
138 if (p == 0)
140 fprintf (stderr, "virtual memory exhausted\n");
141 exit (1);
143 return p;
146 /* Reallocate space P to size N, with error checking. */
148 static char *
149 xrealloc (p, n)
150 char *p;
151 unsigned n;
153 p = realloc (p, n);
154 if (p == 0)
156 fprintf (stderr, "virtual memory exhausted\n");
157 exit (1);
159 return p;
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. */
170 static int
171 readname (name, size, stream)
172 char **name;
173 int *size;
174 FILE *stream;
176 int c;
177 int name_index = 0;
179 if (*name == NULL)
181 *size = 10;
182 *name = (char *) xmalloc (*size);
185 /* Skip blank space. */
186 while ((c = getc (stream)) != EOF && ISSPACE (c))
187 /* Do nothing. */ ;
189 while (c != EOF && !ISSPACE (c))
191 (*name)[name_index++] = c;
192 while (name_index >= *size)
194 *size *= 2;
195 *name = (char *) xrealloc (*name, *size);
197 c = getc (stream);
199 (*name)[name_index] = '\0';
200 return name_index;
203 #ifdef TEST
204 main ()
206 char *s;
208 while (s = getusershell ())
209 puts (s);
210 exit (0);
212 #endif