45 lines
809 B
C
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);
|
|
}
|