If you want to open up a file using the mg startup file (~/.mg) using the
find-file command, e.g:

find-file main.c

mg will give an odd message of "File read error", but the file opens anyway.
However, if by accident you try to open a non-existant file mg will segv. 

After investigating I found mg uses a static FILE pointer that is reused
in fileio.c, and in this case the dual opening of the ~/.mg file and
main.c was screwing things up.

This diff removes the static variable and passes the file pointer amongst
function calls. Now an ~/.mg file such as:

find-file main.c
find-file file.c
find-file README

works as expected. Opening three buffers with the files contents in each one.
Also, if a non-existant file is attemped to be opened mg doesn't crash.

comments/ok?

-lum

Index: def.h
===================================================================
RCS file: /cvs/src/usr.bin/mg/def.h,v
retrieving revision 1.120
diff -u -p -r1.120 def.h
--- def.h       12 Apr 2012 04:47:59 -0000      1.120
+++ def.h       23 May 2012 07:19:13 -0000
@@ -349,7 +349,7 @@ int          filewrite(int, int);
 int             filesave(int, int);
 int             buffsave(struct buffer *);
 int             makebkfile(int, int);
-int             writeout(struct buffer *, char *);
+int             writeout(FILE **, struct buffer *, char *);
 void            upmodes(struct buffer *);
 size_t          xbasename(char *, const char *, size_t);
 
@@ -430,12 +430,12 @@ int                getxtra(struct list *, struct list
 void            free_file_list(struct list *);
 
 /* fileio.c */
-int             ffropen(const char *, struct buffer *);
-void            ffstat(struct buffer *);
-int             ffwopen(const char *, struct buffer *);
-int             ffclose(struct buffer *);
-int             ffputbuf(struct buffer *);
-int             ffgetline(char *, int, int *);
+int             ffropen(FILE **, const char *, struct buffer *);
+void            ffstat(FILE *, struct buffer *);
+int             ffwopen(FILE **, const char *, struct buffer *);
+int             ffclose(FILE *, struct buffer *);
+int             ffputbuf(FILE *, struct buffer *);
+int             ffgetline(FILE *, char *, int, int *);
 int             fbackupfile(const char *);
 char           *adjustname(const char *, int);
 char           *startupfile(char *);
Index: extend.c
===================================================================
RCS file: /cvs/src/usr.bin/mg/extend.c,v
retrieving revision 1.52
diff -u -p -r1.52 extend.c
--- extend.c    12 Apr 2012 04:47:59 -0000      1.52
+++ extend.c    23 May 2012 07:19:13 -0000
@@ -652,16 +652,18 @@ load(const char *fname)
        int      s = TRUE, line;
        int      nbytes = 0;
        char     excbuf[128];
+       FILE    *ffp;
 
        if ((fname = adjustname(fname, TRUE)) == NULL)
                /* just to be careful */
                return (FALSE);
 
-       if (ffropen(fname, NULL) != FIOSUC)
+       if (ffropen(&ffp, fname, NULL) != FIOSUC)
                return (FALSE);
 
        line = 0;
-       while ((s = ffgetline(excbuf, sizeof(excbuf) - 1, &nbytes)) == FIOSUC) {
+       while ((s = ffgetline(ffp, excbuf, sizeof(excbuf) - 1, &nbytes))
+           == FIOSUC) {
                line++;
                excbuf[nbytes] = '\0';
                if (excline(excbuf) != TRUE) {
@@ -670,7 +672,7 @@ load(const char *fname)
                        break;
                }
        }
-       (void)ffclose(NULL);
+       (void)ffclose(ffp, NULL);
        excbuf[nbytes] = '\0';
        if (s != FIOEOF || (nbytes && excline(excbuf) != TRUE))
                return (FALSE);
Index: file.c
===================================================================
RCS file: /cvs/src/usr.bin/mg/file.c,v
retrieving revision 1.79
diff -u -p -r1.79 file.c
--- file.c      10 May 2012 16:07:46 -0000      1.79
+++ file.c      23 May 2012 07:19:13 -0000
@@ -294,6 +294,7 @@ insertfile(char *fname, char *newname, i
        int      nbytes, s, nline = 0, siz, x, x2;
        int      opos;                  /* offset we started at */
        int      oline;                 /* original line number */
+        FILE    *ffp;
 
        if (replacebuf == TRUE)
                x = undo_enable(FFRAND, 0);
@@ -317,7 +318,8 @@ insertfile(char *fname, char *newname, i
        }
 
        /* hard file open */
-       if ((s = ffropen(fname, (replacebuf == TRUE) ? bp : NULL)) == FIOERR)
+       if ((s = ffropen(&ffp, fname, (replacebuf == TRUE) ? bp : NULL))
+           == FIOERR)
                goto out;
        if (s == FIOFNF) {
                /* file not found */
@@ -358,7 +360,7 @@ insertfile(char *fname, char *newname, i
 
        nline = 0;
        siz = 0;
-       while ((s = ffgetline(line, linesize, &nbytes)) != FIOERR) {
+       while ((s = ffgetline(ffp, line, linesize, &nbytes)) != FIOERR) {
 retry:
                siz += nbytes + 1;
                switch (s) {
@@ -400,7 +402,7 @@ retry:
                                bcopy(line, cp, linesize);
                                free(line);
                                line = cp;
-                               s = ffgetline(line + linesize, linesize,
+                               s = ffgetline(ffp, line + linesize, linesize,
                                    &nbytes);
                                nbytes += linesize;
                                linesize = newsize;
@@ -416,7 +418,7 @@ retry:
        }
 endoffile:
        /* ignore errors */
-       (void)ffclose(NULL);
+       (void)ffclose(ffp, NULL);
        /* don't zap an error */
        if (s == FIOEOF) {
                if (nline == 1)
@@ -497,6 +499,7 @@ filewrite(int f, int n)
        int      s;
        char     fname[NFILEN], bn[NBUFN], tmp[NFILEN + 25];
        char    *adjfname, *bufp;
+        FILE    *ffp;
 
        if (getbufcwd(fname, sizeof(fname)) != TRUE)
                fname[0] = '\0';
@@ -520,7 +523,7 @@ filewrite(int f, int n)
 
        /* old attributes are no longer current */
        bzero(&curbp->b_fi, sizeof(curbp->b_fi));
-       if ((s = writeout(curbp, adjfname)) == TRUE) {
+       if ((s = writeout(&ffp, curbp, adjfname)) == TRUE) {
                (void)strlcpy(curbp->b_fname, adjfname, sizeof(curbp->b_fname));
                if (getbufcwd(curbp->b_cwd, sizeof(curbp->b_cwd)) != TRUE)
                        (void)strlcpy(curbp->b_cwd, "/", sizeof(curbp->b_cwd));
@@ -567,6 +570,7 @@ int
 buffsave(struct buffer *bp)
 {
        int      s;
+        FILE    *ffp;
 
        /* return, no changes */
        if ((bp->b_flag & BFCHG) == 0) {
@@ -598,7 +602,7 @@ buffsave(struct buffer *bp)
                    (s = eyesno("Backup error, save anyway")) != TRUE)
                        return (s);
        }
-       if ((s = writeout(bp, bp->b_fname)) == TRUE) {
+       if ((s = writeout(&ffp, bp, bp->b_fname)) == TRUE) {
                (void)fupdstat(bp);
                bp->b_flag &= ~(BFCHG | BFBAK);
                upmodes(bp);
@@ -640,22 +644,22 @@ makebkfile(int f, int n)
  * You may want to call fupdstat() after using this function.
  */
 int
-writeout(struct buffer *bp, char *fn)
+writeout(FILE ** ffp, struct buffer *bp, char *fn)
 {
        int      s;
 
        /* open writes message */
-       if ((s = ffwopen(fn, bp)) != FIOSUC)
+       if ((s = ffwopen(ffp, fn, bp)) != FIOSUC)
                return (FALSE);
-       s = ffputbuf(bp);
+       s = ffputbuf(*ffp, bp);
        if (s == FIOSUC) {
                /* no write error */
-               s = ffclose(bp);
+               s = ffclose(*ffp, bp);
                if (s == FIOSUC)
                        ewprintf("Wrote %s", fn);
        } else {
                /* print a message indicating write error */
-               (void)ffclose(bp);
+               (void)ffclose(*ffp, bp);
                ewprintf("Unable to write %s", fn);
        }
        return (s == FIOSUC);
Index: fileio.c
===================================================================
RCS file: /cvs/src/usr.bin/mg/fileio.c,v
retrieving revision 1.88
diff -u -p -r1.88 fileio.c
--- fileio.c    23 May 2012 05:29:22 -0000      1.88
+++ fileio.c    23 May 2012 07:19:13 -0000
@@ -22,15 +22,13 @@
 
 #include "kbd.h"
 
-static FILE    *ffp;
-
 /*
  * Open a file for reading.
  */
 int
-ffropen(const char *fn, struct buffer *bp)
+ffropen(FILE ** ffp, const char *fn, struct buffer *bp)
 {
-       if ((ffp = fopen(fn, "r")) == NULL) {
+       if ((*ffp = fopen(fn, "r")) == NULL) {
                if (errno == ENOENT)
                        return (FIOFNF);
                return (FIOERR);
@@ -40,7 +38,7 @@ ffropen(const char *fn, struct buffer *b
        if (fisdir(fn) == TRUE)
                return (FIODIR);
 
-       ffstat(bp);
+       ffstat(*ffp, bp);
        
        return (FIOSUC);
 }
@@ -49,7 +47,7 @@ ffropen(const char *fn, struct buffer *b
  * Update stat/dirty info
  */
 void
-ffstat(struct buffer *bp)
+ffstat(FILE *ffp, struct buffer *bp)
 {
        struct stat     sb;
 
@@ -71,13 +69,15 @@ ffstat(struct buffer *bp)
 int
 fupdstat(struct buffer *bp)
 {
+       FILE *ffp;
+
        if ((ffp = fopen(bp->b_fname, "r")) == NULL) {
                if (errno == ENOENT)
                        return (FIOFNF);
                return (FIOERR);
        }
-       ffstat(bp);
-       (void)ffclose(bp);
+       ffstat(ffp, bp);
+       (void)ffclose(ffp, bp);
        return (FIOSUC);
 }
 
@@ -85,7 +85,7 @@ fupdstat(struct buffer *bp)
  * Open a file for writing.
  */
 int
-ffwopen(const char *fn, struct buffer *bp)
+ffwopen(FILE ** ffp, const char *fn, struct buffer *bp)
 {
        int     fd;
        mode_t  fmode = DEFFILEMODE;
@@ -100,7 +100,7 @@ ffwopen(const char *fn, struct buffer *b
                return (FIOERR);
        }
 
-       if ((ffp = fdopen(fd, "w")) == NULL) {
+       if ((*ffp = fdopen(fd, "w")) == NULL) {
                ewprintf("Cannot open file for writing : %s", strerror(errno));
                close(fd);
                return (FIOERR);
@@ -125,7 +125,7 @@ ffwopen(const char *fn, struct buffer *b
  */
 /* ARGSUSED */
 int
-ffclose(struct buffer *bp)
+ffclose(FILE *ffp, struct buffer *bp)
 {
        if (fclose(ffp) == 0)
                return (FIOSUC);
@@ -137,7 +137,7 @@ ffclose(struct buffer *bp)
  * buffer. Return the status.
  */
 int
-ffputbuf(struct buffer *bp)
+ffputbuf(FILE *ffp, struct buffer *bp)
 {
        struct line   *lp, *lpend;
 
@@ -170,7 +170,7 @@ ffputbuf(struct buffer *bp)
  * If the line length exceeds nbuf, FIOLONG is returned.
  */
 int
-ffgetline(char *buf, int nbuf, int *nbytes)
+ffgetline(FILE *ffp, char *buf, int nbuf, int *nbytes)
 {
        int     c, i;

Reply via email to