used a random block size if not given an argv[1].
[unix-history] / usr / src / usr.bin / error / touch.c
index 4805fd9..493d944 100644 (file)
@@ -1,4 +1,19 @@
-static char *sccsid = "@(#)touch.c     1.5 (Berkeley) %G%";
+/*
+ * Copyright (c) 1980 Regents of the University of California.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms are permitted
+ * provided that this notice is preserved and that due credit is given
+ * to the University of California at Berkeley. The name of the University
+ * may not be used to endorse or promote products derived from this
+ * software without specific prior written permission. This software
+ * is provided ``as is'' without express or implied warranty.
+ */
+
+#ifndef lint
+static char sccsid[] = "@(#)touch.c    5.2 (Berkeley) %G%";
+#endif /* not lint */
+
 #include <stdio.h>
 #include <ctype.h>
 #include <sys/types.h>
 #include <stdio.h>
 #include <ctype.h>
 #include <sys/types.h>
@@ -241,9 +256,13 @@ hackfile(name, files, ix, nerrors)
        boolean previewed;
        int     errordest;      /* where errors go*/
 
        boolean previewed;
        int     errordest;      /* where errors go*/
 
-       previewed = preview(name, nerrors, files, ix);
-
-       errordest = settotouch(name);
+       if (!oktotouch(name)) {
+               previewed = FALSE;
+               errordest = TOSTDOUT;
+       } else {
+               previewed = preview(name, nerrors, files, ix);
+               errordest = settotouch(name);
+       }
 
        if (errordest != TOSTDOUT)
                touchedfiles[ix] = TRUE;
 
        if (errordest != TOSTDOUT)
                touchedfiles[ix] = TRUE;
@@ -270,18 +289,16 @@ boolean preview(name, nerrors, files, ix)
        int     back;
        reg     Eptr    *erpp;
 
        int     back;
        reg     Eptr    *erpp;
 
-       if (!oktotouch(name))
-               return(false);
        if (nerrors <= 0)
        if (nerrors <= 0)
-               return(false);
-       back = false;
+               return(FALSE);
+       back = FALSE;
        if(query){
                switch(inquire(terse
                    ? "Preview? "
                    : "Do you want to preview the errors first? ")){
                case Q_YES:
                case Q_yes:
        if(query){
                switch(inquire(terse
                    ? "Preview? "
                    : "Do you want to preview the errors first? ")){
                case Q_YES:
                case Q_yes:
-                       back = true;
+                       back = TRUE;
                        EITERATE(erpp, files, ix){
                                errorprint(stdout, *erpp, TRUE);
                        }
                        EITERATE(erpp, files, ix){
                                errorprint(stdout, *erpp, TRUE);
                        }
@@ -543,9 +560,20 @@ writetouched(overwrite)
        reg     FILE    *localfile;
        reg     FILE    *tmpfile;
                int     botch;
        reg     FILE    *localfile;
        reg     FILE    *tmpfile;
                int     botch;
+               int     oktorm;
 
 
+       botch = 0;
+       oktorm = 1;
        while((nread = fread(edbuf, 1, sizeof(edbuf), o_touchedfile)) != NULL){
        while((nread = fread(edbuf, 1, sizeof(edbuf), o_touchedfile)) != NULL){
-               fwrite(edbuf, 1, nread, n_touchedfile);
+               if (nread != fwrite(edbuf, 1, nread, n_touchedfile)){
+                       /*
+                        *      Catastrophe in temporary area: file system full?
+                        */
+                       botch = 1;
+                       fprintf(stderr,
+                         "%s: write failure: No errors inserted in \"%s\"\n",
+                         processname, o_name);
+               }
        }
        fclose(n_touchedfile);
        fclose(o_touchedfile);
        }
        fclose(n_touchedfile);
        fclose(o_touchedfile);
@@ -553,7 +581,7 @@ writetouched(overwrite)
         *      Now, copy the temp file back over the original
         *      file, thus preserving links, etc
         */
         *      Now, copy the temp file back over the original
         *      file, thus preserving links, etc
         */
-       if (overwrite){
+       if (botch == 0 && overwrite){
                botch = 0;
                localfile = NULL;
                tmpfile = NULL;
                botch = 0;
                localfile = NULL;
                tmpfile = NULL;
@@ -568,17 +596,18 @@ writetouched(overwrite)
                                processname, n_name);
                        botch++;
                }
                                processname, n_name);
                        botch++;
                }
-               if (!botch){
-                       while((nread=fread(edbuf, 1, sizeof(edbuf), tmpfile))
-                                       != NULL){
-                               fwrite(edbuf, 1, nread, localfile);
-                       }
-               }
+               if (!botch)
+                       oktorm = mustoverwrite(localfile, tmpfile);
                if (localfile != NULL)
                        fclose(localfile);
                if (tmpfile != NULL)
                        fclose(tmpfile);
        }
                if (localfile != NULL)
                        fclose(localfile);
                if (tmpfile != NULL)
                        fclose(tmpfile);
        }
+       if (oktorm == 0){
+               fprintf(stderr, "%s: Catastrophe: A copy of \"%s\: was saved in \"%s\"\n",
+                       processname, o_name, n_name);
+               exit(1);
+       }
        /*
         *      Kiss the temp file good bye
         */
        /*
         *      Kiss the temp file good bye
         */
@@ -586,6 +615,59 @@ writetouched(overwrite)
        tempfileopen = FALSE;
        return(TRUE);
 }
        tempfileopen = FALSE;
        return(TRUE);
 }
+/*
+ *     return 1 if the tmpfile can be removed after writing it out
+ */
+int mustoverwrite(preciousfile, tmpfile)
+       FILE    *preciousfile;
+       FILE    *tmpfile;
+{
+       int     nread;
+
+       while((nread = fread(edbuf, 1, sizeof(edbuf), tmpfile)) != NULL){
+               if (mustwrite(edbuf, nread, preciousfile) == 0)
+                       return(0);
+       }
+       return(1);
+}
+/*
+ *     return 0 on catastrophe
+ */
+mustwrite(base, n, preciousfile)
+       char    *base;
+       int     n;
+       FILE    *preciousfile;
+{
+       int     nwrote;
+
+       if (n <= 0)
+               return(1);
+       nwrote = fwrite(base, 1, n, preciousfile);
+       if (nwrote == n)
+               return(1);
+       perror(processname);
+       switch(inquire(terse
+           ? "Botch overwriting: retry? "
+           : "Botch overwriting the source file: retry? ")){
+       case Q_YES:
+       case Q_yes:
+               mustwrite(base + nwrote, n - nwrote, preciousfile);
+               return(1);
+       case Q_NO:
+       case Q_no:
+               switch(inquire("Are you sure? ")){
+               case Q_YES:
+               case Q_yes:
+                       return(0);
+               case Q_NO:
+               case Q_no:
+                       mustwrite(base + nwrote, n - nwrote, preciousfile);
+                       return(1);
+               }
+       default:
+               return(0);
+       }
+}
 
 onintr()
 {
 
 onintr()
 {
@@ -627,6 +709,9 @@ int inquire(fmt, a1, a2)
        /*VARARGS1*/
 {
        char    buffer[128];
        /*VARARGS1*/
 {
        char    buffer[128];
+
+       if (queryfile == NULL)
+               return(0);
        for(;;){
                do{
                        fflush(stdout);
        for(;;){
                do{
                        fflush(stdout);