diff options
author | John Ankarström <john@ankarstrom.se> | 2021-07-09 21:55:48 +0200 |
---|---|---|
committer | John Ankarström <john@ankarstrom.se> | 2021-07-09 22:01:31 +0200 |
commit | da540383a99d13009e66e34bce13804647e264bc (patch) | |
tree | cd35661db4bfd796b5ea3980d7862a68973138ab | |
parent | b9dde5d325655037ab804469a1557c7c4a4dcfc6 (diff) | |
download | build-da540383a99d13009e66e34bce13804647e264bc.tar.gz |
Allocate inside condition
-rw-r--r-- | build.c | 19 |
1 files changed, 9 insertions, 10 deletions
@@ -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++) { |