(IN_CTYPE_DOMAIN): Define.
[gnulib.git] / lib / getusershell.c
1 /* getusershell.c -- Return names of valid user shells.
2    Copyright (C) 1991, 1997 Free Software Foundation, Inc.
3
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.
8
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.
13
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.  */
17
18 /* Written by David MacKenzie <djm@gnu.ai.mit.edu> */
19
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 #ifndef SHELLS_FILE
25 /* File containing a list of nonrestricted shells, one per line. */
26 # define SHELLS_FILE "/etc/shells"
27 #endif
28
29 #include <stdio.h>
30 #include <ctype.h>
31
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
37
38 #define ISSPACE(c) (IN_CTYPE_DOMAIN (c) && isspace (c))
39
40 #ifdef STDC_HEADERS
41 # include <stdlib.h>
42 #else
43 char *malloc ();
44 char *realloc ();
45 #endif
46
47 char *xstrdup ();
48
49 static int readname ();
50
51 /* List of shells to use if the shells file is missing. */
52 static char const* const default_shells[] =
53 {
54   "/bin/sh", "/bin/csh", "/usr/bin/sh", "/usr/bin/csh", NULL
55 };
56
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;
60
61 /* Input stream from the shells file. */
62 static FILE *shellstream = NULL;
63
64 /* Line of input from the shells file. */
65 static char *line = NULL;
66
67 /* Number of bytes allocated for `line'. */
68 static int line_size = 0;
69 \f
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.  */
74
75 char *
76 getusershell ()
77 {
78   if (default_index > 0)
79     {
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;
84     }
85
86   if (shellstream == NULL)
87     {
88       shellstream = fopen (SHELLS_FILE, "r");
89       if (shellstream == NULL)
90         {
91           /* No shells file.  Use the default list.  */
92           default_index = 1;
93           return xstrdup (default_shells[0]);
94         }
95     }
96
97   while (readname (&line, &line_size, shellstream))
98     {
99       if (*line != '#')
100         return line;
101     }
102   return NULL;                  /* End of file. */
103 }
104
105 /* Rewind the shells file. */
106
107 void
108 setusershell ()
109 {
110   default_index = 0;
111   if (shellstream == NULL)
112     shellstream = fopen (SHELLS_FILE, "r");
113   else
114     fseek (shellstream, 0L, 0);
115 }
116
117 /* Close the shells file. */
118
119 void
120 endusershell ()
121 {
122   if (shellstream)
123     {
124       fclose (shellstream);
125       shellstream = NULL;
126     }
127 }
128
129 /* Allocate N bytes of memory dynamically, with error checking.  */
130
131 static char *
132 xmalloc (n)
133      unsigned n;
134 {
135   char *p;
136
137   p = malloc (n);
138   if (p == 0)
139     {
140       fprintf (stderr, "virtual memory exhausted\n");
141       exit (1);
142     }
143   return p;
144 }
145
146 /* Reallocate space P to size N, with error checking.  */
147
148 static char *
149 xrealloc (p, n)
150      char *p;
151      unsigned n;
152 {
153   p = realloc (p, n);
154   if (p == 0)
155     {
156       fprintf (stderr, "virtual memory exhausted\n");
157       exit (1);
158     }
159   return p;
160 }
161
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.
166
167    Return the number of characters placed in *NAME
168    if some nonempty sequence was found, otherwise 0.  */
169
170 static int
171 readname (name, size, stream)
172      char **name;
173      int *size;
174      FILE *stream;
175 {
176   int c;
177   int name_index = 0;
178
179   if (*name == NULL)
180     {
181       *size = 10;
182       *name = (char *) xmalloc (*size);
183     }
184
185   /* Skip blank space.  */
186   while ((c = getc (stream)) != EOF && ISSPACE (c))
187     /* Do nothing. */ ;
188
189   while (c != EOF && !ISSPACE (c))
190     {
191       (*name)[name_index++] = c;
192       while (name_index >= *size)
193         {
194           *size *= 2;
195           *name = (char *) xrealloc (*name, *size);
196         }
197       c = getc (stream);
198     }
199   (*name)[name_index] = '\0';
200   return name_index;
201 }
202
203 #ifdef TEST
204 main ()
205 {
206   char *s;
207
208   while (s = getusershell ())
209     puts (s);
210   exit (0);
211 }
212 #endif