~ubuntu-branches/ubuntu/wily/libuser/wily

« back to all changes in this revision

Viewing changes to apps/lid.c

  • Committer: Bazaar Package Importer
  • Author(s): Pierre Habouzit
  • Date: 2006-09-03 21:58:15 UTC
  • mto: (2.1.1 edgy) (1.1.5 upstream) (3.1.1 lenny)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20060903215815-rtvvfzhaer8ymyp4
Tags: upstream-0.54.6-2.1.dfsg.1
ImportĀ upstreamĀ versionĀ 0.54.6-2.1.dfsg.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * Copyright (C) 2001, 2002, 2004 Red Hat, Inc.
 
2
 * Copyright (C) 2001, 2002, 2004, 2006 Red Hat, Inc.
3
3
 *
4
4
 * This is free software; you can redistribute it and/or modify it under
5
5
 * the terms of the GNU Library General Public License as published by
16
16
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
17
 */
18
18
 
19
 
#ident "$Id: lid.c,v 1.18 2004/11/13 23:50:28 mitr Exp $"
 
19
#ident "$Id: lid.c,v 1.20 2006/03/06 03:02:52 mitr Exp $"
20
20
 
21
21
#ifdef HAVE_CONFIG_H
22
22
#include "config.h"
31
31
#include "../lib/user.h"
32
32
#include "apputil.h"
33
33
 
 
34
static void
 
35
do_id (struct lu_context *ctx, const char *name, int nameonly,
 
36
       GValueArray *(*enumerate) (lu_context_t *, const char *, lu_error_t **),
 
37
       gboolean (*lookup) (lu_context_t *, const char *, struct lu_ent *,
 
38
                           lu_error_t **),
 
39
       const char *id_attribute, const char *id_descr)
 
40
{
 
41
        GValueArray *values;
 
42
        struct lu_error *error;
 
43
 
 
44
        error = NULL;
 
45
        values = enumerate(ctx, name, &error);
 
46
        if (error != NULL) {
 
47
                fprintf(stderr, _("Error looking up %s: %s\n"), name,
 
48
                        lu_strerror(error));
 
49
                lu_error_free(&error);
 
50
                exit(1);
 
51
        }
 
52
        if (values != NULL) {
 
53
                struct lu_ent *ent;
 
54
                size_t i;
 
55
 
 
56
                ent = lu_ent_new();
 
57
                for (i = 0; i < values->n_values; i++) {
 
58
                        GValue *value;
 
59
                        const char *found;
 
60
 
 
61
                        value = g_value_array_get_nth(values, i);
 
62
                        found = g_value_get_string(value);
 
63
                        if (!nameonly && lookup(ctx, found, ent, &error)) {
 
64
                                GValueArray *attrs;
 
65
 
 
66
                                attrs = lu_ent_get(ent, id_attribute);
 
67
                                if (attrs != NULL) {
 
68
                                        char *tmp;
 
69
 
 
70
                                        value = g_value_array_get_nth(attrs,
 
71
                                                                      0);
 
72
                                        tmp = lu_value_strdup(value);
 
73
                                        g_print(" %s(%s=%s)\n", found,
 
74
                                                id_descr, tmp);
 
75
                                        g_free(tmp);
 
76
                                } else
 
77
                                        g_print(" %s\n", found);
 
78
                        } else {
 
79
                                if (error != NULL)
 
80
                                        lu_error_free(&error);
 
81
                                g_print(" %s\n", found);
 
82
                        }
 
83
                        lu_ent_clear_all(ent);
 
84
                }
 
85
                lu_ent_free(ent);
 
86
                g_value_array_free(values);
 
87
        }
 
88
}
 
89
 
34
90
int
35
91
main(int argc, const char **argv)
36
92
{
37
 
        const char *name = NULL;
38
 
        char *tmp = NULL;
39
 
        struct lu_context *ctx = NULL;
 
93
        const char *name;
 
94
        struct lu_context *ctx;
40
95
        struct lu_error *error = NULL;
41
 
        struct lu_ent *ent = NULL;
42
 
        GValueArray *values, *attrs;
43
 
        GValue *value;
44
96
        int interactive = FALSE;
45
97
        int groupflag = FALSE, nameonly = FALSE;
46
98
        int c;
47
 
        size_t i;
48
99
        poptContext popt;
49
 
        struct passwd *pwd = NULL;
50
 
        struct group *grp = NULL;
51
100
        struct poptOption options[] = {
52
101
                {"interactive", 'i', POPT_ARG_NONE, &interactive, 0,
53
102
                 "prompt for all information", NULL},
77
126
 
78
127
        if (name == NULL) {
79
128
                if (groupflag) {
 
129
                        struct group *grp;
 
130
 
80
131
                        grp = getgrgid(getgid());
81
132
                        if (grp != NULL) {
82
133
                                fprintf(stderr, _("No group name specified, "
89
140
                                exit(1);
90
141
                        }
91
142
                } else {
 
143
                        struct passwd *pwd;
 
144
 
92
145
                        pwd = getpwuid(getuid());
93
146
                        if (pwd != NULL) {
94
147
                                fprintf(stderr, _("No user name specified, "
113
166
                return 1;
114
167
        }
115
168
 
116
 
        if (groupflag) {
117
 
                values = lu_users_enumerate_by_group(ctx, name, &error);
118
 
                if (values != NULL) {
119
 
                        ent = lu_ent_new();
120
 
                        for (i = 0; i < values->n_values; i++) {
121
 
                                value = g_value_array_get_nth(values, i);
122
 
                                name = g_value_get_string(value);
123
 
                                if (!nameonly
124
 
                                    && lu_user_lookup_name(ctx,
125
 
                                                           name,
126
 
                                                           ent,
127
 
                                                           &error)) {
128
 
                                        attrs = lu_ent_get(ent, LU_UIDNUMBER);
129
 
                                        if (attrs != NULL) {
130
 
                                                value = g_value_array_get_nth(attrs,
131
 
                                                                              0);
132
 
                                                tmp = lu_value_strdup(value);
133
 
                                                g_print(" %s(uid=%s)\n",
134
 
                                                        name, tmp);
135
 
                                                g_free(tmp);
136
 
                                        } else {
137
 
                                                g_print(" %s\n", name);
138
 
                                        }
139
 
                                } else {
140
 
                                        if (error) {
141
 
                                                lu_error_free(&error);
142
 
                                        }
143
 
                                        g_print(" %s\n", name);
144
 
                                }
145
 
                                lu_ent_clear_all(ent);
146
 
                        }
147
 
                        lu_ent_free(ent);
148
 
                        g_value_array_free(values);
149
 
                }
150
 
        } else {
151
 
                values = lu_groups_enumerate_by_user(ctx, name, &error);
152
 
                if (values != NULL) {
153
 
                        ent = lu_ent_new();
154
 
                        for (i = 0; i < values->n_values; i++) {
155
 
                                value = g_value_array_get_nth(values, i);
156
 
                                name = g_value_get_string(value);
157
 
                                if (!nameonly &&
158
 
                                    lu_group_lookup_name(ctx,
159
 
                                                         name,
160
 
                                                         ent,
161
 
                                                         &error)) {
162
 
                                        attrs = lu_ent_get(ent, LU_GIDNUMBER);
163
 
                                        if (attrs != NULL) {
164
 
                                                value = g_value_array_get_nth(attrs,
165
 
                                                                              0);
166
 
                                                tmp = lu_value_strdup(value);
167
 
                                                g_print(" %s(gid=%s)\n",
168
 
                                                        name,
169
 
                                                        tmp);
170
 
                                                g_free(tmp);
171
 
                                        } else {
172
 
                                                g_print(" %s\n", name);
173
 
                                        }
174
 
                                } else {
175
 
                                        if (error) {
176
 
                                                lu_error_free(&error);
177
 
                                        }
178
 
                                        g_print(" %s\n", name);
179
 
                                }
180
 
                                lu_ent_clear_all(ent);
181
 
                        }
182
 
                        lu_ent_free(ent);
183
 
                        g_value_array_free(values);
184
 
                }
185
 
        }
 
169
        if (groupflag)
 
170
                do_id(ctx, name, nameonly, lu_users_enumerate_by_group,
 
171
                      lu_user_lookup_name, LU_UIDNUMBER, "uid");
 
172
        else
 
173
                do_id(ctx, name, nameonly, lu_groups_enumerate_by_user,
 
174
                      lu_group_lookup_name, LU_GIDNUMBER, "gid");
186
175
 
187
176
        lu_end(ctx);
188
177