Allow use of --avoid=extensions-aix.
[gnulib.git] / lib / getusershell.c
blob356b8023d5ed685f25f2463003648db0f129543b
1 /* getusershell.c -- Return names of valid user shells.
3 Copyright (C) 1991, 1997, 2000-2001, 2003-2006, 2008-2024 Free Software
4 Foundation, Inc.
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <https://www.gnu.org/licenses/>. */
19 /* Written by David MacKenzie <djm@gnu.ai.mit.edu> */
21 #include <config.h>
23 /* Specification. */
24 #include <unistd.h>
26 #ifndef SHELLS_FILE
27 # ifndef __DJGPP__
28 /* File containing a list of nonrestricted shells, one per line. */
29 # define SHELLS_FILE "/etc/shells"
30 # else
31 /* This is a horrible kludge. Isn't there a better way? */
32 # define SHELLS_FILE "/dev/env/DJDIR/etc/shells"
33 # endif
34 #endif
36 #include <stdlib.h>
37 #include <string.h>
38 #include <ctype.h>
40 #include "stdio--.h"
41 #include "filename.h"
42 #include "xalloc.h"
44 #if ! defined ADDITIONAL_DEFAULT_SHELLS && defined __MSDOS__
45 # define ADDITIONAL_DEFAULT_SHELLS \
46 "c:/dos/command.com", "c:/windows/command.com", "c:/command.com",
47 #else
48 # define ADDITIONAL_DEFAULT_SHELLS /* empty */
49 #endif
51 /* List of shells to use if the shells file is missing. */
52 static char const* const default_shells[] =
54 ADDITIONAL_DEFAULT_SHELLS
55 "/bin/sh", "/bin/csh", "/usr/bin/sh", "/usr/bin/csh", NULL
58 /* Index of the next shell in 'default_shells' to return.
59 0 means we are not using 'default_shells'. */
60 static size_t default_index = 0;
62 /* Input stream from the shells file. */
63 static FILE *shellstream = NULL;
65 /* Line of input from the shells file. */
66 static char *line = NULL;
68 /* Number of bytes allocated for 'line'. */
69 static size_t line_size = 0;
71 /* Return an entry from the shells file, ignoring comment lines.
72 If the file doesn't exist, use the list in DEFAULT_SHELLS (above).
73 In any case, the returned string is in memory allocated through malloc.
74 Return NULL if there are no more entries. */
76 char *
77 getusershell (void)
79 if (default_index > 0)
81 if (default_shells[default_index])
82 /* Not at the end of the list yet. */
83 return xstrdup (default_shells[default_index++]);
84 return NULL;
87 if (shellstream == NULL)
89 shellstream = fopen (SHELLS_FILE, "r");
90 if (shellstream == NULL)
92 /* No shells file. Use the default list. */
93 default_index = 1;
94 return xstrdup (default_shells[0]);
98 for (;;)
100 ssize_t nread = getline (&line, &line_size, shellstream);
102 /* End of file. */
103 if (nread == -1)
104 return NULL;
105 /* Skip empty lines. */
106 else if (nread > 1)
108 char *start = line;
109 char *comment = strchr (start, '#');
110 char *end;
112 if (comment != NULL)
114 /* Trim the comment mark. */
115 comment = '\0';
116 end = comment;
118 else
120 /* Trim the newline. */
121 end = start + nread;
122 if (end[-1] == '\n')
123 *--end = '\0';
126 /* Skip leading whitespace. */
127 while (start < end && isspace ((unsigned char) start[0]))
128 start++;
129 /* Trim trailing whitespace. */
130 while (start < end && isspace ((unsigned char) end[-1]))
131 *--end = '\0';
133 /* Only return absolute file names. */
134 if (start < end && IS_ABSOLUTE_FILE_NAME (start))
135 return start;
140 /* Rewind the shells file. */
142 void
143 setusershell (void)
145 default_index = 0;
146 if (shellstream)
147 rewind (shellstream);
150 /* Close the shells file. */
152 void
153 endusershell (void)
155 if (shellstream)
157 fclose (shellstream);
158 shellstream = NULL;
162 #ifdef TEST
164 main (void)
166 char *s;
168 while (s = getusershell ())
169 puts (s);
170 exit (0);
172 #endif