more close-on-exec fixes, mostly using new "e" flag to fopen
[musl] / src / legacy / getusershell.c
1 #define _GNU_SOURCE
2 #include <stdio.h>
3 #include <string.h>
4 #include <stdlib.h>
5
6 static const char defshells[] = "/bin/sh\n/bin/csh\n";
7
8 static char *line;
9 static size_t linesize;
10 static FILE *f;
11
12 void endusershell(void)
13 {
14         if (f) fclose(f);
15         f = 0;
16 }
17
18 void setusershell(void)
19 {
20         if (!f) f = fopen("/etc/shells", "rbe");
21         if (!f) f = fmemopen((void *)defshells, sizeof defshells - 1, "rb");
22 }
23
24 char *getusershell(void)
25 {
26         ssize_t l;
27         if (!f) setusershell();
28         if (!f) return 0;
29         l = getline(&line, &linesize, f);
30         if (l <= 0) return 0;
31         if (line[l-1]=='\n') line[l-1]=0;
32         return line;
33 }