Merge branch 'vendor/BINUTILS221'
[dragonfly.git] / contrib / opie / libmissing / getusershell.c
1 /* getusershell.c: minimal implementation of the getusershell() and
2    endusershell() library routines for systems that don't have them.
3
4 %%% portions-copyright-cmetz
5 Portions of this software are Copyright 1996 by Craig Metz, All Rights
6 Reserved. The Inner Net License Version 2 applies to these portions of
7 the software.
8 You should have received a copy of the license with this software. If
9 you didn't get a copy, you may request one from <license@inner.net>.
10
11 Portions of this software are Copyright 1995 by Randall Atkinson and Dan
12 McDonald, All Rights Reserved. All Rights under this copyright are assigned
13 to the U.S. Naval Research Laboratory (NRL). The NRL Copyright Notice and
14 License Agreement applies to this software.
15
16         History:
17
18         Modified by cmetz for OPIE 2.2. Use FUNCTION declaration et al.
19         Modified at NRL for OPIE 2.1. Remove trailing newlines from
20                 /etc/shells entries. Fixed infinite loop. Fixed a bug
21                 where second invocation on would fail.
22         Written at NRL for OPIE 2.0.
23 */
24 #include "opie_cfg.h"
25 #include <stdio.h>
26 #if HAVE_STRING_H
27 #include <string.h>
28 #endif /* HAVE_STRING_H */
29 #include "opie.h"
30
31 static FILE *fh = NULL;
32 static char *internal[] = {"/bin/sh", "/bin/csh", NULL};
33 static int i = 0;
34 static char buffer[1024];
35
36 char *getusershell FUNCTION_NOARGS
37 {
38   char *c;
39
40   if (!fh)
41     fh = fopen("/etc/shells", "r");
42
43   if (fh) {
44     if (fgets(buffer, sizeof(buffer), fh)) {
45       if (c = strchr(buffer, '\n'))
46         *c = 0;
47       return buffer;
48     } else {
49       fclose(fh);
50       return NULL;
51     }
52   } else {
53     if (internal[i])
54       return internal[i++];
55     else
56       return NULL;
57   }
58 }
59
60 VOIDRET endusershell FUNCTION_NOARGS
61 {
62   if (fh) {
63     fclose(fh);
64     fh = NULL;
65   }
66   i = 0;
67 }