minimalc/buildtest.c

70 lines
1.7 KiB
C

#include "int.h"
#include "sys.h"
int main(int argc, char* argv[]) {
// Test the write syscall
intptr_t n = write(STDOUT, "Hello\n", 6);
if(n != 6) return n;
// Test argc
for(int i = 0; i < argc; ++i){
int len = strlen(argv[i]);
write(STDOUT, argv[i], len);
write(STDOUT, "\n", 1);
}
// 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);
}
// Test sleep
write(STDOUT, "Sleeping for 5 secs\n", 20);
struct timespec sleeptime;
sleeptime.tv_sec = 5;
while (nanosleep(&sleeptime, &sleeptime) != 0) {}
write(STDOUT, "DONE\n", 5);
return 69;
}