PatchworkOS
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 spawn_fd_t fds[] = {
9 {.child = STDIN_FILENO, .parent = STDIN_FILENO},
10 {.child = STDOUT_FILENO, .parent = STDOUT_FILENO},
11 {.child = STDERR_FILENO, .parent = STDERR_FILENO},
13 };
14 const char* argv[] = {"/bin/shell", command, NULL};
15 pid_t shell = spawn(argv, fds, NULL, NULL);
16 if (shell == ERR)
17 {
18 return -1;
19 }
20
21 fd_t wait = openf("/proc/%d/wait", shell);
22 if (wait == ERR)
23 {
24 return -1;
25 }
26
27 char buf[MAX_PATH];
28 if (read(wait, buf, MAX_PATH) == ERR)
29 {
30 close(wait);
31 return -1;
32 }
33
34 close(wait);
35 return atoi(buf);
36}
#define MAX_PATH
Maximum length of filepaths.
Definition MAX_PATH.h:11
uint64_t close(fd_t fd)
System call for closing files.
Definition close.c:9
fd_t openf(const char *_RESTRICT format,...)
Wrapper for opening files with a formatted path.
Definition openf.c:9
#define STDOUT_FILENO
Definition io.h:45
uint64_t read(fd_t fd, void *buffer, uint64_t count)
System call for reading from files.
Definition read.c:9
#define STDERR_FILENO
Definition io.h:46
#define STDIN_FILENO
Definition io.h:44
pid_t spawn(const char **argv, const spawn_fd_t *fds, const char *cwd, spawn_attr_t *attr)
System call for creating child processes.
Definition spawn.c:6
#define SPAWN_FD_END
Spawn fds termination constant.
Definition proc.h:82
#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
Stucture used to duplicate fds in spawn().
Definition proc.h:55
fd_t child
The destination file descriptor in the child.
Definition proc.h:56
int system(const char *command)
Definition system.c:6