diff options
author | John Ankarstrom <john@ankarstrom.se> | 2021-07-06 21:39:59 +0200 |
---|---|---|
committer | John Ankarstrom <john@ankarstrom.se> | 2021-07-06 21:39:59 +0200 |
commit | 0861d76717ceaff31bf8172e6fa117c8f03d536e (patch) | |
tree | 3957aac34aae6a3ef17124d9dff83a45fbc6cab9 | |
parent | dcb7758a74fb5ff4f8b6eecb6589223a408f3f9b (diff) | |
download | build-0861d76717ceaff31bf8172e6fa117c8f03d536e.tar.gz |
Add -d flag
-rw-r--r-- | build.c | 87 |
1 files changed, 64 insertions, 23 deletions
@@ -4,21 +4,37 @@ #include <stdio.h> #include <string.h> #include <sys/stat.h> +#include <unistd.h> #define USAGE "usage: %s file [...]\n", argv[0] +extern char *optarg; +extern int optind; + int main(int argc, char *argv[]) { char *b, *bb, *bbb, buf[1024], *cmd[32], dep[1024], *t, *tt, tgt[64]; FILE *fp; - int i, icmd, j, max; + int c, dflag, i, icmd, j, max; struct stat sb, ssb; tgt[0] = dep[0] = 0; /* process arguments */ - if (argc-1 == 0) { + dflag = 0; + while ((c = getopt(argc, argv, "d")) != -1) + switch (c) { + case 'd': + dflag++; + break; + default: + fprintf(stderr, USAGE); + return 1; + } + argc -= optind; + argv += optind; + if (argc == 0) { fprintf(stderr, USAGE); return 1; } @@ -30,7 +46,7 @@ main(int argc, char *argv[]) } /* process each file */ - for (i = 1, icmd = 0; i < argc; i++, icmd = 0) { + for (i = icmd = 0; i < argc; i++, icmd = 0) { fp = fopen(argv[i], "r"); if (!fp) err(1, "fopen"); @@ -55,12 +71,21 @@ main(int argc, char *argv[]) strncpy(tgt,b+1,sizeof(tgt)-1); } + + if (dflag < 3) continue; + fprintf(stderr, + "%s: command line '%s'\n", + argv[i], buf); continue; } - /* depend line */ + /* depends line */ if (*(b+1) == '%' && *(b+2) == ' ') { strncpy(dep, b+3, sizeof(dep)-1); + if (dflag < 3) continue; + fprintf(stderr, + "%s: depends line '%s'\n", + argv[i], buf); continue; } } @@ -83,32 +108,48 @@ main(int argc, char *argv[]) err(1, t); if (stat(argv[i], &sb)) err(1, argv[i]); - if (sb.st_mtime > ssb.st_mtime) + if (sb.st_mtime > ssb.st_mtime) { + if (dflag) + fprintf(stderr, "%s: building because " + "%s is modified\n", argv[i], argv[i]); goto build; + } + + if (!*dep) + goto uptodate; /* compare dependencies > target */ - if (*dep) { - for (t = dep; *t; t++) - if (!isspace(*t)) break; - for (j = 0; t[j]; j++) { - if (isspace(t[j]) || t[j+1] == 0) { - tt = strdup(t); - tt[j+(t[j+1]==0)] = 0; - - if (stat(tt, &sb)) - err(1, tt); - free(tt); - if (sb.st_mtime > ssb.st_mtime) - goto build; - - for (; t[j]; j++) - if (!isspace(t[j])) break; - t += j; - j = 0; + for (t = dep; *t; t++) + if (!isspace(*t)) break; + for (j = 0; t[j]; j++) { + if (isspace(t[j]) || t[j+1] == 0) { + tt = strdup(t); + tt[j+(t[j+1]==0)] = 0; + + if (dflag > 1) + fprintf(stderr, "%s: depend '%s'\n", + argv[i], tt); + + if (stat(tt, &sb)) + err(1, tt); + free(tt); + if (sb.st_mtime > ssb.st_mtime) { + if (dflag) + fprintf(stderr, + "%s: building because " + "%s is modified\n", + argv[i], tt); + goto build; } + + for (; t[j]; j++) + if (!isspace(t[j])) break; + t += j; + j = 0; } } +uptodate: fprintf(stderr, "%s: already up-to-date\n", argv[i]); goto done; |