diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..771b8a4 --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,18 @@ +{ + "configurations": [ + { + "name": "Linux", + "includePath": [ + "${workspaceFolder}/**", + "${workspaceFolder}/include" + ], + "defines": [], + "compilerPath": "/bin/gcc", + "cStandard": "c17", + "cppStandard": "c++98", + "intelliSenseMode": "linux-gcc-x64", + "configurationProvider": "ms-vscode.makefile-tools" + } + ], + "version": 4 +} \ No newline at end of file diff --git a/README.md b/README.md index c027c6f..3a6f3f2 100644 --- a/README.md +++ b/README.md @@ -10,14 +10,16 @@ Requirements RHEL-like: `sudo dnf install readline-devel` +Debian[12]: `sudo apt install libreadline-dev` ## Installation -`make` +Normal Build: `make` +Debug Build: `make debug` ## Usage -`bin/run` +`make test` ## Considerations and Caveats @@ -28,7 +30,6 @@ If you need to use these kinds of applications, do not use this program to wrap ## Example -`bin/run` ## Documentation diff --git a/include/debug.h b/include/debug.h index 0c183b8..672ed13 100644 --- a/include/debug.h +++ b/include/debug.h @@ -1,3 +1,6 @@ +#ifndef DEBUG_H +#define DEBUG_H + #if defined(DEBUG) && DEBUG > 0 #define DEBUG_PRINT(fmt, ...) fprintf(stderr, "DEBUG: %s:%d:%s(): " fmt, \ __FILE__, __LINE__, __func__, ##__VA_ARGS__) @@ -11,51 +14,4 @@ #define COLORTEST() #endif - - -// You must free the result if result is non-NULL. -char *str_replace(char *orig, char *rep, char *with) { - char *result; // the return string - char *ins; // the next insert point - char *tmp; // varies - int len_rep; // length of rep (the string to remove) - int len_with; // length of with (the string to replace rep with) - int len_front; // distance between rep and end of last rep - int count; // number of replacements - - // sanity checks and initialization - if (!orig || !rep) - return NULL; - len_rep = strlen(rep); - if (len_rep == 0) - return NULL; // empty rep causes infinite loop during count - if (!with) - with = ""; - len_with = strlen(with); - - // count the number of replacements needed - ins = orig; - for (count = 0; tmp = strstr(ins, rep); ++count) { - ins = tmp + len_rep; - } - - tmp = result = malloc(strlen(orig) + (len_with - len_rep) * count + 1); - - if (!result) - return NULL; - - // first time through the loop, all the variable are set correctly - // from here on, - // tmp points to the end of the result string - // ins points to the next occurrence of rep in orig - // orig points to the remainder of orig after "end of rep" - while (count--) { - ins = strstr(orig, rep); - len_front = ins - orig; - tmp = strncpy(tmp, orig, len_front) + len_front; - tmp = strcpy(tmp, with) + len_with; - orig += len_front + len_rep; // move to next "end of rep" - } - strcpy(tmp, orig); - return result; -} \ No newline at end of file +#endif \ No newline at end of file diff --git a/include/notmine.h b/include/notmine.h new file mode 100644 index 0000000..004a40d --- /dev/null +++ b/include/notmine.h @@ -0,0 +1,8 @@ +#ifndef NOTMINE_H +#define NOTMINE_H + +// You must free the result if result is non-NULL. +char *str_replace(char *orig, char *rep, char *with); + + +#endif \ No newline at end of file diff --git a/src/main.c b/src/main.c index 2b48aba..64cf10a 100644 --- a/src/main.c +++ b/src/main.c @@ -13,6 +13,7 @@ // Custom debug include for some debug macros and functions #include +#include #define BUFCHUNKSIZE 4096 #define BUFSAFEPAD 8 @@ -38,7 +39,7 @@ void child_process(char *argv[]){ } void processline(char *line){ - char debugstring[] = "HEHE:"KRED; + //char debugstring[] = "HEHE:"KRED; char endstring[] = KNRM"\n"; char *newline; newline = str_replace(line, "rw-", KRED"rw-"KNRM); @@ -59,7 +60,7 @@ void processline(char *line){ void processbuffer(char *roll_buffer, size_t read_length) { //this is where some optimization could come in with using malloc and then freeing subset of memory; dog garn dynamic memory allocation - int found_index; + //int found_index; char *found_char = strtok(roll_buffer, "\r\n"); while(found_char != NULL){ processline(found_char); diff --git a/src/notmine.c b/src/notmine.c new file mode 100644 index 0000000..8c9408f --- /dev/null +++ b/src/notmine.c @@ -0,0 +1,50 @@ +#include +#include +#include + +//https://stackoverflow.com/a/779960/8229149 +char *str_replace(char *orig, char *rep, char *with) { + char *result; // the return string + char *ins; // the next insert point + char *tmp; // varies + int len_rep; // length of rep (the string to remove) + int len_with; // length of with (the string to replace rep with) + int len_front; // distance between rep and end of last rep + int count; // number of replacements + + // sanity checks and initialization + if (!orig || !rep) + return NULL; + len_rep = strlen(rep); + if (len_rep == 0) + return NULL; // empty rep causes infinite loop during count + if (!with) + with = ""; + len_with = strlen(with); + + // count the number of replacements needed + ins = orig; + for (count = 0; tmp = strstr(ins, rep); ++count) { + ins = tmp + len_rep; + } + + tmp = result = malloc(strlen(orig) + (len_with - len_rep) * count + 1); + + if (!result) + return NULL; + + // first time through the loop, all the variable are set correctly + // from here on, + // tmp points to the end of the result string + // ins points to the next occurrence of rep in orig + // orig points to the remainder of orig after "end of rep" + while (count--) { + ins = strstr(orig, rep); + len_front = ins - orig; + tmp = strncpy(tmp, orig, len_front) + len_front; + tmp = strcpy(tmp, with) + len_with; + orig += len_front + len_rep; // move to next "end of rep" + } + strcpy(tmp, orig); + return result; +} \ No newline at end of file