summaryrefslogtreecommitdiff
path: root/usr.bin
diff options
context:
space:
mode:
authorlum <lum@cvs.openbsd.org>2012-04-11 14:16:58 +0000
committerlum <lum@cvs.openbsd.org>2012-04-11 14:16:58 +0000
commit2e7b1ef3f1a13eeb211b89808692bd1b8a582349 (patch)
treef24a28e3ac7c267ec161a06d90ef8d8ffb50bcc0 /usr.bin
parenta35c168fa1b9d49800920f96c7cfc1a779280c82 (diff)
When writing a file via 'C-x C-w', ask user if they want to overwrite an
existing file. This mimics emacs behaviour. Reviewed by Sunil Nimmagadda.
Diffstat (limited to 'usr.bin')
-rw-r--r--usr.bin/mg/file.c16
1 files changed, 14 insertions, 2 deletions
diff --git a/usr.bin/mg/file.c b/usr.bin/mg/file.c
index 210c404b23b..67a489e7663 100644
--- a/usr.bin/mg/file.c
+++ b/usr.bin/mg/file.c
@@ -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 <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) {