added -r option to remove extra files.
[unix-history] / usr / src / usr.bin / rdist / docmd.c
index a10ea3c..f9c4b73 100644 (file)
@@ -1,5 +1,5 @@
 #ifndef lint
 #ifndef lint
-static char *sccsid = "@(#)docmd.c     4.3 (Berkeley) 83/10/10";
+static char *sccsid = "@(#)docmd.c     4.5 (Berkeley) 83/10/20";
 #endif
 
 #include "defs.h"
 #endif
 
 #include "defs.h"
@@ -51,13 +51,17 @@ dohcmds(files, hosts, cmds)
                        }
                found:
                        n = 0;
                        }
                found:
                        n = 0;
-                       for (c = cmds; c != NULL; c = c->b_next)
-                               if (c->b_type == INSTALL) {
+                       for (c = cmds; c != NULL; c = c->b_next) {
+                               if (c->b_type != INSTALL)
+                                       continue;
+                               n++;
+                               if (c->b_name == NULL)
+                                       install(f->b_name, f->b_name, 0, c->b_options);
+                               else
                                        install(f->b_name, c->b_name, ddir, c->b_options);
                                        install(f->b_name, c->b_name, ddir, c->b_options);
-                                       n++;
-                               }
+                       }
                        if (n == 0)
                        if (n == 0)
-                               install(f->b_name, f->b_name, 0, 0);
+                               install(f->b_name, f->b_name, 0, options);
                }
                if (!nflag) {
                        /* signal end of connection */
                }
                if (!nflag) {
                        /* signal end of connection */
@@ -67,7 +71,7 @@ dohcmds(files, hosts, cmds)
                }
                for (c = cmds; c != NULL; c = c->b_next)
                        if (c->b_type == NOTIFY)
                }
                for (c = cmds; c != NULL; c = c->b_next)
                        if (c->b_type == NOTIFY)
-                               notify(tmpfile, h->b_name, c->b_args);
+                               notify(tmpfile, h->b_name, c->b_args, 0);
        }
        if (!nflag)
                (void) unlink(tmpfile);
        }
        if (!nflag)
                (void) unlink(tmpfile);
@@ -82,9 +86,8 @@ makeconn(rhost)
        register char *ruser;
        extern char user[];
 
        register char *ruser;
        extern char user[];
 
-       (void) sprintf(buf, "/usr/local/rdist -Server%s%s%s%s%s",
-               vflag ? " -v" : "", qflag ? " -q" : "", nflag ? " -n" : "",
-               yflag ? " -y" : "", debug ? " -D" : "");
+       (void) sprintf(buf, "/usr/local/rdist -Server%s%s",
+               nflag ? " -n" : "", qflag ? " -q" : "");
 
        ruser = rindex(rhost, '.');
        if (ruser != NULL) {
 
        ruser = rindex(rhost, '.');
        if (ruser != NULL) {
@@ -114,9 +117,9 @@ makeconn(rhost)
  * destdir = 1 if destination should be a directory
  * (i.e., more than one source is being copied to the same destination).
  */
  * destdir = 1 if destination should be a directory
  * (i.e., more than one source is being copied to the same destination).
  */
-install(src, dest, destdir, options)
+install(src, dest, destdir, opts)
        char *src, *dest;
        char *src, *dest;
-       int destdir, options;
+       int destdir, opts;
 {
        register char *cp;
 
 {
        register char *cp;
 
@@ -124,8 +127,10 @@ install(src, dest, destdir, options)
                return;
 
        if (nflag || debug) {
                return;
 
        if (nflag || debug) {
-               printf("%s%s %s %s\n", options & VERIFY ? "verify" : "install",
-                       options & WHOLE ? " -w" : "", src, dest);
+               printf("%s%s%s%s %s %s\n", opts & VERIFY ? "verify":"install",
+                       opts & WHOLE ? " -w" : "",
+                       opts & YOUNGER ? " -y" : "",
+                       opts & REMOVE ? " -r" : "", src, dest);
                if (nflag)
                        return;
        }
                if (nflag)
                        return;
        }
@@ -137,9 +142,13 @@ install(src, dest, destdir, options)
                printf("buf = %s", buf);
        (void) write(rem, buf, strlen(buf));
 
                printf("buf = %s", buf);
        (void) write(rem, buf, strlen(buf));
 
-       if (!destdir && (options & WHOLE))
-               options |= STRIP;
-       sendf(src, NULL, options);
+       if (!destdir && (opts & WHOLE))
+               opts |= STRIP;
+       if (opts & REMOVE) {
+               opts &= ~REMOVE;
+               rmchk(src, NULL, opts);
+       }
+       sendf(src, NULL, opts);
 }
 
 struct tstamp {
 }
 
 struct tstamp {
@@ -160,6 +169,8 @@ dofcmds(files, stamps, cmds)
        register struct block *b;
        register struct tstamp *t;
        register char **cpp;
        register struct block *b;
        register struct tstamp *t;
        register char **cpp;
+       struct timeval tv[2];
+       struct timezone tz;
        struct stat stb;
        extern char *tmpinc;
 
        struct stat stb;
        extern char *tmpinc;
 
@@ -167,7 +178,7 @@ dofcmds(files, stamps, cmds)
                printf("dofcmds()\n");
 
        files = expand(files, 0);
                printf("dofcmds()\n");
 
        files = expand(files, 0);
-       stamps = expand(stamps, 1);
+       stamps = expand(stamps, 0);
        if (files == NULL)
                fatal("no files to be updated\n");
        if (stamps == NULL)
        if (files == NULL)
                fatal("no files to be updated\n");
        if (stamps == NULL)
@@ -186,7 +197,10 @@ dofcmds(files, stamps, cmds)
                if (debug)
                        printf("%s: %d\n", b->b_name, stb.st_mtime);
                t->lastmod = stb.st_mtime;
                if (debug)
                        printf("%s: %d\n", b->b_name, stb.st_mtime);
                t->lastmod = stb.st_mtime;
-               if (!nflag && !vflag) {
+               (void) gettimeofday(&tv[0], &tz);
+               tv[1] = tv[0];
+               (void) utimes(b->b_name, tv);
+               if (!nflag && !(options & VERIFY)) {
                        if ((t->tfp = fopen(tmpfile, "w")) == NULL)
                                error("%s: %s\n", b->b_name, sys_errlist[errno]);
                        (*tmpinc)++;
                        if ((t->tfp = fopen(tmpfile, "w")) == NULL)
                                error("%s: %s\n", b->b_name, sys_errlist[errno]);
                        (*tmpinc)++;
@@ -194,6 +208,7 @@ dofcmds(files, stamps, cmds)
                        t->tfp = NULL;
                t++;
        }
                        t->tfp = NULL;
                t++;
        }
+
        for (b = files; b != NULL; b = b->b_next) {
                if (filec) {
                        for (cpp = filev; *cpp; cpp++)
        for (b = files; b != NULL; b = b->b_next) {
                if (filec) {
                        for (cpp = filev; *cpp; cpp++)
@@ -205,16 +220,15 @@ dofcmds(files, stamps, cmds)
                tp = NULL;
                cmptime(b->b_name);
        }
                tp = NULL;
                cmptime(b->b_name);
        }
-       if (!nflag && !vflag)
-               for (t = ts; t < &ts[nstamps]; t++)
-                       if (t->tfp != NULL)
-                               (void) fclose(t->tfp);
+
        *tmpinc = 'A';
        *tmpinc = 'A';
-       while (nstamps--) {
+       for (t = ts; t < &ts[nstamps]; t++) {
+               if (t->tfp != NULL)
+                       (void) fclose(t->tfp);
                for (b = cmds; b != NULL; b = b->b_next)
                        if (b->b_type == NOTIFY)
                for (b = cmds; b != NULL; b = b->b_next)
                        if (b->b_type == NOTIFY)
-                               notify(tmpfile, NULL, b->b_args);
-               if (!nflag && !vflag)
+                               notify(tmpfile, NULL, b->b_args, t->lastmod);
+               if (!nflag && !(options & VERIFY))
                        (void) unlink(tmpfile);
                (*tmpinc)++;
        }
                        (void) unlink(tmpfile);
                (*tmpinc)++;
        }
@@ -263,9 +277,8 @@ cmptime(name)
        }
 
        for (t = ts; t < &ts[nstamps]; t++) {
        }
 
        for (t = ts; t < &ts[nstamps]; t++) {
-               if (stb.st_mtime <= t->lastmod)
-                       return;
-               log(t->tfp, "updating: %s\n", name);
+               if (stb.st_mtime > t->lastmod)
+                       log(t->tfp, "new: %s\n", name);
        }
 }
 
        }
 }
 
@@ -309,16 +322,19 @@ rcmptime(st)
 
 /*
  * Notify the list of people the changes that were made.
 
 /*
  * Notify the list of people the changes that were made.
+ * rhost == NULL if we are mailing a list of changes compared to at time
+ * stamp file.
  */
  */
-notify(file, rhost, to)
+notify(file, rhost, to, lmod)
        char *file, *rhost;
        register struct block *to;
        char *file, *rhost;
        register struct block *to;
+       time_t lmod;
 {
        register int fd, len;
        FILE *pf, *popen();
        struct stat stb;
 
 {
        register int fd, len;
        FILE *pf, *popen();
        struct stat stb;
 
-       if (vflag)
+       if (options & VERIFY)
                return;
        if (!qflag) {
                printf("notify ");
                return;
        if (!qflag) {
                printf("notify ");
@@ -356,16 +372,24 @@ notify(file, rhost, to)
         */
        fprintf(pf, "From: rdist (Remote distribution program)\n");
        fprintf(pf, "To:");
         */
        fprintf(pf, "From: rdist (Remote distribution program)\n");
        fprintf(pf, "To:");
+       if (!any('@', to->b_name) && host != NULL)
+               fprintf(pf, " %s@%s", to->b_name, rhost);
+       else
+               fprintf(pf, " %s", to->b_name);
+       to = to->b_next;
        while (to != NULL) {
                if (!any('@', to->b_name) && host != NULL)
        while (to != NULL) {
                if (!any('@', to->b_name) && host != NULL)
-                       fprintf(pf, " %s@%s", to->b_name, rhost);
+                       fprintf(pf, ", %s@%s", to->b_name, rhost);
                else
                else
-                       fprintf(pf, " %s", to->b_name);
+                       fprintf(pf, ", %s", to->b_name);
                to = to->b_next;
        }
        putc('\n', pf);
                to = to->b_next;
        }
        putc('\n', pf);
-       fprintf(pf, "Subject: files updated by rdist from %s to %s\n",
-               host, rhost);
+       if (rhost != NULL)
+               fprintf(pf, "Subject: files updated by rdist from %s to %s\n",
+                       host, rhost);
+       else
+               fprintf(pf, "Subject: files updated after %s\n", ctime(&lmod));
        putc('\n', pf);
 
        while ((len = read(fd, buf, BUFSIZ)) > 0)
        putc('\n', pf);
 
        while ((len = read(fd, buf, BUFSIZ)) > 0)