Currently if you have a file 'test' and cat it:

$ cat test
abc
def
ghk
$

Then open 'test' in mg and remove the newline at the end of the buffer and try to save it, mg will offer the opportunity to add a newline at the end of the buffer:

"No newline at end of file, add one? (y or n)"

Before you answer this question, if you then cat the 'test' file again you will see:

$ cat test
$

mg is waiting for your answer before writing the contents of the buffer to disk (the file it has already opened for writing).

This diff moves the check for the newline at the end of the buffer earlier so file data doesn't 'disappear' for an indefinite period (or arguably infinite period, if mg or the machine mg is running on crashes) before the user answers yes or no.

Comments/ok?
Mark

Index: def.h
===================================================================
RCS file: /cvs/src/usr.bin/mg/def.h,v
retrieving revision 1.157
diff -u -p -u -p -r1.157 def.h
--- def.h       13 Dec 2018 14:59:16 -0000      1.157
+++ def.h       20 Jun 2019 17:17:06 -0000
@@ -466,7 +466,7 @@ int          ffropen(FILE **, const char *, str
 void            ffstat(FILE *, struct buffer *);
 int             ffwopen(FILE **, const char *, struct buffer *);
 int             ffclose(FILE *, struct buffer *);
-int             ffputbuf(FILE *, struct buffer *);
+int             ffputbuf(FILE *, struct buffer *, int);
 int             ffgetline(FILE *, char *, int, int *);
 int             fbackupfile(const char *);
 char           *adjustname(const char *, int);
Index: file.c
===================================================================
RCS file: /cvs/src/usr.bin/mg/file.c,v
retrieving revision 1.100
diff -u -p -u -p -r1.100 file.c
--- file.c      2 Jan 2016 10:39:19 -0000       1.100
+++ file.c      20 Jun 2019 17:17:06 -0000
@@ -668,9 +668,10 @@ makebkfile(int f, int n)
 int
 writeout(FILE ** ffp, struct buffer *bp, char *fn)
 {
-       struct stat     statbuf;
-       int      s;
-       char     dp[NFILEN];
+       struct stat      statbuf;
+       struct line     *lpend;
+       int              s, eobnl;
+       char             dp[NFILEN];

        if (stat(fn, &statbuf) == -1 && errno == ENOENT) {
                errno = 0;
@@ -686,10 +687,16 @@ writeout(FILE ** ffp, struct buffer *bp,
                        return (FIOERR);
                }
         }
+       lpend = bp->b_headp;
+       eobnl = 0;
+       if (llength(lback(lpend)) != 0) {
+               if (eyorn("No newline at end of file, add one") == TRUE)
+                       eobnl = 1;
+       }
        /* open writes message */
        if ((s = ffwopen(ffp, fn, bp)) != FIOSUC)
                return (FALSE);
-       s = ffputbuf(*ffp, bp);
+       s = ffputbuf(*ffp, bp, eobnl);
        if (s == FIOSUC) {
                /* no write error */
                s = ffclose(*ffp, bp);
Index: fileio.c
===================================================================
RCS file: /cvs/src/usr.bin/mg/fileio.c,v
retrieving revision 1.105
diff -u -p -u -p -r1.105 fileio.c
--- fileio.c    13 Apr 2018 14:11:37 -0000      1.105
+++ fileio.c    20 Jun 2019 17:17:06 -0000
@@ -150,11 +150,12 @@ ffclose(FILE *ffp, struct buffer *bp)
  * buffer. Return the status.
  */
 int
-ffputbuf(FILE *ffp, struct buffer *bp)
+ffputbuf(FILE *ffp, struct buffer *bp, int eobnl)
 {
-       struct line   *lp, *lpend;
+       struct line     *lp, *lpend;

        lpend = bp->b_headp;
+
        for (lp = lforw(lpend); lp != lpend; lp = lforw(lp)) {
                if (fwrite(ltext(lp), 1, llength(lp), ffp) != llength(lp)) {
                        dobeep();
@@ -164,14 +165,9 @@ ffputbuf(FILE *ffp, struct buffer *bp)
                if (lforw(lp) != lpend)         /* no implied \n on last line */
                        putc('\n', ffp);
        }
-       /*
-        * XXX should be variable controlled (once we have variables)
-        */
-       if (llength(lback(lpend)) != 0) {
-               if (eyorn("No newline at end of file, add one") == TRUE) {
-                       lnewline_at(lback(lpend), llength(lback(lpend)));
-                       putc('\n', ffp);
-               }
+       if (eobnl) {
+               lnewline_at(lback(lpend), llength(lback(lpend)));
+               putc('\n', ffp);
        }
        return (FIOSUC);
 }

Reply via email to