summaryrefslogtreecommitdiffstats
path: root/util.c
diff options
context:
space:
mode:
authorBert Münnich <ber.t@gmx.com>2011-09-29 12:43:36 +0200
committerBert Münnich <ber.t@gmx.com>2011-09-29 12:43:36 +0200
commit8dcf682de9dc9bf5fe4a98205acfa2835499baf2 (patch)
tree155c2cd6f8be1dc7b7b73d9bf18a2af1766dc79f /util.c
parent22d4e991d5726ca034924fa697a32a42578202c2 (diff)
downloadnsxiv-8dcf682de9dc9bf5fe4a98205acfa2835499baf2.tar.zst
Made all conditionals more precise
Diffstat (limited to 'util.c')
-rw-r--r--util.c92
1 files changed, 47 insertions, 45 deletions
diff --git a/util.c b/util.c
index a205312..772908d 100644
--- a/util.c
+++ b/util.c
@@ -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;
}