From eb450c7edfd8c3ccf9c14f92da9e35cb2b09300d Mon Sep 17 00:00:00 2001 From: fenner Date: Sat, 30 Nov 2002 23:12:59 +0000 Subject: If both the unlink and the open fail, return the errno from the unlink (very likely EPERM), since the errno from the open might be a confusing ETXTBSY. Approved by: re MFC After: 1 week --- usr.bin/xinstall/xinstall.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'usr.bin/xinstall') diff --git a/usr.bin/xinstall/xinstall.c b/usr.bin/xinstall/xinstall.c index ca30c34..cc27f6e 100644 --- a/usr.bin/xinstall/xinstall.c +++ b/usr.bin/xinstall/xinstall.c @@ -598,6 +598,8 @@ int create_newfile(const char *path, int target, struct stat *sbp) { char backup[MAXPATHLEN]; + int saved_errno = 0; + int newfd; if (target) { /* @@ -621,10 +623,14 @@ create_newfile(const char *path, int target, struct stat *sbp) if (rename(path, backup) < 0) err(EX_OSERR, "rename: %s to %s", path, backup); } else - (void)unlink(path); + if (unlink(path) < 0) + saved_errno = errno; } - return (open(path, O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR)); + newfd = open(path, O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR); + if (newfd < 0 && saved_errno != 0) + errno = saved_errno; + return newfd; } /* -- cgit v1.1