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

« back to all changes in this revision

Viewing changes to apps/lgroupadd.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, 2004 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: lgroupadd.c,v 1.28 2004/11/13 23:50:28 mitr Exp $"
 
20
 
 
21
#ifdef HAVE_CONFIG_H
 
22
#include "config.h"
 
23
#endif
 
24
#include <errno.h>
 
25
#include <inttypes.h>
 
26
#include <stdio.h>
 
27
#include <stdlib.h>
 
28
#include <unistd.h>
 
29
#include <libintl.h>
 
30
#include <locale.h>
 
31
#include <popt.h>
 
32
#include <string.h>
 
33
#include "../lib/user_private.h"
 
34
#include "apputil.h"
 
35
 
 
36
int
 
37
main(int argc, const char **argv)
 
38
{
 
39
        const char *name = NULL, *gid_number_str = NULL;
 
40
        gid_t gidNumber = LU_VALUE_INVALID_ID;
 
41
        struct lu_context *ctx = NULL;
 
42
        struct lu_ent *ent = NULL;
 
43
        struct lu_error *error = NULL;
 
44
        int interactive = FALSE;
 
45
        int system_account = FALSE;
 
46
        int c;
 
47
 
 
48
        poptContext popt;
 
49
        struct poptOption options[] = {
 
50
                {"interactive", 'i', POPT_ARG_NONE, &interactive, 0,
 
51
                 "prompt for all information", NULL},
 
52
                {"gid", 'g', POPT_ARG_STRING, &gid_number_str, 0,
 
53
                 "gid to force for new group", "NUM"},
 
54
                {"reserved", 'r', POPT_ARG_NONE, &system_account, 0,
 
55
                 "make this a system group", NULL},
 
56
                POPT_AUTOHELP POPT_TABLEEND
 
57
        };
 
58
 
 
59
        /* Set up i18n. */
 
60
        bindtextdomain(PACKAGE, LOCALEDIR);
 
61
        textdomain(PACKAGE);
 
62
        setlocale(LC_ALL, "");
 
63
 
 
64
        /* Parse arguments. */
 
65
        popt = poptGetContext("lgroupadd", argc, argv, options, 0);
 
66
        poptSetOtherOptionHelp(popt, _("[OPTION...] group"));
 
67
        c = poptGetNextOpt(popt);
 
68
        if (c != -1) {
 
69
                fprintf(stderr, _("Error parsing arguments: %s.\n"),
 
70
                        poptStrerror(c));
 
71
                poptPrintUsage(popt, stderr, 0);
 
72
                exit(1);
 
73
        }
 
74
        name = poptGetArg(popt);
 
75
 
 
76
        /* We require a group name to be specified. */
 
77
        if (name == NULL) {
 
78
                fprintf(stderr, _("No group name specified.\n"));
 
79
                poptPrintUsage(popt, stderr, 0);
 
80
                return 1;
 
81
        }
 
82
 
 
83
        if (gid_number_str != NULL) {
 
84
                intmax_t val;
 
85
                char *p;
 
86
 
 
87
                errno = 0;
 
88
                val = strtoimax(gid_number_str, &p, 10);
 
89
                if (errno != 0 || *p != 0 || p == gid_number_str
 
90
                    || (gid_t)val != val) {
 
91
                        fprintf(stderr, _("Invalid group ID %s\n"),
 
92
                                gid_number_str);
 
93
                        poptPrintUsage(popt, stderr, 0);
 
94
                        return 1;
 
95
                }
 
96
                gidNumber = val;
 
97
        }
 
98
                
 
99
        /* Start up the library. */
 
100
        ctx = lu_start(NULL, 0, NULL, NULL,
 
101
                       interactive ? lu_prompt_console :
 
102
                       lu_prompt_console_quiet, NULL, &error);
 
103
        if (ctx == NULL) {
 
104
                fprintf(stderr, _("Error initializing %s: %s.\n"), PACKAGE,
 
105
                        lu_strerror(error));
 
106
                return 1;
 
107
        }
 
108
 
 
109
        /* Create a group entity object holding sensible defaults for a
 
110
         * new group. */
 
111
        ent = lu_ent_new();
 
112
        lu_group_default(ctx, name, system_account, ent);
 
113
 
 
114
        /* If the user specified a particular GID number, override the
 
115
         * default. */
 
116
        if (gidNumber != LU_VALUE_INVALID_ID) {
 
117
                GValue value;
 
118
 
 
119
                memset(&value, 0, sizeof(value));
 
120
                lu_value_init_set_id(&value, gidNumber);
 
121
                lu_ent_clear(ent, LU_GIDNUMBER);
 
122
                lu_ent_add(ent, LU_GIDNUMBER, &value);
 
123
                g_value_unset(&value);
 
124
        }
 
125
 
 
126
        /* Try to create the group. */
 
127
        if (lu_group_add(ctx, ent, &error) == FALSE) {
 
128
                fprintf(stderr, _("Group creation failed: %s\n"),
 
129
                        lu_strerror(error));
 
130
                return 2;
 
131
        }
 
132
 
 
133
        lu_hup_nscd();
 
134
 
 
135
        lu_ent_free(ent);
 
136
 
 
137
        lu_end(ctx);
 
138
 
 
139
        return 0;
 
140
}