From 8f218561c608f2eaac446af3b35298ffcc3fce42 Mon Sep 17 00:00:00 2001 From: tv Date: Mon, 11 Aug 2025 21:43:11 +0200 Subject: exec: init --- exec.c | 105 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 exec.c (limited to 'exec.c') diff --git a/exec.c b/exec.c new file mode 100644 index 0000000..739ae74 --- /dev/null +++ b/exec.c @@ -0,0 +1,105 @@ +#include +#include +#include + +static void usage(const char *progname) { + fprintf(stderr, "Usage: %s [-n name] COMMAND [ARGS...]\n", progname); +} + +int main(int argc, char *argv[]) { + int opt; + char *cmd_name = NULL; + + // Parse options + while ((opt = getopt(argc, argv, "n:")) != -1) { + switch (opt) { + case 'n': + cmd_name = optarg; + break; + case '?': + usage(argv[0]); + return 1; + } + } + + // Remaining args are COMMAND [ARGS...] + if (optind >= argc) { + usage(argv[0]); + return 1; + } + + char *command = argv[optind]; + char *final_argv0 = cmd_name ? cmd_name : command; + + // Prepare new argv for execvp + int new_argc = argc - optind; + char *new_argv[argc + 1]; + + new_argv[0] = final_argv0; + for (int i = 1; i < new_argc; i++) { + new_argv[i] = argv[optind + i]; + } + new_argv[new_argc] = NULL; + + execvp(command, new_argv); + perror("execvp"); + return 1; +} + +//#define _GNU_SOURCE +//#include +//#include +//#include +//#include +//#include +// +//Int main(int argc, char *argv[]) { +// char *command_name = NULL; +// +// // Define long options +// static struct option long_options[] = { +// {"name", required_argument, 0, 'n'}, +// {0, 0, 0, 0} +// }; +// +// // Parse options +// int opt_index = 0; +// int c; +// while ((c = getopt_long(argc, argv, "n:", long_options, &opt_index)) != -1) { +// switch (c) { +// case 'n': +// command_name = optarg; +// break; +// default: +// fprintf(stderr, "Usage: %s --name=NEW_NAME COMMAND [ARGS...]\n", argv[0]); +// return 1; +// } +// } +// +// if (!command_name || optind >= argc) { +// fprintf(stderr, "Usage: %s --name=NEW_NAME COMMAND [ARGS...]\n", argv[0]); +// return 1; +// } +// +// // Construct new argv with custom argv[0] +// int new_argc = argc - optind + 1; +// char **new_argv = malloc(sizeof(char *) * (new_argc + 1)); +// if (!new_argv) { +// perror("malloc failed"); +// return 1; +// } +// +// new_argv[0] = command_name; +// for (int i = 0; i < argc - optind; ++i) { +// new_argv[i + 1] = argv[optind + i]; +// } +// new_argv[new_argc] = NULL; +// +// // Exec the command +// execvp(new_argv[0], new_argv); +// +// // If we’re here, exec failed +// perror("execvp failed"); +// free(new_argv); +// return 1; +//} -- cgit v1.2.3