aboutsummaryrefslogtreecommitdiff
path: root/lib/lib.c
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2018-04-03 22:11:43 -0500
committerRob Landley <rob@landley.net>2018-04-03 22:11:43 -0500
commit0043e99318bfade48c7a378997b691694b06edd0 (patch)
tree1e6218410f50fc72de43f8bba0d4d57852d89f0e /lib/lib.c
parent9448f8e57ed6ff85e756ec5e9545f99e6376e778 (diff)
downloadtoybox-0043e99318bfade48c7a378997b691694b06edd0.tar.gz
Factor out xtempfile()
Diffstat (limited to 'lib/lib.c')
-rw-r--r--lib/lib.c12
1 files changed, 4 insertions, 8 deletions
diff --git a/lib/lib.c b/lib/lib.c
index 83e4db45..ea678011 100644
--- a/lib/lib.c
+++ b/lib/lib.c
@@ -700,18 +700,14 @@ static void tempfile_handler(void)
int copy_tempfile(int fdin, char *name, char **tempname)
{
struct stat statbuf;
- int fd;
- int ignored __attribute__((__unused__));
+ int fd = xtempfile(name, tempname), ignored __attribute__((__unused__));
- *tempname = xmprintf("%s%s", name, "XXXXXX");
- if(-1 == (fd = mkstemp(*tempname))) error_exit("no temp file");
+ // Record tempfile for exit cleanup if interrupted
if (!tempfile2zap) sigatexit(tempfile_handler);
tempfile2zap = *tempname;
- // Set permissions of output file (ignoring errors, usually due to nonroot)
-
- fstat(fdin, &statbuf);
- fchmod(fd, statbuf.st_mode);
+ // Set permissions of output file.
+ if (!fstat(fdin, &statbuf)) fchmod(fd, statbuf.st_mode);
// We chmod before chown, which strips the suid bit. Caller has to explicitly
// switch it back on if they want to keep suid.