From a2a2c42cfd9e9f216a78168611acbeeba60efecc Mon Sep 17 00:00:00 2001 From: louie Date: Wed, 24 Jun 2020 20:28:25 -0700 Subject: Added wildcard functionality for config --- entry.c | 2 +- entry.o | Bin 3520 -> 3520 bytes read_cfg.c | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++++--------- read_cfg.o | Bin 2968 -> 4544 bytes 4 files changed, 88 insertions(+), 15 deletions(-) diff --git a/entry.c b/entry.c index 6b5e9ce..4b6d03d 100644 --- a/entry.c +++ b/entry.c @@ -23,7 +23,7 @@ char *get_epath(ENTRY *e); ENTRY *create_entry(char *new_name, char *new_path){ ENTRY *new; - //check if file exists + //double check if file exists if(access(new_path, F_OK) == -1){ printf("Error: Invalid File Name \"%s\"\n", new_path); return NULL; diff --git a/entry.o b/entry.o index fc225bc..0c2cc9d 100644 Binary files a/entry.o and b/entry.o differ diff --git a/read_cfg.c b/read_cfg.c index c7dd47f..11a2626 100644 --- a/read_cfg.c +++ b/read_cfg.c @@ -9,10 +9,15 @@ #include "entry.h" #include "group.h" #define BUF_LEN 1024 //maybe move this line to the header file +#define MAX_ARGS 5 +//public void cfg_interp(); void check_line(char *buffer); +//private +void handle_fname(char *path, char *group); + void cfg_interp(){ FILE *fp; char buffer[BUF_LEN]; @@ -47,32 +52,100 @@ void cfg_interp(){ //END DEBUG */ + fclose(fp); return; } void check_line(char *buffer){ char *delims = " \t\n"; char *tok = strtok(buffer, delims); - ENTRY *new; + char *args[MAX_ARGS]; + int i; + + //initialize args to NULL + for(i = 0; i < MAX_ARGS; i++){ + args[i] = NULL; + } + + i = 0; + //record all arguments in the line + while(tok != NULL){ + if(i >= MAX_ARGS){ + printf("Error: too many arguments\n"); + return; + } + args[i] = tok; + tok = strtok(NULL, delims); + i++; + } //add entry(ies) to a group: first arg is the file(s), second arg is the group to add to //TODO add potential dash functions //TODO account for spaces in file name - //TODO add regex support for adding patterns - //TODO allow adding directories (might fall into regex support) - if(!(strcmp(tok, "add"))){ - //look at first arg - tok = strtok(NULL, delims); - new = create_entry(tok, tok); - //look at second arg - tok = strtok(NULL, delims); - if(new != NULL) group_add(tok, new); - } + //TODO add support for "-R" recursive adding + //TODO add sorting functionality + if(!(strcmp(args[0], "add"))) handle_fname(args[1], args[2]); //create a new group - if(!(strcmp(tok, "addGroup"))){ - tok = strtok(NULL, delims); - group_add(tok, NULL); + if(!(strcmp(args[0], "addGroup"))) group_add(args[1], NULL); + + return; +} + +void handle_fname(char *path, char *group){ + ENTRY *new; + char path_cpy[BUF_LEN]; + int plen = strlen(path); + char *dirname; + DIR *dp; + struct dirent *fname; + int i = 1; + + if(path == NULL || group == NULL){ + printf("Error: too few arguments for \"add\"\n"); + return; + } + + //file is not recognized, perhaps it has a wildcard? + if(access(path, F_OK) == -1){ + if(path[plen-i] == '*'){ + //look for a directory + while(path[plen-i] != '/' && (plen-i >= 0)){ + i++; + } + dirname = path; + dirname[plen-i+1] = '\0'; + dp = opendir(dirname); + + //the directory is real + if(dp != NULL){ + while(fname = readdir(dp)){ + path_cpy[0] = '\0'; + strcat(path_cpy, dirname); + strcat(path_cpy, fname->d_name); + + //check if path is a file (and not a directory/symlink/etc.) + if(fname->d_type == DT_REG){ + new = create_entry(path_cpy, path_cpy); + if(new != NULL) group_add(group, new); + } + } + + //FIXME breaks if I close the dir (WHY?!?!?!?) + closedir(dp); + } + + //directory is not real, report error to the user + else printf("Error: \"%s\" bad path\n", dirname); + } + } + + //file name is okay + else{ + new = create_entry(path, path); + if(new != NULL){ + group_add(group, new); + } } return; diff --git a/read_cfg.o b/read_cfg.o index 768690e..d9b9a05 100644 Binary files a/read_cfg.o and b/read_cfg.o differ -- cgit