=================================================================== RCS file: /cvsrepo/anoncvs/cvs/src/usr.bin/mg/file.c,v retrieving revision 1.76 retrieving revision 1.77 diff -u -r1.76 -r1.77 --- src/usr.bin/mg/file.c 2011/08/31 08:58:29 1.76 +++ src/usr.bin/mg/file.c 2012/04/11 14:16:57 1.77 @@ -1,4 +1,4 @@ -/* $OpenBSD: file.c,v 1.76 2011/08/31 08:58:29 lum Exp $ */ +/* $OpenBSD: file.c,v 1.77 2012/04/11 14:16:57 lum Exp $ */ /* This file is in the public domain. */ @@ -8,6 +8,8 @@ #include "def.h" +#include + #include size_t xdirname(char *, const char *, size_t); @@ -491,8 +493,9 @@ 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 @@ 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) {