diff options
author | louie <lshprung@yahoo.com> | 2020-06-24 20:28:25 -0700 |
---|---|---|
committer | louie <lshprung@yahoo.com> | 2020-06-24 20:28:25 -0700 |
commit | a2a2c42cfd9e9f216a78168611acbeeba60efecc (patch) | |
tree | aeb26d7e9096a04a5f36069a0ff0812c9f22e79a /read_cfg.c | |
parent | 43eeda3d0982160d7863dd2268d989f79c24f03f (diff) |
Added wildcard functionality for config
Diffstat (limited to 'read_cfg.c')
-rw-r--r-- | read_cfg.c | 101 |
1 files changed, 87 insertions, 14 deletions
@@ -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; |