76 lines
1.5 KiB
C
76 lines
1.5 KiB
C
// init.c - first user-mode process
|
|
#include <stdint.h>
|
|
#include <stdbool.h>
|
|
#include <stddef.h>
|
|
#include <syscall.h>
|
|
|
|
#define STATUS_SUCCESS 0u
|
|
|
|
void itoa(int num, char* str) {
|
|
int i = 0;
|
|
int is_negative = 0;
|
|
|
|
// Handle 0 explicitly
|
|
if (num == 0) {
|
|
str[i++] = '0';
|
|
str[i] = '\0';
|
|
return;
|
|
}
|
|
|
|
// Handle negative numbers
|
|
if (num < 0) {
|
|
is_negative = 1;
|
|
num = -num;
|
|
}
|
|
|
|
// Process individual digits (result is in reverse)
|
|
while (num != 0) {
|
|
int rem = num % 10;
|
|
str[i++] = rem + '0';
|
|
num = num / 10;
|
|
}
|
|
|
|
if (is_negative) {
|
|
str[i++] = '-';
|
|
}
|
|
|
|
str[i] = '\0'; // Null terminate
|
|
|
|
// Reverse the string
|
|
int start = 0;
|
|
int end = i - 1;
|
|
while (start < end) {
|
|
char temp = str[start];
|
|
str[start] = str[end];
|
|
str[end] = temp;
|
|
start++;
|
|
end--;
|
|
}
|
|
}
|
|
|
|
void _start(const char* args)
|
|
{
|
|
uint32_t h = 0;
|
|
uint32_t s = 0;
|
|
|
|
SysDebugPrint("[init] starting\n");
|
|
s = SysCreateProcess(args, &h, NULL);
|
|
if (s != STATUS_SUCCESS) {
|
|
SysDebugPrint("[init] FATAL: failed to start!\n");
|
|
char buffer[12];
|
|
itoa(s, buffer);
|
|
SysDebugPrint(buffer); SysDebugPrint("\n");
|
|
SysExitProcess(1);
|
|
}
|
|
|
|
SysDebugPrint("[init] waiting\n");
|
|
s = SysWaitForProcess(h);
|
|
if (s != STATUS_SUCCESS)
|
|
SysDebugPrint("[init] WARNING: SysWaitForProcess returned error\n");
|
|
SysCloseHandle(h);
|
|
|
|
SysDebugPrint("[init] done\n");
|
|
|
|
SysExitProcess(0);
|
|
}
|