X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Usermode%2FApplications%2FCLIShell_src%2Fmain.c;h=aa1d9abec48744cd0f5182998adb526848ee1439;hb=f6b468cee21ebf1c9b98efec5315950aa043ead5;hp=f753788de35c4218682dfedf31d8965ef48fcea4;hpb=f8528eeb40cbea394df23878f78ff9cb19f25771;p=tpg%2Facess2.git diff --git a/Usermode/Applications/CLIShell_src/main.c b/Usermode/Applications/CLIShell_src/main.c index f753788d..aa1d9abe 100644 --- a/Usermode/Applications/CLIShell_src/main.c +++ b/Usermode/Applications/CLIShell_src/main.c @@ -1,9 +1,9 @@ /* - AcessOS Shell Version 2 -- Based on IOOS CLI Shell -*/ + * AcessOS Shell Version 3 + */ #include #include +#include #include "header.h" #define _stdin 0 @@ -15,9 +15,8 @@ char *ReadCommandLine(int *Length); void Parse_Args(char *str, char **dest); void Command_Colour(int argc, char **argv); void Command_Clear(int argc, char **argv); -//void Command_Ls(int argc, char **argv); void Command_Cd(int argc, char **argv); -//void Command_Cat(int argc, char **argv); +void Command_Dir(int argc, char **argv); // ==== CONSTANT GLOBALS ==== char *cCOLOUR_NAMES[8] = {"black", "red", "green", "yellow", "blue", "magenta", "cyan", "white"}; @@ -25,13 +24,14 @@ struct { char *name; void (*fcn)(int argc, char **argv); } cBUILTINS[] = { - {"colour", Command_Colour}, {"clear", Command_Clear}, {"cd", Command_Cd} + {"colour", Command_Colour}, {"clear", Command_Clear}, + {"cd", Command_Cd}, {"dir", Command_Dir} }; #define BUILTIN_COUNT (sizeof(cBUILTINS)/sizeof(cBUILTINS[0])) // ==== LOCAL VARIABLES ==== char gsCommandBuffer[1024]; -char *gsCurrentDirectory = "/"; +char *gsCurrentDirectory = NULL; char gsTmpBuffer[1024]; char **gasCommandHistory; int giLastCommand = 0; @@ -50,9 +50,19 @@ int main(int argc, char *argv[], char *envp[]) //Command_Clear(0, NULL); + { + char *tmp = getenv("CWD"); + if(tmp) { + gsCurrentDirectory = malloc(strlen(tmp)+1); + strcpy(gsCurrentDirectory, tmp); + } else { + gsCurrentDirectory = malloc(2); + strcpy(gsCurrentDirectory, "/"); + } + } + write(_stdout, 1, "\n"); - write(_stdout, 36, "Acess Shell Version 3\n"); - write(_stdout, 30, " Based on CLI Shell for IOOS\n"); + write(_stdout, 22, "Acess Shell Version 3\n"); write(_stdout, 2, "\n"); for(;;) { @@ -60,6 +70,7 @@ int main(int argc, char *argv[], char *envp[]) if(saArgs[0]) free(saArgs); if(!bCached) free(sCommandStr); bCached = 0; + write(_stdout, 1, "\n"); write(_stdout, strlen(gsCurrentDirectory), gsCurrentDirectory); write(_stdout, 3, "$ "); @@ -97,35 +108,37 @@ int main(int argc, char *argv[], char *envp[]) cBUILTINS[length].fcn(iArgCount-1, &saArgs[1]); break; } - } - // Calling a file - if(length == BUILTIN_COUNT) - { - GeneratePath(saArgs[1], gsCurrentDirectory, gsTmpBuffer); - // Use length in place of fp - length = open(gsTmpBuffer, 0); - // Check file existence - if(length == -1) { - Print("Unknown Command: `");Print(saArgs[1]);Print("'\n"); // Error Message - continue; - } - // Check if the file is a directory - finfo( length, &info ); - close( length ); - if(info.flags & FILEFLAG_DIRECTORY) { - Print("`");Print(saArgs[1]); // Error Message - Print("' is a directory.\n"); - continue; - } - pid = clone(CLONE_VM, 0); - if(pid == 0) execve(gsTmpBuffer, &saArgs[1], NULL); - if(pid <= 0) { - Print("Unablt to create process: `");Print(gsTmpBuffer);Print("'\n"); // Error Message - //SysDebug("pid = %i\n", pid); - } - else { - //waitpid(pid, K_WAITPID_DIE); - } + } + + if(length != BUILTIN_COUNT) continue; + + // - Calling a file + GeneratePath(saArgs[1], gsCurrentDirectory, gsTmpBuffer); + // Use length in place of fp + length = open(gsTmpBuffer, 0); + // Check file existence + if(length == -1) { + Print("Unknown Command: `");Print(saArgs[1]);Print("'\n"); // Error Message + continue; + } + // Check if the file is a directory + finfo( length, &info, 0 ); + close( length ); + if(info.flags & FILEFLAG_DIRECTORY) { + Print("`");Print(saArgs[1]); // Error Message + Print("' is a directory.\n"); + continue; + } + // Load new executable + pid = clone(CLONE_VM, 0); + if(pid == 0) execve(gsTmpBuffer, &saArgs[1], envp); + if(pid <= 0) { + Print("Unablt to create process: `");Print(gsTmpBuffer);Print("'\n"); // Error Message + //SysDebug("pid = %i\n", pid); + } + else { + int status; + waittid(pid, &status); } } } @@ -315,7 +328,7 @@ void Command_Cd(int argc, char **argv) write(_stdout, 26, "Directory does not exist\n"); return; } - finfo(fp, &stats); + finfo(fp, &stats, 0); close(fp); if( !(stats.flags & FILEFLAG_DIRECTORY) ) { @@ -323,6 +336,124 @@ void Command_Cd(int argc, char **argv) return; } + free(gsCurrentDirectory); + gsCurrentDirectory = malloc(strlen(tmpPath)+1); strcpy(gsCurrentDirectory, tmpPath); } +/** + */ +void Command_Dir(int argc, char **argv) +{ + int dp, fp, dirLen; + char modeStr[11] = "RWXrwxRWX "; + char tmpPath[1024]; + char *fileName; + t_sysFInfo info; + t_sysACL acl; + + // Generate Directory Path + if(argc > 1) + dirLen = GeneratePath(argv[1], gsCurrentDirectory, tmpPath); + else + { + strcpy(tmpPath, gsCurrentDirectory); + } + dirLen = strlen(tmpPath); + + // Open Directory + dp = open(tmpPath, OPENFLAG_READ); + // Check if file opened + if(dp == -1) + { + //printf("Unable to open directory `%s', File cannot be found\n", tmpPath); + write(_stdout, 27, "Unable to open directory `"); + write(_stdout, strlen(tmpPath)+1, tmpPath); + write(_stdout, 25, "', File cannot be found\n"); + return; + } + // Get File Stats + if( finfo(dp, &info, 0) == -1 ) + { + close(dp); + write(_stdout, 34, "stat Failed, Bad File Descriptor\n"); + return; + } + // Check if it's a directory + if(!(info.flags & FILEFLAG_DIRECTORY)) + { + close(dp); + write(_stdout, 27, "Unable to open directory `"); + write(_stdout, strlen(tmpPath)+1, tmpPath); + write(_stdout, 20, "', Not a directory\n"); + return; + } + + // Append Shash for file paths + if(tmpPath[dirLen-1] != '/') + { + tmpPath[dirLen++] = '/'; + tmpPath[dirLen] = '\0'; + } + + fileName = (char*)(tmpPath+dirLen); + // Read Directory Content + while( (fp = readdir(dp, fileName)) ) + { + if(fp < 0) + { + if(fp == -3) + write(_stdout, 42, "Invalid Permissions to traverse directory\n"); + break; + } + // Open File + fp = open(tmpPath, 0); + if(fp == -1) continue; + // Get File Stats + finfo(fp, &info, 0); + + if(info.flags & FILEFLAG_DIRECTORY) + write(_stdout, 1, "d"); + else + write(_stdout, 1, "-"); + + // Print Mode + acl.group = 0; acl.id = info.uid; + _SysGetACL(fp, &acl); + if(acl.perms & 1) modeStr[0] = 'r'; else modeStr[0] = '-'; + if(acl.perms & 2) modeStr[1] = 'w'; else modeStr[1] = '-'; + if(acl.perms & 8) modeStr[2] = 'x'; else modeStr[2] = '-'; + acl.group = 1; acl.id = info.gid; + _SysGetACL(fp, &acl); + if(acl.perms & 1) modeStr[3] = 'r'; else modeStr[3] = '-'; + if(acl.perms & 1) modeStr[4] = 'w'; else modeStr[4] = '-'; + if(acl.perms & 1) modeStr[5] = 'x'; else modeStr[5] = '-'; + acl.group = 1; acl.id = -1; + _SysGetACL(fp, &acl); + if(acl.perms & 1) modeStr[6] = 'r'; else modeStr[6] = '-'; + if(acl.perms & 1) modeStr[7] = 'w'; else modeStr[7] = '-'; + if(acl.perms & 1) modeStr[8] = 'x'; else modeStr[8] = '-'; + write(_stdout, 10, modeStr); + close(fp); + + // Colour Code + if(info.flags & FILEFLAG_DIRECTORY) // Directory: Green + write(_stdout, 6, "\x1B[32m"); + // Default: White + + // Print Name + write(_stdout, strlen(fileName), fileName); + + // Print slash if applicable + if(info.flags & FILEFLAG_DIRECTORY) + write(_stdout, 1, "/"); + + // Revert Colour + write(_stdout, 6, "\x1B[37m"); + + // Newline! + write(_stdout, 1, "\n"); + } + // Close Directory + close(dp); +}