rendered paste body#include "api.h"#include "lib.h"#define ALWAYS_REPORT_EXIT_CODE 0#define PROMPT "->"#define BUF_LEN 256char curr_line[BUF_LEN];int count;int read_line() { for(int i=0;i<BUF_LEN;++i) { char c = getch(); switch(c) { case 0: // null byte, probably a keyboard key we've not mapped yet. --i; continue; case 8: // backspace. i-=2; continue; case '\n': c=0; default: curr_line[i]=c; break; } if(c==0) return i; } return -1;}int main(void) { print("Welcome to the shell ;)\n"); for(;;) { print(PROMPT); flush(); int len=read_line(); if(len==-1) { printf("\nLine too long!\n"); continue; } if(len==0) continue; int background=0; if(curr_line[len-1]=='&') { background=1; curr_line[len-1]=0; } int program_fd =sys_open(curr_line,0); if(program_fd==-1) { printf("No such program: %s - errno=%d\n",curr_line,errno); continue; } int retval=0; int childpid=sys_fork(); switch(childpid) { case -1: printf("Error forking: %d\n",childpid); break; case 0: sys_exec(program_fd); print("Could not execute program\n"); sys_exit(1); // doesn't return... default: sys_close(program_fd); if(background==1) { printf("Running %d in background.\n",childpid); }else{ sys_wait(&retval); if(retval || ALWAYS_REPORT_EXIT_CODE) printf("%s returned %d\n",curr_line,retval); } break; } } return 0;}