diff --git a/Makefile b/Makefile index 6b7dc69..f803d6e 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ # (C) Copyright 2014-2021 Chris Dorman, some rights reserved (GPLv2) # Some changes and tweaks from Menchers -VERSION = \"0.4.4\" +VERSION = \"0.4.5\" VERSION_EXTRA = \"$(EXTRA)\" PREFIX ?= /usr diff --git a/README.txt b/README.txt index d69d645..f3af02c 100644 --- a/README.txt +++ b/README.txt @@ -36,8 +36,14 @@ at this moment! * Time example: * time -* Isdir function, example: +* Isdir and isfile function, example: * isdir "examples/" -> returns 0 for not, 1 for is + * isfile "examples/functions.ss" -> returns 1, its there + +* mkdir and mkfile functions, example: + * mkfile "file.txt" -> creates empty 'file.txt' + * mkdir "testing/" -> creates directory 'testing' + * mkfile "testing/file.txt" -> creates directory 'testing', and creates 'file.txt' * Delete example: * delete "file.txt" @@ -125,6 +131,12 @@ Changelog Changes between version bumps in SlideScript. Hoping to have a lightweight top-down scripting language by V1.0.0 release! From there it will be molding and preserving the art. +* V0.4.5 + * Multiple bug fixes in not setting null byte to dynamic mem before strcat is used + * Function isfile, as well as mkfile and mkdir are added + * Added grep like function, search! + * Cleanup code + * V0.4.4 * Softened the kill program errors, added warn functions behind the scenes * Added backquote function processing in variables diff --git a/src/deps.h b/src/deps.h index 12a092a..c36abc8 100644 --- a/src/deps.h +++ b/src/deps.h @@ -24,7 +24,8 @@ #include #include #include -// For checking if file exists +// For file handling +#include #include // For string searching #include diff --git a/src/functions.c b/src/functions.c index 235fd0c..fc416a3 100644 --- a/src/functions.c +++ b/src/functions.c @@ -25,6 +25,11 @@ char *process_line(char *line) static char concatbuf[MAX_CONCAT_BUF+1]; static char filebuf[MAX_READFILE_LEN+1]; + // Make sure static buffers are empty before initialize + // This was a big bug here for a minute. + bzero(filebuf, MAX_READFILE_LEN); + bzero(concatbuf, MAX_CONCAT_BUF); + tok_srch = strtok(line, "=\" |"); /* reuse function */ @@ -42,8 +47,83 @@ char *process_line(char *line) if(strncmp("exit", tok_srch, 4) == 0) syn_error("ss:exit called"); + + /* mkdir function, and mkfile functions */ + else if(strncmp("mkdir",tok_srch,5) == 0) + { + int parsed; + tok_srch = strtok(NULL, "\""); + if(tok_srch == NULL) + { + syn_warn("ss:warn:mkdir syntax error, missing quote?"); + return NULL; + } + + if(strtok(NULL, "\"")==NULL) + { + syn_warn("ss:warn:mkdir syntax error, missing end quote?"); + return NULL; + } + + parsed = file_exists(parse_vars(tok_srch)); + if(parsed == 1) + { + syn_warn("ss:warn:mkdir, file exists"); + return NULL; + } + + // Create directory if need pe + int mkpret = mkpath(parse_vars(tok_srch), 0755); + + if(mkpret == -1) + syn_warn("ss:warn:mkdir, failed to make directory"); + + return NULL; + } + + else if(strncmp("mkfile",tok_srch,5) == 0) + { + int parsed; + tok_srch = strtok(NULL, "\""); + if(tok_srch == NULL) + { + syn_warn("ss:warn:mkfile syntax error, missing quote?"); + return NULL; + } + + if(strtok(NULL, "\"")==NULL) + { + syn_warn("ss:warn:mkfile syntax error, missing end quote?"); + return NULL; + } + + parsed = file_exists(parse_vars(tok_srch)); + if(parsed == 1) + { + syn_warn("ss:warn:mkfile, file exists"); + return NULL; + } + + // Create directory if need pe + int mkpret = mkpath(parse_vars(tok_srch), 0755); + + if(mkpret == -1) + syn_warn("ss:warn:mkfile, failed to form parent path, attempting to write file."); + + FILE *cfile = fopen(parse_vars(tok_srch), "w"); + if(cfile == NULL) + { + syn_warn("ss:warn:mkfile, failed to open file"); + return NULL; + } + + fprintf(cfile, "%d", '\0'); + fclose(cfile); + return NULL; + } + + /* time function */ - /* print function */ else if(strncmp("time",tok_srch,4) == 0) { char *parsed; @@ -51,7 +131,35 @@ char *process_line(char *line) return strip_nl(parsed); } - /* print function */ + /* isfile function */ + else if(strncmp("isfile",tok_srch,5) == 0) + { + int parsed; + tok_srch = strtok(NULL, "\""); + if(tok_srch == NULL) + { + syn_warn("ss:warn:isfile syntax error, missing quote?"); + return NULL; + } + + if(strtok(NULL, "\"")==NULL) + { + syn_warn("ss:warn:isfile syntax error, missing end quote?"); + return NULL; + } + + parsed = file_exists(parse_vars(tok_srch)); + if(parsed == 1) + { + return "1"; + } + else + { + return "0"; + } + } + + /* isdir function */ else if(strncmp("isdir",tok_srch,5) == 0) { int parsed; @@ -527,6 +635,8 @@ char *process_line(char *line) FILE* search_file = NULL; /* strtok to filename of function */ + bzero(filename, MAX_FILENAME_LEN); + tok_srch = strtok(NULL, "\""); if(tok_srch == NULL) { @@ -553,7 +663,6 @@ char *process_line(char *line) /* Check if file exists and can be opened */ if(search_file == NULL) { - fclose(search_file); syn_warn("ss:warn:search, failed to open file"); return NULL; } @@ -624,7 +733,6 @@ char *process_line(char *line) /* Check if file exists and can be opened */ if(write_file == NULL) { - fclose(write_file); syn_warn("ss:warn:write, failed to open file"); return NULL; } @@ -708,6 +816,8 @@ char *process_line(char *line) dynfile = (char *)malloc(read_size + 1); + *dynfile = '\0'; + /* Check if file was opened successfully */ if(read_file == NULL) { @@ -779,7 +889,6 @@ char *process_line(char *line) /* strtok to the content that will be written to file */ if(read_file == NULL) { - fclose(read_file); syn_warn("ss:warn:cat, failed to open file"); return NULL; } @@ -820,6 +929,8 @@ char *process_line(char *line) dynfile = (char *)malloc(read_size + 1); + *dynfile = '\0'; + while(fgets(read_line, sizeof(read_line), read_file) != NULL) { strcat(dynfile, read_line); diff --git a/src/pipe.c b/src/pipe.c index cab09c9..46e705a 100644 --- a/src/pipe.c +++ b/src/pipe.c @@ -42,6 +42,9 @@ void ss_piping(char *string) for(int vv = 0; vv < MAX_PIPE_CMDS; vv++) bzero(spipe[vv].command,MAX_STRING_LEN); stringmem = (char *) malloc(strlen(string)+1); + + *stringmem = '\0'; + if(stringmem == NULL) syn_error("ss:error:memory mapping error..."); sprintf(stringmem, "%s", string); diff --git a/src/search.c b/src/search.c index f433979..8a905bb 100644 --- a/src/search.c +++ b/src/search.c @@ -19,6 +19,8 @@ char *ss_search(const char *filename, FILE *fp, char *pat) char tmpbuf[TMPBUF_LEN+1]; int flags = REG_NOSUB; /* don't need where-matched info */ + bzero(retbuf, MAX_SEARCH_LEN); + regcomp(&pattern, pat, flags); while(getline(&s_buf, &size, fp) != -1) @@ -49,5 +51,7 @@ char *ss_search(const char *filename, FILE *fp, char *pat) free(s_buf); + regfree(&pattern); + return retbuf; } diff --git a/src/util.c b/src/util.c index ed5cff9..5672f21 100644 --- a/src/util.c +++ b/src/util.c @@ -57,6 +57,28 @@ int is_dir(char *path) return 0; } + + +// mkpath function for mkdir and mkfile +int mkpath(char* file_path, mode_t mode) +{ + assert(file_path && *file_path); + for (char* p = strchr(file_path + 1, '/'); p; p = strchr(p + 1, '/')) + { + *p = '\0'; + if (mkdir(file_path, mode) == -1) + { + if (errno != EEXIST) + { + *p = '/'; + return -1; + } + } + *p = '/'; + } + return 0; +} + char *ss_concat(char *str1, char *str2) { char *dymem = (char *) malloc((strlen(str1)+strlen(str2)) + 3); diff --git a/src/util.h b/src/util.h index bd0f06a..3f51370 100644 --- a/src/util.h +++ b/src/util.h @@ -10,6 +10,7 @@ void syn_warn(char *message); char *strip_nl(char *string); int file_exists(char *path); int is_dir(char *path); +int mkpath(char* file_path, mode_t mode); char *ss_concat(char *str1, char *str2); void parse_args(int argc, char** argv); char *ss_time();