Bunch of minor pedantic cleanups to get rid of warnings and tidy up the

code.
This commit is contained in:
Bruce Hill 2019-11-24 22:11:54 -08:00
parent 6c98653cca
commit 56cb3a2d66
4 changed files with 31 additions and 32 deletions

View File

@ -3,14 +3,13 @@ PREFIX=
CC=gcc CC=gcc
O=-O2 O=-O2
CFLAGS=-std=c99 -D_XOPEN_SOURCE=500 -D_GNU_SOURCE -D_POSIX_C_SOURCE=200809L CFLAGS=-std=c99 -D_XOPEN_SOURCE=500 -D_GNU_SOURCE -D_POSIX_C_SOURCE=200809L
CWARN=-Wall -Wpedantic -Wno-unknown-pragmas CWARN=-Wall -Wpedantic -Wextra -Wno-unknown-pragmas -Wno-missing-field-initializers\
#CWARN += -fsanitize=address -fno-omit-frame-pointer -Wno-padded -Wsign-conversion -Wno-missing-noreturn -Wno-cast-qual -Wtype-limits
#CFLAGS += -fsanitize=address -fno-omit-frame-pointer
G= G=
ifeq ($(shell uname),Darwin) ifeq ($(shell uname),Darwin)
CFLAGS += -D_DARWIN_C_SOURCE CFLAGS += -D_DARWIN_C_SOURCE
CWARN += -Weverything -Wno-missing-field-initializers -Wno-padded\
-Wno-missing-noreturn -Wno-cast-qual
endif endif
ifneq (, $(SH)) ifneq (, $(SH))

40
bb.c
View File

@ -64,7 +64,7 @@ void cleanup_and_raise(int sig)
raise(sig); raise(sig);
// This code will only ever be run if sig is SIGTSTP/SIGSTOP, otherwise, raise() won't return: // This code will only ever be run if sig is SIGTSTP/SIGSTOP, otherwise, raise() won't return:
init_term(); init_term();
struct sigaction sa = {.sa_handler = &cleanup_and_raise, .sa_flags = SA_NODEFER | SA_RESETHAND}; struct sigaction sa = {.sa_handler = &cleanup_and_raise, .sa_flags = (int)(SA_NODEFER | SA_RESETHAND)};
sigaction(sig, &sa, NULL); sigaction(sig, &sa, NULL);
} }
@ -201,7 +201,7 @@ void handle_next_key_binding(bb_t *bb)
} while (key == -1); } while (key == -1);
binding = NULL; binding = NULL;
for (int i = 0; bindings[i].script && i < sizeof(bindings)/sizeof(bindings[0]); i++) { for (size_t i = 0; bindings[i].script && i < sizeof(bindings)/sizeof(bindings[0]); i++) {
if (key == bindings[i].key) { if (key == bindings[i].key) {
binding = &bindings[i]; binding = &bindings[i];
break; break;
@ -285,7 +285,7 @@ static int is_simple_bbcmd(const char *s)
* Warning: this does not deduplicate entries, and it's best if there aren't * Warning: this does not deduplicate entries, and it's best if there aren't
* duplicate entries hanging around. * duplicate entries hanging around.
*/ */
entry_t* load_entry(bb_t *bb, const char *path, int clear_dots) entry_t* load_entry(bb_t *bb, const char *path)
{ {
struct stat linkedstat, filestat; struct stat linkedstat, filestat;
if (!path || !path[0]) return NULL; if (!path || !path[0]) return NULL;
@ -293,7 +293,7 @@ entry_t* load_entry(bb_t *bb, const char *path, int clear_dots)
char pbuf[PATH_MAX] = {0}; char pbuf[PATH_MAX] = {0};
char *slash = strrchr(path, '/'); char *slash = strrchr(path, '/');
if (slash) { if (slash) {
strncpy(pbuf, path, (slash - path)); strncpy(pbuf, path, (size_t)(slash - path));
normalize_path(bb->path, pbuf, pbuf); normalize_path(bb->path, pbuf, pbuf);
strcat(pbuf, slash); strcat(pbuf, slash);
} else { } else {
@ -472,7 +472,7 @@ int populate_files(bb_t *bb, const char *path)
if ((size_t)bb->nfiles + 1 > space) if ((size_t)bb->nfiles + 1 > space)
bb->files = memcheck(realloc(bb->files, (space += 100)*sizeof(void*))); bb->files = memcheck(realloc(bb->files, (space += 100)*sizeof(void*)));
// Don't normalize path so we can get "." and ".." // Don't normalize path so we can get "." and ".."
entry_t *entry = load_entry(bb, dp->d_name, 0); entry_t *entry = load_entry(bb, dp->d_name);
if (!entry) err("Failed to load entry: '%s'", dp->d_name); if (!entry) err("Failed to load entry: '%s'", dp->d_name);
entry->index = bb->nfiles; entry->index = bb->nfiles;
bb->files[bb->nfiles++] = entry; bb->files[bb->nfiles++] = entry;
@ -490,11 +490,11 @@ int populate_files(bb_t *bb, const char *path)
if (samedir) { if (samedir) {
set_scroll(bb, old_scroll); set_scroll(bb, old_scroll);
if (old_selected[0]) { if (old_selected[0]) {
entry_t *e = load_entry(bb, old_selected, 0); entry_t *e = load_entry(bb, old_selected);
if (e) set_cursor(bb, e->index); if (e) set_cursor(bb, e->index);
} }
} else { } else {
entry_t *p = load_entry(bb, prev, 0); entry_t *p = load_entry(bb, prev);
if (p) { if (p) {
if (IS_VIEWED(p)) set_cursor(bb, p->index); if (IS_VIEWED(p)) set_cursor(bb, p->index);
else try_free_entry(p); else try_free_entry(p);
@ -509,18 +509,18 @@ int populate_files(bb_t *bb, const char *path)
void print_bindings(int fd) void print_bindings(int fd)
{ {
char buf[1000], buf2[1024]; char buf[1000], buf2[1024];
for (int i = 0; bindings[i].script && i < sizeof(bindings)/sizeof(bindings[0]); i++) { for (size_t i = 0; bindings[i].script && i < sizeof(bindings)/sizeof(bindings[0]); i++) {
if (bindings[i].key == -1) { if (bindings[i].key == -1) {
const char *label = bindings[i].description; const char *label = bindings[i].description;
sprintf(buf, "\n\033[33;1;4m\033[%dG%s\033[0m\n", (winsize.ws_col-(int)strlen(label))/2, label); sprintf(buf, "\n\033[33;1;4m\033[%dG%s\033[0m\n", (winsize.ws_col-(int)strlen(label))/2, label);
write(fd, buf, strlen(buf)); write(fd, buf, strlen(buf));
continue; continue;
} }
int to_skip = -1; size_t shared = 0;
char *p = buf; char *p = buf;
for (int j = i; bindings[j].script && strcmp(bindings[j].description, bindings[i].description) == 0; j++) { for (size_t j = i; bindings[j].script && strcmp(bindings[j].description, bindings[i].description) == 0; j++) {
if (j > i) p = stpcpy(p, ", "); if (j > i) p = stpcpy(p, ", ");
++to_skip; ++shared;
int key = bindings[j].key; int key = bindings[j].key;
p = bkeyname(key, p); p = bkeyname(key, p);
} }
@ -531,7 +531,7 @@ void print_bindings(int fd)
bindings[i].description); bindings[i].description);
write(fd, buf2, strlen(buf2)); write(fd, buf2, strlen(buf2));
write(fd, "\033[0m\n", strlen("\033[0m\n")); write(fd, "\033[0m\n", strlen("\033[0m\n"));
i += to_skip; i += shared - 1;
} }
write(fd, "\n", 1); write(fd, "\n", 1);
} }
@ -574,7 +574,7 @@ void run_bbcmd(bb_t *bb, const char *cmd)
int is_section = strcmp(key, "Section") == 0; int is_section = strcmp(key, "Section") == 0;
int keyval = bkeywithname(key); int keyval = bkeywithname(key);
if (keyval == -1 && !is_section) continue; if (keyval == -1 && !is_section) continue;
for (int i = 0; i < sizeof(bindings)/sizeof(bindings[0]); i++) { for (size_t i = 0; i < sizeof(bindings)/sizeof(bindings[0]); i++) {
if (bindings[i].script && (bindings[i].key != keyval || is_section)) if (bindings[i].script && (bindings[i].key != keyval || is_section))
continue; continue;
binding_t binding = {keyval, memcheck(strdup(script)), binding_t binding = {keyval, memcheck(strdup(script)),
@ -599,7 +599,7 @@ void run_bbcmd(bb_t *bb, const char *cmd)
} else if (matches_cmd(cmd, "deselect:")) { // +deselect } else if (matches_cmd(cmd, "deselect:")) { // +deselect
char pbuf[PATH_MAX]; char pbuf[PATH_MAX];
normalize_path(bb->path, value, pbuf); normalize_path(bb->path, value, pbuf);
entry_t *e = load_entry(bb, pbuf, 0); entry_t *e = load_entry(bb, pbuf);
if (e) { if (e) {
set_selected(bb, e, 0); set_selected(bb, e, 0);
return; return;
@ -635,7 +635,7 @@ void run_bbcmd(bb_t *bb, const char *cmd)
init_term(); init_term();
dirty = 1; dirty = 1;
} else if (matches_cmd(cmd, "goto:")) { // +goto: } else if (matches_cmd(cmd, "goto:")) { // +goto:
entry_t *e = load_entry(bb, value, 1); entry_t *e = load_entry(bb, value);
if (!e) { if (!e) {
warn("Could not find file to go to: \"%s\".", value); warn("Could not find file to go to: \"%s\".", value);
return; return;
@ -701,7 +701,7 @@ void run_bbcmd(bb_t *bb, const char *cmd)
for (int i = 0; i < bb->nfiles; i++) for (int i = 0; i < bb->nfiles; i++)
set_selected(bb, bb->files[i], 1); set_selected(bb, bb->files[i], 1);
} else { } else {
entry_t *e = load_entry(bb, value, 1); entry_t *e = load_entry(bb, value);
if (e) set_selected(bb, e, 1); if (e) set_selected(bb, e, 1);
} }
} else if (matches_cmd(cmd, "sort:")) { // +sort: } else if (matches_cmd(cmd, "sort:")) { // +sort:
@ -712,7 +712,7 @@ void run_bbcmd(bb_t *bb, const char *cmd)
} else if (matches_cmd(cmd, "toggle:") || matches_cmd(cmd, "toggle")) { // +toggle } else if (matches_cmd(cmd, "toggle:") || matches_cmd(cmd, "toggle")) { // +toggle
if (!value && !bb->nfiles) return; if (!value && !bb->nfiles) return;
if (!value) value = bb->files[bb->cursor]->fullname; if (!value) value = bb->files[bb->cursor]->fullname;
entry_t *e = load_entry(bb, value, 1); entry_t *e = load_entry(bb, value);
if (!e) { if (!e) {
warn("Could not find file to toggle: \"%s\".", value); warn("Could not find file to toggle: \"%s\".", value);
return; return;
@ -955,7 +955,7 @@ int run_script(bb_t *bb, const char *cmd)
fclose(tty_out); tty_out = NULL; fclose(tty_out); tty_out = NULL;
fclose(tty_in); tty_in = NULL; fclose(tty_in); tty_in = NULL;
setpgid(0, 0); setpgid(0, 0);
char **args = memcheck(calloc(4 + bb->nselected + 1, sizeof(char*))); char **args = memcheck(calloc(4 + (size_t)bb->nselected + 1, sizeof(char*)));
int i = 0; int i = 0;
args[i++] = SH; args[i++] = SH;
args[i++] = "-c"; args[i++] = "-c";
@ -1248,8 +1248,8 @@ int main(int argc, char *argv[])
struct sigaction sa_winch = {.sa_handler = &update_term_size}; struct sigaction sa_winch = {.sa_handler = &update_term_size};
sigaction(SIGWINCH, &sa_winch, NULL); sigaction(SIGWINCH, &sa_winch, NULL);
int signals[] = {SIGTERM, SIGINT, SIGXCPU, SIGXFSZ, SIGVTALRM, SIGPROF, SIGSEGV, SIGTSTP}; int signals[] = {SIGTERM, SIGINT, SIGXCPU, SIGXFSZ, SIGVTALRM, SIGPROF, SIGSEGV, SIGTSTP};
struct sigaction sa = {.sa_handler = &cleanup_and_raise, .sa_flags = SA_NODEFER | SA_RESETHAND}; struct sigaction sa = {.sa_handler = &cleanup_and_raise, .sa_flags = (int)(SA_NODEFER | SA_RESETHAND)};
for (int i = 0; i < sizeof(signals)/sizeof(signals[0]); i++) for (size_t i = 0; i < sizeof(signals)/sizeof(signals[0]); i++)
sigaction(signals[i], &sa, NULL); sigaction(signals[i], &sa, NULL);
write(cmdfd, "\0", 1); write(cmdfd, "\0", 1);

8
bb.h
View File

@ -176,10 +176,10 @@ typedef struct proc_s {
#define MAX_BINDINGS 1024 #define MAX_BINDINGS 1024
binding_t bindings[MAX_BINDINGS]; static binding_t bindings[MAX_BINDINGS];
// Column widths and titles: // Column widths and titles:
const column_t columns[] = { static const column_t columns[] = {
['*'] = {2, "*"}, ['*'] = {2, "*"},
['a'] = {21, " Accessed"}, ['a'] = {21, " Accessed"},
['c'] = {21, " Created"}, ['c'] = {21, " Created"},
@ -205,7 +205,7 @@ static int fputs_escaped(FILE *f, const char *str, const char *color);
static void handle_next_key_binding(bb_t *bb); static void handle_next_key_binding(bb_t *bb);
static void init_term(void); static void init_term(void);
static int is_simple_bbcmd(const char *s); static int is_simple_bbcmd(const char *s);
static entry_t* load_entry(bb_t *bb, const char *path, int clear_dots); static entry_t* load_entry(bb_t *bb, const char *path);
static inline int matches_cmd(const char *str, const char *cmd); static inline int matches_cmd(const char *str, const char *cmd);
static void* memcheck(void *p); static void* memcheck(void *p);
static char* normalize_path(const char *root, const char *path, char *pbuf); static char* normalize_path(const char *root, const char *path, char *pbuf);
@ -327,7 +327,7 @@ PICK ";\n"
#endif #endif
; ;
const char *runstartup = static const char *runstartup =
"[ ! \"$XDG_CONFIG_HOME\" ] && XDG_CONFIG_HOME=~/.config;\n" "[ ! \"$XDG_CONFIG_HOME\" ] && XDG_CONFIG_HOME=~/.config;\n"
"[ ! \"$sysconfdir\" ] && sysconfdir=/etc;\n" "[ ! \"$sysconfdir\" ] && sysconfdir=/etc;\n"
"for path in \"$XDG_CONFIG_HOME/bb\" \"$sysconfdir/xdg/bb\" .; do\n" "for path in \"$XDG_CONFIG_HOME/bb\" \"$sysconfdir/xdg/bb\" .; do\n"

View File

@ -130,7 +130,7 @@ static inline int nextchar(int fd)
return read(fd, &c, 1) == 1 ? c : -1; return read(fd, &c, 1) == 1 ? c : -1;
} }
static inline char nextnum(int fd, char c, int *n) static inline int nextnum(int fd, int c, int *n)
{ {
for (*n = 0; '0' <= c && c <= '9'; c = nextchar(fd)) for (*n = 0; '0' <= c && c <= '9'; c = nextchar(fd))
*n = 10*(*n) + (c - '0'); *n = 10*(*n) + (c - '0');
@ -296,7 +296,7 @@ char *bkeyname(int key, char *buf)
if (key & MOD_ALT) buf = stpcpy(buf, "Alt-"); if (key & MOD_ALT) buf = stpcpy(buf, "Alt-");
if (key & MOD_SHIFT) buf = stpcpy(buf, "Shift-"); if (key & MOD_SHIFT) buf = stpcpy(buf, "Shift-");
key &= ~(MOD_META | MOD_CTRL | MOD_ALT | MOD_SHIFT); key &= ~(MOD_META | MOD_CTRL | MOD_ALT | MOD_SHIFT);
for (int i = 0; i < sizeof(key_names)/sizeof(key_names[0]); i++) { for (size_t i = 0; i < sizeof(key_names)/sizeof(key_names[0]); i++) {
if (key_names[i].key == key) { if (key_names[i].key == key) {
return stpcpy(buf, key_names[i].name); return stpcpy(buf, key_names[i].name);
} }
@ -318,11 +318,11 @@ int bkeywithname(const char *name)
{"Super-", MOD_META}, {"Ctrl-", MOD_CTRL}, {"Alt-", MOD_ALT}, {"Shift-", MOD_SHIFT} {"Super-", MOD_META}, {"Ctrl-", MOD_CTRL}, {"Alt-", MOD_ALT}, {"Shift-", MOD_SHIFT}
}; };
check_names: check_names:
for (int i = 0; i < sizeof(key_names)/sizeof(key_names[0]); i++) { for (size_t i = 0; i < sizeof(key_names)/sizeof(key_names[0]); i++) {
if (strcmp(key_names[i].name, name) == 0) if (strcmp(key_names[i].name, name) == 0)
return modifiers | key_names[i].key; return modifiers | key_names[i].key;
} }
for (int i = 0; i < sizeof(modnames)/sizeof(modnames[0]); i++) { for (size_t i = 0; i < sizeof(modnames)/sizeof(modnames[0]); i++) {
if (strncmp(name, modnames[i].prefix, strlen(modnames[i].prefix)) == 0) { if (strncmp(name, modnames[i].prefix, strlen(modnames[i].prefix)) == 0) {
modifiers |= modnames[i].modifier; modifiers |= modnames[i].modifier;
name += strlen(modnames[i].prefix); name += strlen(modnames[i].prefix);