1
0
Fork 0
mirror of https://github.com/tailix/libkernaux.git synced 2024-11-13 11:04:27 -05:00

Some basic implementation of command line parser

This commit is contained in:
Alex Kotov 2020-12-02 01:55:09 +05:00
parent 48a88f1abf
commit 67aba6718f
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08

View file

@ -31,5 +31,34 @@ kernaux_bool kernaux_cmdline_parse(
kernaux_memset(buffer, '\0', argv_count_max * arg_size_max);
if (cmdline[0] == '\0') {
return KERNAUX_TRUE;
}
unsigned int start = 0;
for (unsigned int index = 1; ; ++index) {
const char prev = cmdline[index - 1];
const char cur = cmdline[index];
if ((cur == ' ' || cur == '\0') && prev != ' ') {
const unsigned size = index - start + 1;
// TODO: check size
argv[(*argc)++] = buffer;
kernaux_strncpy(buffer, &cmdline[start], size - 1);
buffer += size;
}
if (prev == ' ' && cur != ' ' && cur != '\0') {
start = index;
}
if (cur == '\0') {
break;
}
}
return KERNAUX_TRUE;
}