From 7fc5856e6453fc60d73150cda10196b932adddd5 Mon Sep 17 00:00:00 2001 From: FRIGN Date: Thu, 13 Nov 2014 22:16:29 +0100 Subject: [PATCH] Tweak NULL-pointer checks Use !p and p when comparing pointers as opposed to explicit checks against NULL. This is generally easier to read. --- cksum.c | 2 +- readlink.c | 2 +- sort.c | 8 ++++---- split.c | 4 ++-- uniq.c | 4 ++-- util/crypt.c | 6 +++--- util/getlines.c | 2 +- uudecode.c | 12 ++++++------ 8 files changed, 20 insertions(+), 20 deletions(-) diff --git a/cksum.c b/cksum.c index 9e914ec..9867796 100644 --- a/cksum.c +++ b/cksum.c @@ -114,7 +114,7 @@ cksum(FILE *fp, const char *s) ck = (ck << 8) ^ crctab[(ck >> 24) ^ (i & 0xFF)]; printf("%"PRIu32" %lu", ~ck, (unsigned long)len); - if (s != NULL) + if (s) printf(" %s", s); putchar('\n'); } diff --git a/readlink.c b/readlink.c index 2d6cc94..10b7160 100644 --- a/readlink.c +++ b/readlink.c @@ -43,7 +43,7 @@ main(int argc, char *argv[]) return 1; if (fflag) { - if (realpath(argv[0], buf) == NULL) + if (!realpath(argv[0], buf)) exit(1); } else { if ((n = readlink(argv[0], buf, sizeof(buf) - 1)) < 0) diff --git a/sort.c b/sort.c index 5513442..ebe0d82 100644 --- a/sort.c +++ b/sort.c @@ -238,12 +238,12 @@ skipblank(char *s) static char * nextcol(char *s) { - if (fieldsep == NULL) { + if (!fieldsep) { s = skipblank(s); while(*s && !isblank(*s)) s++; } else { - if (strchr(s, *fieldsep) == NULL) + if (!strchr(s, *fieldsep)) s = strchr(s, '\0'); else s = strchr(s, *fieldsep) + 1; @@ -274,11 +274,11 @@ columns(char *line, const struct keydef *kd) else end = nextcol(end); } else { - if ((end = strchr(line, '\n')) == NULL) + if (!(end = strchr(line, '\n'))) end = strchr(line, '\0'); } - if ((res = strndup(start, end - start)) == NULL) + if (!(res = strndup(start, end - start))) enprintf(2, "strndup:"); return res; } diff --git a/split.c b/split.c index 22f5029..6bced97 100644 --- a/split.c +++ b/split.c @@ -36,11 +36,11 @@ main(int argc, char *argv[]) case 'b': always = 1; tmp = ARGF(); - if (tmp == NULL) + if (!tmp) break; size = strtoull(tmp, &end, 10); - if (*end == '\0') + if (!*end) break; switch (toupper((int)*end)) { case 'K': diff --git a/uniq.c b/uniq.c index 7d4caba..f07c40a 100644 --- a/uniq.c +++ b/uniq.c @@ -62,7 +62,7 @@ main(int argc, char *argv[]) static void uniqline(char *l) { - int linesequel = ((l == NULL) || (prevline == NULL)) + int linesequel = (!l || !prevline) ? l == prevline : !strcmp(l, prevline); @@ -71,7 +71,7 @@ uniqline(char *l) return; } - if (prevline != NULL) { + if (prevline) { if ((prevlinecount == 1 && !dflag) || (prevlinecount != 1 && !uflag)) { printf(countfmt, prevlinecount); diff --git a/util/crypt.c b/util/crypt.c index cbdf2df..c331cf4 100644 --- a/util/crypt.c +++ b/util/crypt.c @@ -46,7 +46,7 @@ cryptcheck(char *sumfile, int argc, char *argv[], int r, nonmatch = 0, formatsucks = 0, noread = 0, ret = 0; size_t bufsiz = 0; - if (sumfile == NULL) + if (!sumfile) cfp = stdin; else if (!(cfp = fopen(sumfile, "r"))) eprintf("fopen %s:", sumfile); @@ -81,7 +81,7 @@ cryptcheck(char *sumfile, int argc, char *argv[], } fclose(fp); } - if (sumfile != NULL) + if (sumfile) fclose(cfp); free(line); if (formatsucks > 0) { @@ -111,7 +111,7 @@ cryptmain(int argc, char *argv[], mdprint(md, "", sz); } else { for (; argc > 0; argc--) { - if ((fp = fopen(*argv, "r")) == NULL) { + if (!(fp = fopen(*argv, "r"))) { weprintf("fopen %s:", *argv); ret = 1; continue; diff --git a/util/getlines.c b/util/getlines.c index a766ba4..ee9457f 100644 --- a/util/getlines.c +++ b/util/getlines.c @@ -17,7 +17,7 @@ getlines(FILE *fp, struct linebuf *b) if (++b->nlines > b->capacity) { b->capacity += 512; nline = realloc(b->lines, b->capacity * sizeof(*b->lines)); - if (nline == NULL) + if (!nline) eprintf("realloc:"); b->lines = nline; } diff --git a/uudecode.c b/uudecode.c index 3ad4fed..09b5d63 100644 --- a/uudecode.c +++ b/uudecode.c @@ -40,14 +40,14 @@ main(int argc, char *argv[]) if (argc == 0) { parseheader(stdin, "", "begin ", &mode, &fname); - if ((nfp = parsefile(fname)) == NULL) + if (!(nfp = parsefile(fname))) eprintf("fopen %s:", fname); uudecode(stdin, nfp); } else { - if ((fp = fopen(argv[0], "r")) == NULL) + if (!(fp = fopen(argv[0], "r"))) eprintf("fopen %s:", argv[0]); parseheader(fp, argv[0], "begin ", &mode, &fname); - if ((nfp = parsefile(fname)) == NULL) + if (!(nfp = parsefile(fname))) eprintf("fopen %s:", fname); uudecode(fp, nfp); } @@ -93,18 +93,18 @@ parseheader(FILE *fp, const char *s, const char *header, mode_t *mode, char **fn char *p, *q; size_t n; - if (fgets(bufs, sizeof(bufs), fp) == NULL) + if (!fgets(bufs, sizeof(bufs), fp)) if (ferror(fp)) eprintf("%s: read error:", s); if (bufs[0] == '\0' || feof(fp)) eprintf("empty or nil header string\n"); - if ((p = strchr(bufs, '\n')) == NULL) + if (!(p = strchr(bufs, '\n'))) eprintf("header string too long or non-newline terminated file\n"); p = bufs; if (strncmp(bufs, header, strlen(header)) != 0) eprintf("malformed header prefix\n"); p += strlen(header); - if ((q = strchr(p, ' ')) == NULL) + if (!(q = strchr(p, ' '))) eprintf("malformed mode string in header\n"); *q++ = '\0'; /* now mode should be null terminated, q points to fname */