1
1

Rewrote exclude.c to the new style

Этот коммит содержится в:
Yorhel 2009-04-11 10:48:24 +02:00
родитель cc8cc99213
Коммит 6c0a56a26f
6 изменённых файлов: 76 добавлений и 43 удалений

Просмотреть файл

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

Просмотреть файл

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

Просмотреть файл

@ -24,43 +24,36 @@
*/
#include "ncdu.h"
#include "exclude.h"
struct exclude {
char *pattern;
struct exclude *next;
};
struct exclude *excludes = NULL,
*last = NULL;
} *excludes = NULL;
void addExclude(char *pat) {
struct exclude *n;
void exclude_add(char *pat) {
struct exclude **n;
n = (struct exclude *) malloc(sizeof(struct exclude));
n->pattern = (char *) malloc(strlen(pat)+1);
strcpy(n->pattern, pat);
n->next = NULL;
n = &excludes;
while(*n != NULL)
n = &((*n)->next);
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;
}

34
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

Просмотреть файл

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

Просмотреть файл

@ -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 *);