diff --git a/Makefile b/Makefile index a37b476..d687ebb 100644 --- a/Makefile +++ b/Makefile @@ -3,14 +3,13 @@ PREFIX= CC=gcc O=-O2 CFLAGS=-std=c99 -D_XOPEN_SOURCE=500 -D_GNU_SOURCE -D_POSIX_C_SOURCE=200809L -CWARN=-Wall -Wpedantic -Wno-unknown-pragmas -#CWARN += -fsanitize=address -fno-omit-frame-pointer +CWARN=-Wall -Wpedantic -Wextra -Wno-unknown-pragmas -Wno-missing-field-initializers\ + -Wno-padded -Wsign-conversion -Wno-missing-noreturn -Wno-cast-qual -Wtype-limits +#CFLAGS += -fsanitize=address -fno-omit-frame-pointer G= ifeq ($(shell uname),Darwin) CFLAGS += -D_DARWIN_C_SOURCE - CWARN += -Weverything -Wno-missing-field-initializers -Wno-padded\ - -Wno-missing-noreturn -Wno-cast-qual endif ifneq (, $(SH)) diff --git a/bb.c b/bb.c index c5625b5..3359fe3 100644 --- a/bb.c +++ b/bb.c @@ -64,7 +64,7 @@ void cleanup_and_raise(int sig) raise(sig); // This code will only ever be run if sig is SIGTSTP/SIGSTOP, otherwise, raise() won't return: 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); } @@ -201,7 +201,7 @@ void handle_next_key_binding(bb_t *bb) } while (key == -1); 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) { binding = &bindings[i]; 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 * 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; 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 *slash = strrchr(path, '/'); if (slash) { - strncpy(pbuf, path, (slash - path)); + strncpy(pbuf, path, (size_t)(slash - path)); normalize_path(bb->path, pbuf, pbuf); strcat(pbuf, slash); } else { @@ -472,7 +472,7 @@ int populate_files(bb_t *bb, const char *path) if ((size_t)bb->nfiles + 1 > space) bb->files = memcheck(realloc(bb->files, (space += 100)*sizeof(void*))); // 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); entry->index = bb->nfiles; bb->files[bb->nfiles++] = entry; @@ -490,11 +490,11 @@ int populate_files(bb_t *bb, const char *path) if (samedir) { set_scroll(bb, old_scroll); 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); } } else { - entry_t *p = load_entry(bb, prev, 0); + entry_t *p = load_entry(bb, prev); if (p) { if (IS_VIEWED(p)) set_cursor(bb, p->index); else try_free_entry(p); @@ -509,18 +509,18 @@ int populate_files(bb_t *bb, const char *path) void print_bindings(int fd) { 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) { 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); write(fd, buf, strlen(buf)); continue; } - int to_skip = -1; + size_t shared = 0; 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, ", "); - ++to_skip; + ++shared; int key = bindings[j].key; p = bkeyname(key, p); } @@ -531,7 +531,7 @@ void print_bindings(int fd) bindings[i].description); write(fd, buf2, strlen(buf2)); write(fd, "\033[0m\n", strlen("\033[0m\n")); - i += to_skip; + i += shared - 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 keyval = bkeywithname(key); 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)) continue; 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 char pbuf[PATH_MAX]; normalize_path(bb->path, value, pbuf); - entry_t *e = load_entry(bb, pbuf, 0); + entry_t *e = load_entry(bb, pbuf); if (e) { set_selected(bb, e, 0); return; @@ -635,7 +635,7 @@ void run_bbcmd(bb_t *bb, const char *cmd) init_term(); dirty = 1; } else if (matches_cmd(cmd, "goto:")) { // +goto: - entry_t *e = load_entry(bb, value, 1); + entry_t *e = load_entry(bb, value); if (!e) { warn("Could not find file to go to: \"%s\".", value); return; @@ -701,7 +701,7 @@ void run_bbcmd(bb_t *bb, const char *cmd) for (int i = 0; i < bb->nfiles; i++) set_selected(bb, bb->files[i], 1); } else { - entry_t *e = load_entry(bb, value, 1); + entry_t *e = load_entry(bb, value); if (e) set_selected(bb, e, 1); } } 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 if (!value && !bb->nfiles) return; 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) { warn("Could not find file to toggle: \"%s\".", value); return; @@ -955,7 +955,7 @@ int run_script(bb_t *bb, const char *cmd) fclose(tty_out); tty_out = NULL; fclose(tty_in); tty_in = NULL; 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; args[i++] = SH; args[i++] = "-c"; @@ -1248,8 +1248,8 @@ int main(int argc, char *argv[]) struct sigaction sa_winch = {.sa_handler = &update_term_size}; sigaction(SIGWINCH, &sa_winch, NULL); int signals[] = {SIGTERM, SIGINT, SIGXCPU, SIGXFSZ, SIGVTALRM, SIGPROF, SIGSEGV, SIGTSTP}; - struct sigaction sa = {.sa_handler = &cleanup_and_raise, .sa_flags = SA_NODEFER | SA_RESETHAND}; - for (int i = 0; i < sizeof(signals)/sizeof(signals[0]); i++) + struct sigaction sa = {.sa_handler = &cleanup_and_raise, .sa_flags = (int)(SA_NODEFER | SA_RESETHAND)}; + for (size_t i = 0; i < sizeof(signals)/sizeof(signals[0]); i++) sigaction(signals[i], &sa, NULL); write(cmdfd, "\0", 1); diff --git a/bb.h b/bb.h index 9752cd8..787b2e5 100644 --- a/bb.h +++ b/bb.h @@ -176,10 +176,10 @@ typedef struct proc_s { #define MAX_BINDINGS 1024 -binding_t bindings[MAX_BINDINGS]; +static binding_t bindings[MAX_BINDINGS]; // Column widths and titles: -const column_t columns[] = { +static const column_t columns[] = { ['*'] = {2, "*"}, ['a'] = {21, " Accessed"}, ['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 init_term(void); 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 void* memcheck(void *p); static char* normalize_path(const char *root, const char *path, char *pbuf); @@ -327,7 +327,7 @@ PICK ";\n" #endif ; -const char *runstartup = +static const char *runstartup = "[ ! \"$XDG_CONFIG_HOME\" ] && XDG_CONFIG_HOME=~/.config;\n" "[ ! \"$sysconfdir\" ] && sysconfdir=/etc;\n" "for path in \"$XDG_CONFIG_HOME/bb\" \"$sysconfdir/xdg/bb\" .; do\n" diff --git a/bterm.h b/bterm.h index c61d465..1ef7d38 100644 --- a/bterm.h +++ b/bterm.h @@ -130,7 +130,7 @@ static inline int nextchar(int fd) 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)) *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_SHIFT) buf = stpcpy(buf, "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) { 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} }; 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) 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) { modifiers |= modnames[i].modifier; name += strlen(modnames[i].prefix);