summaryrefslogtreecommitdiffstats
path: root/libc/unistd/usershell.c
blob: 2a6b66504acc07c6eb7b51af2e30ee36067657c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/* setusershell(), getusershell(), endusershell() for uClibc.
 *
 * Copyright (C) 2010 Bernhard Reutner-Fischer <uclibc@uclibc.org>
 *
 * Licensed under the LGPL v2.1 or later, see the file COPYING.LIB in
 * this tarball.
 */
/* My manpage reads:
 * The  getusershell() function returns the next line from the file
 * /etc/shells, opening the file if necessary. The line should contain
 * the pathname of a valid user shell.  If /etc/shells does not exist
 * or is unreadable, getusershell() behaves as if /bin/sh and /bin/csh
 * were listed in the file.
 * The getusershell() function returns a NULL pointer on end-of-file.
 */
#include <unistd.h>
#include <stdlib.h>
#include <paths.h>
#include <string.h>
#include "internal/parse_config.h"

#if defined __USE_BSD || (defined __USE_XOPEN && !defined __USE_UNIX98)

static const char * const defaultsh[] = { _PATH_BSHELL, _PATH_CSHELL, NULL};
static char *shellb, **shells;
static parser_t *shellp;

void endusershell(void)
{
	if (shellp) {
		shells = (char**) shellb;
		while (shells && *shells) {
			char*xxx = *shells++;
			free(xxx);
		}
		config_close(shellp);
		shellp = NULL;
	}
	free(shellb);
	shellb = NULL;
	shells = NULL;
}
void setusershell(void)
{
	endusershell();
	shellp = config_open(_PATH_SHELLS);
	if (shellp == NULL)
		shells = (char **)defaultsh;
	else {
		char **shell = NULL;
		int pos = 0;

		while (config_read(shellp, &shell, 1, 1, "# \t", PARSE_NORMAL))
		{
			shellb = realloc(shellb, (pos + 2) * sizeof(char*));
			shells = (char**) shellb + pos++;
			*shells++ = strdup(*shell);
			*shells = NULL;

		}
		shells = (char **)shellb;
	}
}
char *getusershell(void)
{
	char *sh;
	if (shells == NULL)
		setusershell();
	sh = *shells;
	if (sh)
		shells++;
	return sh;
}
#endif