From da540383a99d13009e66e34bce13804647e264bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Fri, 9 Jul 2021 21:55:48 +0200 Subject: Allocate inside condition --- build.c | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/build.c b/build.c index f0b497e..4efcede 100644 --- a/build.c +++ b/build.c @@ -38,14 +38,13 @@ main(int argc, char *argv[]) struct stat sb, ssb; /* Allocate memory. */ - for (i = 0; i < MAXCMDS; i++) { - cmd[i] = malloc(MAXCMD*sizeof(char)); - if (!cmd[i]) err(1, "malloc"); - } - dep = malloc(MAXDEP*sizeof(char)); - if (!dep) err(1, "malloc"); - tgt = malloc(MAXTGT*sizeof(char)); - if (!tgt) err(1, "malloc"); + for (i = 0; i < MAXCMDS; i++) + if (!(cmd[i] = malloc(MAXCMD))) + err(1, "malloc"); + if (!(dep = malloc(MAXDEP))) + err(1, "malloc"); + if (!(tgt = malloc(MAXTGT))) + err(1, "malloc"); tgt[0] = dep[0] = 0; @@ -73,8 +72,8 @@ main(int argc, char *argv[]) /* Process dependencies and commands in each file. */ for (i = icmd = 0; i < argc; i++, icmd = 0) { - fp = fopen(argv[i], "r"); - if (!fp) err(1, "fopen"); + if (!(fp = fopen(argv[i], "r"))) + err(1, "fopen"); /* Read line by line, at most twenty. */ for (j = 0; j < 20 && fgets(buf, MAXBUF, fp); j++) { -- cgit v1.2.3