65 lines
1.4 KiB
C
65 lines
1.4 KiB
C
#include "int.h"
|
|
#include "sys.h"
|
|
|
|
|
|
int main() {
|
|
// Test the write syscall
|
|
intptr_t n = write(STDOUT, "Hello\n", 6);
|
|
if(n != 6) return n;
|
|
|
|
// Test the fork syscall
|
|
uint64_t pid = fork();
|
|
// Print the pid in hex
|
|
char msg[17] = {' '};
|
|
msg[16] = '\n';
|
|
for(int i = 0; i < 16; ++i) {
|
|
char nibble = (pid >> ((15 - i) * 4)) & 0xf;
|
|
char c;
|
|
if (nibble > 9) {c = nibble + '7';}
|
|
else {c = nibble + '0';}
|
|
msg[i] = c;
|
|
}
|
|
write(STDOUT, msg, 17);
|
|
|
|
// Child process exits
|
|
if(pid == 0) return 0;
|
|
//TODO: wait on child to remove zombie process
|
|
|
|
// Test the read syscall
|
|
#define INPUT_BUFFER_LEN 4096
|
|
char input_buffer[INPUT_BUFFER_LEN] = {0};
|
|
int32_t fout = openc("outfile", O_RDWR | O_CREAT | O_TRUNC, 0664);
|
|
|
|
write(STDOUT, "Enter some text:", 16);
|
|
intptr_t n_read = read(STDIN, input_buffer, INPUT_BUFFER_LEN);
|
|
write(STDOUT, input_buffer, n_read);
|
|
if(fout > 0) {
|
|
write(fout, input_buffer, n_read);
|
|
fsync(fout);
|
|
close(fout);
|
|
}
|
|
|
|
// Test the open syscall
|
|
int32_t file = open("/proc/version", O_RDONLY);
|
|
if (file > 0) {
|
|
int i = read(file, input_buffer, INPUT_BUFFER_LEN);
|
|
while(i > 0) {
|
|
write(STDOUT, input_buffer, i);
|
|
i = read(file, input_buffer, INPUT_BUFFER_LEN);
|
|
}
|
|
} else {
|
|
write(STDOUT, "Could not open /proc/version\n", 29);
|
|
}
|
|
return 69;
|
|
}
|
|
|
|
void __libc_start_main() {exit(main());}
|
|
|
|
void _start() {
|
|
__libc_start_main();
|
|
}
|
|
|
|
void __start() {
|
|
_start();
|
|
}
|