From 5243e2daf4b4b9389bb4b6740609b27a168d8ed2 Mon Sep 17 00:00:00 2001 From: Yorhel Date: Mon, 31 Oct 2011 15:09:12 +0100 Subject: Code cleanup: Use `static' where applicable. This should also allow compilers to generate better code. --- src/browser.c | 10 +++------ src/calc.c | 36 +++++++++++++++---------------- src/delete.c | 16 ++++++-------- src/dirlist.c | 12 +++++------ src/main.c | 14 ++++++------ src/path.c | 9 ++++---- src/util.c | 68 ++++++++++++++++++++++++++++------------------------------- 7 files changed, 77 insertions(+), 88 deletions(-) diff --git a/src/browser.c b/src/browser.c index 5df5f13..92bc4d7 100644 --- a/src/browser.c +++ b/src/browser.c @@ -30,15 +30,11 @@ #include -int graph = 1, - show_as = 0, - info_show = 0, - info_page = 0, - info_start = 0; +static int graph = 1, show_as = 0, info_show = 0, info_page = 0, info_start = 0; -void browse_draw_info(struct dir *dr) { +static void browse_draw_info(struct dir *dr) { struct dir *t; int i; @@ -90,7 +86,7 @@ void browse_draw_info(struct dir *dr) { } -void browse_draw_item(struct dir *n, int row) { +static void browse_draw_item(struct dir *n, int row) { char ct, dt, *size, gr[11]; int i, o; float pc; diff --git a/src/calc.c b/src/calc.c index 1088ba7..ed94909 100644 --- a/src/calc.c +++ b/src/calc.c @@ -46,27 +46,27 @@ char calc_smfs = 0; /* global vars for internal use */ -char failed; /* 1 on fatal error */ -char *curpath; /* last lstat()'ed item, used for excludes matching and display */ -char *lasterr; /* last unreadable dir/item */ -char errmsg[128]; /* error message, when failed=1 */ -struct dir *root; /* root directory struct we're calculating */ -struct dir *orig; /* original directory, when recalculating */ -dev_t curdev; /* current device we're calculating on */ -int anpos; /* position of the animation string */ -int curpathl = 0, lasterrl = 0; +static char failed; /* 1 on fatal error */ +static char *curpath; /* last lstat()'ed item, used for excludes matching and display */ +static char *lasterr; /* last unreadable dir/item */ +static char errmsg[128]; /* error message, when failed=1 */ +static struct dir *root; /* root directory struct we're calculating */ +static struct dir *orig; /* original directory, when recalculating */ +static dev_t curdev; /* current device we're calculating on */ +static int anpos; /* position of the animation string */ +static int curpathl = 0, lasterrl = 0; // Table of struct dir items with more than one link (in order to detect hard links) #define links_dir_hash(d) (kh_int64_hash_func((khint64_t)d->dev) ^ kh_int64_hash_func((khint64_t)d->ino)) #define links_dir_equal(a, b) ((a)->dev == (b)->dev && (a)->ino == (b)->ino) KHASH_INIT(hl, struct dir *, char, 0, links_dir_hash, links_dir_equal); -khash_t(hl) *links = NULL; +static khash_t(hl) *links = NULL; /* adds name to curpath */ -void calc_enterpath(char *name) { +static void calc_enterpath(char *name) { int n; n = strlen(curpath)+strlen(name)+2; @@ -81,7 +81,7 @@ void calc_enterpath(char *name) { /* removes last component from curpath */ -void calc_leavepath() { +static void calc_leavepath() { char *tmp; if((tmp = strrchr(curpath, '/')) == NULL) strcpy(curpath, "/"); @@ -93,7 +93,7 @@ void calc_leavepath() { /* recursively checks a dir structure for hard links and fills the lookup array */ -void calc_hlink_init(struct dir *d) { +static void calc_hlink_init(struct dir *d) { struct dir *t; for(t=d->sub; t!=NULL; t=t->next) @@ -108,7 +108,7 @@ void calc_hlink_init(struct dir *d) { /* checks an individual file and updates the flags and cicrular linked list, * also updates the sizes of the parent dirs */ -void calc_hlink_check(struct dir *d) { +static void calc_hlink_check(struct dir *d) { struct dir *t, *pt, *par; int i; @@ -144,7 +144,7 @@ void calc_hlink_check(struct dir *d) { } -int calc_item(struct dir *par, char *name) { +static int calc_item(struct dir *par, char *name) { struct dir *t, *d; struct stat fs; @@ -216,7 +216,7 @@ int calc_item(struct dir *par, char *name) { /* recursively walk through the directory tree, assumes path resides in the cwd */ -int calc_dir(struct dir *dest, char *name) { +static int calc_dir(struct dir *dest, char *name) { struct dir *t; DIR *dir; struct dirent *dr; @@ -295,7 +295,7 @@ int calc_dir(struct dir *dest, char *name) { } -void calc_draw_progress() { +static void calc_draw_progress() { static char antext[15] = "Calculating..."; char ani[15]; int i; @@ -333,7 +333,7 @@ void calc_draw_progress() { } -void calc_draw_error(char *cur, char *msg) { +static void calc_draw_error(char *cur, char *msg) { nccreate(7, 60, "Error!"); attron(A_BOLD); diff --git a/src/delete.c b/src/delete.c index d53c55e..c19b6e2 100644 --- a/src/delete.c +++ b/src/delete.c @@ -36,14 +36,12 @@ #define DS_FAILED 2 -struct dir *root, *nextsel, *curdir; -char noconfirm = 0, - ignoreerr = 0, - state, seloption; -int lasterrno; +static struct dir *root, *nextsel, *curdir; +static char noconfirm = 0, ignoreerr = 0, state, seloption; +static int lasterrno; -void delete_draw_confirm() { +static void delete_draw_confirm() { nccreate(6, 60, "Confirm delete"); ncprint(1, 2, "Are you sure you want to delete \"%s\"%c", @@ -68,7 +66,7 @@ void delete_draw_confirm() { } -void delete_draw_progress() { +static void delete_draw_progress() { nccreate(6, 60, "Deleting..."); ncaddstr(1, 2, cropstr(getpath(curdir), 47)); @@ -76,7 +74,7 @@ void delete_draw_progress() { } -void delete_draw_error() { +static void delete_draw_error() { nccreate(6, 60, "Error!"); ncprint(1, 2, "Can't delete %s:", cropstr(getpath(curdir), 42)); @@ -165,7 +163,7 @@ int delete_key(int ch) { } -int delete_dir(struct dir *dr) { +static int delete_dir(struct dir *dr) { struct dir *nxt, *cur; int r; diff --git a/src/dirlist.c b/src/dirlist.c index a4810d8..ba15907 100644 --- a/src/dirlist.c +++ b/src/dirlist.c @@ -39,8 +39,8 @@ int dirlist_sort_desc = 1, dirlist_hidden = 0; /* private state vars */ -struct dir dirlist_parent_alloc; -struct dir *head, *head_real, *selected, *top = NULL; +static struct dir dirlist_parent_alloc; +static struct dir *head, *head_real, *selected, *top = NULL; @@ -50,7 +50,7 @@ struct dir *head, *head_real, *selected, *top = NULL; -int dirlist_cmp(struct dir *x, struct dir *y) { +static int dirlist_cmp(struct dir *x, struct dir *y) { int r; /* dirs are always before files when that option is set */ @@ -90,7 +90,7 @@ int dirlist_cmp(struct dir *x, struct dir *y) { } -struct dir *dirlist_sort(struct dir *list) { +static struct dir *dirlist_sort(struct dir *list) { struct dir *p, *q, *e, *tail; int insize, nmerges, psize, qsize, i; @@ -142,7 +142,7 @@ struct dir *dirlist_sort(struct dir *list) { * - makes sure one, and only one, visible item is selected * - updates the dirlist_(maxs|maxa) values * - makes sure that the FF_BSEL bits are correct */ -void dirlist_fixup() { +static void dirlist_fixup() { struct dir *t; /* we're going to determine the selected items from the list itself, so reset this one */ @@ -225,7 +225,7 @@ struct dir *dirlist_next(struct dir *d) { } -struct dir *dirlist_prev(struct dir *d) { +static struct dir *dirlist_prev(struct dir *d) { if(!head || !d) return NULL; while((d = d->prev)) { diff --git a/src/main.c b/src/main.c index a0ab2c4..f2dd790 100644 --- a/src/main.c +++ b/src/main.c @@ -33,16 +33,16 @@ #include #include -int pstate; -int min_rows = 17, - min_cols = 60; +int pstate; int read_only = 0; -long update_delay = 100, - lastupdate = 999; +long update_delay = 100; + +static int min_rows = 17, min_cols = 60; +static long lastupdate = 999; -void screen_draw() { +static void screen_draw() { switch(pstate) { case ST_CALC: calc_draw(); break; case ST_BROWSE: browse_draw(); break; @@ -92,7 +92,7 @@ int input_handle(int wait) { /* parse command line */ -char *argv_parse(int argc, char **argv) { +static char *argv_parse(int argc, char **argv) { int i, j, len; char *dir = NULL; diff --git a/src/path.c b/src/path.c index c78cdac..d51694c 100644 --- a/src/path.c +++ b/src/path.c @@ -48,7 +48,7 @@ a pointer to a reversed array of components is stored in res and the number of components is returned. cur is modified, and res has to be free()d after use */ -int path_split(char *cur, char ***res) { +static int path_split(char *cur, char ***res) { char **old; int i, j, n; @@ -91,7 +91,7 @@ int path_split(char *cur, char ***res) { /* copies path and prepends cwd if needed, to ensure an absolute path return value has to be free()'d manually */ -char *path_absolute(const char *path) { +static char *path_absolute(const char *path) { int i, n; char *ret; @@ -125,8 +125,8 @@ char *path_absolute(const char *path) { /* NOTE: cwd and the memory cur points to are unreliable after calling this function */ -char *path_real_rec(char *cur, int *links) { - int i, j, n, tmpl, lnkl = 0; +static char *path_real_rec(char *cur, int *links) { + int i, n, tmpl, lnkl = 0; char **arr, *tmp, *lnk, *ret = NULL; tmpl = strlen(cur)+1; @@ -138,7 +138,6 @@ char *path_real_rec(char *cur, int *links) { /* re-create full path */ strcpy(tmp, "/"); if(i > 0) { - j = 1; lnkl = RPATH_CNKSZ; lnk = malloc(lnkl); if(chdir("/") < 0) diff --git a/src/util.c b/src/util.c index 640b18b..a1d7470 100644 --- a/src/util.c +++ b/src/util.c @@ -32,37 +32,30 @@ int winrows, wincols; int subwinr, subwinc; -char cropstrdat[4096]; -char formatsizedat[9]; /* "xxx.xMiB" */ -char fullsizedat[20]; /* max: 999.999.999.999.999 */ -char *getpathdat; -int getpathdatl = 0; - -struct dir **links; -int linksl = 0, linkst = 0; - char *cropstr(const char *from, int s) { + static char dat[4096]; int i, j, o = strlen(from); if(o < s) { - strcpy(cropstrdat, from); - return cropstrdat; + strcpy(dat, from); + return dat; } j=s/2-3; for(i=0; iparent) list[c++] = d; - getpathdat[0] = '\0'; + dat[0] = '\0'; while(c--) { if(list[c]->parent) - strcat(getpathdat, "/"); - strcat(getpathdat, list[c]->name); + strcat(dat, "/"); + strcat(dat, list[c]->name); } free(list); - return getpathdat; + return dat; } -- cgit v1.2.3