1
1

Removed stcalc and used several global variables instead

Considering it's now all internal in calc.c, anyway.
Этот коммит содержится в:
Yorhel 2009-04-18 14:36:24 +02:00
родитель 372f69853c
Коммит 4a751f0564

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

@ -61,20 +61,20 @@
#endif #endif
/* external vars */
int calc_delay = 100; int calc_delay = 100;
char calc_smfs = 0; char calc_smfs = 0;
struct { /* global vars for internal use */
char err; /* 1/0, error or not */ char failed; /* 1 on fatal error */
char cur[PATH_MAX]; /* current dir/item */ char curpath[PATH_MAX]; /* last lstat()'ed item */
char lasterr[PATH_MAX]; /* last unreadable dir/item */ char lasterr[PATH_MAX]; /* last unreadable dir/item */
char errmsg[128]; /* error message, when err=1 */ char errmsg[128]; /* error message, when err=1 */
struct dir *parent; /* parent directory for the calculation */ struct dir *root; /* root directory struct we're calculating */
struct dir *orig; /* original directory, when recalculating */ struct dir *orig; /* original directory, when recalculating */
dev_t curdev; /* current device we're calculating on */ dev_t curdev; /* current device we're calculating on */
suseconds_t lastupdate; /* time of the last screen update */ suseconds_t lastupdate; /* time of the last screen update */
int anpos; /* position of the animation string */ int anpos; /* position of the animation string */
} stcalc;
/* My own implementation of realpath() /* My own implementation of realpath()
@ -214,7 +214,7 @@ int calc_item(struct dir *par, char *path, char *name) {
/* lstat */ /* lstat */
strcpy(tmp, path); strcpy(tmp, path);
strcat(tmp, name); strcat(tmp, name);
strcpy(stcalc.cur, tmp); strcpy(curpath, tmp);
if(lstat(tmp, &fs)) { if(lstat(tmp, &fs)) {
d->flags |= FF_ERR; d->flags |= FF_ERR;
return 0; return 0;
@ -224,7 +224,7 @@ int calc_item(struct dir *par, char *path, char *name) {
if(exclude_match(tmp)) if(exclude_match(tmp))
d->flags |= FF_EXL; d->flags |= FF_EXL;
if(calc_smfs && stcalc.curdev != fs.st_dev) if(calc_smfs && curdev != fs.st_dev)
d->flags |= FF_OTHFS; d->flags |= FF_OTHFS;
/* determine type of this item */ /* determine type of this item */
@ -264,7 +264,7 @@ int calc_dir(struct dir *dest, char *path) {
/* open directory */ /* open directory */
if((dir = opendir(path)) == NULL) { if((dir = opendir(path)) == NULL) {
strcpy(stcalc.lasterr, path); strcpy(lasterr, path);
dest->flags |= FF_ERR; dest->flags |= FF_ERR;
t = dest; t = dest;
while((t = t->parent) != NULL) while((t = t->parent) != NULL)
@ -322,32 +322,32 @@ void calc_draw_progress() {
char ani[15]; char ani[15];
int i; int i;
nccreate(10, 60, stcalc.orig ? "Calculating..." : "Recalculating..."); nccreate(10, 60, orig ? "Calculating..." : "Recalculating...");
ncprint(2, 2, "Total items: %-8d size: %s", ncprint(2, 2, "Total items: %-8d size: %s",
stcalc.parent->items, formatsize(stcalc.parent->size)); root->items, formatsize(root->size));
ncprint(3, 2, "Current dir: %s", cropstr(stcalc.cur, 43)); ncprint(3, 2, "Current dir: %s", cropstr(curpath, 43));
ncaddstr(8, 43, "Press q to quit"); ncaddstr(8, 43, "Press q to quit");
/* show warning if we couldn't open a dir */ /* show warning if we couldn't open a dir */
if(stcalc.lasterr[0] != '\0') { if(lasterr[0] != '\0') {
attron(A_BOLD); attron(A_BOLD);
ncaddstr(5, 2, "Warning:"); ncaddstr(5, 2, "Warning:");
attroff(A_BOLD); attroff(A_BOLD);
ncprint(5, 11, "could not open %-32s", cropstr(stcalc.lasterr, 32)); ncprint(5, 11, "could not open %-32s", cropstr(lasterr, 32));
ncaddstr(6, 3, "some directory sizes may not be correct"); ncaddstr(6, 3, "some directory sizes may not be correct");
} }
/* animation - but only if the screen refreshes more than or once every second */ /* animation - but only if the screen refreshes more than or once every second */
if(calc_delay <= 1000) { if(calc_delay <= 1000) {
if(++stcalc.anpos == 28) if(++anpos == 28)
stcalc.anpos = 0; anpos = 0;
strcpy(ani, " "); strcpy(ani, " ");
if(stcalc.anpos < 14) if(anpos < 14)
for(i=0; i<=stcalc.anpos; i++) for(i=0; i<=anpos; i++)
ani[i] = antext[i]; ani[i] = antext[i];
else else
for(i=13; i>stcalc.anpos-14; i--) for(i=13; i>anpos-14; i--)
ani[i] = antext[i]; ani[i] = antext[i];
} else } else
strcpy(ani, antext); strcpy(ani, antext);
@ -371,17 +371,17 @@ void calc_draw_error(char *cur, char *msg) {
int calc_draw() { int calc_draw() {
struct timeval tv; struct timeval tv;
if(stcalc.err) { if(failed) {
calc_draw_error(stcalc.cur, stcalc.errmsg); calc_draw_error(curpath, errmsg);
return 0; return 0;
} }
/* should we really draw the screen again? */ /* should we really draw the screen again? */
gettimeofday(&tv, (void *)NULL); gettimeofday(&tv, (void *)NULL);
tv.tv_usec = (1000*(tv.tv_sec % 1000) + (tv.tv_usec / 1000)) / calc_delay; tv.tv_usec = (1000*(tv.tv_sec % 1000) + (tv.tv_usec / 1000)) / calc_delay;
if(stcalc.lastupdate != tv.tv_usec) { if(lastupdate != tv.tv_usec) {
calc_draw_progress(); calc_draw_progress();
stcalc.lastupdate = tv.tv_usec; lastupdate = tv.tv_usec;
return 0; return 0;
} }
return 1; return 1;
@ -389,7 +389,7 @@ int calc_draw() {
int calc_key(int ch) { int calc_key(int ch) {
if(stcalc.err) if(failed)
return 1; return 1;
if(ch == 'q') if(ch == 'q')
return 1; return 1;
@ -403,9 +403,9 @@ void calc_process() {
struct dir *t; struct dir *t;
/* check root directory */ /* check root directory */
if(rpath(stcalc.cur, tmp) == NULL || lstat(tmp, &fs) != 0 || !S_ISDIR(fs.st_mode)) { if(rpath(curpath, tmp) == NULL || lstat(tmp, &fs) != 0 || !S_ISDIR(fs.st_mode)) {
stcalc.err = 1; failed = 1;
strcpy(stcalc.errmsg, "Directory not found"); strcpy(errmsg, "Directory not found");
goto fail; goto fail;
} }
@ -414,62 +414,60 @@ void calc_process() {
t->size = fs.st_blocks * S_BLKSIZE; t->size = fs.st_blocks * S_BLKSIZE;
t->asize = fs.st_size; t->asize = fs.st_size;
t->flags |= FF_DIR; t->flags |= FF_DIR;
if(stcalc.orig) { if(orig) {
t->parent = stcalc.orig->parent; t->parent = orig->parent;
t->next = stcalc.orig->next; t->next = orig->next;
} }
t->name = (char *) malloc(strlen(tmp)+1); t->name = (char *) malloc(strlen(tmp)+1);
strcpy(t->name, stcalc.orig ? stcalc.orig->name : tmp); strcpy(t->name, orig ? orig->name : tmp);
stcalc.parent = t; root = t;
stcalc.curdev = fs.st_dev; curdev = fs.st_dev;
/* update parents, if any */ /* update parents, if any */
if(stcalc.orig) { if(orig) {
for(t=t->parent; t!=NULL; t=t->parent) { for(t=t->parent; t!=NULL; t=t->parent) {
t->size += stcalc.parent->size; t->size += root->size;
t->asize += stcalc.parent->asize; t->asize += root->asize;
t->items++; t->items++;
} }
} }
/* start calculating */ /* start calculating */
if(!calc_dir(stcalc.parent, tmp) && !stcalc.err) { if(!calc_dir(root, tmp) && !failed) {
pstate = ST_BROWSE; pstate = ST_BROWSE;
stbrowse.cur = stcalc.parent->sub; stbrowse.cur = root->sub;
/* update references and free original item */ /* update references and free original item */
if(stcalc.orig) { if(orig) {
if(stcalc.orig->parent) { if(orig->parent) {
t = stcalc.orig->parent->sub; t = orig->parent->sub;
if(t == stcalc.orig) if(t == orig)
stcalc.orig->parent->sub = stcalc.parent; orig->parent->sub = root;
else if(t != NULL) else if(t != NULL)
for(; t->next!=NULL; t=t->next) for(; t->next!=NULL; t=t->next)
if(t->next == stcalc.orig) if(t->next == orig)
t->next = stcalc.parent; t->next = root;
} }
freedir(stcalc.orig); freedir(orig);
} }
return; return;
} }
/* something went wrong... */ /* something went wrong... */
freedir(stcalc.parent); freedir(root);
fail: fail:
while(stcalc.err && !input_handle(0)) while(failed && !input_handle(0))
; ;
pstate = stcalc.orig ? ST_BROWSE : ST_QUIT; pstate = orig ? ST_BROWSE : ST_QUIT;
return; return;
} }
void calc_init(char *dir, struct dir *orig) { void calc_init(char *dir, struct dir *org) {
stcalc.err = 0; failed = anpos = lasterr[0] = 0;
stcalc.lastupdate = 999; lastupdate = 999;
stcalc.lasterr[0] = 0; orig = org;
stcalc.anpos = 0; strcpy(curpath, dir);
stcalc.orig = orig;
strcpy(stcalc.cur, dir);
pstate = ST_CALC; pstate = ST_CALC;
} }