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

Subversion Repositories or1k_old

[/] [or1k_old/] [trunk/] [linux/] [uClibc/] [test/] [pwd_grp/] [getgroups.c] - Blame information for rev 1782

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1325 phoenix
/* This test was ripped out of GNU 'id' from coreutils-5.0
2
 * by Erik Andersen.
3
 *
4
 *
5
 * id is Copyright (C) 1989-2003 Free Software Foundation, Inc.
6
 * and licensed under the GPL v2 or later, and was written by
7
 * Arnold Robbins, with a major rewrite by David MacKenzie,
8
 */
9
 
10
#include <stdio.h>
11
#include <stdlib.h>
12
#include <unistd.h>
13
#include <sys/types.h>
14
#include <pwd.h>
15
#include <grp.h>
16
#include <err.h>
17
 
18
/* The number of errors encountered so far. */
19
static int problems = 0;
20
 
21
/* Print the name or value of group ID GID. */
22
static void
23
print_group (gid_t gid)
24
{
25
    struct group *grp = NULL;
26
 
27
    grp = getgrgid (gid);
28
    if (grp == NULL)
29
    {
30
        warn("cannot find name for group ID %u", gid);
31
        problems++;
32
    }
33
 
34
    if (grp == NULL)
35
        printf ("%u", (unsigned) gid);
36
    else
37
        printf ("%s", grp->gr_name);
38
}
39
 
40
static int
41
xgetgroups (gid_t gid, int *n_groups, gid_t **groups)
42
{
43
    int max_n_groups;
44
    int ng;
45
    gid_t *g;
46
    int fail = 0;
47
 
48
    max_n_groups = getgroups (0, NULL);
49
 
50
    /* Add 1 just in case max_n_groups is zero.  */
51
    g = (gid_t *) malloc (max_n_groups * sizeof (gid_t) + 1);
52
    if (g==NULL)
53
        err(EXIT_FAILURE, "out of memory");
54
    ng = getgroups (max_n_groups, g);
55
 
56
    if (ng < 0)
57
    {
58
        warn("cannot get supplemental group list");
59
        ++fail;
60
        free (groups);
61
    }
62
    if (!fail)
63
    {
64
        *n_groups = ng;
65
        *groups = g;
66
    }
67
    return fail;
68
}
69
 
70
/* Print all of the distinct groups the user is in. */
71
int main (int argc, char **argv)
72
{
73
    struct passwd *pwd;
74
 
75
    pwd = getpwuid (getuid());
76
    if (pwd == NULL)
77
        problems++;
78
 
79
    print_group (getgid());
80
    if (getegid() != getgid())
81
    {
82
        putchar (' ');
83
        print_group (getegid());
84
    }
85
 
86
    {
87
        int n_groups;
88
        gid_t *groups;
89
        register int i;
90
 
91
        if (xgetgroups ((pwd ? pwd->pw_gid : (gid_t) -1),
92
                    &n_groups, &groups))
93
        {
94
            return ++problems;
95
        }
96
 
97
        for (i = 0; i < n_groups; i++)
98
            if (groups[i] != getgid() && groups[i] != getegid())
99
            {
100
                putchar (' ');
101
                print_group (groups[i]);
102
            }
103
        free (groups);
104
    }
105
    putchar('\n');
106
    return (problems != 0);
107
}
108
 

powered by: WebSVN 2.1.0

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