summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorlum <lum@openbsd.org>2012-05-25 04:56:58 +0000
committerlum <lum@openbsd.org>2012-05-25 04:56:58 +0000
commit5187416ac85cde0ce45e76bec828866ef0789c06 (patch)
tree3c39c6c4bfe8e4ab71e52a42f5e8a556668276c3
parentChange the KERN_FILE_BYPID and KERN_FILE_BYUID modes of the KERN_FILE2 (diff)
downloadwireguard-openbsd-5187416ac85cde0ce45e76bec828866ef0789c06.tar.xz
wireguard-openbsd-5187416ac85cde0ce45e76bec828866ef0789c06.zip
Remove static FILE pointer used for handling files in fileio.c. Pass
by reference instead. This allows the mg startup file to open other files without unexpected things happening. Discussed with Sunil Nimmagadda.
-rw-r--r--usr.bin/mg/def.h16
-rw-r--r--usr.bin/mg/extend.c10
-rw-r--r--usr.bin/mg/file.c28
-rw-r--r--usr.bin/mg/fileio.c28
4 files changed, 44 insertions, 38 deletions
diff --git a/usr.bin/mg/def.h b/usr.bin/mg/def.h
index 9bd52408d29..6d6a7bd56e1 100644
--- a/usr.bin/mg/def.h
+++ b/usr.bin/mg/def.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: def.h,v 1.120 2012/04/12 04:47:59 lum Exp $ */
+/* $OpenBSD: def.h,v 1.121 2012/05/25 04:56:58 lum Exp $ */
/* This file is in the public domain. */
@@ -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 *, int, int);
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 *);
diff --git a/usr.bin/mg/extend.c b/usr.bin/mg/extend.c
index e6334962f01..2f47de6b5fb 100644
--- a/usr.bin/mg/extend.c
+++ b/usr.bin/mg/extend.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: extend.c,v 1.52 2012/04/12 04:47:59 lum Exp $ */
+/* $OpenBSD: extend.c,v 1.53 2012/05/25 04:56:58 lum Exp $ */
/* This file is in the public domain. */
@@ -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);
diff --git a/usr.bin/mg/file.c b/usr.bin/mg/file.c
index e736c73b03d..7e920598a8a 100644
--- a/usr.bin/mg/file.c
+++ b/usr.bin/mg/file.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: file.c,v 1.79 2012/05/10 16:07:46 lum Exp $ */
+/* $OpenBSD: file.c,v 1.80 2012/05/25 04:56:58 lum Exp $ */
/* This file is in the public domain. */
@@ -294,6 +294,7 @@ insertfile(char *fname, char *newname, int replacebuf)
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, int replacebuf)
}
/* 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, int replacebuf)
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);
diff --git a/usr.bin/mg/fileio.c b/usr.bin/mg/fileio.c
index 3833bc614f1..3c8b0c96f96 100644
--- a/usr.bin/mg/fileio.c
+++ b/usr.bin/mg/fileio.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: fileio.c,v 1.88 2012/05/23 05:29:22 lum Exp $ */
+/* $OpenBSD: fileio.c,v 1.89 2012/05/25 04:56:58 lum Exp $ */
/* This file is in the public domain. */
@@ -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 *bp)
if (fisdir(fn) == TRUE)
return (FIODIR);
- ffstat(bp);
+ ffstat(*ffp, bp);
return (FIOSUC);
}
@@ -49,7 +47,7 @@ ffropen(const char *fn, struct buffer *bp)
* 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 *bp)
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 *bp)
*/
/* 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;