Clean up printing code in buildtest
This commit is contained in:
35
buildtest.c
35
buildtest.c
@@ -1,6 +1,22 @@
|
||||
#include "int.h"
|
||||
#include "sys.h"
|
||||
|
||||
#define print(str) write(STDOUT, str, (sizeof str) - 1)
|
||||
void printhex(uint64_t num) {
|
||||
char msg[17] = {' '};
|
||||
msg[16] = '\n';
|
||||
for (int i = 0; i < 16; ++i) {
|
||||
char nibble = (num >> ((15 - i) * 4)) & 0xf;
|
||||
char c;
|
||||
if (nibble > 9) {
|
||||
c = nibble + '7';
|
||||
} else {
|
||||
c = nibble + '0';
|
||||
}
|
||||
msg[i] = c;
|
||||
}
|
||||
write(STDOUT, msg, 17);
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
// Test the write syscall
|
||||
@@ -17,16 +33,7 @@ int main(int argc, char* argv[]) {
|
||||
// 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);
|
||||
printhex(pid);
|
||||
|
||||
// Child process exits
|
||||
if(pid == 0) return 0;
|
||||
@@ -37,7 +44,7 @@ int main(int argc, char* argv[]) {
|
||||
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);
|
||||
print("Enter some text:");
|
||||
intptr_t n_read = read(STDIN, input_buffer, INPUT_BUFFER_LEN);
|
||||
write(STDOUT, input_buffer, n_read);
|
||||
if(fout > 0) {
|
||||
@@ -55,15 +62,15 @@ int main(int argc, char* argv[]) {
|
||||
i = read(file, input_buffer, INPUT_BUFFER_LEN);
|
||||
}
|
||||
} else {
|
||||
write(STDOUT, "Could not open /proc/version\n", 29);
|
||||
print("Could not open /proc/version\n");
|
||||
}
|
||||
|
||||
// Test sleep
|
||||
write(STDOUT, "Sleeping for 5 secs\n", 20);
|
||||
print("Sleeping for 5 secs\n");
|
||||
struct timespec sleeptime;
|
||||
sleeptime.tv_sec = 5;
|
||||
while (nanosleep(&sleeptime, &sleeptime) != 0) {}
|
||||
write(STDOUT, "DONE\n", 5);
|
||||
print("DONE\n");
|
||||
|
||||
return 69;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user