libc.obj: add system
This commit is contained in:
52
programs/develop/ktcc/trunk/libc.obj/source/stdlib/system.c
Normal file
52
programs/develop/ktcc/trunk/libc.obj/source/stdlib/system.c
Normal file
@@ -0,0 +1,52 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/ksys.h>
|
||||
|
||||
static int __system_parse_command(const char* command, char** path, char** args)
|
||||
{
|
||||
if (command == NULL || path == NULL || args == NULL) {
|
||||
return -1;
|
||||
}
|
||||
const char* space = strchr(command, ' ');
|
||||
if (space == NULL) {
|
||||
*path = strdup(command);
|
||||
if (*path == NULL) {
|
||||
return -1;
|
||||
}
|
||||
*args = NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
size_t path_len = space - command;
|
||||
*path = (char*)malloc(path_len + 1);
|
||||
if (*path == NULL) {
|
||||
return -1;
|
||||
}
|
||||
strncpy(*path, command, path_len);
|
||||
(*path)[path_len] = '\0';
|
||||
|
||||
size_t args_len = strlen(space) + 1;
|
||||
*args = (char*)malloc(args_len + 1);
|
||||
if (*args == NULL) {
|
||||
free(*path);
|
||||
return -1;
|
||||
}
|
||||
strcpy(*args, space);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int system(const char* command)
|
||||
{
|
||||
if (command == NULL || *command == '\0')
|
||||
return 1;
|
||||
char* cmd;
|
||||
char* args;
|
||||
|
||||
int ret = _ksys_exec(cmd, args);
|
||||
|
||||
free(cmd);
|
||||
free(args);
|
||||
|
||||
return ret;
|
||||
}
|
||||
Reference in New Issue
Block a user