Mercurial > hg > octave-kai > gnulib-hg
annotate lib/getusershell.c @ 4655:4f71e1292ad1
Remove K&R cruft.
author | Paul Eggert <eggert@cs.ucla.edu> |
---|---|
date | Tue, 09 Sep 2003 19:23:55 +0000 |
parents | 3cb007557d70 |
children | 4f4fb4d3874d |
rev | line source |
---|---|
9 | 1 /* getusershell.c -- Return names of valid user shells. |
4541
3cb007557d70
(readname): Remove casts no longer required in C89.
Paul Eggert <eggert@cs.ucla.edu>
parents:
3618
diff
changeset
|
2 Copyright (C) 1991, 1997, 2000, 2001, 2003 Free Software Foundation, Inc. |
9 | 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 | |
651
242f0fe39aa7
update FSF address in copyright
Jim Meyering <jim@meyering.net>
parents:
307
diff
changeset
|
15 along with this program; if not, write to the Free Software Foundation, |
242f0fe39aa7
update FSF address in copyright
Jim Meyering <jim@meyering.net>
parents:
307
diff
changeset
|
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
9 | 17 |
18 /* Written by David MacKenzie <djm@gnu.ai.mit.edu> */ | |
19 | |
125 | 20 #ifdef HAVE_CONFIG_H |
653 | 21 # include <config.h> |
125 | 22 #endif |
23 | |
9 | 24 #ifndef SHELLS_FILE |
3052
5d2b5bde7c6f
[!SHELLS_FILE && __DJGPP__]: Define
Jim Meyering <jim@meyering.net>
parents:
2966
diff
changeset
|
25 # ifndef __DJGPP__ |
9 | 26 /* File containing a list of nonrestricted shells, one per line. */ |
3052
5d2b5bde7c6f
[!SHELLS_FILE && __DJGPP__]: Define
Jim Meyering <jim@meyering.net>
parents:
2966
diff
changeset
|
27 # define SHELLS_FILE "/etc/shells" |
5d2b5bde7c6f
[!SHELLS_FILE && __DJGPP__]: Define
Jim Meyering <jim@meyering.net>
parents:
2966
diff
changeset
|
28 # else |
5d2b5bde7c6f
[!SHELLS_FILE && __DJGPP__]: Define
Jim Meyering <jim@meyering.net>
parents:
2966
diff
changeset
|
29 /* This is a horrible kludge. Isn't there a better way? */ |
5d2b5bde7c6f
[!SHELLS_FILE && __DJGPP__]: Define
Jim Meyering <jim@meyering.net>
parents:
2966
diff
changeset
|
30 # define SHELLS_FILE "/dev/env/DJDIR/etc/shells" |
5d2b5bde7c6f
[!SHELLS_FILE && __DJGPP__]: Define
Jim Meyering <jim@meyering.net>
parents:
2966
diff
changeset
|
31 # endif |
9 | 32 #endif |
33 | |
34 #include <stdio.h> | |
4655 | 35 #include <stdlib.h> |
9 | 36 #include <ctype.h> |
3618 | 37 #include "unlocked-io.h" |
2654
c0220b12ea7d
(xmalloc, xrealloc): Remove functions.
Jim Meyering <jim@meyering.net>
parents:
1041
diff
changeset
|
38 #include "xalloc.h" |
9 | 39 |
1041 | 40 #if defined (STDC_HEADERS) || (!defined (isascii) && !defined (HAVE_ISASCII)) |
41 # define IN_CTYPE_DOMAIN(c) 1 | |
42 #else | |
43 # define IN_CTYPE_DOMAIN(c) isascii(c) | |
44 #endif | |
45 | |
46 #define ISSPACE(c) (IN_CTYPE_DOMAIN (c) && isspace (c)) | |
47 | |
4655 | 48 static int readname (char **, int *, FILE *); |
9 | 49 |
3052
5d2b5bde7c6f
[!SHELLS_FILE && __DJGPP__]: Define
Jim Meyering <jim@meyering.net>
parents:
2966
diff
changeset
|
50 #if ! defined ADDITIONAL_DEFAULT_SHELLS && defined __MSDOS__ |
5d2b5bde7c6f
[!SHELLS_FILE && __DJGPP__]: Define
Jim Meyering <jim@meyering.net>
parents:
2966
diff
changeset
|
51 # define ADDITIONAL_DEFAULT_SHELLS \ |
5d2b5bde7c6f
[!SHELLS_FILE && __DJGPP__]: Define
Jim Meyering <jim@meyering.net>
parents:
2966
diff
changeset
|
52 "c:/dos/command.com", "c:/windows/command.com", "c:/command.com", |
5d2b5bde7c6f
[!SHELLS_FILE && __DJGPP__]: Define
Jim Meyering <jim@meyering.net>
parents:
2966
diff
changeset
|
53 #else |
5d2b5bde7c6f
[!SHELLS_FILE && __DJGPP__]: Define
Jim Meyering <jim@meyering.net>
parents:
2966
diff
changeset
|
54 # define ADDITIONAL_DEFAULT_SHELLS /* empty */ |
5d2b5bde7c6f
[!SHELLS_FILE && __DJGPP__]: Define
Jim Meyering <jim@meyering.net>
parents:
2966
diff
changeset
|
55 #endif |
5d2b5bde7c6f
[!SHELLS_FILE && __DJGPP__]: Define
Jim Meyering <jim@meyering.net>
parents:
2966
diff
changeset
|
56 |
9 | 57 /* List of shells to use if the shells file is missing. */ |
16
01c6d40adf9d
Make tables static and const when possible.
Jim Meyering <jim@meyering.net>
parents:
9
diff
changeset
|
58 static char const* const default_shells[] = |
9 | 59 { |
3052
5d2b5bde7c6f
[!SHELLS_FILE && __DJGPP__]: Define
Jim Meyering <jim@meyering.net>
parents:
2966
diff
changeset
|
60 ADDITIONAL_DEFAULT_SHELLS |
9 | 61 "/bin/sh", "/bin/csh", "/usr/bin/sh", "/usr/bin/csh", NULL |
62 }; | |
63 | |
64 /* Index of the next shell in `default_shells' to return. | |
65 0 means we are not using `default_shells'. */ | |
66 static int default_index = 0; | |
67 | |
68 /* Input stream from the shells file. */ | |
69 static FILE *shellstream = NULL; | |
70 | |
71 /* Line of input from the shells file. */ | |
72 static char *line = NULL; | |
73 | |
74 /* Number of bytes allocated for `line'. */ | |
75 static int line_size = 0; | |
76 | |
77 /* Return an entry from the shells file, ignoring comment lines. | |
167 | 78 If the file doesn't exist, use the list in DEFAULT_SHELLS (above). |
79 In any case, the returned string is in memory allocated through malloc. | |
9 | 80 Return NULL if there are no more entries. */ |
81 | |
82 char * | |
4655 | 83 getusershell (void) |
9 | 84 { |
85 if (default_index > 0) | |
86 { | |
87 if (default_shells[default_index]) | |
88 /* Not at the end of the list yet. */ | |
167 | 89 return xstrdup (default_shells[default_index++]); |
9 | 90 return NULL; |
91 } | |
92 | |
93 if (shellstream == NULL) | |
94 { | |
95 shellstream = fopen (SHELLS_FILE, "r"); | |
96 if (shellstream == NULL) | |
97 { | |
98 /* No shells file. Use the default list. */ | |
99 default_index = 1; | |
167 | 100 return xstrdup (default_shells[0]); |
9 | 101 } |
102 } | |
103 | |
104 while (readname (&line, &line_size, shellstream)) | |
105 { | |
106 if (*line != '#') | |
107 return line; | |
108 } | |
109 return NULL; /* End of file. */ | |
110 } | |
111 | |
112 /* Rewind the shells file. */ | |
113 | |
114 void | |
4655 | 115 setusershell (void) |
9 | 116 { |
117 default_index = 0; | |
2966
8d2c63fa3a16
(setusershell): Use rewind rather than
Jim Meyering <jim@meyering.net>
parents:
2928
diff
changeset
|
118 if (shellstream) |
8d2c63fa3a16
(setusershell): Use rewind rather than
Jim Meyering <jim@meyering.net>
parents:
2928
diff
changeset
|
119 rewind (shellstream); |
9 | 120 } |
121 | |
122 /* Close the shells file. */ | |
123 | |
124 void | |
4655 | 125 endusershell (void) |
9 | 126 { |
127 if (shellstream) | |
128 { | |
129 fclose (shellstream); | |
130 shellstream = NULL; | |
131 } | |
132 } | |
133 | |
134 /* Read a line from STREAM, removing any newline at the end. | |
135 Place the result in *NAME, which is malloc'd | |
136 and/or realloc'd as necessary and can start out NULL, | |
137 and whose size is passed and returned in *SIZE. | |
138 | |
139 Return the number of characters placed in *NAME | |
140 if some nonempty sequence was found, otherwise 0. */ | |
141 | |
142 static int | |
4655 | 143 readname (char **name, int *size, FILE *stream) |
9 | 144 { |
145 int c; | |
146 int name_index = 0; | |
147 | |
148 if (*name == NULL) | |
149 { | |
150 *size = 10; | |
4541
3cb007557d70
(readname): Remove casts no longer required in C89.
Paul Eggert <eggert@cs.ucla.edu>
parents:
3618
diff
changeset
|
151 *name = xmalloc (*size); |
9 | 152 } |
153 | |
154 /* Skip blank space. */ | |
1041 | 155 while ((c = getc (stream)) != EOF && ISSPACE (c)) |
9 | 156 /* Do nothing. */ ; |
790 | 157 |
1041 | 158 while (c != EOF && !ISSPACE (c)) |
9 | 159 { |
160 (*name)[name_index++] = c; | |
161 while (name_index >= *size) | |
162 { | |
163 *size *= 2; | |
4541
3cb007557d70
(readname): Remove casts no longer required in C89.
Paul Eggert <eggert@cs.ucla.edu>
parents:
3618
diff
changeset
|
164 *name = xrealloc (*name, *size); |
9 | 165 } |
166 c = getc (stream); | |
167 } | |
168 (*name)[name_index] = '\0'; | |
169 return name_index; | |
170 } | |
171 | |
172 #ifdef TEST | |
4655 | 173 int |
174 main (void) | |
9 | 175 { |
176 char *s; | |
177 | |
178 while (s = getusershell ()) | |
179 puts (s); | |
180 exit (0); | |
181 } | |
182 #endif |