[tyndur-devel] [PATCH 2/7] libc: cmdline_get_argc/copy_argv
Kevin Wolf ·
+ libc: Neue Funktionen cmdline_get_argc und cmdline_copy_argv, die aus einer gegebenen Kommandozeile argc auslesen bzw. ein gegebenes argv aus der Kommandozeile befuellen * libc: crt0.c benutzt die neuen Funktionen statt der eigenen Implementierung Signed-off-by: Kevin Wolf <kevin at tyndur.org> --- src/modules/include/init.h | 2 ++ src/modules/lib/crt0.c | 42 ++++++++++-------------------------------- src/modules/lib/init.c | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 32 deletions(-) diff --git a/src/modules/include/init.h b/src/modules/include/init.h index 195511a..d431cee 100644 --- a/src/modules/include/init.h +++ b/src/modules/include/init.h @@ -43,6 +43,8 @@ char *init_service_get_name(pid_t pid); void init_process_exit(int result); pid_t init_execute(const char* cmd); +int cmdline_get_argc(const char* args); +void cmdline_copy_argv(char* args, const char** argv, size_t argc); diff --git a/src/modules/lib/crt0.c b/src/modules/lib/crt0.c index 4c3192d..fb42271 100644 --- a/src/modules/lib/crt0.c +++ b/src/modules/lib/crt0.c @@ -27,8 +27,9 @@ */ #include <stdlib.h> -#include "syscall.h" -#include "rpc.h" +#include <syscall.h> +#include <rpc.h> +#include <init.h> #include <string.h> #include <stdio.h> @@ -56,7 +57,9 @@ static void call_constructors(void) void __attribute__((weak)) _start(void) { - //v(); + char* args = NULL; + int argc; + init_memory_manager(); init_messaging(); init_sync_messages(); @@ -70,37 +73,12 @@ void __attribute__((weak)) _start(void) //Kommandozeilenargumente Parsen ---- //TODO: Das ist alles noch sehr primitiv //Erst den String vom Kernel holen - char* args = get_cmdline(); - int argc, pos, f; - argc = 0; - f = 0; - //Jetzt werden die Argumente gezaehlt. - for (pos = 0; pos < strlen(args); pos++) { - if (args[pos] == ' ') { - // mehrere leerzeichen direkt hintereinander nur einmal z�hlen - if(f == 1) - { - argc++; - } - f = 0; - } - else - { - f = 1; - } - } - if(f == 1) - { - argc++; - } + args = get_cmdline(); + argc = cmdline_get_argc(args); char* argv[argc + 1]; - argv[0] = strtok(args, " "); - for(pos = 1; pos < argc; pos++) - { - argv[pos] = strtok(NULL, " "); - } - argv[argc] = NULL; + + cmdline_copy_argv(args, (const char**) argv, argc); call_constructors(); int result = main(argc, argv); diff --git a/src/modules/lib/init.c b/src/modules/lib/init.c index 3e021c8..ea0a7d2 100644 --- a/src/modules/lib/init.c +++ b/src/modules/lib/init.c @@ -35,6 +35,7 @@ #include "stdlib.h" #include <io.h> #include <unistd.h> +#include <ctype.h> #include <dir.h> // Kindprozess eintragen @@ -177,6 +178,43 @@ pid_t init_execute(const char* cmd) return pid; } +/* + * Zaehlt die Argumente in einer Kommandozeile. Mehrere aufeinanderfolgende + * Leerzeichen werden als ein einziger Trenner aufgefasst. + */ +int cmdline_get_argc(const char* args) +{ + bool space = false; + int argc = 0; + int pos; + + for (pos = 0; pos < strlen(args); pos++) { + if (isspace(args[pos]) && !space) { + argc++; + } + + space = isspace(args[pos]); + } + + if (!space) { + argc++; + } + + return argc; +} + +void cmdline_copy_argv(char* args, const char** argv, size_t argc) +{ + int pos; + argv[0] = strtok(args, " "); + + for(pos = 1; pos < argc; pos++) { + argv[pos] = strtok(NULL, " "); + } + + argv[argc] = NULL; +} + int init_dev_register(struct init_dev_desc* dev, size_t size) { return rpc_get_int(1, "DEV_REG ", size, (char*) dev); -- 1.6.0.2