Made all conditionals more precise

This commit is contained in:
Bert Münnich 2011-09-29 12:43:36 +02:00
parent 22d4e991d5
commit 8dcf682de9
8 changed files with 238 additions and 236 deletions

View File

@ -53,7 +53,7 @@ bool it_quit(arg_t a) {
bool it_switch_mode(arg_t a) { bool it_switch_mode(arg_t a) {
if (mode == MODE_IMAGE) { if (mode == MODE_IMAGE) {
if (!tns.thumbs) if (tns.thumbs == NULL)
tns_init(&tns, filecnt, &win); tns_init(&tns, filecnt, &win);
img_close(&img, false); img_close(&img, false);
reset_timeout(reset_cursor); reset_timeout(reset_cursor);
@ -273,21 +273,19 @@ bool i_zoom(arg_t a) {
} }
bool i_fit_to_win(arg_t a) { bool i_fit_to_win(arg_t a) {
bool ret; bool ret = false;
if (mode == MODE_IMAGE) { if (mode == MODE_IMAGE) {
if ((ret = img_fit_win(&img))) if ((ret = img_fit_win(&img)))
img_center(&img); img_center(&img);
return ret;
} else {
return false;
} }
return ret;
} }
bool i_fit_to_img(arg_t a) { bool i_fit_to_img(arg_t a) {
int x, y; int x, y;
unsigned int w, h; unsigned int w, h;
bool ret; bool ret = false;
if (mode == MODE_IMAGE) { if (mode == MODE_IMAGE) {
x = MAX(0, win.x + img.x); x = MAX(0, win.x + img.x);
@ -298,10 +296,8 @@ bool i_fit_to_img(arg_t a) {
img.x = x - win.x; img.x = x - win.x;
img.y = y - win.y; img.y = y - win.y;
} }
return ret;
} else {
return false;
} }
return ret;
} }
bool i_rotate(arg_t a) { bool i_rotate(arg_t a) {
@ -381,7 +377,7 @@ bool it_open_with(arg_t a) {
const char *prog = (const char*) a; const char *prog = (const char*) a;
pid_t pid; pid_t pid;
if (!prog || !*prog) if (prog == NULL || *prog == '\0')
return false; return false;
if ((pid = fork()) == 0) { if ((pid = fork()) == 0) {
@ -392,7 +388,6 @@ bool it_open_with(arg_t a) {
} else if (pid < 0) { } else if (pid < 0) {
warn("could not fork. program was: %s", prog); warn("could not fork. program was: %s", prog);
} }
return false; return false;
} }
@ -401,8 +396,8 @@ bool it_shell_cmd(arg_t a) {
const char *cmdline = (const char*) a; const char *cmdline = (const char*) a;
pid_t pid; pid_t pid;
if (!cmdline || !*cmdline) if (cmdline == NULL || *cmdline == '\0')
return 0; return false;
n = mode == MODE_IMAGE ? fileidx : tns.sel; n = mode == MODE_IMAGE ? fileidx : tns.sel;
@ -424,7 +419,7 @@ bool it_shell_cmd(arg_t a) {
win_set_cursor(&win, CURSOR_WATCH); win_set_cursor(&win, CURSOR_WATCH);
waitpid(pid, &status, 0); waitpid(pid, &status, 0);
if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) if (WIFEXITED(status) == 0 || WEXITSTATUS(status) != 0)
warn("child exited with non-zero return value: %d. command line was: %s", warn("child exited with non-zero return value: %d. command line was: %s",
WEXITSTATUS(status), cmdline); WEXITSTATUS(status), cmdline);
@ -440,6 +435,5 @@ bool it_shell_cmd(arg_t a) {
if (tns.sel >= tns.cnt) if (tns.sel >= tns.cnt)
tns.sel = tns.cnt - 1; tns.sel = tns.cnt - 1;
} }
return true; return true;
} }

View File

@ -14,10 +14,11 @@ inline int puts_if(const char *s, int c) {
} }
int main(int argc, char **argv) { int main(int argc, char **argv) {
int i, n = 0; int i;
unsigned int n = 0;
for (i = 1; i < argc; i++) { for (i = 1; i < argc; i++) {
switch (argv[i][0] != '-' || argv[i][2] ? -1 : argv[i][1]) { switch ((argv[i][0] != '-' || argv[i][2] != '\0') ? -1 : argv[i][1]) {
case 'D': case 'D':
n += PUT_MACRO(EXIF_SUPPORT); n += PUT_MACRO(EXIF_SUPPORT);
n += PUT_MACRO(GIF_SUPPORT); n += PUT_MACRO(GIF_SUPPORT);
@ -31,7 +32,7 @@ int main(int argc, char **argv) {
return 1; return 1;
} }
} }
if (n) if (n > 0)
printf("\n"); printf("\n");
return 0; return 0;
} }

60
image.c
View File

@ -49,7 +49,7 @@ void img_init(img_t *img, win_t *win) {
zoom_min = zoom_levels[0] / 100.0; zoom_min = zoom_levels[0] / 100.0;
zoom_max = zoom_levels[ARRLEN(zoom_levels) - 1] / 100.0; zoom_max = zoom_levels[ARRLEN(zoom_levels) - 1] / 100.0;
if (!img || !win) if (img == NULL || win == NULL)
return; return;
imlib_context_set_display(win->env.dpy); imlib_context_set_display(win->env.dpy);
@ -77,15 +77,15 @@ void exif_auto_orientate(const fileinfo_t *file) {
ExifEntry *entry; ExifEntry *entry;
int byte_order, orientation; int byte_order, orientation;
if (!(ed = exif_data_new_from_file(file->path))) if ((ed = exif_data_new_from_file(file->path)) == NULL)
return; return;
entry = exif_content_get_entry(ed->ifd[EXIF_IFD_0], EXIF_TAG_ORIENTATION); entry = exif_content_get_entry(ed->ifd[EXIF_IFD_0], EXIF_TAG_ORIENTATION);
if (entry) { if (entry != NULL) {
byte_order = exif_data_get_byte_order(ed); byte_order = exif_data_get_byte_order(ed);
orientation = exif_get_short(entry->data, byte_order); orientation = exif_get_short(entry->data, byte_order);
} }
exif_data_unref(ed); exif_data_unref(ed);
if (!entry) if (entry == NULL)
return; return;
switch (orientation) { switch (orientation) {
@ -145,7 +145,7 @@ bool img_load_gif(img_t *img, const fileinfo_t *file) {
img->multi.sel = 0; img->multi.sel = 0;
gif = DGifOpenFileName(file->path); gif = DGifOpenFileName(file->path);
if (!gif) { if (gif == NULL) {
warn("could not open gif file: %s", file->name); warn("could not open gif file: %s", file->name);
return false; return false;
} }
@ -215,7 +215,7 @@ bool img_load_gif(img_t *img, const fileinfo_t *file) {
if (i < y || i >= y + h || j < x || j >= x + w || if (i < y || i >= y + h || j < x || j >= x + w ||
rows[i-y][j-x] == transp) rows[i-y][j-x] == transp)
{ {
if (prev_frame) if (prev_frame != NULL)
*ptr = prev_frame[i * sw + j]; *ptr = prev_frame[i * sw + j];
else else
*ptr = bgpixel; *ptr = bgpixel;
@ -236,7 +236,7 @@ bool img_load_gif(img_t *img, const fileinfo_t *file) {
free(rows); free(rows);
free(data); free(data);
if (!im) { if (im == NULL) {
err = true; err = true;
break; break;
} }
@ -286,10 +286,12 @@ bool img_load_gif(img_t *img, const fileinfo_t *file) {
bool img_load(img_t *img, const fileinfo_t *file) { bool img_load(img_t *img, const fileinfo_t *file) {
const char *fmt; const char *fmt;
if (!img || !file || !file->name || !file->path) if (img == NULL || file == NULL || file->name == NULL || file->path == NULL)
return false; return false;
if (access(file->path, R_OK) || !(img->im = imlib_load_image(file->path))) { if (access(file->path, R_OK) < 0 ||
(img->im = imlib_load_image(file->path)) == NULL)
{
warn("could not open image: %s", file->name); warn("could not open image: %s", file->name);
return false; return false;
} }
@ -324,17 +326,17 @@ bool img_load(img_t *img, const fileinfo_t *file) {
void img_close(img_t *img, bool decache) { void img_close(img_t *img, bool decache) {
int i; int i;
if (!img) if (img == NULL)
return; return;
if (img->multi.cnt) { if (img->multi.cnt > 0) {
for (i = 0; i < img->multi.cnt; i++) { for (i = 0; i < img->multi.cnt; i++) {
imlib_context_set_image(img->multi.frames[i].im); imlib_context_set_image(img->multi.frames[i].im);
imlib_free_image(); imlib_free_image();
} }
img->multi.cnt = 0; img->multi.cnt = 0;
img->im = NULL; img->im = NULL;
} else if (img->im) { } else if (img->im != NULL) {
imlib_context_set_image(img->im); imlib_context_set_image(img->im);
if (decache) if (decache)
imlib_free_image_and_decache(); imlib_free_image_and_decache();
@ -348,7 +350,7 @@ void img_check_pan(img_t *img, bool moved) {
win_t *win; win_t *win;
int ox, oy; int ox, oy;
if (!img || !img->im || !img->win) if (img == NULL || img->im == NULL || img->win == NULL)
return; return;
win = img->win; win = img->win;
@ -379,7 +381,7 @@ void img_check_pan(img_t *img, bool moved) {
bool img_fit(img_t *img) { bool img_fit(img_t *img) {
float z, zmax, zw, zh; float z, zmax, zw, zh;
if (!img || !img->im || !img->win) if (img == NULL || img->im == NULL || img->win == NULL)
return false; return false;
if (img->scalemode == SCALE_ZOOM) if (img->scalemode == SCALE_ZOOM)
return false; return false;
@ -406,7 +408,7 @@ void img_render(img_t *img) {
int sx, sy, sw, sh; int sx, sy, sw, sh;
int dx, dy, dw, dh; int dx, dy, dw, dh;
if (!img || !img->im || !img->win) if (img == NULL || img->im == NULL || img->win == NULL)
return; return;
win = img->win; win = img->win;
@ -461,7 +463,7 @@ void img_render(img_t *img) {
imlib_context_set_image(img->im); imlib_context_set_image(img->im);
if (imlib_image_has_alpha() && !img->alpha) if (!img->alpha && imlib_image_has_alpha())
win_draw_rect(win, win->pm, dx, dy, dw, dh, True, 0, win->white); win_draw_rect(win, win->pm, dx, dy, dw, dh, True, 0, win->white);
imlib_context_set_drawable(win->pm); imlib_context_set_drawable(win->pm);
@ -473,7 +475,7 @@ void img_render(img_t *img) {
} }
bool img_fit_win(img_t *img) { bool img_fit_win(img_t *img) {
if (!img || !img->im) if (img == NULL || img->im == NULL)
return false; return false;
img->scalemode = SCALE_FIT; img->scalemode = SCALE_FIT;
@ -483,7 +485,7 @@ bool img_fit_win(img_t *img) {
bool img_center(img_t *img) { bool img_center(img_t *img) {
int ox, oy; int ox, oy;
if (!img || !img->im || !img->win) if (img == NULL || img->im == NULL || img->win == NULL)
return false; return false;
ox = img->x; ox = img->x;
@ -501,7 +503,7 @@ bool img_center(img_t *img) {
} }
bool img_zoom(img_t *img, float z) { bool img_zoom(img_t *img, float z) {
if (!img || !img->im || !img->win) if (img == NULL || img->im == NULL || img->win == NULL)
return false; return false;
z = MAX(z, zoom_min); z = MAX(z, zoom_min);
@ -524,7 +526,7 @@ bool img_zoom(img_t *img, float z) {
bool img_zoom_in(img_t *img) { bool img_zoom_in(img_t *img) {
int i; int i;
if (!img || !img->im) if (img == NULL || img->im == NULL)
return false; return false;
for (i = 1; i < ARRLEN(zoom_levels); i++) { for (i = 1; i < ARRLEN(zoom_levels); i++) {
@ -537,7 +539,7 @@ bool img_zoom_in(img_t *img) {
bool img_zoom_out(img_t *img) { bool img_zoom_out(img_t *img) {
int i; int i;
if (!img || !img->im) if (img == NULL || img->im == NULL)
return false; return false;
for (i = ARRLEN(zoom_levels) - 2; i >= 0; i--) { for (i = ARRLEN(zoom_levels) - 2; i >= 0; i--) {
@ -550,7 +552,7 @@ bool img_zoom_out(img_t *img) {
bool img_move(img_t *img, int dx, int dy) { bool img_move(img_t *img, int dx, int dy) {
int ox, oy; int ox, oy;
if (!img || !img->im) if (img == NULL || img->im == NULL)
return false; return false;
ox = img->x; ox = img->x;
@ -570,7 +572,7 @@ bool img_move(img_t *img, int dx, int dy) {
} }
bool img_pan(img_t *img, direction_t dir, bool screen) { bool img_pan(img_t *img, direction_t dir, bool screen) {
if (!img || !img->im || !img->win) if (img == NULL || img->im == NULL || img->win == NULL)
return false; return false;
switch (dir) { switch (dir) {
@ -589,7 +591,7 @@ bool img_pan(img_t *img, direction_t dir, bool screen) {
bool img_pan_edge(img_t *img, direction_t dir) { bool img_pan_edge(img_t *img, direction_t dir) {
int ox, oy; int ox, oy;
if (!img || !img->im || !img->win) if (img == NULL || img->im == NULL || img->win == NULL)
return false; return false;
ox = img->x; ox = img->x;
@ -624,7 +626,7 @@ void img_rotate(img_t *img, int d) {
win_t *win; win_t *win;
int ox, oy, tmp; int ox, oy, tmp;
if (!img || !img->im || !img->win) if (img == NULL || img->im == NULL || img->win == NULL)
return; return;
win = img->win; win = img->win;
@ -654,7 +656,7 @@ void img_rotate_right(img_t *img) {
} }
void img_toggle_antialias(img_t *img) { void img_toggle_antialias(img_t *img) {
if (!img || !img->im) if (img == NULL || img->im == NULL)
return; return;
img->aa = !img->aa; img->aa = !img->aa;
@ -664,7 +666,7 @@ void img_toggle_antialias(img_t *img) {
} }
bool img_frame_goto(img_t *img, int n) { bool img_frame_goto(img_t *img, int n) {
if (!img || !img->im) if (img == NULL || img->im == NULL)
return false; return false;
if (n < 0 || n >= img->multi.cnt || n == img->multi.sel) if (n < 0 || n >= img->multi.cnt || n == img->multi.sel)
return false; return false;
@ -682,7 +684,7 @@ bool img_frame_goto(img_t *img, int n) {
} }
bool img_frame_navigate(img_t *img, int d) { bool img_frame_navigate(img_t *img, int d) {
if (!img || !img->im || !img->multi.cnt || !d) if (img == NULL|| img->im == NULL || img->multi.cnt == 0 || d == 0)
return false; return false;
d += img->multi.sel; d += img->multi.sel;
@ -695,7 +697,7 @@ bool img_frame_navigate(img_t *img, int d) {
} }
bool img_frame_animate(img_t *img, bool restart) { bool img_frame_animate(img_t *img, bool restart) {
if (!img || !img->im || !img->multi.cnt) if (img == NULL || img->im == NULL || img->multi.cnt == 0)
return false; return false;
if (img->multi.sel + 1 >= img->multi.cnt) { if (img->multi.sel + 1 >= img->multi.cnt) {

108
main.c
View File

@ -73,9 +73,10 @@ timeout_t timeouts[] = {
}; };
void cleanup() { void cleanup() {
static int in = 0; static bool in = false;
if (!in++) { if (!in) {
in = true;
img_close(&img, false); img_close(&img, false);
tns_free(&tns); tns_free(&tns);
win_close(&win); win_close(&win);
@ -83,10 +84,10 @@ void cleanup() {
} }
void check_add_file(char *filename) { void check_add_file(char *filename) {
if (!filename || !*filename) if (filename == NULL || *filename == '\0')
return; return;
if (access(filename, R_OK)) { if (access(filename, R_OK) < 0) {
warn("could not open file: %s", filename); warn("could not open file: %s", filename);
return; return;
} }
@ -97,7 +98,7 @@ void check_add_file(char *filename) {
} }
if (*filename != '/') { if (*filename != '/') {
files[fileidx].path = absolute_path(filename); files[fileidx].path = absolute_path(filename);
if (!files[fileidx].path) { if (files[fileidx].path == NULL) {
warn("could not get absolute path of file: %s\n", filename); warn("could not get absolute path of file: %s\n", filename);
return; return;
} }
@ -173,7 +174,7 @@ bool check_timeouts(struct timeval *t) {
tdiff = tv_diff(&timeouts[i].when, &now); tdiff = tv_diff(&timeouts[i].when, &now);
if (tdiff <= 0) { if (tdiff <= 0) {
timeouts[i].active = false; timeouts[i].active = false;
if (timeouts[i].handler) if (timeouts[i].handler != NULL)
timeouts[i].handler(); timeouts[i].handler();
i = tmin = -1; i = tmin = -1;
} else if (tmin < 0 || tdiff < tmin) { } else if (tmin < 0 || tdiff < tmin) {
@ -182,7 +183,7 @@ bool check_timeouts(struct timeval *t) {
} }
i++; i++;
} }
if (tmin > 0 && t) if (tmin > 0 && t != NULL)
tv_set_msec(t, tmin); tv_set_msec(t, tmin);
return tmin > 0; return tmin > 0;
} }
@ -204,12 +205,12 @@ void load_image(int new) {
files[new].loaded = true; files[new].loaded = true;
fileidx = new; fileidx = new;
if (!stat(files[new].path, &fstats)) if (stat(files[new].path, &fstats) == 0)
filesize = fstats.st_size; filesize = fstats.st_size;
else else
filesize = 0; filesize = 0;
if (img.multi.cnt && img.multi.animate) if (img.multi.cnt > 0 && img.multi.animate)
set_timeout(animate, img.multi.frames[img.multi.sel].delay, true); set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
else else
reset_timeout(animate); reset_timeout(animate);
@ -238,7 +239,7 @@ void update_title() {
} else { } else {
sshow_info[0] = '\0'; sshow_info[0] = '\0';
} }
if (img.multi.cnt) if (img.multi.cnt > 0)
snprintf(frame_info, sizeof(frame_info), "{%d/%d} ", snprintf(frame_info, sizeof(frame_info), "{%d/%d} ",
img.multi.sel + 1, img.multi.cnt); img.multi.sel + 1, img.multi.cnt);
else else
@ -329,14 +330,14 @@ void on_keypress(XKeyEvent *kev) {
KeySym ksym; KeySym ksym;
char key; char key;
if (!kev) if (kev == NULL)
return; return;
XLookupString(kev, &key, 1, &ksym, NULL); XLookupString(kev, &key, 1, &ksym, NULL);
for (i = 0; i < ARRLEN(keys); i++) { for (i = 0; i < ARRLEN(keys); i++) {
if (keys[i].ksym == ksym && keymask(&keys[i], kev->state)) { if (keys[i].ksym == ksym && keymask(&keys[i], kev->state)) {
if (keys[i].cmd && keys[i].cmd(keys[i].arg)) if (keys[i].cmd != NULL && keys[i].cmd(keys[i].arg))
redraw(); redraw();
return; return;
} }
@ -346,7 +347,7 @@ void on_keypress(XKeyEvent *kev) {
void on_buttonpress(XButtonEvent *bev) { void on_buttonpress(XButtonEvent *bev) {
int i, sel; int i, sel;
if (!bev) if (bev == NULL)
return; return;
if (mode == MODE_IMAGE) { if (mode == MODE_IMAGE) {
@ -357,7 +358,7 @@ void on_buttonpress(XButtonEvent *bev) {
if (buttons[i].button == bev->button && if (buttons[i].button == bev->button &&
buttonmask(&buttons[i], bev->state)) buttonmask(&buttons[i], bev->state))
{ {
if (buttons[i].cmd && buttons[i].cmd(buttons[i].arg)) if (buttons[i].cmd != NULL && buttons[i].cmd(buttons[i].arg))
redraw(); redraw();
return; return;
} }
@ -397,9 +398,9 @@ void run() {
redraw(); redraw();
while (1) { while (true) {
while (mode == MODE_THUMB && tns.cnt < filecnt && while (mode == MODE_THUMB && tns.cnt < filecnt &&
!XPending(win.env.dpy)) XPending(win.env.dpy) == 0)
{ {
/* load thumbnails */ /* load thumbnails */
set_timeout(redraw, TO_REDRAW_THUMBS, false); set_timeout(redraw, TO_REDRAW_THUMBS, false);
@ -413,7 +414,7 @@ void run() {
check_timeouts(NULL); check_timeouts(NULL);
} }
while (!XPending(win.env.dpy) && check_timeouts(&timeout)) { while (XPending(win.env.dpy) == 0 && check_timeouts(&timeout)) {
/* wait for timeouts */ /* wait for timeouts */
xfd = ConnectionNumber(win.env.dpy); xfd = ConnectionNumber(win.env.dpy);
FD_ZERO(&fds); FD_ZERO(&fds);
@ -421,35 +422,34 @@ void run() {
select(xfd + 1, &fds, 0, 0, &timeout); select(xfd + 1, &fds, 0, 0, &timeout);
} }
if (!XNextEvent(win.env.dpy, &ev)) { XNextEvent(win.env.dpy, &ev);
switch (ev.type) {
/* handle events */ /* handle events */
switch (ev.type) { case ButtonPress:
case ButtonPress: on_buttonpress(&ev.xbutton);
on_buttonpress(&ev.xbutton); break;
break; case ClientMessage:
case ClientMessage: if ((Atom) ev.xclient.data.l[0] == wm_delete_win)
if ((Atom) ev.xclient.data.l[0] == wm_delete_win) return;
return; break;
break; case ConfigureNotify:
case ConfigureNotify: if (win_configure(&win, &ev.xconfigure)) {
if (win_configure(&win, &ev.xconfigure)) { set_timeout(redraw, TO_REDRAW_RESIZE, false);
set_timeout(redraw, TO_REDRAW_RESIZE, false); if (mode == MODE_IMAGE)
if (mode == MODE_IMAGE) img.checkpan = true;
img.checkpan = true; else
else tns.dirty = true;
tns.dirty = true; }
} break;
break; case KeyPress:
case KeyPress: on_keypress(&ev.xkey);
on_keypress(&ev.xkey); break;
break; case MotionNotify:
case MotionNotify: if (mode == MODE_IMAGE) {
if (mode == MODE_IMAGE) { win_set_cursor(&win, CURSOR_ARROW);
win_set_cursor(&win, CURSOR_ARROW); set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
set_timeout(reset_cursor, TO_CURSOR_HIDE, true); }
} break;
break;
}
} }
} }
} }
@ -474,7 +474,7 @@ int main(int argc, char **argv) {
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }
if (!options->filecnt) { if (options->filecnt == 0) {
print_usage(); print_usage();
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@ -495,25 +495,29 @@ int main(int argc, char **argv) {
filename[len-1] = '\0'; filename[len-1] = '\0';
check_add_file(filename); check_add_file(filename);
} }
if (filename) if (filename != NULL)
free(filename); free(filename);
} else { } else {
for (i = 0; i < options->filecnt; i++) { for (i = 0; i < options->filecnt; i++) {
filename = options->filenames[i]; filename = options->filenames[i];
if (stat(filename, &fstats) || !S_ISDIR(fstats.st_mode)) { if (stat(filename, &fstats) < 0) {
warn("could not stat file: %s", filename);
continue;
}
if (!S_ISDIR(fstats.st_mode)) {
check_add_file(filename); check_add_file(filename);
} else { } else {
if (!options->recursive) { if (!options->recursive) {
warn("ignoring directory: %s", filename); warn("ignoring directory: %s", filename);
continue; continue;
} }
if (r_opendir(&dir, filename)) { if (r_opendir(&dir, filename) < 0) {
warn("could not open directory: %s", filename); warn("could not open directory: %s", filename);
continue; continue;
} }
start = fileidx; start = fileidx;
while ((filename = r_readdir(&dir))) { while ((filename = r_readdir(&dir)) != NULL) {
check_add_file(filename); check_add_file(filename);
free((void*) filename); free((void*) filename);
} }
@ -524,7 +528,7 @@ int main(int argc, char **argv) {
} }
} }
if (!fileidx) { if (fileidx == 0) {
fprintf(stderr, "sxiv: no valid image file given, aborting\n"); fprintf(stderr, "sxiv: no valid image file given, aborting\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@ -548,7 +552,7 @@ int main(int argc, char **argv) {
} }
win_open(&win); win_open(&win);
run(); run();
cleanup(); cleanup();

View File

@ -95,7 +95,7 @@ void parse_options(int argc, char **argv) {
print_usage(); print_usage();
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
case 'n': case 'n':
if (!sscanf(optarg, "%d", &t) || t < 1) { if (sscanf(optarg, "%d", &t) <= 0 || t < 1) {
fprintf(stderr, "sxiv: invalid argument for option -n: %s\n", fprintf(stderr, "sxiv: invalid argument for option -n: %s\n",
optarg); optarg);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
@ -127,7 +127,7 @@ void parse_options(int argc, char **argv) {
break; break;
case 'z': case 'z':
_options.scalemode = SCALE_ZOOM; _options.scalemode = SCALE_ZOOM;
if (!sscanf(optarg, "%d", &t) || t <= 0) { if (sscanf(optarg, "%d", &t) <= 0 || t <= 0) {
fprintf(stderr, "sxiv: invalid argument for option -z: %s\n", fprintf(stderr, "sxiv: invalid argument for option -z: %s\n",
optarg); optarg);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);

127
thumbs.c
View File

@ -41,23 +41,23 @@ char *cache_dir = NULL;
bool tns_cache_enabled() { bool tns_cache_enabled() {
struct stat stats; struct stat stats;
return cache_dir && !stat(cache_dir, &stats) && S_ISDIR(stats.st_mode) && return cache_dir != NULL && stat(cache_dir, &stats) == 0 &&
!access(cache_dir, W_OK); S_ISDIR(stats.st_mode) && access(cache_dir, W_OK) == 0;
} }
char* tns_cache_filepath(const char *filepath) { char* tns_cache_filepath(const char *filepath) {
size_t len; size_t len;
char *cfile = NULL; char *cfile = NULL;
if (!cache_dir || !filepath || *filepath != '/') if (cache_dir == NULL || filepath == NULL || *filepath != '/')
return NULL; return NULL;
if (strncmp(filepath, cache_dir, strlen(cache_dir))) { if (strncmp(filepath, cache_dir, strlen(cache_dir)) != 0) {
/* don't cache images inside the cache directory! */
len = strlen(cache_dir) + strlen(filepath) + 6; len = strlen(cache_dir) + strlen(filepath) + 6;
cfile = (char*) s_malloc(len); cfile = (char*) s_malloc(len);
snprintf(cfile, len, "%s/%s.png", cache_dir, filepath + 1); snprintf(cfile, len, "%s/%s.png", cache_dir, filepath + 1);
} }
return cfile; return cfile;
} }
@ -66,18 +66,16 @@ Imlib_Image* tns_cache_load(const char *filepath) {
struct stat cstats, fstats; struct stat cstats, fstats;
Imlib_Image *im = NULL; Imlib_Image *im = NULL;
if (!filepath) if (filepath == NULL)
return NULL;
if (stat(filepath, &fstats) < 0)
return NULL; return NULL;
if (stat(filepath, &fstats)) if ((cfile = tns_cache_filepath(filepath)) != NULL) {
return NULL; if (stat(cfile, &cstats) == 0 && cstats.st_mtime == fstats.st_mtime)
if ((cfile = tns_cache_filepath(filepath))) {
if (!stat(cfile, &cstats) && cstats.st_mtime == fstats.st_mtime)
im = imlib_load_image(cfile); im = imlib_load_image(cfile);
free(cfile); free(cfile);
} }
return im; return im;
} }
@ -87,32 +85,35 @@ void tns_cache_write(thumb_t *t, bool force) {
struct utimbuf times; struct utimbuf times;
Imlib_Load_Error err = 0; Imlib_Load_Error err = 0;
if (!t || !t->im || !t->file || !t->file->name || !t->file->path) if (t == NULL || t->im == NULL)
return;
if (t->file == NULL || t->file->name == NULL || t->file->path == NULL)
return;
if (stat(t->file->path, &fstats) < 0)
return; return;
if (stat(t->file->path, &fstats)) if ((cfile = tns_cache_filepath(t->file->path)) != NULL) {
return; if (force || stat(cfile, &cstats) < 0 ||
cstats.st_mtime != fstats.st_mtime)
if ((cfile = tns_cache_filepath(t->file->path))) { {
if (force || stat(cfile, &cstats) || cstats.st_mtime != fstats.st_mtime) { if ((dirend = strrchr(cfile, '/')) != NULL) {
if ((dirend = strrchr(cfile, '/'))) {
*dirend = '\0'; *dirend = '\0';
err = r_mkdir(cfile); err = r_mkdir(cfile);
*dirend = '/'; *dirend = '/';
} }
if (!err) { if (err == 0) {
imlib_context_set_image(t->im); imlib_context_set_image(t->im);
imlib_image_set_format("png"); imlib_image_set_format("png");
imlib_save_image_with_error_return(cfile, &err); imlib_save_image_with_error_return(cfile, &err);
} }
if (err) { if (err == 0) {
warn("could not cache thumbnail: %s", t->file->name);
} else {
times.actime = fstats.st_atime; times.actime = fstats.st_atime;
times.modtime = fstats.st_mtime; times.modtime = fstats.st_mtime;
utime(cfile, &times); utime(cfile, &times);
} else {
warn("could not cache thumbnail: %s", t->file->name);
} }
} }
free(cfile); free(cfile);
@ -125,33 +126,32 @@ void tns_clean_cache(tns_t *tns) {
char *cfile, *filename, *tpos; char *cfile, *filename, *tpos;
r_dir_t dir; r_dir_t dir;
if (!cache_dir) if (cache_dir == NULL)
return; return;
if (r_opendir(&dir, cache_dir)) { if (r_opendir(&dir, cache_dir) < 0) {
warn("could not open thumbnail cache directory: %s", cache_dir); warn("could not open thumbnail cache directory: %s", cache_dir);
return; return;
} }
dirlen = strlen(cache_dir); dirlen = strlen(cache_dir);
while ((cfile = r_readdir(&dir))) { while ((cfile = r_readdir(&dir)) != NULL) {
filename = cfile + dirlen; filename = cfile + dirlen;
delete = false; delete = false;
if ((tpos = strrchr(filename, '.'))) { if ((tpos = strrchr(filename, '.')) != NULL) {
*tpos = '\0'; *tpos = '\0';
if (access(filename, F_OK)) if (access(filename, F_OK) < 0)
delete = true; delete = true;
*tpos = '.'; *tpos = '.';
} }
if (delete) {
if (delete && unlink(cfile)) if (unlink(cfile) < 0)
warn("could not delete cache file: %s", cfile); warn("could not delete cache file: %s", cfile);
}
free(cfile); free(cfile);
} }
r_closedir(&dir); r_closedir(&dir);
} }
@ -160,10 +160,10 @@ void tns_init(tns_t *tns, int cnt, win_t *win) {
int len; int len;
char *homedir; char *homedir;
if (!tns) if (tns == NULL)
return; return;
if (cnt) { if (cnt > 0) {
tns->thumbs = (thumb_t*) s_malloc(cnt * sizeof(thumb_t)); tns->thumbs = (thumb_t*) s_malloc(cnt * sizeof(thumb_t));
memset(tns->thumbs, 0, cnt * sizeof(thumb_t)); memset(tns->thumbs, 0, cnt * sizeof(thumb_t));
} else { } else {
@ -176,8 +176,8 @@ void tns_init(tns_t *tns, int cnt, win_t *win) {
tns->alpha = true; tns->alpha = true;
tns->dirty = false; tns->dirty = false;
if ((homedir = getenv("HOME"))) { if ((homedir = getenv("HOME")) != NULL) {
if (cache_dir) if (cache_dir != NULL)
free(cache_dir); free(cache_dir);
len = strlen(homedir) + 10; len = strlen(homedir) + 10;
cache_dir = (char*) s_malloc(len * sizeof(char)); cache_dir = (char*) s_malloc(len * sizeof(char));
@ -190,12 +190,12 @@ void tns_init(tns_t *tns, int cnt, win_t *win) {
void tns_free(tns_t *tns) { void tns_free(tns_t *tns) {
int i; int i;
if (!tns) if (tns != NULL)
return; return;
if (tns->thumbs) { if (tns->thumbs != NULL) {
for (i = 0; i < tns->cnt; i++) { for (i = 0; i < tns->cnt; i++) {
if (tns->thumbs[i].im) { if (tns->thumbs[i].im != NULL) {
imlib_context_set_image(tns->thumbs[i].im); imlib_context_set_image(tns->thumbs[i].im);
imlib_free_image(); imlib_free_image();
} }
@ -204,7 +204,7 @@ void tns_free(tns_t *tns) {
tns->thumbs = NULL; tns->thumbs = NULL;
} }
if (cache_dir) { if (cache_dir != NULL) {
free(cache_dir); free(cache_dir);
cache_dir = NULL; cache_dir = NULL;
} }
@ -220,31 +220,34 @@ bool tns_load(tns_t *tns, int n, const fileinfo_t *file,
Imlib_Image *im; Imlib_Image *im;
const char *fmt; const char *fmt;
if (!tns || !tns->thumbs || !file || !file->name || !file->path) if (tns == NULL || tns->thumbs == NULL)
return false;
if (file == NULL || file->name == NULL || file->path == NULL)
return false; return false;
if (n < 0 || n >= tns->cap) if (n < 0 || n >= tns->cap)
return false; return false;
t = &tns->thumbs[n]; t = &tns->thumbs[n];
t->file = file; t->file = file;
if (t->im) { if (t->im != NULL) {
imlib_context_set_image(t->im); imlib_context_set_image(t->im);
imlib_free_image(); imlib_free_image();
} }
if ((use_cache = tns_cache_enabled())) { if ((use_cache = tns_cache_enabled())) {
if (!force && (im = tns_cache_load(file->path))) if (!force && (im = tns_cache_load(file->path)) != NULL)
cache_hit = true; cache_hit = true;
} }
if (!cache_hit && if (!cache_hit) {
(access(file->path, R_OK) || !(im = imlib_load_image(file->path)))) if (access(file->path, R_OK) < 0 ||
{ (im = imlib_load_image(file->path)) == NULL)
if (!silent) {
warn("could not open image: %s", file->name); if (!silent)
return false; warn("could not open image: %s", file->name);
return false;
}
} }
imlib_context_set_image(im); imlib_context_set_image(im);
@ -267,7 +270,8 @@ bool tns_load(tns_t *tns, int n, const fileinfo_t *file,
t->w = z * w; t->w = z * w;
t->h = z * h; t->h = z * h;
if (!(t->im = imlib_create_cropped_scaled_image(0, 0, w, h, t->w, t->h))) t->im = imlib_create_cropped_scaled_image(0, 0, w, h, t->w, t->h);
if (t->im == NULL)
die("could not allocate memory"); die("could not allocate memory");
imlib_free_image_and_decache(); imlib_free_image_and_decache();
@ -282,7 +286,7 @@ bool tns_load(tns_t *tns, int n, const fileinfo_t *file,
void tns_check_view(tns_t *tns, bool scrolled) { void tns_check_view(tns_t *tns, bool scrolled) {
int r; int r;
if (!tns) if (tns == NULL)
return; return;
tns->first -= tns->first % tns->cols; tns->first -= tns->first % tns->cols;
@ -311,7 +315,7 @@ void tns_render(tns_t *tns) {
win_t *win; win_t *win;
int i, cnt, r, x, y; int i, cnt, r, x, y;
if (!tns || !tns->thumbs || !tns->win) if (tns == NULL || tns->thumbs == NULL || tns->win == NULL)
return; return;
if (!tns->dirty) if (!tns->dirty)
return; return;
@ -345,7 +349,7 @@ void tns_render(tns_t *tns) {
t->y = y + (THUMB_SIZE - t->h) / 2; t->y = y + (THUMB_SIZE - t->h) / 2;
imlib_context_set_image(t->im); imlib_context_set_image(t->im);
if (imlib_image_has_alpha() && !tns->alpha) if (!tns->alpha && imlib_image_has_alpha())
win_draw_rect(win, win->pm, t->x, t->y, t->w, t->h, true, 0, win->white); win_draw_rect(win, win->pm, t->x, t->y, t->w, t->h, true, 0, win->white);
imlib_render_image_part_on_drawable_at_size(0, 0, t->w, t->h, imlib_render_image_part_on_drawable_at_size(0, 0, t->w, t->h,
@ -357,7 +361,6 @@ void tns_render(tns_t *tns) {
x += thumb_dim; x += thumb_dim;
} }
} }
tns->dirty = false; tns->dirty = false;
tns_highlight(tns, tns->sel, true); tns_highlight(tns, tns->sel, true);
} }
@ -368,7 +371,7 @@ void tns_highlight(tns_t *tns, int n, bool hl) {
int x, y; int x, y;
unsigned long col; unsigned long col;
if (!tns || !tns->thumbs || !tns->win) if (tns == NULL || tns->thumbs == NULL || tns->win == NULL)
return; return;
win = tns->win; win = tns->win;
@ -388,14 +391,13 @@ void tns_highlight(tns_t *tns, int n, bool hl) {
win_draw_rect(win, win->pm, x - 3, y - 3, THUMB_SIZE + 6, THUMB_SIZE + 6, win_draw_rect(win, win->pm, x - 3, y - 3, THUMB_SIZE + 6, THUMB_SIZE + 6,
false, 2, col); false, 2, col);
} }
win_draw(win); win_draw(win);
} }
bool tns_move_selection(tns_t *tns, direction_t dir) { bool tns_move_selection(tns_t *tns, direction_t dir) {
int old; int old;
if (!tns || !tns->thumbs) if (tns == NULL || tns->thumbs == NULL)
return false; return false;
old = tns->sel; old = tns->sel;
@ -425,14 +427,13 @@ bool tns_move_selection(tns_t *tns, direction_t dir) {
if (!tns->dirty) if (!tns->dirty)
tns_highlight(tns, tns->sel, true); tns_highlight(tns, tns->sel, true);
} }
return tns->sel != old; return tns->sel != old;
} }
bool tns_scroll(tns_t *tns, direction_t dir) { bool tns_scroll(tns_t *tns, direction_t dir) {
int old; int old;
if (!tns) if (tns == NULL)
return false; return false;
old = tns->first; old = tns->first;
@ -446,16 +447,14 @@ bool tns_scroll(tns_t *tns, direction_t dir) {
tns_check_view(tns, true); tns_check_view(tns, true);
tns->dirty = true; tns->dirty = true;
} }
return tns->first != old; return tns->first != old;
} }
int tns_translate(tns_t *tns, int x, int y) { int tns_translate(tns_t *tns, int x, int y) {
int n; int n;
if (!tns || !tns->thumbs) if (tns == NULL || tns->thumbs == NULL)
return -1; return -1;
if (x < tns->x || y < tns->y) if (x < tns->x || y < tns->y)
return -1; return -1;

92
util.c
View File

@ -39,13 +39,15 @@ void cleanup();
void* s_malloc(size_t size) { void* s_malloc(size_t size) {
void *ptr; void *ptr;
if (!(ptr = malloc(size))) ptr = malloc(size);
if (ptr == NULL)
die("could not allocate memory"); die("could not allocate memory");
return ptr; return ptr;
} }
void* s_realloc(void *ptr, size_t size) { void* s_realloc(void *ptr, size_t size) {
if (!(ptr = realloc(ptr, size))) ptr = realloc(ptr, size);
if (ptr == NULL)
die("could not allocate memory"); die("could not allocate memory");
return ptr; return ptr;
} }
@ -53,8 +55,9 @@ void* s_realloc(void *ptr, size_t size) {
char* s_strdup(char *s) { char* s_strdup(char *s) {
char *d = NULL; char *d = NULL;
if (s) { if (s != NULL) {
if (!(d = malloc(strlen(s) + 1))) d = malloc(strlen(s) + 1);
if (d == NULL)
die("could not allocate memory"); die("could not allocate memory");
strcpy(d, s); strcpy(d, s);
} }
@ -64,7 +67,7 @@ char* s_strdup(char *s) {
void warn(const char* fmt, ...) { void warn(const char* fmt, ...) {
va_list args; va_list args;
if (!fmt || options->quiet) if (fmt == NULL || options->quiet)
return; return;
va_start(args, fmt); va_start(args, fmt);
@ -77,7 +80,7 @@ void warn(const char* fmt, ...) {
void die(const char* fmt, ...) { void die(const char* fmt, ...) {
va_list args; va_list args;
if (!fmt) if (fmt == NULL)
return; return;
va_start(args, fmt); va_start(args, fmt);
@ -94,17 +97,17 @@ ssize_t get_line(char **buf, size_t *n, FILE *stream) {
size_t len; size_t len;
char *s; char *s;
if (!stream || feof(stream) || ferror(stream)) if (stream == NULL || feof(stream) || ferror(stream))
return -1; return -1;
if (!*buf || !*n) { if (*buf == NULL || *n == 0) {
*n = BUF_SIZE; *n = BUF_SIZE;
*buf = (char*) s_malloc(*n); *buf = (char*) s_malloc(*n);
} }
s = *buf; s = *buf;
while (1) { while (true) {
if (!fgets(s, *n - (s - *buf), stream)) if (fgets(s, *n - (s - *buf), stream) == NULL)
return -1; return -1;
len = strlen(s); len = strlen(s);
if (feof(stream)) if (feof(stream))
@ -119,7 +122,6 @@ ssize_t get_line(char **buf, size_t *n, FILE *stream) {
s += len; s += len;
} }
} }
return s - *buf + len; return s - *buf + len;
} }
@ -147,41 +149,41 @@ char* absolute_path(const char *filename) {
char *dir, *dirname = NULL, *path = NULL, *s; char *dir, *dirname = NULL, *path = NULL, *s;
char *cwd = NULL, *twd = NULL; char *cwd = NULL, *twd = NULL;
if (!filename || *filename == '\0' || *filename == '/') if (filename == NULL || *filename == '\0' || *filename == '/')
return NULL; return NULL;
len = FNAME_LEN; len = FNAME_LEN;
cwd = (char*) s_malloc(len); cwd = (char*) s_malloc(len);
while (!(s = getcwd(cwd, len)) && errno == ERANGE) { while ((s = getcwd(cwd, len)) == NULL && errno == ERANGE) {
len *= 2; len *= 2;
cwd = (char*) s_realloc(cwd, len); cwd = (char*) s_realloc(cwd, len);
} }
if (!s) if (s == NULL)
goto error; goto error;
s = strrchr(filename, '/'); s = strrchr(filename, '/');
if (s) { if (s != NULL) {
len = s - filename; len = s - filename;
dirname = (char*) s_malloc(len + 1); dirname = (char*) s_malloc(len + 1);
strncpy(dirname, filename, len); strncpy(dirname, filename, len);
dirname[len] = '\0'; dirname[len] = '\0';
basename = s + 1; basename = s + 1;
if (chdir(cwd)) if (chdir(cwd) < 0)
/* we're not able to come back afterwards */ /* we're not able to come back afterwards */
goto error; goto error;
if (chdir(dirname)) if (chdir(dirname) < 0)
goto error; goto error;
len = FNAME_LEN; len = FNAME_LEN;
twd = (char*) s_malloc(len); twd = (char*) s_malloc(len);
while (!(s = getcwd(twd, len)) && errno == ERANGE) { while ((s = getcwd(twd, len)) == NULL && errno == ERANGE) {
len *= 2; len *= 2;
twd = (char*) s_realloc(twd, len); twd = (char*) s_realloc(twd, len);
} }
if (chdir(cwd)) if (chdir(cwd) < 0)
die("could not revert to prior working directory"); die("could not revert to prior working directory");
if (!s) if (s == NULL)
goto error; goto error;
dir = twd; dir = twd;
} else { } else {
@ -197,27 +199,27 @@ char* absolute_path(const char *filename) {
goto end; goto end;
error: error:
if (path) { if (path != NULL) {
free(path); free(path);
path = NULL; path = NULL;
} }
end: end:
if (dirname) if (dirname != NULL)
free(dirname); free(dirname);
if (cwd) if (cwd != NULL)
free(cwd); free(cwd);
if (twd) if (twd != NULL)
free(twd); free(twd);
return path; return path;
} }
int r_opendir(r_dir_t *rdir, const char *dirname) { int r_opendir(r_dir_t *rdir, const char *dirname) {
if (!rdir || !dirname || !*dirname) if (rdir == NULL || dirname == NULL || *dirname == '\0')
return -1; return -1;
if (!(rdir->dir = opendir(dirname))) { if ((rdir->dir = opendir(dirname)) == NULL) {
rdir->name = NULL; rdir->name = NULL;
rdir->stack = NULL; rdir->stack = NULL;
return -1; return -1;
@ -236,22 +238,22 @@ int r_opendir(r_dir_t *rdir, const char *dirname) {
int r_closedir(r_dir_t *rdir) { int r_closedir(r_dir_t *rdir) {
int ret = 0; int ret = 0;
if (!rdir) if (rdir == NULL)
return -1; return -1;
if (rdir->stack) { if (rdir->stack != NULL) {
while (rdir->stlen > 0) while (rdir->stlen > 0)
free(rdir->stack[--rdir->stlen]); free(rdir->stack[--rdir->stlen]);
free(rdir->stack); free(rdir->stack);
rdir->stack = NULL; rdir->stack = NULL;
} }
if (rdir->dir) { if (rdir->dir != NULL) {
if (!(ret = closedir(rdir->dir))) if ((ret = closedir(rdir->dir)) == 0)
rdir->dir = NULL; rdir->dir = NULL;
} }
if (rdir->d && rdir->name) { if (rdir->d != 0 && rdir->name != NULL) {
free(rdir->name); free(rdir->name);
rdir->name = NULL; rdir->name = NULL;
} }
@ -265,11 +267,11 @@ char* r_readdir(r_dir_t *rdir) {
struct dirent *dentry; struct dirent *dentry;
struct stat fstats; struct stat fstats;
if (!rdir || !rdir->dir || !rdir->name) if (rdir == NULL || rdir->dir == NULL || rdir->name == NULL)
return NULL; return NULL;
while (1) { while (true) {
if (rdir->dir && (dentry = readdir(rdir->dir))) { if (rdir->dir != NULL && (dentry = readdir(rdir->dir)) != NULL) {
if (streq(dentry->d_name, ".") || streq(dentry->d_name, "..")) if (streq(dentry->d_name, ".") || streq(dentry->d_name, ".."))
continue; continue;
@ -279,7 +281,9 @@ char* r_readdir(r_dir_t *rdir) {
rdir->name[strlen(rdir->name)-1] == '/' ? "" : "/", rdir->name[strlen(rdir->name)-1] == '/' ? "" : "/",
dentry->d_name); dentry->d_name);
if (!stat(filename, &fstats) && S_ISDIR(fstats.st_mode)) { if (stat(filename, &fstats) < 0)
continue;
if (S_ISDIR(fstats.st_mode)) {
/* put subdirectory on the stack */ /* put subdirectory on the stack */
if (rdir->stlen == rdir->stcap) { if (rdir->stlen == rdir->stcap) {
rdir->stcap *= 2; rdir->stcap *= 2;
@ -295,19 +299,17 @@ char* r_readdir(r_dir_t *rdir) {
if (rdir->stlen > 0) { if (rdir->stlen > 0) {
/* open next subdirectory */ /* open next subdirectory */
closedir(rdir->dir); closedir(rdir->dir);
if (rdir->d) if (rdir->d != 0)
free(rdir->name); free(rdir->name);
rdir->name = rdir->stack[--rdir->stlen]; rdir->name = rdir->stack[--rdir->stlen];
rdir->d = 1; rdir->d = 1;
if (!(rdir->dir = opendir(rdir->name))) if ((rdir->dir = opendir(rdir->name)) == NULL)
warn("could not open directory: %s", rdir->name); warn("could not open directory: %s", rdir->name);
continue; continue;
} }
/* no more entries */ /* no more entries */
break; break;
} }
return NULL; return NULL;
} }
@ -316,10 +318,10 @@ int r_mkdir(const char *path) {
struct stat stats; struct stat stats;
int err = 0; int err = 0;
if (!path || !*path) if (path == NULL || *path == '\0')
return -1; return -1;
if (!stat(path, &stats)) { if (stat(path, &stats) == 0) {
if (S_ISDIR(stats.st_mode)) { if (S_ISDIR(stats.st_mode)) {
return 0; return 0;
} else { } else {
@ -331,16 +333,16 @@ int r_mkdir(const char *path) {
d = dir = (char*) s_malloc(strlen(path) + 1); d = dir = (char*) s_malloc(strlen(path) + 1);
strcpy(dir, path); strcpy(dir, path);
while (d != NULL && !err) { while (d != NULL && err == 0) {
d = strchr(d + 1, '/'); d = strchr(d + 1, '/');
if (d != NULL) if (d != NULL)
*d = '\0'; *d = '\0';
if (access(dir, F_OK) && errno == ENOENT) { if (access(dir, F_OK) < 0 && errno == ENOENT) {
if (mkdir(dir, 0755)) { if (mkdir(dir, 0755) < 0) {
warn("could not create directory: %s", dir); warn("could not create directory: %s", dir);
err = -1; err = -1;
} }
} else if (stat(dir, &stats) || !S_ISDIR(stats.st_mode)) { } else if (stat(dir, &stats) < 0 || !S_ISDIR(stats.st_mode)) {
warn("not a directory: %s", dir); warn("not a directory: %s", dir);
err = -1; err = -1;
} }

View File

@ -40,11 +40,11 @@ void win_init(win_t *win) {
win_env_t *e; win_env_t *e;
XColor col; XColor col;
if (!win) if (win == NULL)
return; return;
e = &win->env; e = &win->env;
if (!(e->dpy = XOpenDisplay(NULL))) if ((e->dpy = XOpenDisplay(NULL)) == NULL)
die("could not open display"); die("could not open display");
e->scr = DefaultScreen(e->dpy); e->scr = DefaultScreen(e->dpy);
@ -58,7 +58,7 @@ void win_init(win_t *win) {
win->white = WhitePixel(e->dpy, e->scr); win->white = WhitePixel(e->dpy, e->scr);
if (XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), BG_COLOR, if (XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), BG_COLOR,
&col, &col)) &col, &col) != 0)
{ {
win->bgcol = col.pixel; win->bgcol = col.pixel;
} else { } else {
@ -66,7 +66,7 @@ void win_init(win_t *win) {
} }
if (XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), SEL_COLOR, if (XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), SEL_COLOR,
&col, &col)) &col, &col) != 0)
{ {
win->selcol = col.pixel; win->selcol = col.pixel;
} else { } else {
@ -81,7 +81,7 @@ void win_init(win_t *win) {
void win_set_sizehints(win_t *win) { void win_set_sizehints(win_t *win) {
XSizeHints sizehints; XSizeHints sizehints;
if (!win || !win->xwin) if (win == NULL || win->xwin == None)
return; return;
sizehints.flags = PMinSize | PMaxSize; sizehints.flags = PMinSize | PMaxSize;
@ -100,32 +100,32 @@ void win_open(win_t *win) {
Pixmap none; Pixmap none;
int gmask; int gmask;
if (!win) if (win == NULL)
return; return;
e = &win->env; e = &win->env;
/* determine window offsets, width & height */ /* determine window offsets, width & height */
if (!options->geometry) if (options->geometry == NULL)
gmask = 0; gmask = 0;
else else
gmask = XParseGeometry(options->geometry, &win->x, &win->y, gmask = XParseGeometry(options->geometry, &win->x, &win->y,
&win->w, &win->h); &win->w, &win->h);
if (!(gmask & WidthValue)) if ((gmask & WidthValue) == 0)
win->w = WIN_WIDTH; win->w = WIN_WIDTH;
if (win->w > e->scrw) if (win->w > e->scrw)
win->w = e->scrw; win->w = e->scrw;
if (!(gmask & HeightValue)) if ((gmask & HeightValue) == 0)
win->h = WIN_HEIGHT; win->h = WIN_HEIGHT;
if (win->h > e->scrh) if (win->h > e->scrh)
win->h = e->scrh; win->h = e->scrh;
if (!(gmask & XValue)) if ((gmask & XValue) == 0)
win->x = (e->scrw - win->w) / 2; win->x = (e->scrw - win->w) / 2;
else if (gmask & XNegative) else if ((gmask & XNegative) != 0)
win->x += e->scrw - win->w; win->x += e->scrw - win->w;
if (!(gmask & YValue)) if ((gmask & YValue) == 0)
win->y = (e->scrh - win->h) / 2; win->y = (e->scrh - win->h) / 2;
else if (gmask & YNegative) else if ((gmask & YNegative) != 0)
win->y += e->scrh - win->h; win->y += e->scrh - win->h;
win->xwin = XCreateWindow(e->dpy, RootWindow(e->dpy, e->scr), win->xwin = XCreateWindow(e->dpy, RootWindow(e->dpy, e->scr),
@ -141,8 +141,8 @@ void win_open(win_t *win) {
chand = XCreateFontCursor(e->dpy, XC_fleur); chand = XCreateFontCursor(e->dpy, XC_fleur);
cwatch = XCreateFontCursor(e->dpy, XC_watch); cwatch = XCreateFontCursor(e->dpy, XC_watch);
if (!XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), "black", if (XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), "black",
&col, &col)) &col, &col) == 0)
{ {
die("could not allocate color: black"); die("could not allocate color: black");
} }
@ -171,7 +171,7 @@ void win_open(win_t *win) {
} }
void win_close(win_t *win) { void win_close(win_t *win) {
if (!win || !win->xwin) if (win == NULL || win->xwin == None)
return; return;
XFreeCursor(win->env.dpy, carrow); XFreeCursor(win->env.dpy, carrow);
@ -188,7 +188,7 @@ void win_close(win_t *win) {
bool win_configure(win_t *win, XConfigureEvent *c) { bool win_configure(win_t *win, XConfigureEvent *c) {
bool changed; bool changed;
if (!win) if (win == NULL)
return false; return false;
changed = win->w != c->width || win->h != c->height; changed = win->w != c->width || win->h != c->height;
@ -203,7 +203,7 @@ bool win_configure(win_t *win, XConfigureEvent *c) {
} }
bool win_moveresize(win_t *win, int x, int y, unsigned int w, unsigned int h) { bool win_moveresize(win_t *win, int x, int y, unsigned int w, unsigned int h) {
if (!win || !win->xwin) if (win == NULL || win->xwin == None)
return false; return false;
x = MAX(0, x); x = MAX(0, x);
@ -231,7 +231,7 @@ void win_toggle_fullscreen(win_t *win) {
XEvent ev; XEvent ev;
XClientMessageEvent *cm; XClientMessageEvent *cm;
if (!win || !win->xwin) if (win == NULL || win->xwin == None)
return; return;
win->fullscreen = !win->fullscreen; win->fullscreen = !win->fullscreen;
@ -255,13 +255,13 @@ void win_clear(win_t *win) {
win_env_t *e; win_env_t *e;
XGCValues gcval; XGCValues gcval;
if (!win || !win->xwin) if (win == NULL || win->xwin == None)
return; return;
e = &win->env; e = &win->env;
gcval.foreground = win->fullscreen ? win->black : win->bgcol; gcval.foreground = win->fullscreen ? win->black : win->bgcol;
if (win->pm) if (win->pm != None)
XFreePixmap(e->dpy, win->pm); XFreePixmap(e->dpy, win->pm);
win->pm = XCreatePixmap(e->dpy, win->xwin, e->scrw, e->scrh, e->depth); win->pm = XCreatePixmap(e->dpy, win->xwin, e->scrw, e->scrh, e->depth);
@ -270,7 +270,7 @@ void win_clear(win_t *win) {
} }
void win_draw(win_t *win) { void win_draw(win_t *win) {
if (!win || !win->xwin) if (win == NULL || win->xwin == None)
return; return;
XSetWindowBackgroundPixmap(win->env.dpy, win->xwin, win->pm); XSetWindowBackgroundPixmap(win->env.dpy, win->xwin, win->pm);
@ -281,7 +281,7 @@ void win_draw_rect(win_t *win, Pixmap pm, int x, int y, int w, int h,
bool fill, int lw, unsigned long col) { bool fill, int lw, unsigned long col) {
XGCValues gcval; XGCValues gcval;
if (!win || !pm) if (win == NULL || pm == None)
return; return;
gcval.line_width = lw; gcval.line_width = lw;
@ -295,10 +295,10 @@ void win_draw_rect(win_t *win, Pixmap pm, int x, int y, int w, int h,
} }
void win_set_title(win_t *win, const char *title) { void win_set_title(win_t *win, const char *title) {
if (!win || !win->xwin) if (win == NULL || win->xwin == None)
return; return;
if (!title) if (title == NULL)
title = "sxiv"; title = "sxiv";
XStoreName(win->env.dpy, win->xwin, title); XStoreName(win->env.dpy, win->xwin, title);
@ -315,7 +315,7 @@ void win_set_title(win_t *win, const char *title) {
} }
void win_set_cursor(win_t *win, cursor_t cursor) { void win_set_cursor(win_t *win, cursor_t cursor) {
if (!win || !win->xwin) if (win == NULL || win->xwin == None)
return; return;
switch (cursor) { switch (cursor) {