From d8f19d9417b12325b8918e117514d1a278515f9d Mon Sep 17 00:00:00 2001 From: Peter Wemm Date: Fri, 12 Jan 1996 08:57:10 +0000 Subject: [PATCH] Make a little more effort to avoid touching certain generated files if they were not changed. This makes 'make depend' more useful. --- usr.sbin/config/config.h | 1 + usr.sbin/config/main.c | 65 ++++++++++++++++++++++++++++++++++++ usr.sbin/config/mkglue.c | 5 +-- usr.sbin/config/mkioconf.c | 10 +++--- usr.sbin/config/mkmakefile.c | 5 +-- usr.sbin/config/mkswapconf.c | 9 +++-- 6 files changed, 84 insertions(+), 11 deletions(-) diff --git a/usr.sbin/config/config.h b/usr.sbin/config/config.h index 41e492fc9123..65c9b66bb585 100644 --- a/usr.sbin/config/config.h +++ b/usr.sbin/config/config.h @@ -192,6 +192,7 @@ char *get_word(); char *get_quoted_word(); char *path(); char *raise(); +void moveifchanged(); int do_trace; diff --git a/usr.sbin/config/main.c b/usr.sbin/config/main.c index 3369504765a9..a203769e164f 100644 --- a/usr.sbin/config/main.c +++ b/usr.sbin/config/main.c @@ -44,8 +44,11 @@ static char sccsid[] = "@(#)main.c 8.1 (Berkeley) 6/6/93"; #include #include #include +#include #include #include +#include +#include #include "y.tab.h" #include "config.h" @@ -328,3 +331,65 @@ path(file) } return (cp); } + +/* + * moveifchanged -- + * compare two files; rename if changed. + */ +void +moveifchanged(const char *from_name, const char *to_name) +{ + char *p, *q; + int changed; + size_t tsize; + struct stat from_sb, to_sb; + int from_fd, to_fd; + + changed = 0; + + if ((from_fd = open(from_name, O_RDONLY)) < 0) + err(EX_OSERR, "moveifchanged open(%s)", from_name); + + if ((to_fd = open(to_name, O_RDONLY)) < 0) + changed++; + + if (!changed && fstat(from_fd, &from_sb) < 0) + err(EX_OSERR, "moveifchanged fstat(%s)", from_name); + + if (!changed && fstat(to_fd, &to_sb) < 0) + err(EX_OSERR, "moveifchanged fstat(%s)", to_name); + + if (!changed && from_sb.st_size != to_sb.st_size) + changed++; + + tsize = (size_t)from_sb.st_size; + + if (!changed) { + p = mmap(NULL, tsize, PROT_READ, 0, from_fd, (off_t)0); + if ((long)p == -1) + err(EX_OSERR, "mmap %s", from_name); + q = mmap(NULL, tsize, PROT_READ, 0, to_fd, (off_t)0); + if ((long)q == -1) + err(EX_OSERR, "mmap %s", to_name); + + changed = memcmp(p, q, tsize); + munmap(p, tsize); + munmap(q, tsize); + } + if (changed) { + if (rename(from_name, to_name) < 0) + err(EX_OSERR, "rename(%s, %s)", from_name, to_name); + } else { + if (unlink(from_name) < 0) + err(EX_OSERR, "unlink(%s, %s)", from_name); + } + +#ifdef DIAG + if (changed) + printf("CHANGED! rename (%s, %s)\n", from_name, to_name); + else + printf("SAME! unlink (%s)\n", from_name); +#endif + + return; +} diff --git a/usr.sbin/config/mkglue.c b/usr.sbin/config/mkglue.c index aa5ecfe26f2a..d5174c7d840f 100644 --- a/usr.sbin/config/mkglue.c +++ b/usr.sbin/config/mkglue.c @@ -346,9 +346,9 @@ vector() int dev_id; FILE *fp; - fp = fopen(path("vector.h"), "w"); + fp = fopen(path("vector.h.new"), "w"); if (fp == NULL) { - perror(path("vector.h")); + perror(path("vector.h.new")); exit(1); } fprintf(fp, "/*\n"); @@ -367,6 +367,7 @@ vector() fprintf(fp, "\"\n\n"); fprintf(fp, "#define\tNR_DEVICES\t%d\n", dev_id); (void) fclose(fp); + moveifchanged(path("vector.h.new"), path("vector.h")); } vector_devtab(fp, table, dev_idp) diff --git a/usr.sbin/config/mkioconf.c b/usr.sbin/config/mkioconf.c index 378b6fb936f6..9eda8f026e31 100644 --- a/usr.sbin/config/mkioconf.c +++ b/usr.sbin/config/mkioconf.c @@ -608,9 +608,9 @@ i386_ioconf() static char *old_d_name; static char old_shandler[32 + 1]; - fp = fopen(path("ioconf.c"), "w"); + fp = fopen(path("ioconf.c.new"), "w"); if (fp == 0) { - perror(path("ioconf.c")); + perror(path("ioconf.c.new")); exit(1); } fprintf(fp, "/*\n"); @@ -622,9 +622,9 @@ i386_ioconf() fprintf(fp, "#include \"ioconf.h\"\n"); fprintf(fp, "\n"); fprintf(fp, "#define C (caddr_t)\n"); - fp1 = fopen(path("ioconf.h"), "w"); + fp1 = fopen(path("ioconf.h.new"), "w"); if (fp1 == 0) { - perror(path("ioconf.h")); + perror(path("ioconf.h.new")); exit(1); } fprintf(fp1, "/*\n"); @@ -701,6 +701,8 @@ i386_ioconf() fprintf(fp1, "\n"); fprintf(fp1, "#endif /* IOCONF_H */\n"); (void) fclose(fp1); + moveifchanged(path("ioconf.c.new"), path("ioconf.c")); + moveifchanged(path("ioconf.h.new"), path("ioconf.h")); } isa_biotab(fp, table) diff --git a/usr.sbin/config/mkmakefile.c b/usr.sbin/config/mkmakefile.c index c129ee60e1e0..0f057f472742 100644 --- a/usr.sbin/config/mkmakefile.c +++ b/usr.sbin/config/mkmakefile.c @@ -150,9 +150,9 @@ makefile() perror(line); exit(1); } - ofp = fopen(path("Makefile"), "w"); + ofp = fopen(path("Makefile.new"), "w"); if (ofp == 0) { - perror(path("Makefile")); + perror(path("Makefile.new")); exit(1); } fprintf(ofp, "KERN_IDENT=%s\n", raise(ident)); @@ -234,6 +234,7 @@ makefile() } (void) fclose(ifp); (void) fclose(ofp); + moveifchanged(path("Makefile.new"), path("Makefile")); #ifdef notyet if (warn_make_clean) { printf("WARNING: Unknown options used (not in ../../conf/options or ./options.%s).\n", machinename); diff --git a/usr.sbin/config/mkswapconf.c b/usr.sbin/config/mkswapconf.c index 39d964b561cc..1d5756ff1ff2 100644 --- a/usr.sbin/config/mkswapconf.c +++ b/usr.sbin/config/mkswapconf.c @@ -66,6 +66,7 @@ do_swap(fl) register struct file_list *fl; { FILE *fp; + char newswapname[80]; char swapname[80]; register struct file_list *swap; dev_t dev; @@ -75,9 +76,10 @@ do_swap(fl) return (fl->f_next); } (void) sprintf(swapname, "swap%s.c", fl->f_fn); - fp = fopen(path(swapname), "w"); + (void) sprintf(newswapname, "swap%s.c.new", fl->f_fn); + fp = fopen(path(newswapname), "w"); if (fp == 0) { - perror(path(swapname)); + perror(path(newswapname)); exit(1); } fprintf(fp, "#include \n"); @@ -90,7 +92,7 @@ do_swap(fl) */ swap = fl->f_next; if (swap == 0 || swap->f_type != SWAPSPEC) { - (void) unlink(path(swapname)); + (void) unlink(path(newswapname)); fclose(fp); return (swap); } @@ -107,6 +109,7 @@ do_swap(fl) fprintf(fp, "\n"); fprintf(fp, "void\nsetconf()\n{\n}\n"); fclose(fp); + moveifchanged(path(newswapname), path(swapname)); return (swap); }