From d7fa91b54c7dcce8dedbe32f37cf849f530f8864 Mon Sep 17 00:00:00 2001 From: louie Date: Mon, 28 Dec 2020 12:47:47 -0800 Subject: Cleaned up ifdefs --- Makefile | 27 ++- draw.c | 62 +----- draw.h | 15 ++ read_cfg.c | 552 ++++++++++------------------------------------------- read_cfg.h | 18 +- unix/draw.c | 16 ++ unix/draw.h | 6 + unix/read_cfg.c | 238 +++++++++++++++++++++++ unix/read_cfg.h | 12 ++ windows/draw.c | 43 +++++ windows/draw.h | 6 + windows/read_cfg.c | 236 +++++++++++++++++++++++ windows/read_cfg.h | 12 ++ 13 files changed, 724 insertions(+), 519 deletions(-) create mode 100644 draw.h create mode 100644 unix/draw.c create mode 100644 unix/draw.h create mode 100644 unix/read_cfg.c create mode 100644 unix/read_cfg.h create mode 100644 windows/draw.c create mode 100644 windows/draw.h create mode 100644 windows/read_cfg.c create mode 100644 windows/read_cfg.h diff --git a/Makefile b/Makefile index 3ce8010..ad0bdab 100644 --- a/Makefile +++ b/Makefile @@ -3,14 +3,33 @@ NAME = tml LIBS = -lncurses PREFIX = /usr/local -$(NAME): draw.o read_cfg.o group.o entry.o - $(CC) -o $(NAME) draw.o read_cfg.o group.o entry.o $(LIBS) -draw.o: draw.c read_cfg.h group.h entry.h -read_cfg.o: read_cfg.c group.o entry.o +ifeq ($(OS),Windows_NT) + +$(NAME): draw.o read_cfg.o group.o entry.o windows/draw.o windows/read_cfg.o + $(CC) -o $(NAME) draw.o read_cfg.o group.o entry.o windows/draw.o windows/read_cfg.o $(LIBS) + +draw.o: draw.c read_cfg.h group.h entry.h windows/draw.h windows/read_cfg.h +windows/draw.o: windows/draw.c windows/draw.h draw.h +read_cfg.o: read_cfg.c group.o entry.o windows/read_cfg.h +windows/read_cfg.o: windows/read_cfg.c windows/read_cfg.h read_cfg.h + +else + +$(NAME): draw.o read_cfg.o group.o entry.o unix/draw.o unix/read_cfg.o + $(CC) -o $(NAME) draw.o read_cfg.o group.o entry.o unix/draw.o unix/read_cfg.o $(LIBS) + +draw.o: draw.c read_cfg.h group.h entry.h unix/draw.h unix/read_cfg.h +unix/draw.o: unix/draw.c unix/draw.h draw.h +read_cfg.o: read_cfg.c group.o entry.o unix/read_cfg.h +windows/read_cfg.o: unix/read_cfg.c unix/read_cfg.h read_cfg.h + +endif + group.o: group.c group.h entry.h entry.o: entry.c entry.h read_cfg.h group.h + .PHONY: clean clean: rm *.o $(NAME) diff --git a/draw.c b/draw.c index c4d00c8..2a09332 100644 --- a/draw.c +++ b/draw.c @@ -1,9 +1,12 @@ //Windows Compatability #if defined _WIN32 || defined _WIN64 #include -#include +#include "windows/draw.h" +#include "windows/read_cfg.h" #else #include +#include "unix/draw.h" +#include "unix/read_cfg.h" #endif #include @@ -13,8 +16,8 @@ #include "entry.h" #include "group.h" #include "read_cfg.h" -#define MAX_LEN 6 #define BUF_LEN 1024 +#define MAX_LEN 6 #define GAP_SIZE 1 #define WIDTH (getmaxx(stdscr)) //width of the entire term #define HEIGHT (getmaxy(stdscr)) //height of the entire term @@ -29,9 +32,6 @@ void switch_col(); void trav_col(int new_i); int locateChar(char input); char *get_launch(); -#if defined _WIN32 || defined _WIN64 -void win_launch(); -#endif WINDOW *group_win = NULL; WINDOW *entry_win = NULL; @@ -56,11 +56,7 @@ int main(int argc, char **argv){ //if a config path was given as an argument, set it accordingly if(argc > 2 && (!strcmp(argv[1], "-c") || !strcmp(argv[1], "--cfg_path"))) strcpy(cfg_path, argv[2]); -#if defined _WIN32 || defined _WIN64 - else strcpy(cfg_path, find_config_win()); -#else else strcpy(cfg_path, find_config()); -#endif //Fill Groups cfg_interp(cfg_path); //read the contents of the cfg file @@ -129,14 +125,7 @@ int main(int argc, char **argv){ break; case 10: //enter key - -#if defined _WIN32 || defined _WIN64 - win_launch(); -#else - strcpy(full_command, get_launch()); - strcat(full_command, " > /dev/null 2>&1 &"); - system(full_command); -#endif + launch(); break; case 27: //escape key @@ -466,42 +455,3 @@ char *get_launch(){ return full_command; } - -#if defined _WIN32 || defined _WIN64 -void win_launch(){ - char *program = get_gprog(g[g_hover]); - char *flags = get_gflags(g[g_hover]); - char *path = get_epath(e[e_hover]); - bool quotes = get_gquotes(g[g_hover]); - char file[BUF_LEN]; - char params[BUF_LEN]; - - file[0] = '\0'; - - if(!(strcmp(program, "./"))){ - strcat(file, "\""); - strcat(file, path); - strcat(file, "\""); - ShellExecute(NULL, NULL, file, NULL, NULL, SW_SHOW); - } - - else{ - if(quotes) strcat(file, "\""); - strcat(file, program); - if(quotes) strcat(file, "\""); - - params[0] = '\0'; - if(flags[0] != '\0'){ - strcat(params, flags); - strcat(params, " "); - } - strcat(params, "\""); - strcat(params, path); - strcat(params, "\""); - - ShellExecute(NULL, NULL, file, params, NULL, SW_SHOW); - } - - return; -} -#endif diff --git a/draw.h b/draw.h new file mode 100644 index 0000000..275b8bb --- /dev/null +++ b/draw.h @@ -0,0 +1,15 @@ +#ifndef DRAW_H +#define DRAW_H + +//These functions are needed by either unix/draw.c or windows/draw.c and must be supplied via a header file + +#define BUF_LEN 1024 + +extern int g_hover; +extern int e_hover; +extern struct group **g; +extern struct entry **e; + +char *get_launch(); + +#endif diff --git a/read_cfg.c b/read_cfg.c index 5f479c4..8a4f902 100644 --- a/read_cfg.c +++ b/read_cfg.c @@ -1,44 +1,37 @@ +#if defined _WIN32 || defined _WIN64 +#include "windows/read_cfg.h" +#else +#include "unix/read_cfg.h" +#endif + #include -#include -#include #include #include #include #include -#include -#include -#include #include "entry.h" #include "group.h" -#define BUF_LEN 1024 //maybe move this line to the header file +#define BUF_LEN 1024 #define MAX_ARGS 5 #define OPTION_CNT 14 //public -#if defined _WIN32 || defined _WIN64 -char *find_config_win(); -#else -char *find_config(); -#endif void cfg_interp(char *path); -int get_compmode(); bool get_sort(); bool get_case_sensitivity(); void refer_to_doc(); - -//private -void mkconfig_wizard(char *path); -void check_line(char *buffer, char **options, int ln); -int check_option(char *arg, char **options); -void handle_fname(char *path, char *group, bool recurs, bool force, char *name, int ln); void addme(char *path, char *group, bool force, char *name); -char *autoAlias(char *path); int search_ch(char *str, char c); int search_last_ch(char *str, char c); int wild_cmp(char *wild, char *literal); char *strip_quotes(char *str); void error_mes(int ln, char *message); +//private +void check_line(char *buffer, char **options, int ln); +int check_option(char *arg, char **options); +char *autoAlias(char *path); + //turn on or off sorting (A-Z); On by default bool sort = true; @@ -48,67 +41,6 @@ bool hr = false; //turn foldCase (insensitive case searching) on or off; On by default bool fold_case = true; -#if defined _WIN32 || defined _WIN64 -//for Windows Compatability, this will be '\\' (otherwise '/') -char sep = '\\'; -#else -char sep = '/'; -#endif - -#if defined _WIN32 || defined _WIN64 -char *find_config_win(){ - char *appdata = getenv("APPDATA"); - char *path = malloc(sizeof(char) * BUF_LEN); - int check_count = 1; - char choices[check_count][BUF_LEN]; - int i; - - sprintf(choices[0], "%s%ctml%cconfig", appdata, sep, sep); - - for(i = 0; i < check_count; i++){ - path = choices[i]; - printf("Checking for config at %s: ", choices[i]); - if(access(path, R_OK) == 0){ - printf("Using config \"%s\"\n\n", path); - return path; - } - else printf("File does not exist\n"); - } - - //TODO no config exists, ask user if they want to autogenerate one - mkconfig_wizard(choices[0]); - path = choices[0]; - return path; -} - -#else -char *find_config(){ - char *home = getenv("HOME"); - char *path = malloc(sizeof(char) * BUF_LEN); - char choices[2][BUF_LEN]; - int check_count = 2; - int i; - - sprintf(choices[0], "%s%c.config%ctml%cconfig", home, sep, sep, sep); - sprintf(choices[1], "%s%c.tml%cconfig", home, sep, sep); - - for(i = 0; i < check_count; i++){ - path = choices[i]; - printf("Checking for config at %s: ", choices[i]); - if(access(path, R_OK) == 0){ - printf("Using config \"%s\"\n\n", path); - return path; - } - else printf("File does not exist\n"); - } - - //TODO no config exists, ask user if they want to autogenerate one - mkconfig_wizard(choices[0]); - path = choices[0]; - return path; -} -#endif - void cfg_interp(char *path){ FILE *fp; char buffer[BUF_LEN]; @@ -180,174 +112,109 @@ void refer_to_doc(){ return; } -void mkconfig_wizard(char *path){ - char input; - FILE *fp; - -#if defined _WIN32 || defined _WIN64 - char *home = getenv("USERPROFILE"); - char *appdata = getenv("APPDATA"); -#else - char *home = getenv("HOME"); -#endif +void addme(char *path, char *group, bool force, char *name){ + ENTRY *new; + char auto_name[BUF_LEN]; - printf("\nNo configuration file found. Auto-generate one now at \"%s\"? [Y/n] ", path); - fflush(stdout); - scanf(" %c", &input); + //check if a name was given as argument + if(name != NULL){ + //strip quotes from the name + name = strip_quotes(name); + new = create_entry(name, path, force); + } - if(input == 'n'){ - printf("Configuration will not be auto-generated\n"); - refer_to_doc(); - exit(0); - + //check if autoAlias is on. If it is, go to the autoAlias function + else if(hr){ + strcpy(auto_name, autoAlias(path)); + new = create_entry(auto_name, path, force); } - printf("Generating configuration file at \"%s\"...\n", path); + else new = create_entry(path, path, force); + if(new != NULL) group_add(group, new); - //ensure directories have been created -#if defined _WIN32 || defined _WIN64 - if(appdata == NULL){ - printf("Failed: \%APPDATA\% is NULL\n"); - exit(1); - } + return; +} - if(home == NULL){ - printf("Failed: \%USERPROFILE\% is NULL\n"); - exit(1); +int search_ch(char *str, char c){ + int i = 0; + + while(str[i] != '\0'){ + if(str[i] == c) return i; + i++; } - sprintf(path, "%s%ctml%c", appdata, sep, sep); - mkdir(path); + return -1; +} + +int search_last_ch(char *str, char c){ + int i = 0; + int last_i = -1; - sprintf(path, "%s%ctml%cconfig", appdata, sep, sep); -#else - if(home == NULL){ - printf("Failed: HOME is NULL\n"); - exit(1); + while(str[i] != '\0'){ + if(str[i] == c) last_i = i; + i++; } - sprintf(path, "%s%c.config%c", home, sep, sep); - mkdir(path, 0755); + return last_i; +} + +//return 0 if match, 1 if not +//TODO only supports one wildcard per entry +int wild_cmp(char *wild, char *literal){ + int i; + + while(*wild != '\0'){ + //traverse until wildcard + if(*wild != '*'){ + if(*wild != *literal) return 1; + wild++; + literal++; + } - sprintf(path, "%s%c.config%ctml%c", home, sep, sep, sep); - mkdir(path, 0755); + //found wildcard, find the end of both names and comapre from the back + else{ + i = 0; + wild++; + while(*wild != '\0'){ + i++; + wild++; + } + while(*literal != '\0'){ + literal++; + } - sprintf(path, "%s%c.config%ctml%cconfig", home, sep, sep, sep); -#endif + while(i > 0){ + wild--; + literal--; + if(*wild != *literal) return 1; + i--; + } - //open file for writing, make sure non-NULL - fp = fopen(path, "w"); - if(fp == NULL){ - printf("Failed: \"%s\" could not be open for writing\n", path); - exit(1); + return 0; + } } - //write to file (in Windows, executing a file will open it in its default application) -#if defined _WIN32 || defined _WIN64 - fprintf(fp, "# This file was auto-generated by tml. See docs\\tml-config.md or tml-config(5) for documentation\n" - "# By default, no launcher is specified for any group. When no launcher is specified on the Windows build of tml, media files will be opened with their default application.\n" - "# It is generally recommended to specify a launcher for groups containing media files using the \"setLauncher\" command\n\n" - "# Recursively add files from %s%cMusic%c to Music group\n" - "addGroup Music\n", home, sep, sep); - fprintf(fp, "addR %s%cMusic%c*.aac Music\n", home, sep, sep); - fprintf(fp, "addR %s%cMusic%c*.aiff Music\n", home, sep, sep); - fprintf(fp, "addR %s%cMusic%c*.alac Music\n", home, sep, sep); - fprintf(fp, "addR %s%cMusic%c*.au Music\n", home, sep, sep); - fprintf(fp, "addR %s%cMusic%c*.flac Music\n", home, sep, sep); - fprintf(fp, "addR %s%cMusic%c*.m4a Music\n", home, sep, sep); - fprintf(fp, "addR %s%cMusic%c*.mp3 Music\n", home, sep, sep); - fprintf(fp, "addR %s%cMusic%c*.ogg Music\n", home, sep, sep); - fprintf(fp, "addR %s%cMusic%c*.pcm Music\n", home, sep, sep); - fprintf(fp, "addR %s%cMusic%c*.wav Music\n", home, sep, sep); - fprintf(fp, "addR %s%cMusic%c*.wma Music\n\n", home, sep, sep); - fprintf(fp, "# Recursively add files from %s%cPictures%c to Pictures group\n" - "addGroup Pictures\n", home, sep, sep); - fprintf(fp, "addR %s%cPictures%c*.epi Pictures\n", home, sep, sep); - fprintf(fp, "addR %s%cPictures%c*.eps Pictures\n", home, sep, sep); - fprintf(fp, "addR %s%cPictures%c*.eps2 Pictures\n", home, sep, sep); - fprintf(fp, "addR %s%cPictures%c*.eps3 Pictures\n", home, sep, sep); - fprintf(fp, "addR %s%cPictures%c*.epsf Pictures\n", home, sep, sep); - fprintf(fp, "addR %s%cPictures%c*.epsi Pictures\n", home, sep, sep); - fprintf(fp, "addR %s%cPictures%c*.ept Pictures\n", home, sep, sep); - fprintf(fp, "addR %s%cPictures%c*.gif Pictures\n", home, sep, sep); - fprintf(fp, "addR %s%cPictures%c*.gfa Pictures\n", home, sep, sep); - fprintf(fp, "addR %s%cPictures%c*.giff Pictures\n", home, sep, sep); - fprintf(fp, "addR %s%cPictures%c*.jpeg Pictures\n", home, sep, sep); - fprintf(fp, "addR %s%cPictures%c*.jpg Pictures\n", home, sep, sep); - fprintf(fp, "addR %s%cPictures%c*.png Pictures\n", home, sep, sep); - fprintf(fp, "addR %s%cPictures%c*.svg Pictures\n", home, sep, sep); - fprintf(fp, "addR %s%cPictures%c*.svgz Pictures\n", home, sep, sep); - fprintf(fp, "addR %s%cPictures%c*.tif Pictures\n", home, sep, sep); - fprintf(fp, "addR %s%cPictures%c*.tiff Pictures\n\n", home, sep, sep); - fprintf(fp, "# Recursively add files from %s%cVideos%c to Videos group\n" - "addGroup Videos\n", home, sep, sep); - fprintf(fp, "addR %s%cVideos%c*.asf Videos\n", home, sep, sep); - fprintf(fp, "addR %s%cVideos%c*.avi Videos\n", home, sep, sep); - fprintf(fp, "addR %s%cVideos%c*.flv Videos\n", home, sep, sep); - fprintf(fp, "addR %s%cVideos%c*.mk3d Videos\n", home, sep, sep); - fprintf(fp, "addR %s%cVideos%c*.mkv Videos\n", home, sep, sep); - fprintf(fp, "addR %s%cVideos%c*.mov Videos\n", home, sep, sep); - fprintf(fp, "addR %s%cVideos%c*.mp4 Videos\n", home, sep, sep); - fprintf(fp, "addR %s%cVideos%c*.qt Videos\n", home, sep, sep); - fprintf(fp, "addR %s%cVideos%c*.webm Videos\n", home, sep, sep); - fprintf(fp, "addR %s%cVideos%c*.wmv Videos\n", home, sep, sep); -#else - fprintf(fp, "# This file was auto-generated by tml. See docs/tml-config.md or tml-config(5) for documentation\n" - "# The default launcher is set to \"xdg-open\" which will open files based on the relevant default application set through xdg\n\n" - "# Recursively add files from %s%cMusic%c to Music group\n" - "addGroup Music\n" - "setLauncher Music xdg-open\n", home, sep, sep); - fprintf(fp, "addR %s%cMusic%c*.aac Music\n", home, sep, sep); - fprintf(fp, "addR %s%cMusic%c*.aiff Music\n", home, sep, sep); - fprintf(fp, "addR %s%cMusic%c*.alac Music\n", home, sep, sep); - fprintf(fp, "addR %s%cMusic%c*.au Music\n", home, sep, sep); - fprintf(fp, "addR %s%cMusic%c*.flac Music\n", home, sep, sep); - fprintf(fp, "addR %s%cMusic%c*.m4a Music\n", home, sep, sep); - fprintf(fp, "addR %s%cMusic%c*.mp3 Music\n", home, sep, sep); - fprintf(fp, "addR %s%cMusic%c*.ogg Music\n", home, sep, sep); - fprintf(fp, "addR %s%cMusic%c*.pcm Music\n", home, sep, sep); - fprintf(fp, "addR %s%cMusic%c*.wav Music\n", home, sep, sep); - fprintf(fp, "addR %s%cMusic%c*.wma Music\n\n", home, sep, sep); - fprintf(fp, "# Recursively add files from %s%cPictures%c to Pictures group\n" - "addGroup Pictures\n" - "setLauncher Pictures xdg-open\n", home, sep, sep); - fprintf(fp, "addR %s%cPictures%c*.epi Pictures\n", home, sep, sep); - fprintf(fp, "addR %s%cPictures%c*.eps Pictures\n", home, sep, sep); - fprintf(fp, "addR %s%cPictures%c*.eps2 Pictures\n", home, sep, sep); - fprintf(fp, "addR %s%cPictures%c*.eps3 Pictures\n", home, sep, sep); - fprintf(fp, "addR %s%cPictures%c*.epsf Pictures\n", home, sep, sep); - fprintf(fp, "addR %s%cPictures%c*.epsi Pictures\n", home, sep, sep); - fprintf(fp, "addR %s%cPictures%c*.ept Pictures\n", home, sep, sep); - fprintf(fp, "addR %s%cPictures%c*.gif Pictures\n", home, sep, sep); - fprintf(fp, "addR %s%cPictures%c*.gfa Pictures\n", home, sep, sep); - fprintf(fp, "addR %s%cPictures%c*.giff Pictures\n", home, sep, sep); - fprintf(fp, "addR %s%cPictures%c*.jpeg Pictures\n", home, sep, sep); - fprintf(fp, "addR %s%cPictures%c*.jpg Pictures\n", home, sep, sep); - fprintf(fp, "addR %s%cPictures%c*.png Pictures\n", home, sep, sep); - fprintf(fp, "addR %s%cPictures%c*.svg Pictures\n", home, sep, sep); - fprintf(fp, "addR %s%cPictures%c*.svgz Pictures\n", home, sep, sep); - fprintf(fp, "addR %s%cPictures%c*.tif Pictures\n", home, sep, sep); - fprintf(fp, "addR %s%cPictures%c*.tiff Pictures\n\n", home, sep, sep); - fprintf(fp, "# Recursively add files from %s%cVideos%c to Videos group\n" - "addGroup Videos\n" - "setLauncher Videos xdg-open\n", home, sep, sep); - fprintf(fp, "addR %s%cVideos%c*.asf Videos\n", home, sep, sep); - fprintf(fp, "addR %s%cVideos%c*.avi Videos\n", home, sep, sep); - fprintf(fp, "addR %s%cVideos%c*.flv Videos\n", home, sep, sep); - fprintf(fp, "addR %s%cVideos%c*.mk3d Videos\n", home, sep, sep); - fprintf(fp, "addR %s%cVideos%c*.mkv Videos\n", home, sep, sep); - fprintf(fp, "addR %s%cVideos%c*.mov Videos\n", home, sep, sep); - fprintf(fp, "addR %s%cVideos%c*.mp4 Videos\n", home, sep, sep); - fprintf(fp, "addR %s%cVideos%c*.qt Videos\n", home, sep, sep); - fprintf(fp, "addR %s%cVideos%c*.webm Videos\n", home, sep, sep); - fprintf(fp, "addR %s%cVideos%c*.wmv Videos\n", home, sep, sep); -#endif + return 0; +} - fclose(fp); - printf("done\nIt is highly recommended to further tweak the configuration file! [press any key to continue]"); - fflush(stdout); - getchar(); - getchar(); + +char *strip_quotes(char *str){ + char *stripped_str = malloc(sizeof(char) * BUF_LEN); + + if(str[0] == '"'){ + stripped_str = &str[1]; + stripped_str[strlen(stripped_str) - 1] = '\0'; + return stripped_str; + } + + return str; +} + +void error_mes(int ln, char *message){ + + assert(message != NULL); + + printf("Configuration File Error:\nOn line %d: %s\n\n", ln, message); return; } @@ -591,149 +458,6 @@ int check_option(char *arg, char **options){ return -1; } -//TODO augment to involve recurs -//TODO could use some cleanup... -void handle_fname(char *path, char *group, bool recurs, bool force, char *name, int ln){ - ENTRY *new; - char *search; //pointer for traversing path - char full_path_cpy[BUF_LEN]; - char relative_path_cpy[BUF_LEN]; - char arg_cpy[BUF_LEN]; - char auto_name[BUF_LEN]; - int plen = strlen(path); - char *dirname; - char *local_arg; //for use in addR - DIR *dp; - struct dirent *fname; - int i; - char *error_p; //helper for complex error messages - - assert(path != NULL && group != NULL); - - if(path[0] == '\0' || group[0] == '\0'){ - error_mes(ln, "Too few arguments for \"add\""); - return; - } - - //address potential quotes - strcpy(full_path_cpy, strip_quotes(path)); - - //don't check that the path arg is valid when forced - if(force) addme(full_path_cpy, group, force, name); - - //file is not recognized, perhaps it has a wildcard? - //TODO finish rewriting a more robust wildcard thingy - else if(access(full_path_cpy, F_OK) == -1){ - i = search_ch(full_path_cpy, '*'); - if(i > -1){ - //look for a directory - while(full_path_cpy[i] != sep && (i >= 0)){ - i--; - } - dirname = full_path_cpy; - strcpy(arg_cpy, full_path_cpy); - dirname[i+1] = '\0'; - dp = opendir(dirname); - - //the directory is real - if(dp != NULL){ - while(fname = readdir(dp)){ - relative_path_cpy[0] = '\0'; - strcat(relative_path_cpy, dirname); - strcat(relative_path_cpy, fname->d_name); - -#if defined _WIN32 || defined _WIN64 - //Windows cannot tell file types (TODO), so just add relatively indiscriminantly - if(!(wild_cmp(&arg_cpy[i+1], fname->d_name))) addme(relative_path_cpy, group, force, name); - - //if the recursive option was specified, run handle_fname on this directory, but for security reasons, do not consider directories that start with a '.' - else if(recurs && fname->d_name[0] != '.'){ - i = search_last_ch(arg_cpy, sep); - local_arg = &arg_cpy[i+1]; - strcat(relative_path_cpy, &sep); - strcat(relative_path_cpy, local_arg); - handle_fname(relative_path_cpy, group, 1, 0, NULL, ln); - } -#else - //check if path is a file (and not a directory/symlink/etc.) and regex matches - if(fname->d_type == DT_REG && !(wild_cmp(&arg_cpy[i+1], fname->d_name))) addme(relative_path_cpy, group, force, name); - - //if the recursive option was specified and the path is a directory, run handle_fname on this directory, but for security reasons, do not consider directories that start with a '.' - else if(recurs && fname->d_type == DT_DIR && fname->d_name[0] != '.'){ - i = search_last_ch(arg_cpy, sep); - local_arg = &arg_cpy[i+1]; - strcat(relative_path_cpy, &sep); - strcat(relative_path_cpy, local_arg); - handle_fname(relative_path_cpy, group, 1, 0, NULL, ln); - } -#endif - - } - - closedir(dp); - } - -#if defined _WIN32 || defined _WIN64 - //directory is not real, report error to the user - else if(!recurs){ - error_p = malloc(sizeof(char) * 1024); - sprintf(error_p, "\"%s\" bad path", dirname); - error_mes(ln, error_p); - free(error_p); - //printf("Error: \"%s\" bad path\n", dirname); - } - } -#else - //directory is not real, report error to the user - else{ - error_p = malloc(sizeof(char) * 1024); - sprintf(error_p, "\"%s\" bad path", dirname); - error_mes(ln, error_p); - free(error_p); - //printf("Error: \"%s\" bad path\n", dirname); - } - } -#endif - - //path is not real, report error to the user - else{ - error_p = malloc(sizeof(char) * 1024); - sprintf(error_p, "\"%s\" bad path", full_path_cpy); - error_mes(ln, error_p); - free(error_p); - } - } - - //file name is okay - //FIXME does not take into account whether the argument is a file (could be a directory, symlink, etc.) - else addme(full_path_cpy, group, force, name); - - return; -} - -void addme(char *path, char *group, bool force, char *name){ - ENTRY *new; - char auto_name[BUF_LEN]; - - //check if a name was given as argument - if(name != NULL){ - //strip quotes from the name - name = strip_quotes(name); - new = create_entry(name, path, force); - } - - //check if autoAlias is on. If it is, go to the autoAlias function - else if(hr){ - strcpy(auto_name, autoAlias(path)); - new = create_entry(auto_name, path, force); - } - - else new = create_entry(path, path, force); - if(new != NULL) group_add(group, new); - - return; -} - char *autoAlias(char *path){ char *hr_name = malloc(sizeof(char) * BUF_LEN); @@ -792,85 +516,5 @@ char *autoAlias(char *path){ return hr_name; } -int search_ch(char *str, char c){ - int i = 0; - while(str[i] != '\0'){ - if(str[i] == c) return i; - i++; - } - - return -1; -} -int search_last_ch(char *str, char c){ - int i = 0; - int last_i = -1; - - while(str[i] != '\0'){ - if(str[i] == c) last_i = i; - i++; - } - - return last_i; -} - -//return 0 if match, 1 if not -//TODO only supports one wildcard per entry -int wild_cmp(char *wild, char *literal){ - int i; - - while(*wild != '\0'){ - //traverse until wildcard - if(*wild != '*'){ - if(*wild != *literal) return 1; - wild++; - literal++; - } - - //found wildcard, find the end of both names and comapre from the back - else{ - i = 0; - wild++; - while(*wild != '\0'){ - i++; - wild++; - } - while(*literal != '\0'){ - literal++; - } - - while(i > 0){ - wild--; - literal--; - if(*wild != *literal) return 1; - i--; - } - - return 0; - } - } - - return 0; -} - -char *strip_quotes(char *str){ - char *stripped_str = malloc(sizeof(char) * BUF_LEN); - - if(str[0] == '"'){ - stripped_str = &str[1]; - stripped_str[strlen(stripped_str) - 1] = '\0'; - return stripped_str; - } - - return str; -} - -void error_mes(int ln, char *message){ - - assert(message != NULL); - - printf("Configuration File Error:\nOn line %d: %s\n\n", ln, message); - - return; -} diff --git a/read_cfg.h b/read_cfg.h index b8edd64..d29bdd2 100644 --- a/read_cfg.h +++ b/read_cfg.h @@ -1,11 +1,7 @@ #ifndef READ_CFG_H #define READ_CFG_H -#if defined _WIN32 || defined _WIN64 -char *find_config_win(); -#else -char *find_config(); -#endif +#define BUF_LEN 1024 void cfg_interp(); @@ -15,4 +11,16 @@ bool get_case_sensitivity(); void refer_to_doc(); +void addme(); + +int search_ch(); + +int search_last_ch(); + +int wild_cmp(); + +char *strip_quotes(); + +void error_mes(); + #endif diff --git a/unix/draw.c b/unix/draw.c new file mode 100644 index 0000000..f6424d0 --- /dev/null +++ b/unix/draw.c @@ -0,0 +1,16 @@ +#include +#include + +#include "../draw.h" + +void launch(); + +void launch(){ + char full_command[BUF_LEN]; + + strcpy(full_command, get_launch()); + strcat(full_command, " > /dev/null 2>&1 &"); + system(full_command); + + return; +} diff --git a/unix/draw.h b/unix/draw.h new file mode 100644 index 0000000..b8e1c24 --- /dev/null +++ b/unix/draw.h @@ -0,0 +1,6 @@ +#ifndef UNIX_DRAW_H +#define UNIX_DRAW_H + +void launch(); + +#endif diff --git a/unix/read_cfg.c b/unix/read_cfg.c new file mode 100644 index 0000000..daa31f8 --- /dev/null +++ b/unix/read_cfg.c @@ -0,0 +1,238 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../read_cfg.h" +#include "../entry.h" + +char *find_config(); +void mkconfig_wizard(); +void handle_fname(char *path, char *group, bool recurs, bool force, char *name, int ln); + +char sep = '/'; + +char *find_config(){ + char *home = getenv("HOME"); + char *path = malloc(sizeof(char) * BUF_LEN); + char choices[2][BUF_LEN]; + int check_count = 2; + int i; + + sprintf(choices[0], "%s%c.config%ctml%cconfig", home, sep, sep, sep); + sprintf(choices[1], "%s%c.tml%cconfig", home, sep, sep); + + for(i = 0; i < check_count; i++){ + path = choices[i]; + printf("Checking for config at %s: ", choices[i]); + if(access(path, R_OK) == 0){ + printf("Using config \"%s\"\n\n", path); + return path; + } + else printf("File does not exist\n"); + } + + //TODO no config exists, ask user if they want to autogenerate one + mkconfig_wizard(choices[0]); + path = choices[0]; + return path; +} + +void mkconfig_wizard(char *path){ + char input; + FILE *fp; + + char *home = getenv("HOME"); + + printf("\nNo configuration file found. Auto-generate one now at \"%s\"? [Y/n] ", path); + fflush(stdout); + scanf(" %c", &input); + + if(input == 'n'){ + printf("Configuration will not be auto-generated\n"); + refer_to_doc(); + exit(0); + + } + + printf("Generating configuration file at \"%s\"...\n", path); + + //ensure directories have been created + if(home == NULL){ + printf("Failed: HOME is NULL\n"); + exit(1); + } + + sprintf(path, "%s%c.config%c", home, sep, sep); + mkdir(path, 0755); + + sprintf(path, "%s%c.config%ctml%c", home, sep, sep, sep); + mkdir(path, 0755); + + sprintf(path, "%s%c.config%ctml%cconfig", home, sep, sep, sep); + + //open file for writing, make sure non-NULL + fp = fopen(path, "w"); + if(fp == NULL){ + printf("Failed: \"%s\" could not be open for writing\n", path); + exit(1); + } + + //write to file + fprintf(fp, "# This file was auto-generated by tml. See docs/tml-config.md or tml-config(5) for documentation\n" + "# The default launcher is set to \"xdg-open\" which will open files based on the relevant default application set through xdg\n\n" + "# Recursively add files from %s%cMusic%c to Music group\n" + "addGroup Music\n" + "setLauncher Music xdg-open\n", home, sep, sep); + fprintf(fp, "addR %s%cMusic%c*.aac Music\n", home, sep, sep); + fprintf(fp, "addR %s%cMusic%c*.aiff Music\n", home, sep, sep); + fprintf(fp, "addR %s%cMusic%c*.alac Music\n", home, sep, sep); + fprintf(fp, "addR %s%cMusic%c*.au Music\n", home, sep, sep); + fprintf(fp, "addR %s%cMusic%c*.flac Music\n", home, sep, sep); + fprintf(fp, "addR %s%cMusic%c*.m4a Music\n", home, sep, sep); + fprintf(fp, "addR %s%cMusic%c*.mp3 Music\n", home, sep, sep); + fprintf(fp, "addR %s%cMusic%c*.ogg Music\n", home, sep, sep); + fprintf(fp, "addR %s%cMusic%c*.pcm Music\n", home, sep, sep); + fprintf(fp, "addR %s%cMusic%c*.wav Music\n", home, sep, sep); + fprintf(fp, "addR %s%cMusic%c*.wma Music\n\n", home, sep, sep); + fprintf(fp, "# Recursively add files from %s%cPictures%c to Pictures group\n" + "addGroup Pictures\n" + "setLauncher Pictures xdg-open\n", home, sep, sep); + fprintf(fp, "addR %s%cPictures%c*.epi Pictures\n", home, sep, sep); + fprintf(fp, "addR %s%cPictures%c*.eps Pictures\n", home, sep, sep); + fprintf(fp, "addR %s%cPictures%c*.eps2 Pictures\n", home, sep, sep); + fprintf(fp, "addR %s%cPictures%c*.eps3 Pictures\n", home, sep, sep); + fprintf(fp, "addR %s%cPictures%c*.epsf Pictures\n", home, sep, sep); + fprintf(fp, "addR %s%cPictures%c*.epsi Pictures\n", home, sep, sep); + fprintf(fp, "addR %s%cPictures%c*.ept Pictures\n", home, sep, sep); + fprintf(fp, "addR %s%cPictures%c*.gif Pictures\n", home, sep, sep); + fprintf(fp, "addR %s%cPictures%c*.gfa Pictures\n", home, sep, sep); + fprintf(fp, "addR %s%cPictures%c*.giff Pictures\n", home, sep, sep); + fprintf(fp, "addR %s%cPictures%c*.jpeg Pictures\n", home, sep, sep); + fprintf(fp, "addR %s%cPictures%c*.jpg Pictures\n", home, sep, sep); + fprintf(fp, "addR %s%cPictures%c*.png Pictures\n", home, sep, sep); + fprintf(fp, "addR %s%cPictures%c*.svg Pictures\n", home, sep, sep); + fprintf(fp, "addR %s%cPictures%c*.svgz Pictures\n", home, sep, sep); + fprintf(fp, "addR %s%cPictures%c*.tif Pictures\n", home, sep, sep); + fprintf(fp, "addR %s%cPictures%c*.tiff Pictures\n\n", home, sep, sep); + fprintf(fp, "# Recursively add files from %s%cVideos%c to Videos group\n" + "addGroup Videos\n" + "setLauncher Videos xdg-open\n", home, sep, sep); + fprintf(fp, "addR %s%cVideos%c*.asf Videos\n", home, sep, sep); + fprintf(fp, "addR %s%cVideos%c*.avi Videos\n", home, sep, sep); + fprintf(fp, "addR %s%cVideos%c*.flv Videos\n", home, sep, sep); + fprintf(fp, "addR %s%cVideos%c*.mk3d Videos\n", home, sep, sep); + fprintf(fp, "addR %s%cVideos%c*.mkv Videos\n", home, sep, sep); + fprintf(fp, "addR %s%cVideos%c*.mov Videos\n", home, sep, sep); + fprintf(fp, "addR %s%cVideos%c*.mp4 Videos\n", home, sep, sep); + fprintf(fp, "addR %s%cVideos%c*.qt Videos\n", home, sep, sep); + fprintf(fp, "addR %s%cVideos%c*.webm Videos\n", home, sep, sep); + fprintf(fp, "addR %s%cVideos%c*.wmv Videos\n", home, sep, sep); + + fclose(fp); + printf("done\nIt is highly recommended to further tweak the configuration file! [press any key to continue]"); + fflush(stdout); + getchar(); + getchar(); + + return; +} + +//TODO augment to involve recurs +//TODO could use some cleanup... +void handle_fname(char *path, char *group, bool recurs, bool force, char *name, int ln){ + ENTRY *new; + char *search; //pointer for traversing path + char full_path_cpy[BUF_LEN]; + char relative_path_cpy[BUF_LEN]; + char arg_cpy[BUF_LEN]; + char auto_name[BUF_LEN]; + int plen = strlen(path); + char *dirname; + char *local_arg; //for use in addR + DIR *dp; + struct dirent *fname; + int i; + char *error_p; //helper for complex error messages + + assert(path != NULL && group != NULL); + + if(path[0] == '\0' || group[0] == '\0'){ + error_mes(ln, "Too few arguments for \"add\""); + return; + } + + //address potential quotes + strcpy(full_path_cpy, strip_quotes(path)); + + //don't check that the path arg is valid when forced + if(force) addme(full_path_cpy, group, force, name); + + //file is not recognized, perhaps it has a wildcard? + //TODO finish rewriting a more robust wildcard thingy + else if(access(full_path_cpy, F_OK) == -1){ + i = search_ch(full_path_cpy, '*'); + if(i > -1){ + //look for a directory + while(full_path_cpy[i] != sep && (i >= 0)){ + i--; + } + dirname = full_path_cpy; + strcpy(arg_cpy, full_path_cpy); + dirname[i+1] = '\0'; + dp = opendir(dirname); + + //the directory is real + if(dp != NULL){ + while(fname = readdir(dp)){ + relative_path_cpy[0] = '\0'; + strcat(relative_path_cpy, dirname); + strcat(relative_path_cpy, fname->d_name); + + //check if path is a file (and not a directory/symlink/etc.) and regex matches + if(fname->d_type == DT_REG && !(wild_cmp(&arg_cpy[i+1], fname->d_name))) addme(relative_path_cpy, group, force, name); + + //if the recursive option was specified and the path is a directory, run handle_fname on this directory, but for security reasons, do not consider directories that start with a '.' + else if(recurs && fname->d_type == DT_DIR && fname->d_name[0] != '.'){ + i = search_last_ch(arg_cpy, sep); + local_arg = &arg_cpy[i+1]; + strcat(relative_path_cpy, &sep); + strcat(relative_path_cpy, local_arg); + handle_fname(relative_path_cpy, group, 1, 0, NULL, ln); + } + + } + + closedir(dp); + } + + //directory is not real, report error to the user + else{ + error_p = malloc(sizeof(char) * 1024); + sprintf(error_p, "\"%s\" bad path", dirname); + error_mes(ln, error_p); + free(error_p); + //printf("Error: \"%s\" bad path\n", dirname); + } + } + + //path is not real, report error to the user + else{ + error_p = malloc(sizeof(char) * 1024); + sprintf(error_p, "\"%s\" bad path", full_path_cpy); + error_mes(ln, error_p); + free(error_p); + } + } + + //file name is okay + //FIXME does not take into account whether the argument is a file (could be a directory, symlink, etc.) + else addme(full_path_cpy, group, force, name); + + return; +} diff --git a/unix/read_cfg.h b/unix/read_cfg.h new file mode 100644 index 0000000..ce0ad88 --- /dev/null +++ b/unix/read_cfg.h @@ -0,0 +1,12 @@ +#ifndef UNIX_READ_CFG_H +#define UNIX_READ_CFG_H + +extern char sep; + +char *find_config(); + +void mkconfig_wizard(); + +void handle_fname(); + +#endif diff --git a/windows/draw.c b/windows/draw.c new file mode 100644 index 0000000..5a69b09 --- /dev/null +++ b/windows/draw.c @@ -0,0 +1,43 @@ +#include +#include + +#include "../draw.h" +#include "../entry.h" +#include "../group.h" + +void launch(){ + char *program = get_gprog(g[g_hover]); + char *flags = get_gflags(g[g_hover]); + char *path = get_epath(e[e_hover]); + bool quotes = get_gquotes(g[g_hover]); + char file[BUF_LEN]; + char params[BUF_LEN]; + + file[0] = '\0'; + + if(!(strcmp(program, "./"))){ + strcat(file, "\""); + strcat(file, path); + strcat(file, "\""); + ShellExecute(NULL, NULL, file, NULL, NULL, SW_SHOW); + } + + else{ + if(quotes) strcat(file, "\""); + strcat(file, program); + if(quotes) strcat(file, "\""); + + params[0] = '\0'; + if(flags[0] != '\0'){ + strcat(params, flags); + strcat(params, " "); + } + strcat(params, "\""); + strcat(params, path); + strcat(params, "\""); + + ShellExecute(NULL, NULL, file, params, NULL, SW_SHOW); + } + + return; +} diff --git a/windows/draw.h b/windows/draw.h new file mode 100644 index 0000000..acde6ee --- /dev/null +++ b/windows/draw.h @@ -0,0 +1,6 @@ +#ifndef WINDOWS_DRAW_H +#define WINDOWS_DRAW_H + +void launch(); + +#endif diff --git a/windows/read_cfg.c b/windows/read_cfg.c new file mode 100644 index 0000000..7194e44 --- /dev/null +++ b/windows/read_cfg.c @@ -0,0 +1,236 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../read_cfg.h" +#include "../entry.h" + +char *find_config(); +void mkconfig_wizard(); +void handle_fname(char *path, char *group, bool recurs, bool force, char *name, int ln); + +char sep = '\\'; + +char *find_config(){ + char *appdata = getenv("APPDATA"); + char *path = malloc(sizeof(char) * BUF_LEN); + int check_count = 1; + char choices[check_count][BUF_LEN]; + int i; + + sprintf(choices[0], "%s%ctml%cconfig", appdata, sep, sep); + + for(i = 0; i < check_count; i++){ + path = choices[i]; + printf("Checking for config at %s: ", choices[i]); + if(access(path, R_OK) == 0){ + printf("Using config \"%s\"\n\n", path); + return path; + } + else printf("File does not exist\n"); + } + + //TODO no config exists, ask user if they want to autogenerate one + mkconfig_wizard(choices[0]); + path = choices[0]; + return path; +} + +void mkconfig_wizard(char *path){ + char input; + FILE *fp; + + char *home = getenv("USERPROFILE"); + char *appdata = getenv("APPDATA"); + + printf("\nNo configuration file found. Auto-generate one now at \"%s\"? [Y/n] ", path); + fflush(stdout); + scanf(" %c", &input); + + if(input == 'n'){ + printf("Configuration will not be auto-generated\n"); + refer_to_doc(); + exit(0); + + } + + printf("Generating configuration file at \"%s\"...\n", path); + + //ensure directories have been created + if(appdata == NULL){ + printf("Failed: \%APPDATA\% is NULL\n"); + exit(1); + } + + if(home == NULL){ + printf("Failed: \%USERPROFILE\% is NULL\n"); + exit(1); + } + + sprintf(path, "%s%ctml%c", appdata, sep, sep); + mkdir(path); + + sprintf(path, "%s%ctml%cconfig", appdata, sep, sep); + + //open file for writing, make sure non-NULL + fp = fopen(path, "w"); + if(fp == NULL){ + printf("Failed: \"%s\" could not be open for writing\n", path); + exit(1); + } + + fprintf(fp, "# This file was auto-generated by tml. See docs\\tml-config.md or tml-config(5) for documentation\n" + "# By default, no launcher is specified for any group. When no launcher is specified on the Windows build of tml, media files will be opened with their default application.\n" + "# It is generally recommended to specify a launcher for groups containing media files using the \"setLauncher\" command\n\n" + "# Recursively add files from %s%cMusic%c to Music group\n" + "addGroup Music\n", home, sep, sep); + fprintf(fp, "addR %s%cMusic%c*.aac Music\n", home, sep, sep); + fprintf(fp, "addR %s%cMusic%c*.aiff Music\n", home, sep, sep); + fprintf(fp, "addR %s%cMusic%c*.alac Music\n", home, sep, sep); + fprintf(fp, "addR %s%cMusic%c*.au Music\n", home, sep, sep); + fprintf(fp, "addR %s%cMusic%c*.flac Music\n", home, sep, sep); + fprintf(fp, "addR %s%cMusic%c*.m4a Music\n", home, sep, sep); + fprintf(fp, "addR %s%cMusic%c*.mp3 Music\n", home, sep, sep); + fprintf(fp, "addR %s%cMusic%c*.ogg Music\n", home, sep, sep); + fprintf(fp, "addR %s%cMusic%c*.pcm Music\n", home, sep, sep); + fprintf(fp, "addR %s%cMusic%c*.wav Music\n", home, sep, sep); + fprintf(fp, "addR %s%cMusic%c*.wma Music\n\n", home, sep, sep); + fprintf(fp, "# Recursively add files from %s%cPictures%c to Pictures group\n" + "addGroup Pictures\n", home, sep, sep); + fprintf(fp, "addR %s%cPictures%c*.epi Pictures\n", home, sep, sep); + fprintf(fp, "addR %s%cPictures%c*.eps Pictures\n", home, sep, sep); + fprintf(fp, "addR %s%cPictures%c*.eps2 Pictures\n", home, sep, sep); + fprintf(fp, "addR %s%cPictures%c*.eps3 Pictures\n", home, sep, sep); + fprintf(fp, "addR %s%cPictures%c*.epsf Pictures\n", home, sep, sep); + fprintf(fp, "addR %s%cPictures%c*.epsi Pictures\n", home, sep, sep); + fprintf(fp, "addR %s%cPictures%c*.ept Pictures\n", home, sep, sep); + fprintf(fp, "addR %s%cPictures%c*.gif Pictures\n", home, sep, sep); + fprintf(fp, "addR %s%cPictures%c*.gfa Pictures\n", home, sep, sep); + fprintf(fp, "addR %s%cPictures%c*.giff Pictures\n", home, sep, sep); + fprintf(fp, "addR %s%cPictures%c*.jpeg Pictures\n", home, sep, sep); + fprintf(fp, "addR %s%cPictures%c*.jpg Pictures\n", home, sep, sep); + fprintf(fp, "addR %s%cPictures%c*.png Pictures\n", home, sep, sep); + fprintf(fp, "addR %s%cPictures%c*.svg Pictures\n", home, sep, sep); + fprintf(fp, "addR %s%cPictures%c*.svgz Pictures\n", home, sep, sep); + fprintf(fp, "addR %s%cPictures%c*.tif Pictures\n", home, sep, sep); + fprintf(fp, "addR %s%cPictures%c*.tiff Pictures\n\n", home, sep, sep); + fprintf(fp, "# Recursively add files from %s%cVideos%c to Videos group\n" + "addGroup Videos\n", home, sep, sep); + fprintf(fp, "addR %s%cVideos%c*.asf Videos\n", home, sep, sep); + fprintf(fp, "addR %s%cVideos%c*.avi Videos\n", home, sep, sep); + fprintf(fp, "addR %s%cVideos%c*.flv Videos\n", home, sep, sep); + fprintf(fp, "addR %s%cVideos%c*.mk3d Videos\n", home, sep, sep); + fprintf(fp, "addR %s%cVideos%c*.mkv Videos\n", home, sep, sep); + fprintf(fp, "addR %s%cVideos%c*.mov Videos\n", home, sep, sep); + fprintf(fp, "addR %s%cVideos%c*.mp4 Videos\n", home, sep, sep); + fprintf(fp, "addR %s%cVideos%c*.qt Videos\n", home, sep, sep); + fprintf(fp, "addR %s%cVideos%c*.webm Videos\n", home, sep, sep); + fprintf(fp, "addR %s%cVideos%c*.wmv Videos\n", home, sep, sep); + + fclose(fp); + printf("done\nIt is highly recommended to further tweak the configuration file! [press any key to continue]"); + fflush(stdout); + getchar(); + getchar(); + + return; +} + +//TODO augment to involve recurs +//TODO could use some cleanup... +void handle_fname(char *path, char *group, bool recurs, bool force, char *name, int ln){ + ENTRY *new; + char *search; //pointer for traversing path + char full_path_cpy[BUF_LEN]; + char relative_path_cpy[BUF_LEN]; + char arg_cpy[BUF_LEN]; + char auto_name[BUF_LEN]; + int plen = strlen(path); + char *dirname; + char *local_arg; //for use in addR + DIR *dp; + struct dirent *fname; + int i; + char *error_p; //helper for complex error messages + + assert(path != NULL && group != NULL); + + if(path[0] == '\0' || group[0] == '\0'){ + error_mes(ln, "Too few arguments for \"add\""); + return; + } + + //address potential quotes + strcpy(full_path_cpy, strip_quotes(path)); + + //don't check that the path arg is valid when forced + if(force) addme(full_path_cpy, group, force, name); + + //file is not recognized, perhaps it has a wildcard? + //TODO finish rewriting a more robust wildcard thingy + else if(access(full_path_cpy, F_OK) == -1){ + i = search_ch(full_path_cpy, '*'); + if(i > -1){ + //look for a directory + while(full_path_cpy[i] != sep && (i >= 0)){ + i--; + } + dirname = full_path_cpy; + strcpy(arg_cpy, full_path_cpy); + dirname[i+1] = '\0'; + dp = opendir(dirname); + + //the directory is real + if(dp != NULL){ + while(fname = readdir(dp)){ + relative_path_cpy[0] = '\0'; + strcat(relative_path_cpy, dirname); + strcat(relative_path_cpy, fname->d_name); + + //Windows cannot tell file types (TODO), so just add relatively indiscriminantly + if(!(wild_cmp(&arg_cpy[i+1], fname->d_name))) addme(relative_path_cpy, group, force, name); + + //if the recursive option was specified, run handle_fname on this directory, but for security reasons, do not consider directories that start with a '.' + else if(recurs && fname->d_name[0] != '.'){ + i = search_last_ch(arg_cpy, sep); + local_arg = &arg_cpy[i+1]; + strcat(relative_path_cpy, &sep); + strcat(relative_path_cpy, local_arg); + handle_fname(relative_path_cpy, group, 1, 0, NULL, ln); + } + } + + closedir(dp); + } + + //directory is not real, report error to the user + else if(!recurs){ + error_p = malloc(sizeof(char) * 1024); + sprintf(error_p, "\"%s\" bad path", dirname); + error_mes(ln, error_p); + free(error_p); + //printf("Error: \"%s\" bad path\n", dirname); + } + } + + //path is not real, report error to the user + else{ + error_p = malloc(sizeof(char) * 1024); + sprintf(error_p, "\"%s\" bad path", full_path_cpy); + error_mes(ln, error_p); + free(error_p); + } + } + + //file name is okay + //FIXME does not take into account whether the argument is a file (could be a directory, symlink, etc.) + else addme(full_path_cpy, group, force, name); + + return; +} diff --git a/windows/read_cfg.h b/windows/read_cfg.h new file mode 100644 index 0000000..37684af --- /dev/null +++ b/windows/read_cfg.h @@ -0,0 +1,12 @@ +#ifndef WINDOWS_READ_CFG_H +#define WINDOWS_READ_CFG_H + +extern char sep; + +char *find_config(); + +void mkconfig_wizard(); + +void handle_fname(); + +#endif -- cgit