aboutsummaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
authorKarel Kočí <cynerd@email.cz>2019-04-28 15:17:38 +0200
committerKarel Kočí <cynerd@email.cz>2019-04-28 15:17:38 +0200
commit3f0fa5587f6e69e8957e674d3ccde6ff8873302e (patch)
tree7eea6858f33fca7b47eb783f79caa14b85c2ebc7 /main.c
parente05d7a634692fd575189995f72c70420a4b220cb (diff)
downloaduroot-3f0fa5587f6e69e8957e674d3ccde6ff8873302e.tar.gz
uroot-3f0fa5587f6e69e8957e674d3ccde6ff8873302e.tar.bz2
uroot-3f0fa5587f6e69e8957e674d3ccde6ff8873302e.zip
Add argument parser
This implements argument parser based on glibc argp.
Diffstat (limited to 'main.c')
-rw-r--r--main.c57
1 files changed, 53 insertions, 4 deletions
diff --git a/main.c b/main.c
index 7957be3..0f4815c 100644
--- a/main.c
+++ b/main.c
@@ -1,7 +1,7 @@
/* uroot - User's root
* main.c Source file with main function
*
- * Copyright (C) 2018 Karel Kočí
+ * Copyright (C) 2018-2019 Karel Kočí
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
@@ -18,6 +18,7 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#define _GNU_SOURCE
+#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
@@ -25,19 +26,67 @@
#include <unistd.h>
#include <stdint.h>
#include <stdbool.h>
+#include <argp.h>
+#include <string.h>
#include "child.h"
#include "utils.h"
#include "sigpipe.h"
#include "conf.h"
+const char *argp_program_version = PACKAGE_STRING;
+const char *argp_program_bug_address = PACKAGE_BUGREPORT;
+static const char usage_doc[] = "[CMD [ARG..]]";
+static const char doc[] = "Tool using Linux namespaces to provide root like functionality for unprivileged users.";
+
+struct opts {
+ char **child_argv;
+ size_t child_argc;
+};
+
+static struct argp_option options[] = {
+ {"debug", 'd', NULL, 0, "Report uroot operations.", 0},
+ {NULL}
+};
+
+static error_t parse_opt(int key, char *arg, struct argp_state *state) {
+ struct opts *opts = state->input;
+ switch (key) {
+ case 'd':
+ // TODO logging
+ break;
+ case ARGP_KEY_ARGS:
+ opts->child_argc = state->argc - state->next;
+ opts->child_argv = malloc((opts->child_argc + 1) * sizeof *opts->child_argv);
+ memcpy(opts->child_argv, state->argv + state->next, opts->child_argc *
+ sizeof *opts->child_argv);
+ opts->child_argv[opts->child_argc] = NULL;
+ break;
+ case ARGP_KEY_INIT:
+ opts->child_argv = NULL;
+ opts->child_argc = 0;
+ break;
+ default:
+ return ARGP_ERR_UNKNOWN;
+ };
+ return 0;
+}
+
+static const struct argp argp_parser = {
+ .options = options,
+ .parser = parse_opt,
+ .args_doc = usage_doc,
+ .doc = doc,
+};
+
int main(int argc, char **argv) {
- // TODO arguments parsing
+ struct opts opts;
+ argp_parse(&argp_parser, argc, argv, ARGP_IN_ORDER, 0, &opts);
struct child_args chargs = (struct child_args) {
.ppid = getpid(),
- .argc = argc,
- .argv = argv,
+ .argc = opts.child_argc,
+ .argv = opts.child_argv,
.sigpipe = sigpipe_new()
};