xref: /aosp_15_r20/external/musl/src/legacy/getusershell.c (revision c9945492fdd68bbe62686c5b452b4dc1be3f8453)
1 #define _GNU_SOURCE
2 #include <stdio.h>
3 #include <unistd.h>
4 
5 static const char defshells[] = "/bin/sh\n/bin/csh\n";
6 
7 static char *line;
8 static size_t linesize;
9 static FILE *f;
10 
endusershell(void)11 void endusershell(void)
12 {
13 	if (f) fclose(f);
14 	f = 0;
15 }
16 
setusershell(void)17 void setusershell(void)
18 {
19 	if (!f) f = fopen("/etc/shells", "rbe");
20 	if (!f) f = fmemopen((void *)defshells, sizeof defshells - 1, "rb");
21 }
22 
getusershell(void)23 char *getusershell(void)
24 {
25 	ssize_t l;
26 	if (!f) setusershell();
27 	if (!f) return 0;
28 	do {
29 		l = getline(&line, &linesize, f);
30 		if (l <= 0) return 0;
31 	} while (line[0] == '#' || line[0] == '\n');
32 	if (line[l-1]=='\n') line[l-1]=0;
33 	return line;
34 }
35