diff --git a/include/kernaux/cmdline.h b/include/kernaux/cmdline.h index fac8892..c930771 100644 --- a/include/kernaux/cmdline.h +++ b/include/kernaux/cmdline.h @@ -1,10 +1,26 @@ #ifndef KERNAUX_INCLUDED_CMDLINE #define KERNAUX_INCLUDED_CMDLINE 1 +#include + +#define KERNAUX_CMDLINE_ERROR_MSG_SIZE_MAX 256 +#define KERNAUX_CMDLINE_ERROR_MSG_SLEN_MAX \ + (KERNAUX_CMDLINE_ERROR_MSG_SIZE_MAX - 1) + #ifdef __cplusplus extern "C" { #endif +kernaux_bool kernaux_cmdline_parse( + const char *cmdline, + char *error_msg, + unsigned int *argc, + char **argv, + char *buffer, + unsigned int argv_count_max, + unsigned int arg_size_max +); + #ifdef __cplusplus } #endif diff --git a/src/cmdline.c b/src/cmdline.c index acf67fc..6831156 100644 --- a/src/cmdline.c +++ b/src/cmdline.c @@ -1,3 +1,35 @@ #include "config.h" #include + +kernaux_bool kernaux_cmdline_parse( + const char *const cmdline, + char *error_msg, + unsigned int *const argc, + char **argv, + char *buffer, + const unsigned int argv_count_max, + const unsigned int arg_size_max +) { + if ( + cmdline == KERNAUX_NULL || + error_msg == KERNAUX_NULL || + argc == KERNAUX_NULL || + argv == KERNAUX_NULL || + argv_count_max == 0 || + arg_size_max == 0 + ) { + return KERNAUX_FALSE; + } + + kernaux_memset(error_msg, '\0', KERNAUX_CMDLINE_ERROR_MSG_SIZE_MAX); + *argc = 0; + + for (unsigned int index = 0; index < argv_count_max; ++index) { + argv[index] = KERNAUX_NULL; + } + + kernaux_memset(buffer, '\0', argv_count_max * arg_size_max); + + return KERNAUX_TRUE; +} diff --git a/tests/test_cmdline.c b/tests/test_cmdline.c index 1a9cc69..43effa3 100644 --- a/tests/test_cmdline.c +++ b/tests/test_cmdline.c @@ -1,6 +1,26 @@ #include +#include + +static const unsigned int ARGV_COUNT_MAX = 1024; +static const unsigned int ARG_SIZE_MAX = 4096; + int main() { + char error_msg[KERNAUX_CMDLINE_ERROR_MSG_SIZE_MAX]; + unsigned int argc; + char *argv[ARGV_COUNT_MAX]; + char buffer[ARGV_COUNT_MAX * ARG_SIZE_MAX]; + + assert(kernaux_cmdline_parse( + "", + error_msg, + &argc, + argv, + buffer, + ARGV_COUNT_MAX, + ARG_SIZE_MAX + )); + return 0; }