1570af302Sopenharmony_ci#define _GNU_SOURCE
2570af302Sopenharmony_ci#include <stdio.h>
3570af302Sopenharmony_ci#include <unistd.h>
4570af302Sopenharmony_ci#include <unsupported_api.h>
5570af302Sopenharmony_ci
6570af302Sopenharmony_cistatic const char defshells[] = "/bin/sh\n/bin/csh\n";
7570af302Sopenharmony_ci
8570af302Sopenharmony_cistatic char *line;
9570af302Sopenharmony_cistatic size_t linesize;
10570af302Sopenharmony_cistatic FILE *f;
11570af302Sopenharmony_ci
12570af302Sopenharmony_civoid endusershell(void)
13570af302Sopenharmony_ci{
14570af302Sopenharmony_ci	UNSUPPORTED_API_VOID(LITEOS_A);
15570af302Sopenharmony_ci	if (f) fclose(f);
16570af302Sopenharmony_ci	f = 0;
17570af302Sopenharmony_ci}
18570af302Sopenharmony_ci
19570af302Sopenharmony_civoid setusershell(void)
20570af302Sopenharmony_ci{
21570af302Sopenharmony_ci	if (!f) f = fopen("/etc/shells", "rbe");
22570af302Sopenharmony_ci	if (!f) f = fmemopen((void *)defshells, sizeof defshells - 1, "rb");
23570af302Sopenharmony_ci}
24570af302Sopenharmony_ci
25570af302Sopenharmony_cichar *getusershell(void)
26570af302Sopenharmony_ci{
27570af302Sopenharmony_ci	ssize_t l;
28570af302Sopenharmony_ci	if (!f) setusershell();
29570af302Sopenharmony_ci	if (!f) return 0;
30570af302Sopenharmony_ci	l = getline(&line, &linesize, f);
31570af302Sopenharmony_ci	if (l <= 0) return 0;
32570af302Sopenharmony_ci	if (line[l-1]=='\n') line[l-1]=0;
33570af302Sopenharmony_ci	return line;
34570af302Sopenharmony_ci}
35