~ubuntu-branches/ubuntu/utopic/libuser/utopic-proposed

« back to all changes in this revision

Viewing changes to samples/testuser.c

  • Committer: Bazaar Package Importer
  • Author(s): Ghe Rivero
  • Date: 2005-09-30 16:22:04 UTC
  • Revision ID: james.westby@ubuntu.com-20050930162204-qubxaa7e2lbovdgh
Tags: upstream-0.54.dfsg.1
ImportĀ upstreamĀ versionĀ 0.54.dfsg.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2000-2002 Red Hat, Inc.
 
3
 *
 
4
 * This is free software; you can redistribute it and/or modify it under
 
5
 * the terms of the GNU Library General Public License as published by
 
6
 * the Free Software Foundation; either version 2 of the License, or
 
7
 * (at your option) any later version.
 
8
 *
 
9
 * This program is distributed in the hope that it will be useful, but
 
10
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
12
 * General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU Library General Public
 
15
 * License along with this program; if not, write to the Free Software
 
16
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
17
 */
 
18
 
 
19
#ident "$Id: testuser.c,v 1.17 2004/11/13 23:50:30 mitr Exp $"
 
20
 
 
21
#ifdef HAVE_CONFIG_H
 
22
#include "config.h"
 
23
#endif
 
24
#include <libintl.h>
 
25
#include <locale.h>
 
26
#include <stdlib.h>
 
27
#include "../lib/user_private.h"
 
28
 
 
29
static void
 
30
dump_attribute(const char *attribute, struct lu_ent *ent)
 
31
{
 
32
        GValueArray *array;
 
33
        GValue *value;
 
34
        size_t i;
 
35
 
 
36
        array = lu_ent_get(ent, attribute);
 
37
        if (array != NULL) {
 
38
                for (i = 0; i < array->n_values; i++) {
 
39
                        value = g_value_array_get_nth(array, i);
 
40
                        if (G_VALUE_HOLDS_STRING(value))
 
41
                                g_print("attribute %s = `%s'\n", attribute,
 
42
                                        g_value_get_string(value));
 
43
                        else if (G_VALUE_HOLDS_LONG(value))
 
44
                                g_print("attribute %s = %ld\n", attribute,
 
45
                                        g_value_get_long(value));
 
46
                        else if (G_VALUE_HOLDS_INT64(value))
 
47
                                g_print("attribute %s = %lld\n", attribute,
 
48
                                        (long long)g_value_get_int64(value));
 
49
                }
 
50
        }
 
51
}
 
52
 
 
53
int
 
54
main(void)
 
55
{
 
56
        struct lu_context *ctx;
 
57
        struct lu_ent *ent, *tmp, *temp;
 
58
        struct lu_error *error = NULL;
 
59
        GList *ret = NULL;
 
60
        size_t i;
 
61
        void *control = NULL;
 
62
 
 
63
        bindtextdomain(PACKAGE, LOCALEDIR);
 
64
        textdomain(PACKAGE);
 
65
        setlocale(LC_ALL, "");
 
66
 
 
67
        control = g_malloc0(65536);
 
68
 
 
69
        ctx =
 
70
            lu_start(NULL, 0, NULL, NULL, lu_prompt_console, NULL, &error);
 
71
 
 
72
        if (ctx == NULL) {
 
73
                fprintf(stderr, gettext("Error initializing %s: %s.\n"),
 
74
                        PACKAGE, lu_strerror(error));
 
75
                exit(1);
 
76
        }
 
77
 
 
78
        g_print(gettext("Default user object classes:\n"));
 
79
        ret = lu_cfg_read(ctx, "userdefaults/objectclass", "bar");
 
80
        for (i = 0; i < g_list_length(ret); i++) {
 
81
                g_print(" %s\n", (char *) g_list_nth(ret, i)->data);
 
82
        }
 
83
 
 
84
        g_print(gettext("Default user attribute names:\n"));
 
85
        ret = lu_cfg_read_keys(ctx, "userdefaults");
 
86
        for (i = 0; i < g_list_length(ret); i++) {
 
87
                g_print(" %s\n", (char *) g_list_nth(ret, i)->data);
 
88
        }
 
89
 
 
90
        g_print(gettext("Getting default user attributes:\n"));
 
91
        ent = lu_ent_new();
 
92
        lu_user_default(ctx, "newuser", FALSE, ent);
 
93
        lu_ent_dump(ent, stdout);
 
94
 
 
95
        dump_attribute(LU_UIDNUMBER, ent);
 
96
 
 
97
        g_print(gettext("Copying user structure:\n"));
 
98
        tmp = lu_ent_new();
 
99
        lu_ent_copy(ent, tmp);
 
100
        temp = lu_ent_new();
 
101
        lu_ent_copy(tmp, temp);
 
102
        lu_ent_dump(temp, stdout);
 
103
 
 
104
        lu_ent_free(ent);
 
105
        lu_ent_free(tmp);
 
106
        lu_ent_free(temp);
 
107
 
 
108
        lu_end(ctx);
 
109
 
 
110
        g_free(control);
 
111
 
 
112
        return 0;
 
113
}