From 03087ed580e9b4336fbbccfb7b8176d1f5a8b042 Mon Sep 17 00:00:00 2001 From: obrien Date: Mon, 9 Jul 2001 04:11:33 +0000 Subject: It is just stupid to have to do ``!rm -f %'' to write a file you own. So lets stop that nonsense and allow `w!' to do something useful. Submitted by: green --- contrib/nvi/common/exf.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'contrib/nvi') diff --git a/contrib/nvi/common/exf.c b/contrib/nvi/common/exf.c index 57f5748..8c4dea5 100644 --- a/contrib/nvi/common/exf.c +++ b/contrib/nvi/common/exf.c @@ -837,10 +837,39 @@ file_write(sp, fm, tm, name, flags) SIGBLOCK; if ((fd = open(name, oflags, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)) < 0) { + if (errno == EACCES && LF_ISSET(FS_FORCE)) { + /* + * If the user owns the file but does not + * have write permission on it, grant it + * automatically for the duration of the + * opening of the file, if possible. + */ + struct stat sb; + mode_t fmode; + + if (stat(name, &sb) != 0) + goto fail_open; + fmode = sb.st_mode; + if (!(sb.st_mode & S_IWUSR) && sb.st_uid == getuid()) + fmode |= S_IWUSR; + else + goto fail_open; + if (chmod(name, fmode) != 0) + goto fail_open; + fd = open(name, oflags, S_IRUSR | S_IWUSR | + S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); + if (fd == -1) + goto fail_open; + (void)fchmod(fd, sb.st_mode); + goto success_open; + fail_open: + errno = EACCES; + } msgq_str(sp, M_SYSERR, name, "%s"); SIGUNBLOCK; return (1); } +success_open: SIGUNBLOCK; /* Try and get a lock. */ -- cgit v1.1