Files
kolibrios/programs/develop/ktcc/trunk/libc.obj/source/stdlib/system.c
Egor00f 2a5f192267
All checks were successful
Build system / Check kernel codestyle (pull_request) Successful in 1m33s
Build system / Build (pull_request) Successful in 10m23s
libc.obj: fix strtok && update change path to exit code
2026-02-20 18:16:15 +05:00

45 lines
809 B
C

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <sys/ksys.h>
#include "_exit.h"
int system(const char* command)
{
const char shell[] = "/sys/shell";
if (command == NULL || *command == '\0') {
FILE* f = fopen(shell, "r");
if (f) {
fclose(f);
}
return f != 0;
}
FILE* f = tmpfile();
if (!f)
return -1;
fputs("#SHS\n", f);
fputs(command, f);
fputs("\nexit\n", f);
int pid = _ksys_exec(shell, f->name);
fclose(f);
if (pid < 0) {
return -1;
}
// wait of end of shell
ksys_thread_t t;
while (_ksys_thread_info(&t, pid) != -1 && t.slot_state != 3 && t.slot_state != 4 && t.slot_state != 9) {
_ksys_thread_yield();
}
return READ_EXIT_CODE(pid, &t);
}