Tizen 2.0 Release
[external/tizen-coreutils.git] / lib / getusershell.c
1 /* getusershell.c -- Return names of valid user shells.
2
3    Copyright (C) 1991, 1997, 2000, 2001, 2003, 2004, 2005, 2006 Free
4    Software Foundation, Inc.
5
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 2, or (at your option)
9    any later version.
10
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.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software Foundation,
18    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
19
20 /* Written by David MacKenzie <djm@gnu.ai.mit.edu> */
21
22 #include <config.h>
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 <stdlib.h>
35 #include <ctype.h>
36
37 #include "stdio--.h"
38 #include "xalloc.h"
39
40 #if USE_UNLOCKED_IO
41 # include "unlocked-io.h"
42 #endif
43
44 static size_t readname (char **, size_t *, FILE *);
45
46 #if ! defined ADDITIONAL_DEFAULT_SHELLS && defined __MSDOS__
47 # define ADDITIONAL_DEFAULT_SHELLS \
48   "c:/dos/command.com", "c:/windows/command.com", "c:/command.com",
49 #else
50 # define ADDITIONAL_DEFAULT_SHELLS /* empty */
51 #endif
52
53 /* List of shells to use if the shells file is missing. */
54 static char const* const default_shells[] =
55 {
56   ADDITIONAL_DEFAULT_SHELLS
57   "/bin/sh", "/bin/csh", "/usr/bin/sh", "/usr/bin/csh", NULL
58 };
59
60 /* Index of the next shell in `default_shells' to return.
61    0 means we are not using `default_shells'. */
62 static size_t default_index = 0;
63
64 /* Input stream from the shells file. */
65 static FILE *shellstream = NULL;
66
67 /* Line of input from the shells file. */
68 static char *line = NULL;
69
70 /* Number of bytes allocated for `line'. */
71 static size_t line_size = 0;
72 \f
73 /* Return an entry from the shells file, ignoring comment lines.
74    If the file doesn't exist, use the list in DEFAULT_SHELLS (above).
75    In any case, the returned string is in memory allocated through malloc.
76    Return NULL if there are no more entries.  */
77
78 char *
79 getusershell (void)
80 {
81   if (default_index > 0)
82     {
83       if (default_shells[default_index])
84         /* Not at the end of the list yet.  */
85         return xstrdup (default_shells[default_index++]);
86       return NULL;
87     }
88
89   if (shellstream == NULL)
90     {
91       shellstream = fopen (SHELLS_FILE, "r");
92       if (shellstream == NULL)
93         {
94           /* No shells file.  Use the default list.  */
95           default_index = 1;
96           return xstrdup (default_shells[0]);
97         }
98     }
99
100   while (readname (&line, &line_size, shellstream))
101     {
102       if (*line != '#')
103         return line;
104     }
105   return NULL;                  /* End of file. */
106 }
107
108 /* Rewind the shells file. */
109
110 void
111 setusershell (void)
112 {
113   default_index = 0;
114   if (shellstream)
115     rewind (shellstream);
116 }
117
118 /* Close the shells file. */
119
120 void
121 endusershell (void)
122 {
123   if (shellstream)
124     {
125       fclose (shellstream);
126       shellstream = NULL;
127     }
128 }
129
130 /* Read a line from STREAM, removing any newline at the end.
131    Place the result in *NAME, which is malloc'd
132    and/or realloc'd as necessary and can start out NULL,
133    and whose size is passed and returned in *SIZE.
134
135    Return the number of bytes placed in *NAME
136    if some nonempty sequence was found, otherwise 0.  */
137
138 static size_t
139 readname (char **name, size_t *size, FILE *stream)
140 {
141   int c;
142   size_t name_index = 0;
143
144   /* Skip blank space.  */
145   while ((c = getc (stream)) != EOF && isspace (c))
146     /* Do nothing. */ ;
147
148   for (;;)
149     {
150       if (*size <= name_index)
151         *name = x2nrealloc (*name, size, sizeof **name);
152       if (c == EOF || isspace (c))
153         break;
154       (*name)[name_index++] = c;
155       c = getc (stream);
156     }
157   (*name)[name_index] = '\0';
158   return name_index;
159 }
160
161 #ifdef TEST
162 int
163 main (void)
164 {
165   char *s;
166
167   while (s = getusershell ())
168     puts (s);
169   exit (0);
170 }
171 #endif