diff --git a/src/Makefile.am b/src/Makefile.am index c01e11f7421852e4ee385291e6c39a317a9a330c..8d349eccd596b19df9f76e1372e8d38cbbebbb89 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -2,4 +2,4 @@ bin_PROGRAMS = ncdu ncdu_SOURCES = browser.c calc.c main.c util.c exclude.c help.c delete.c -noinst_HEADERS = calc.h ncdu.h +noinst_HEADERS = calc.h exclude.h ncdu.h diff --git a/src/calc.c b/src/calc.c index 64580e4618e9ec987e6763a5688deda82f4a88e2..d77634a85a2e88493f26501a7d750dae6c4cced9 100644 --- a/src/calc.c +++ b/src/calc.c @@ -25,6 +25,7 @@ #include "ncdu.h" #include "calc.h" +#include "exclude.h" struct state_calc stcalc; @@ -173,7 +174,7 @@ int calc_item(struct dir *par, char *path, char *name) { } /* check for excludes and same filesystem */ - if(matchExclude(tmp)) + if(exclude_match(tmp)) d->flags |= FF_EXL; if(sflags & SF_SMFS && stcalc.curdev != fs.st_dev) diff --git a/src/exclude.c b/src/exclude.c index df0749a902acc045cb5bef99fead38b326da52a8..2fa79729407be3a9ca383502e04213600c501a5d 100644 --- a/src/exclude.c +++ b/src/exclude.c @@ -24,43 +24,36 @@ */ #include "ncdu.h" +#include "exclude.h" struct exclude { char *pattern; struct exclude *next; -}; +} *excludes = NULL; -struct exclude *excludes = NULL, - *last = NULL; +void exclude_add(char *pat) { + struct exclude **n; -void addExclude(char *pat) { - struct exclude *n; + n = &excludes; + while(*n != NULL) + n = &((*n)->next); - n = (struct exclude *) malloc(sizeof(struct exclude)); - n->pattern = (char *) malloc(strlen(pat)+1); - strcpy(n->pattern, pat); - n->next = NULL; - - if(excludes == NULL) { - excludes = n; - last = excludes; - } else { - last->next = n; - last = last->next; - } + *n = (struct exclude *) calloc(1, sizeof(struct exclude)); + (*n)->pattern = (char *) malloc(strlen(pat)+1); + strcpy((*n)->pattern, pat); } -int addExcludeFile(char *file) { +int exclude_addfile(char *file) { FILE *f; char buf[256]; int len; if((f = fopen(file, "r")) == NULL) - return(1); + return 1; while(fgets(buf, 256, f) != NULL) { len = strlen(buf)-1; @@ -68,29 +61,36 @@ int addExcludeFile(char *file) { buf[len--] = '\0'; if(len < 0) continue; - addExclude(buf); + exclude_add(buf); } fclose(f); - return(0); + return 0; } -int matchExclude(char *path) { - struct exclude *n = excludes; +int exclude_match(char *path) { + struct exclude *n; char *c; - int matched = 0; - if(excludes == NULL) - return(0); - - do { - matched = !fnmatch(n->pattern, path, 0); - for(c = path; *c && !matched; c++) - if(*c == '/' && c[1] != '/') - matched = !fnmatch(n->pattern, c+1, 0); - } while((n = n->next) != NULL && !matched); - - return(matched); + for(n=excludes; n!=NULL; n=n->next) { + if(!fnmatch(n->pattern, path, 0)) + return 1; + for(c = path; *c; c++) + if(*c == '/' && c[1] != '/' && !fnmatch(n->pattern, c+1, 0)) + return 1; + } + return 0; +} + + +void exclude_clear() { + struct exclude *n, *l; + + for(n=excludes; n!=NULL; n=l) { + l = n->next; + free(n); + } + excludes = NULL; } diff --git a/src/exclude.h b/src/exclude.h new file mode 100644 index 0000000000000000000000000000000000000000..6807315775b8fae608089669b818fe62200d0eb2 --- /dev/null +++ b/src/exclude.h @@ -0,0 +1,34 @@ +/* ncdu - NCurses Disk Usage + + Copyright (c) 2007-2009 Yoran Heling + + Permission is hereby granted, free of charge, to any person obtaining + a copy of this software and associated documentation files (the + "Software"), to deal in the Software without restriction, including + without limitation the rights to use, copy, modify, merge, publish, + distribute, sublicense, and/or sell copies of the Software, and to + permit persons to whom the Software is furnished to do so, subject to + the following conditions: + + The above copyright notice and this permission notice shall be included + in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +*/ + +#ifndef _exclude_h +#define _exclude_h + +void exclude_add(char *); +int exclude_addfile(char *); +int exclude_match(char *); +void exclude_clear(); + +#endif diff --git a/src/main.c b/src/main.c index 6dcedea81fe257c4646fc7258042a6428efacbbc..043d474795c733fe3e45a05c809a4b36f94c7434 100644 --- a/src/main.c +++ b/src/main.c @@ -24,6 +24,7 @@ */ #include "ncdu.h" +#include "exclude.h" /* check ncdu.h what these are for */ struct dir *dat; @@ -85,8 +86,8 @@ void argv_parse(int argc, char **argv, char *dir) { exit(1); } else if(strcmp(argv[i], "--exclude") == 0) - addExclude(argv[++i]); - else if(addExcludeFile(argv[++i])) { + exclude_add(argv[++i]); + else if(exclude_addfile(argv[++i])) { printf("Can't open %s: %s\n", argv[i], strerror(errno)); exit(1); } @@ -152,6 +153,7 @@ int main(int argc, char **argv) { erase(); refresh(); endwin(); + exclude_clear(); return 0; } diff --git a/src/ncdu.h b/src/ncdu.h index 989210beb5da7808e85b33350c47ff0e5ef2c07f..392eaf44cb033b1d736f4a777e0f02b7854fd464 100644 --- a/src/ncdu.h +++ b/src/ncdu.h @@ -179,7 +179,3 @@ void showBrowser(void); void showHelp(void); /* delete.c */ struct dir *showDelete(struct dir *); -/* exclude.c */ -void addExclude(char *); -int addExcludeFile(char *); -int matchExclude(char *);