PatchworkOS  966e257
A non-POSIX operating system.
Loading...
Searching...
No Matches
system.c
Go to the documentation of this file.
1#include <stdio.h>
2#include <stdlib.h>
3#include <sys/io.h>
4#include <sys/proc.h>
5
6int system(const char* command)
7{
8 const char* argv[] = {"/bin/shell", command, NULL};
9 pid_t shell = spawn(argv, SPAWN_DEFAULT);
10 if (shell == ERR)
11 {
12 return -1;
13 }
14
15 fd_t wait = open(F("/proc/%d/wait", shell));
16 if (wait == ERR)
17 {
18 return -1;
19 }
20
21 char buf[MAX_PATH];
22 if (read(wait, buf, MAX_PATH) == ERR)
23 {
24 close(wait);
25 return -1;
26 }
27
28 close(wait);
29 return atoi(buf);
30}
#define MAX_PATH
Maximum length of filepaths.
Definition MAX_PATH.h:11
fd_t open(const char *path)
System call for opening files.
Definition open.c:9
uint64_t close(fd_t fd)
System call for closing files.
Definition close.c:9
#define F(format,...)
Format string macro.
Definition io.h:83
uint64_t read(fd_t fd, void *buffer, uint64_t count)
System call for reading from files.
Definition read.c:9
pid_t spawn(const char **argv, spawn_flags_t flags)
System call for spawning new processes.
Definition spawn.c:6
@ SPAWN_DEFAULT
Default spawn behaviour.
Definition proc.h:60
#define NULL
Pointer error value.
Definition NULL.h:23
#define ERR
Integer error value.
Definition ERR.h:17
__UINT64_TYPE__ fd_t
A file descriptor.
Definition fd_t.h:12
__UINT64_TYPE__ pid_t
Process Identifier.
Definition pid_t.h:11
#define atoi(nptr)
Definition stdlib.h:34
int system(const char *command)
Definition system.c:6