2 * Copyright 2013, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
16 #include <RegistrarDefs.h>
17 #include <user_group.h>
18 #include <util/KMessage.h>
20 #include "multiuser_utils.h"
23 extern const char *__progname
;
26 static const char* kUsage
=
27 "Usage: %s [ <options> ] <group name>\n"
28 "Creates a new group <group name>.\n"
32 " Print usage info.\n"
36 print_usage_and_exit(bool error
)
38 fprintf(error
? stderr
: stdout
, kUsage
, __progname
);
44 main(int argc
, const char* const* argv
)
47 static struct option sLongOptions
[] = {
48 { "help", no_argument
, 0, 'h' },
52 opterr
= 0; // don't print errors
53 int c
= getopt_long(argc
, (char**)argv
, "h", sLongOptions
, NULL
);
60 print_usage_and_exit(false);
64 print_usage_and_exit(true);
69 if (optind
!= argc
- 1)
70 print_usage_and_exit(true);
72 const char* group
= argv
[optind
];
75 fprintf(stderr
, "Error: Only root may add groups.\n");
79 // check, if group already exists
80 if (getgrnam(group
) != NULL
) {
81 fprintf(stderr
, "Error: Group \"%s\" already exists.\n", group
);
87 while (getgrgid(gid
) != NULL
)
90 // prepare request for the registrar
91 KMessage
message(BPrivate::B_REG_UPDATE_GROUP
);
92 if (message
.AddInt32("gid", gid
) != B_OK
93 || message
.AddString("name", group
) != B_OK
94 || message
.AddString("password", "x") != B_OK
95 || message
.AddBool("add group", true) != B_OK
) {
96 fprintf(stderr
, "Error: Out of memory!\n");
102 status_t error
= send_authentication_request_to_registrar(message
, reply
);
104 fprintf(stderr
, "Error: Failed to create group: %s\n", strerror(error
));