sfm

Simple file manager
git clone git://git.christosmarg.xyz/sfm.git
Log | Files | Refs | README | LICENSE

commit 7b799ec3119c863e045e68e830f99155546f3ecd
parent 9dfbf13d7a474d642b656f1fcede06a6674c1378
Author: Christos Margiolis <christos@margiolis.net>
Date:   Wed, 20 Jan 2021 00:15:26 +0200

added a simple getopt(3) and improved die messages

Diffstat:
Msfm.c | 55+++++++++++++++++++++++++++++++++++++++----------------
1 file changed, 39 insertions(+), 16 deletions(-)

diff --git a/sfm.c b/sfm.c @@ -179,8 +179,9 @@ static void escape(char *, const char *); static void xdelay(useconds_t); static void echdir(const char *); static void *emalloc(size_t); -static void die(const char *, ...); static void cleanup(void); +static void usage(void); +static void die(const char *, ...); /* useful strings */ static const char *cmds[] = { @@ -268,7 +269,7 @@ cursesinit(void) int i = 1; if (!initscr()) - die("sfm: initscr failed"); + die("initscr:"); noecho(); cbreak(); @@ -292,7 +293,7 @@ entcount(char *path) /* FIXME: repating code, do something! */ if ((dir = opendir(path)) == NULL) - die("sfm: opendir failed"); + die("opendir:"); while ((dent = readdir(dir)) != NULL) { if (!strcmp(dent->d_name, "..") || !strcmp(dent->d_name, ".")) @@ -319,7 +320,7 @@ entget(char *path, ulong n) ents = emalloc(n * sizeof(Entry)); if ((dir = opendir(path)) == NULL) - die("sfm: opendir failed"); + die("opendir:"); while ((dent = readdir(dir)) != NULL) { if (!strcmp(dent->d_name, "..") || !strcmp(dent->d_name, ".")) @@ -850,11 +851,25 @@ emalloc(size_t nb) void *p; if ((p = malloc(nb)) == NULL) - die("sfm: emalloc:"); + die("emalloc:"); return p; } static void +cleanup(void) +{ + entcleanup(); + free(win); + endwin(); +} + +static void +usage(void) +{ + die("usage: %s [-Hi]"); +} + +static void die(const char *fmt, ...) { va_list args; @@ -872,14 +887,6 @@ die(const char *fmt, ...) exit(EXIT_FAILURE); } -static void -cleanup(void) -{ - entcleanup(); - free(win); - endwin(); -} - int main(int argc, char *argv[]) { @@ -895,7 +902,23 @@ main(int argc, char *argv[]) sortfn = namecmp; if (!setlocale(LC_ALL, "")) - die("sfm: no locale support"); + die("setlocale:"); + + while ((ch = getopt(argc, argv, "Hi")) != -1) { + switch (ch) { + case 'H': + f_showall = 1; + break; + case 'i': + f_info = 1; + break; + case '?': /* FALLTHROUGH */ + default: + usage(); + } + } + argc -= optind; + argv += optind; cursesinit(); @@ -904,12 +927,12 @@ main(int argc, char *argv[]) if (f_redraw) { if ((curdir = getcwd(cwd, sizeof(cwd))) == NULL) - die("sfm: can't get cwd"); + die("getcwd:"); entcleanup(); win->nents = entcount(curdir); if ((win->ents = entget(curdir, win->nents)) == NULL) - die("sfm: can't get entries"); + die("entget:"); f_redraw = 0; refresh();