minimalc/buildtest.c

58 lines
1.3 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};
write(STDOUT, "Enter some text:", 16);
intptr_t n_read = read(STDIN, input_buffer, INPUT_BUFFER_LEN);
write(STDOUT, input_buffer, n_read);
// 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();
}