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

View file

@ -14,10 +14,11 @@ inline int puts_if(const char *s, int c) {
}
int main(int argc, char **argv) {
int i, n = 0;
int i;
unsigned int n = 0;
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':
n += PUT_MACRO(EXIF_SUPPORT);
n += PUT_MACRO(GIF_SUPPORT);
@ -31,7 +32,7 @@ int main(int argc, char **argv) {
return 1;
}
}
if (n)
if (n > 0)
printf("\n");
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_max = zoom_levels[ARRLEN(zoom_levels) - 1] / 100.0;
if (!img || !win)
if (img == NULL || win == NULL)
return;
imlib_context_set_display(win->env.dpy);
@ -77,15 +77,15 @@ void exif_auto_orientate(const fileinfo_t *file) {
ExifEntry *entry;
int byte_order, orientation;
if (!(ed = exif_data_new_from_file(file->path)))
if ((ed = exif_data_new_from_file(file->path)) == NULL)
return;
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);
orientation = exif_get_short(entry->data, byte_order);
}
exif_data_unref(ed);
if (!entry)
if (entry == NULL)
return;
switch (orientation) {
@ -145,7 +145,7 @@ bool img_load_gif(img_t *img, const fileinfo_t *file) {
img->multi.sel = 0;
gif = DGifOpenFileName(file->path);
if (!gif) {
if (gif == NULL) {
warn("could not open gif file: %s", file->name);
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 ||
rows[i-y][j-x] == transp)
{
if (prev_frame)
if (prev_frame != NULL)
*ptr = prev_frame[i * sw + j];
else
*ptr = bgpixel;
@ -236,7 +236,7 @@ bool img_load_gif(img_t *img, const fileinfo_t *file) {
free(rows);
free(data);
if (!im) {
if (im == NULL) {
err = true;
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) {
const char *fmt;
if (!img || !file || !file->name || !file->path)
if (img == NULL || file == NULL || file->name == NULL || file->path == NULL)
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);
return false;
}
@ -324,17 +326,17 @@ bool img_load(img_t *img, const fileinfo_t *file) {
void img_close(img_t *img, bool decache) {
int i;
if (!img)
if (img == NULL)
return;
if (img->multi.cnt) {
if (img->multi.cnt > 0) {
for (i = 0; i < img->multi.cnt; i++) {
imlib_context_set_image(img->multi.frames[i].im);
imlib_free_image();
}
img->multi.cnt = 0;
img->im = NULL;
} else if (img->im) {
} else if (img->im != NULL) {
imlib_context_set_image(img->im);
if (decache)
imlib_free_image_and_decache();
@ -348,7 +350,7 @@ void img_check_pan(img_t *img, bool moved) {
win_t *win;
int ox, oy;
if (!img || !img->im || !img->win)
if (img == NULL || img->im == NULL || img->win == NULL)
return;
win = img->win;
@ -379,7 +381,7 @@ void img_check_pan(img_t *img, bool moved) {
bool img_fit(img_t *img) {
float z, zmax, zw, zh;
if (!img || !img->im || !img->win)
if (img == NULL || img->im == NULL || img->win == NULL)
return false;
if (img->scalemode == SCALE_ZOOM)
return false;
@ -406,7 +408,7 @@ void img_render(img_t *img) {
int sx, sy, sw, sh;
int dx, dy, dw, dh;
if (!img || !img->im || !img->win)
if (img == NULL || img->im == NULL || img->win == NULL)
return;
win = img->win;
@ -461,7 +463,7 @@ void img_render(img_t *img) {
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);
imlib_context_set_drawable(win->pm);
@ -473,7 +475,7 @@ void img_render(img_t *img) {
}
bool img_fit_win(img_t *img) {
if (!img || !img->im)
if (img == NULL || img->im == NULL)
return false;
img->scalemode = SCALE_FIT;
@ -483,7 +485,7 @@ bool img_fit_win(img_t *img) {
bool img_center(img_t *img) {
int ox, oy;
if (!img || !img->im || !img->win)
if (img == NULL || img->im == NULL || img->win == NULL)
return false;
ox = img->x;
@ -501,7 +503,7 @@ bool img_center(img_t *img) {
}
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;
z = MAX(z, zoom_min);
@ -524,7 +526,7 @@ bool img_zoom(img_t *img, float z) {
bool img_zoom_in(img_t *img) {
int i;
if (!img || !img->im)
if (img == NULL || img->im == NULL)
return false;
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) {
int i;
if (!img || !img->im)
if (img == NULL || img->im == NULL)
return false;
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) {
int ox, oy;
if (!img || !img->im)
if (img == NULL || img->im == NULL)
return false;
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) {
if (!img || !img->im || !img->win)
if (img == NULL || img->im == NULL || img->win == NULL)
return false;
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) {
int ox, oy;
if (!img || !img->im || !img->win)
if (img == NULL || img->im == NULL || img->win == NULL)
return false;
ox = img->x;
@ -624,7 +626,7 @@ void img_rotate(img_t *img, int d) {
win_t *win;
int ox, oy, tmp;
if (!img || !img->im || !img->win)
if (img == NULL || img->im == NULL || img->win == NULL)
return;
win = img->win;
@ -654,7 +656,7 @@ void img_rotate_right(img_t *img) {
}
void img_toggle_antialias(img_t *img) {
if (!img || !img->im)
if (img == NULL || img->im == NULL)
return;
img->aa = !img->aa;
@ -664,7 +666,7 @@ void img_toggle_antialias(img_t *img) {
}
bool img_frame_goto(img_t *img, int n) {
if (!img || !img->im)
if (img == NULL || img->im == NULL)
return false;
if (n < 0 || n >= img->multi.cnt || n == img->multi.sel)
return false;
@ -682,7 +684,7 @@ bool img_frame_goto(img_t *img, int n) {
}
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;
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) {
if (!img || !img->im || !img->multi.cnt)
if (img == NULL || img->im == NULL || img->multi.cnt == 0)
return false;
if (img->multi.sel + 1 >= img->multi.cnt) {

56
main.c
View file

@ -73,9 +73,10 @@ timeout_t timeouts[] = {
};
void cleanup() {
static int in = 0;
static bool in = false;
if (!in++) {
if (!in) {
in = true;
img_close(&img, false);
tns_free(&tns);
win_close(&win);
@ -83,10 +84,10 @@ void cleanup() {
}
void check_add_file(char *filename) {
if (!filename || !*filename)
if (filename == NULL || *filename == '\0')
return;
if (access(filename, R_OK)) {
if (access(filename, R_OK) < 0) {
warn("could not open file: %s", filename);
return;
}
@ -97,7 +98,7 @@ void check_add_file(char *filename) {
}
if (*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);
return;
}
@ -173,7 +174,7 @@ bool check_timeouts(struct timeval *t) {
tdiff = tv_diff(&timeouts[i].when, &now);
if (tdiff <= 0) {
timeouts[i].active = false;
if (timeouts[i].handler)
if (timeouts[i].handler != NULL)
timeouts[i].handler();
i = tmin = -1;
} else if (tmin < 0 || tdiff < tmin) {
@ -182,7 +183,7 @@ bool check_timeouts(struct timeval *t) {
}
i++;
}
if (tmin > 0 && t)
if (tmin > 0 && t != NULL)
tv_set_msec(t, tmin);
return tmin > 0;
}
@ -204,12 +205,12 @@ void load_image(int new) {
files[new].loaded = true;
fileidx = new;
if (!stat(files[new].path, &fstats))
if (stat(files[new].path, &fstats) == 0)
filesize = fstats.st_size;
else
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);
else
reset_timeout(animate);
@ -238,7 +239,7 @@ void update_title() {
} else {
sshow_info[0] = '\0';
}
if (img.multi.cnt)
if (img.multi.cnt > 0)
snprintf(frame_info, sizeof(frame_info), "{%d/%d} ",
img.multi.sel + 1, img.multi.cnt);
else
@ -329,14 +330,14 @@ void on_keypress(XKeyEvent *kev) {
KeySym ksym;
char key;
if (!kev)
if (kev == NULL)
return;
XLookupString(kev, &key, 1, &ksym, NULL);
for (i = 0; i < ARRLEN(keys); i++) {
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();
return;
}
@ -346,7 +347,7 @@ void on_keypress(XKeyEvent *kev) {
void on_buttonpress(XButtonEvent *bev) {
int i, sel;
if (!bev)
if (bev == NULL)
return;
if (mode == MODE_IMAGE) {
@ -357,7 +358,7 @@ void on_buttonpress(XButtonEvent *bev) {
if (buttons[i].button == bev->button &&
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();
return;
}
@ -397,9 +398,9 @@ void run() {
redraw();
while (1) {
while (true) {
while (mode == MODE_THUMB && tns.cnt < filecnt &&
!XPending(win.env.dpy))
XPending(win.env.dpy) == 0)
{
/* load thumbnails */
set_timeout(redraw, TO_REDRAW_THUMBS, false);
@ -413,7 +414,7 @@ void run() {
check_timeouts(NULL);
}
while (!XPending(win.env.dpy) && check_timeouts(&timeout)) {
while (XPending(win.env.dpy) == 0 && check_timeouts(&timeout)) {
/* wait for timeouts */
xfd = ConnectionNumber(win.env.dpy);
FD_ZERO(&fds);
@ -421,9 +422,9 @@ void run() {
select(xfd + 1, &fds, 0, 0, &timeout);
}
if (!XNextEvent(win.env.dpy, &ev)) {
/* handle events */
XNextEvent(win.env.dpy, &ev);
switch (ev.type) {
/* handle events */
case ButtonPress:
on_buttonpress(&ev.xbutton);
break;
@ -451,7 +452,6 @@ void run() {
break;
}
}
}
}
int fncmp(const void *a, const void *b) {
@ -474,7 +474,7 @@ int main(int argc, char **argv) {
exit(EXIT_SUCCESS);
}
if (!options->filecnt) {
if (options->filecnt == 0) {
print_usage();
exit(EXIT_FAILURE);
}
@ -495,25 +495,29 @@ int main(int argc, char **argv) {
filename[len-1] = '\0';
check_add_file(filename);
}
if (filename)
if (filename != NULL)
free(filename);
} else {
for (i = 0; i < options->filecnt; 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);
} else {
if (!options->recursive) {
warn("ignoring directory: %s", filename);
continue;
}
if (r_opendir(&dir, filename)) {
if (r_opendir(&dir, filename) < 0) {
warn("could not open directory: %s", filename);
continue;
}
start = fileidx;
while ((filename = r_readdir(&dir))) {
while ((filename = r_readdir(&dir)) != NULL) {
check_add_file(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");
exit(EXIT_FAILURE);
}

View file

@ -95,7 +95,7 @@ void parse_options(int argc, char **argv) {
print_usage();
exit(EXIT_SUCCESS);
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",
optarg);
exit(EXIT_FAILURE);
@ -127,7 +127,7 @@ void parse_options(int argc, char **argv) {
break;
case 'z':
_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",
optarg);
exit(EXIT_FAILURE);

117
thumbs.c
View file

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

92
util.c
View file

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

View file

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