OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [uClibc/] [libc/] [unistd/] [usershell.c] - Blame information for rev 1771

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1325 phoenix
/*
2
 * Copyright (c) 1985, 1993
3
 *      The Regents of the University of California.  All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 * 1. Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 * 2. Redistributions in binary form must reproduce the above copyright
11
 *    notice, this list of conditions and the following disclaimer in the
12
 *    documentation and/or other materials provided with the distribution.
13
 * 4. Neither the name of the University nor the names of its contributors
14
 *    may be used to endorse or promote products derived from this software
15
 *    without specific prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
 * SUCH DAMAGE.
28
 *
29
 * This version has been hevily modified for use with uClibc
30
 * November 2002, Erik Andersen <andersen@codepoet.org>
31
 */
32
 
33
#include <sys/param.h>
34
#include <sys/file.h>
35
#include <sys/stat.h>
36
#include <stdio.h>
37
#include <stdio_ext.h>
38
#include <ctype.h>
39
#include <stdlib.h>
40
#include <unistd.h>
41
#include <paths.h>
42
 
43
/*
44
 * Local shells should NOT be added here.  They should be added in
45
 * /etc/shells.
46
 */
47
 
48
static const char * const validsh[] = { _PATH_BSHELL, _PATH_CSHELL, NULL };
49
static char **curshell, **shells, *strings;
50
static char **initshells __P((void));
51
 
52
/*
53
 * Get a list of shells from _PATH_SHELLS, if it exists.
54
 */
55
char * getusershell(void)
56
{
57
    char *ret;
58
 
59
    if (curshell == NULL)
60
        curshell = initshells();
61
    ret = *curshell;
62
    if (ret != NULL)
63
        curshell++;
64
    return (ret);
65
}
66
 
67
static void __free_initshell_memory(void)
68
{
69
    if (shells != NULL) {
70
        free(shells);
71
    }
72
    shells = NULL;
73
    if (strings != NULL) {
74
        free(strings);
75
    }
76
    strings = NULL;
77
}
78
 
79
void endusershell(void)
80
{
81
    __free_initshell_memory();
82
    curshell = NULL;
83
}
84
 
85
void setusershell(void)
86
{
87
 
88
    curshell = initshells();
89
}
90
 
91
static char ** initshells(void)
92
{
93
    register char **sp, *cp;
94
    register FILE *fp;
95
    struct stat statb;
96
    int flen;
97
 
98
    __free_initshell_memory();
99
 
100
    if ((fp = fopen(_PATH_SHELLS, "r")) == NULL)
101
        return (char **) validsh;
102
    if (fstat(fileno(fp), &statb) == -1) {
103
        goto cleanup;
104
    }
105
    if ((strings = malloc((unsigned)statb.st_size + 1)) == NULL) {
106
        goto cleanup;
107
    }
108
    if ((shells = calloc((unsigned)statb.st_size / 3, sizeof (char *))) == NULL) {
109
        goto cleanup;
110
    }
111
    /* No threads using this stream.  */
112
#ifdef __UCLIBC_HAS_THREADS__
113
    __fsetlocking (fp, FSETLOCKING_BYCALLER);
114
#endif
115
    sp = shells;
116
    cp = strings;
117
    flen = statb.st_size;
118
    while (fgets_unlocked(cp, flen - (cp - strings), fp) != NULL) {
119
        while (*cp != '#' && *cp != '/' && *cp != '\0')
120
            cp++;
121
        if (*cp == '#' || *cp == '\0')
122
            continue;
123
        *sp++ = cp;
124
        while (!isspace(*cp) && *cp != '#' && *cp != '\0')
125
            cp++;
126
        *cp++ = '\0';
127
    }
128
    *sp = NULL;
129
    fclose(fp);
130
    return (shells);
131
 
132
cleanup:
133
    __free_initshell_memory();
134
    fclose(fp);
135
    return (char **) validsh;
136
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.