[!SHELLS_FILE && __DJGPP__]: Define
[gnulib.git] / lib / getusershell.c
1 /* getusershell.c -- Return names of valid user shells.
2    Copyright (C) 1991, 1997, 2000 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 # ifndef __DJGPP__
26 /* File containing a list of nonrestricted shells, one per line. */
27 #  define SHELLS_FILE "/etc/shells"
28 # else
29 /* This is a horrible kludge.  Isn't there a better way?  */
30 #  define SHELLS_FILE "/dev/env/DJDIR/etc/shells"
31 # endif
32 #endif
33
34 #include <stdio.h>
35 #if HAVE_STDLIB_H
36 # include <stdlib.h>
37 #endif
38 #include <ctype.h>
39 #include "xalloc.h"
40
41 #if defined (STDC_HEADERS) || (!defined (isascii) && !defined (HAVE_ISASCII))
42 # define IN_CTYPE_DOMAIN(c) 1
43 #else
44 # define IN_CTYPE_DOMAIN(c) isascii(c)
45 #endif
46
47 #define ISSPACE(c) (IN_CTYPE_DOMAIN (c) && isspace (c))
48
49 static int readname ();
50
51 #if ! defined ADDITIONAL_DEFAULT_SHELLS && defined __MSDOS__
52 # define ADDITIONAL_DEFAULT_SHELLS \
53   "c:/dos/command.com", "c:/windows/command.com", "c:/command.com",
54 #else
55 # define ADDITIONAL_DEFAULT_SHELLS /* empty */
56 #endif
57
58 /* List of shells to use if the shells file is missing. */
59 static char const* const default_shells[] =
60 {
61   ADDITIONAL_DEFAULT_SHELLS
62   "/bin/sh", "/bin/csh", "/usr/bin/sh", "/usr/bin/csh", NULL
63 };
64
65 /* Index of the next shell in `default_shells' to return.
66    0 means we are not using `default_shells'. */
67 static int default_index = 0;
68
69 /* Input stream from the shells file. */
70 static FILE *shellstream = NULL;
71
72 /* Line of input from the shells file. */
73 static char *line = NULL;
74
75 /* Number of bytes allocated for `line'. */
76 static int line_size = 0;
77 \f
78 /* Return an entry from the shells file, ignoring comment lines.
79    If the file doesn't exist, use the list in DEFAULT_SHELLS (above).
80    In any case, the returned string is in memory allocated through malloc.
81    Return NULL if there are no more entries.  */
82
83 char *
84 getusershell ()
85 {
86   if (default_index > 0)
87     {
88       if (default_shells[default_index])
89         /* Not at the end of the list yet.  */
90         return xstrdup (default_shells[default_index++]);
91       return NULL;
92     }
93
94   if (shellstream == NULL)
95     {
96       shellstream = fopen (SHELLS_FILE, "r");
97       if (shellstream == NULL)
98         {
99           /* No shells file.  Use the default list.  */
100           default_index = 1;
101           return xstrdup (default_shells[0]);
102         }
103     }
104
105   while (readname (&line, &line_size, shellstream))
106     {
107       if (*line != '#')
108         return line;
109     }
110   return NULL;                  /* End of file. */
111 }
112
113 /* Rewind the shells file. */
114
115 void
116 setusershell ()
117 {
118   default_index = 0;
119   if (shellstream)
120     rewind (shellstream);
121 }
122
123 /* Close the shells file. */
124
125 void
126 endusershell ()
127 {
128   if (shellstream)
129     {
130       fclose (shellstream);
131       shellstream = NULL;
132     }
133 }
134
135 /* Read a line from STREAM, removing any newline at the end.
136    Place the result in *NAME, which is malloc'd
137    and/or realloc'd as necessary and can start out NULL,
138    and whose size is passed and returned in *SIZE.
139
140    Return the number of characters placed in *NAME
141    if some nonempty sequence was found, otherwise 0.  */
142
143 static int
144 readname (name, size, stream)
145      char **name;
146      int *size;
147      FILE *stream;
148 {
149   int c;
150   int name_index = 0;
151
152   if (*name == NULL)
153     {
154       *size = 10;
155       *name = (char *) xmalloc (*size);
156     }
157
158   /* Skip blank space.  */
159   while ((c = getc (stream)) != EOF && ISSPACE (c))
160     /* Do nothing. */ ;
161
162   while (c != EOF && !ISSPACE (c))
163     {
164       (*name)[name_index++] = c;
165       while (name_index >= *size)
166         {
167           *size *= 2;
168           *name = (char *) xrealloc (*name, *size);
169         }
170       c = getc (stream);
171     }
172   (*name)[name_index] = '\0';
173   return name_index;
174 }
175
176 #ifdef TEST
177 main ()
178 {
179   char *s;
180
181   while (s = getusershell ())
182     puts (s);
183   exit (0);
184 }
185 #endif