diff --git a/bin/fex b/bin/fex index 26bcefd..c40c485 100755 Binary files a/bin/fex and b/bin/fex differ diff --git a/inc/fex.h b/inc/fex.h new file mode 100644 index 0000000..a54731b --- /dev/null +++ b/inc/fex.h @@ -0,0 +1,20 @@ +// +// Created by auric on 2/25/24. +// + +#ifndef FEX_H +#define FEX_H + +enum Flags { + FLAG_INSTALL = 1 << 0, + FLAG_SOURCE = 1 << 1, + FLAG_UNINSTALL = 1 << 2, + FLAG_LIST = 1 << 3, + FLAG_SEARCH = 1 << 4, + FLAG_FORCE = 1 << 5, + FLAG_UPGRADE = 1 << 6, +}; + +extern char* flagNames[]; + +#endif //FEX_H diff --git a/inc/sanity.h b/inc/sanity.h index 1858be7..2e01041 100644 --- a/inc/sanity.h +++ b/inc/sanity.h @@ -5,13 +5,15 @@ #ifndef SANITY_H #define SANITY_H -#define SANITY_SUCCESS 0 // 0000 - All checks passed -#define SANITY_CON_FAIL 1 // 0001 - /con check failed -#define SANITY_EXE_FAIL 2 // 0010 - /exe check failed -#define SANITY_UTI_FAIL 4 // 0100 - /uti check failed -#define SANITY_LIB_FAIL 8 // 1000 - /uti/lib check failed -#define SANITY_HDR_FAIL 16 // 100000 - /uti/hdr check failed +#define SANITY_SUCCESS 0 // 0b00000 - All checks passed +#define SANITY_CON_FAIL 1 // 0b00001 - /con check failed +#define SANITY_EXE_FAIL 2 // 0b00010 - /exe check failed +#define SANITY_UTI_FAIL 4 // 0b00100 - /uti check failed +#define SANITY_LIB_FAIL 8 // 0b01000 - /uti/lib check failed +#define SANITY_HDR_FAIL 16 // 0b10000 - /uti/hdr check failed + int isSysSane(); +int isSyntaxSane(unsigned int flags, int pkgCount); #endif //SANITY_H diff --git a/obj/fex.o b/obj/fex.o index cf8ef0f..0109459 100644 Binary files a/obj/fex.o and b/obj/fex.o differ diff --git a/obj/sanity.o b/obj/sanity.o index 6d6737f..c05da53 100644 Binary files a/obj/sanity.o and b/obj/sanity.o differ diff --git a/src/fex.c b/src/fex.c index f6fc774..83723c3 100644 --- a/src/fex.c +++ b/src/fex.c @@ -6,30 +6,22 @@ #include #include #include -#include #include #include "config.h" #include "debug.h" - -enum Flags { - FLAG_INSTALL = 1 << 0, - FLAG_UPDATE = 1 << 1, - FLAG_SOURCE = 1 << 2, - FLAG_UNINSTALL = 1 << 3, - FLAG_LIST = 1 << 4, - FLAG_SEARCH = 1 << 5, - FLAG_FORCE = 1 << 6, - FLAG_UPGRADE = 1 << 7, -}; +#include "fex.h" void help() { nprint("E", "Usage: fex (OPT) (PACKAGES)"); } +char* flagNames[] = {"INSTALL", "SOURCE", "UNINSTALL", "LIST", "SEARCH", "FORCE", "UPGRADE"}; + int main(int argc, char** argv) { - unsigned short flags = 0; + unsigned int flags = 0; char* packages[argc - 1]; int pkgCount = 0; + if (argc < 2) { help(); return 1; @@ -42,25 +34,22 @@ int main(int argc, char** argv) { case 'i': // install, 0 flags |= FLAG_INSTALL; break; - case 'u': // update, 1 - flags |= FLAG_UPDATE; - break; - case 'S': // source, 2 + case 'S': // source, 1 flags |= FLAG_SOURCE; break; - case 'r': // remove, 3 + case 'r': // remove, 2 flags |= FLAG_UNINSTALL; break; - case 'l': // list, 4 + case 'l': // list, 3 flags |= FLAG_LIST; break; - case 's': // search, 5 + case 's': // search, 4 flags |= FLAG_SEARCH; break; - case 'f': // force, 6 + case 'f': // force, 5 flags |= FLAG_FORCE; break; - case 'c': // upgrade; don't ask why I'm using 'c', 7 + case 'u': // upgrade, 6 flags |= FLAG_UPGRADE; break; default: @@ -74,22 +63,32 @@ int main(int argc, char** argv) { } const int sysSanity = isSysSane(); + if ((isSyntaxSane(flags, pkgCount))) { + return 1; + } - #ifdef debugEnabled +#ifdef debugEnabled DEBUG_PRINT("sysSanity: %d", sysSanity); - unsigned int flag_values[] = {0b00000001, 0b00000010, 0b00000100, 0b00001000, 0b00010000, 0b00100000, 0b01000000, 0b10000000}; - char* flag_names[] = {"Install", "Update", "Source", "Uninstall", "List", "Search", "Force", "Upgrade"}; - int num_flags = sizeof(flag_values) / sizeof(flag_values[0]); - for(int i = 0; i < num_flags; i++) { - if (flags & flag_values[i]) { - nprint("D", "%s flag is set.", flag_names[i]); + unsigned int flagValues[] = {0b0000001, 0b0000010, 0b0000100, 0b0001000, 0b0010000, 0b0100000, 0b1000000}; + int numFlags = sizeof(flagValues) / sizeof(flagValues[0]); + for(int i = 0; i < numFlags; i++) { + if (flags & flagValues[i]) { + nprint("D", "%s flag is set.", flagNames[i]); } else { - nprint("D", "%s flag is not set.", flag_names[i]); + nprint("D", "%s flag is not set.", flagNames[i]); } } for(int i = 0; i < pkgCount; i++) { nprint("D", "Package %d: %s", i, packages[i]); } #endif + + /* End Prelogic */ + DEBUG_PRINT("[!] Break parselogic"); + + for(int i = 0; i < numFlags; i++) { + DEBUG_PRINT("Iterating over %s", flagNames[i]); + + } return 0; } \ No newline at end of file diff --git a/src/sanity.c b/src/sanity.c index cc376ff..ba82188 100644 --- a/src/sanity.c +++ b/src/sanity.c @@ -1,33 +1,58 @@ #include #include "../inc/sanity.h" +#include "../inc/fex.h" +#include "../inc/nprint.h" /* - * 0: success - * 1: con failure - * 2: exe failure - * 3: con and exe failure - * 4: uti failure - * 5: uti and con failure - * 6: uti and exe failure - * 7: total failure + * Uses bitwise operations to determine the sanity of the system's directory hierarchy. In particular, it checks if the directories of + * /con + * /exe + * /uti + * /uti/lib + * /uti/hdr + * exist, and if the owner can write to them. This notably does not check if the program is run as superuser. */ +/* For a root check - simply use conditional checking for a zero value in some way, such as !(getuid()) */ + int isSysSane() { - struct stat conStat, exeStat, utiStat; + struct stat conStat, exeStat, utiStat, libStat, hdrStat; int result = 0; - if (stat("/con", &conStat) != 0 || !(conStat.st_mode & S_IWUSR)) { - result |= SANITY_CON_FAIL; - } + char* directoryNames[] = {"/con", "/exe", "/uti", "/uti/lib", "/uti/hdr"}; + struct stat* statVars[] = {&conStat, &exeStat, &utiStat, &libStat, &hdrStat}; + int flags[] = {SANITY_CON_FAIL, SANITY_EXE_FAIL, SANITY_UTI_FAIL, SANITY_LIB_FAIL, SANITY_HDR_FAIL}; + const int numDirectories = sizeof(directoryNames) / sizeof(directoryNames[0]); - if (stat("/exe", &exeStat) != 0 || !(exeStat.st_mode & S_IWUSR)) { - result |= SANITY_EXE_FAIL; - } - - if (stat("/uti", &utiStat) != 0 || !(utiStat.st_mode & S_IWUSR)) { - result |= SANITY_UTI_FAIL; - } + for(int i = 0; i < numDirectories; i++) { + if(stat(directoryNames[i], statVars[i]) != 0 || !(statVars[i]->st_mode & S_IWUSR)) { + result |= flags[i]; + } + }; return result; } +int isSyntaxSane(unsigned int flags, int pkgCount) { + if((flags & FLAG_INSTALL) && (flags & FLAG_UNINSTALL)) { + nprint("E", "You cannot install and uninstall at the same time"); // TODO: allow for this to happen by parsing the flags in order. e.g fex -i japes -u japes2 should install japes and uninstall japes2 + return 1; + } + + if(((flags & FLAG_INSTALL) || (flags & FLAG_UNINSTALL)) && (pkgCount == 0)) { + nprint("E", "You must specify at least one package"); + return 1; + } + + if(flags == FLAG_FORCE) { + nprint("E", "Force may not be used alone"); + return 1; + } + + if((flags == FLAG_LIST) || (flags == FLAG_SEARCH)) { + nprint("E", "List and search must be used alone"); + return 1; + } + + return 0; +}