Rewrote exclude.c to the new style

This commit is contained in:
Yorhel 2009-04-11 10:48:24 +02:00
parent cc8cc99213
commit 6c0a56a26f
6 changed files with 76 additions and 43 deletions

View file

@ -2,4 +2,4 @@ bin_PROGRAMS = ncdu
ncdu_SOURCES = browser.c calc.c main.c util.c exclude.c help.c delete.c 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

View file

@ -25,6 +25,7 @@
#include "ncdu.h" #include "ncdu.h"
#include "calc.h" #include "calc.h"
#include "exclude.h"
struct state_calc stcalc; struct state_calc stcalc;
@ -173,7 +174,7 @@ int calc_item(struct dir *par, char *path, char *name) {
} }
/* check for excludes and same filesystem */ /* check for excludes and same filesystem */
if(matchExclude(tmp)) if(exclude_match(tmp))
d->flags |= FF_EXL; d->flags |= FF_EXL;
if(sflags & SF_SMFS && stcalc.curdev != fs.st_dev) if(sflags & SF_SMFS && stcalc.curdev != fs.st_dev)

View file

@ -24,43 +24,36 @@
*/ */
#include "ncdu.h" #include "ncdu.h"
#include "exclude.h"
struct exclude { struct exclude {
char *pattern; char *pattern;
struct exclude *next; struct exclude *next;
}; } *excludes = NULL;
struct exclude *excludes = NULL,
*last = NULL;
void addExclude(char *pat) { void exclude_add(char *pat) {
struct exclude *n; struct exclude **n;
n = (struct exclude *) malloc(sizeof(struct exclude)); n = &excludes;
n->pattern = (char *) malloc(strlen(pat)+1); while(*n != NULL)
strcpy(n->pattern, pat); n = &((*n)->next);
n->next = NULL;
if(excludes == NULL) { *n = (struct exclude *) calloc(1, sizeof(struct exclude));
excludes = n; (*n)->pattern = (char *) malloc(strlen(pat)+1);
last = excludes; strcpy((*n)->pattern, pat);
} else {
last->next = n;
last = last->next;
}
} }
int addExcludeFile(char *file) { int exclude_addfile(char *file) {
FILE *f; FILE *f;
char buf[256]; char buf[256];
int len; int len;
if((f = fopen(file, "r")) == NULL) if((f = fopen(file, "r")) == NULL)
return(1); return 1;
while(fgets(buf, 256, f) != NULL) { while(fgets(buf, 256, f) != NULL) {
len = strlen(buf)-1; len = strlen(buf)-1;
@ -68,29 +61,36 @@ int addExcludeFile(char *file) {
buf[len--] = '\0'; buf[len--] = '\0';
if(len < 0) if(len < 0)
continue; continue;
addExclude(buf); exclude_add(buf);
} }
fclose(f); fclose(f);
return(0); return 0;
} }
int matchExclude(char *path) { int exclude_match(char *path) {
struct exclude *n = excludes; struct exclude *n;
char *c; char *c;
int matched = 0;
if(excludes == NULL) for(n=excludes; n!=NULL; n=n->next) {
return(0); if(!fnmatch(n->pattern, path, 0))
return 1;
do { for(c = path; *c; c++)
matched = !fnmatch(n->pattern, path, 0); if(*c == '/' && c[1] != '/' && !fnmatch(n->pattern, c+1, 0))
for(c = path; *c && !matched; c++) return 1;
if(*c == '/' && c[1] != '/') }
matched = !fnmatch(n->pattern, c+1, 0); return 0;
} while((n = n->next) != NULL && !matched); }
return(matched);
void exclude_clear() {
struct exclude *n, *l;
for(n=excludes; n!=NULL; n=l) {
l = n->next;
free(n);
}
excludes = NULL;
} }

34
src/exclude.h Normal file
View file

@ -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

View file

@ -24,6 +24,7 @@
*/ */
#include "ncdu.h" #include "ncdu.h"
#include "exclude.h"
/* check ncdu.h what these are for */ /* check ncdu.h what these are for */
struct dir *dat; struct dir *dat;
@ -85,8 +86,8 @@ void argv_parse(int argc, char **argv, char *dir) {
exit(1); exit(1);
} }
else if(strcmp(argv[i], "--exclude") == 0) else if(strcmp(argv[i], "--exclude") == 0)
addExclude(argv[++i]); exclude_add(argv[++i]);
else if(addExcludeFile(argv[++i])) { else if(exclude_addfile(argv[++i])) {
printf("Can't open %s: %s\n", argv[i], strerror(errno)); printf("Can't open %s: %s\n", argv[i], strerror(errno));
exit(1); exit(1);
} }
@ -152,6 +153,7 @@ int main(int argc, char **argv) {
erase(); erase();
refresh(); refresh();
endwin(); endwin();
exclude_clear();
return 0; return 0;
} }

View file

@ -179,7 +179,3 @@ void showBrowser(void);
void showHelp(void); void showHelp(void);
/* delete.c */ /* delete.c */
struct dir *showDelete(struct dir *); struct dir *showDelete(struct dir *);
/* exclude.c */
void addExclude(char *);
int addExcludeFile(char *);
int matchExclude(char *);