Skip to content
Snippets Groups Projects
Commit 6c0a56a2 authored by Yorhel's avatar Yorhel
Browse files

Rewrote exclude.c to the new style

parent cc8cc992
No related branches found
No related tags found
No related merge requests found
...@@ -2,4 +2,4 @@ bin_PROGRAMS = ncdu ...@@ -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
...@@ -25,6 +25,7 @@ ...@@ -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) { ...@@ -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)
......
...@@ -24,43 +24,36 @@ ...@@ -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 exclude_add(char *pat) {
struct exclude **n;
void addExclude(char *pat) { n = &excludes;
struct exclude *n; while(*n != NULL)
n = &((*n)->next);
n = (struct exclude *) malloc(sizeof(struct exclude)); *n = (struct exclude *) calloc(1, sizeof(struct exclude));
n->pattern = (char *) malloc(strlen(pat)+1); (*n)->pattern = (char *) malloc(strlen(pat)+1);
strcpy(n->pattern, pat); strcpy((*n)->pattern, pat);
n->next = NULL;
if(excludes == NULL) {
excludes = n;
last = excludes;
} 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) { ...@@ -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;
} }
/* 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
...@@ -24,6 +24,7 @@ ...@@ -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) { ...@@ -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) { ...@@ -152,6 +153,7 @@ int main(int argc, char **argv) {
erase(); erase();
refresh(); refresh();
endwin(); endwin();
exclude_clear();
return 0; return 0;
} }
......
...@@ -179,7 +179,3 @@ void showBrowser(void); ...@@ -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 *);
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment