This diff makes mg behave more like emacs by checking if the file about
to be written exists.

ok?

-lum

Index: file.c
===================================================================
RCS file: /cvs/src/usr.bin/mg/file.c,v
retrieving revision 1.76
diff -u -p -r1.76 file.c
--- file.c      31 Aug 2011 08:58:29 -0000      1.76
+++ file.c      10 Apr 2012 13:49:07 -0000
@@ -8,6 +8,8 @@
 
 #include "def.h"
 
+#include <sys/stat.h>
+
 #include <libgen.h>
 
 size_t xdirname(char *, const char *, size_t);
@@ -491,8 +493,9 @@ cleanup:
 int
 filewrite(int f, int n)
 {
+       struct stat     statbuf;
        int      s;
-       char     fname[NFILEN], bn[NBUFN];
+       char     fname[NFILEN], bn[NBUFN], tmp[NFILEN + 25];
        char    *adjfname, *bufp;
 
        if (getbufcwd(fname, sizeof(fname)) != TRUE)
@@ -506,6 +509,15 @@ filewrite(int f, int n)
        adjfname = adjustname(fname, TRUE);
        if (adjfname == NULL)
                return (FALSE);
+
+        /* Check if file exists; write checks done later */
+        if (stat(adjfname, &statbuf) == 0) {
+               snprintf(tmp, sizeof(tmp), "File `%s' exists; overwrite",
+                   adjfname);
+               if ((s = eyorn(tmp)) != TRUE)
+                        return (s);
+        }
+
        /* old attributes are no longer current */
        bzero(&curbp->b_fi, sizeof(curbp->b_fi));
        if ((s = writeout(curbp, adjfname)) == TRUE) {

Reply via email to