Retain file extension in copy mode.

This commit is contained in:
Will Dormann 2016-02-02 13:28:09 -05:00 committed by Edward J. Schwartz
parent d16576b2ba
commit f25eafa586

View File

@ -47,6 +47,7 @@
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
#include <signal.h> #include <signal.h>
#include <libgen.h>
#if defined HAVE_SYS_TIME_H #if defined HAVE_SYS_TIME_H
# include <sys/time.h> # include <sys/time.h>
#endif #endif
@ -730,7 +731,7 @@ static void spawn_children(zzuf_opts_t *opts)
if (!tmpdir || !*tmpdir) if (!tmpdir || !*tmpdir)
tmpdir = "/tmp"; tmpdir = "/tmp";
int k = 0; int k = 0, extlen;
for (int j = zz_optind + 1; j < opts->oldargc; ++j) for (int j = zz_optind + 1; j < opts->oldargc; ++j)
{ {
@ -738,12 +739,24 @@ static void spawn_children(zzuf_opts_t *opts)
if (!fpin) if (!fpin)
continue; continue;
// Copy the path name since basename() might clobber it
char *tmpcopy = alloca(strlen(opts->oldargv[j])+1);
strcpy(tmpcopy, opts->oldargv[j]);
char *fbasename = basename(tmpcopy);
char *extension = strrchr(fbasename, '.');
if (!extension) {
extlen = 0;
extension = "";
}
else
extlen = strlen(extension);
#ifdef _WIN32 #ifdef _WIN32
sprintf(tmpname, "%s/zzuf.%i.XXXXXX", tmpdir, GetCurrentProcessId()); sprintf(tmpname, "%s/zzuf.%i.XXXXXX", tmpdir, GetCurrentProcessId());
int fdout = _open(mktemp(tmpname), _O_RDWR, 0600); int fdout = _open(mktemp(tmpname), _O_RDWR, 0600);
#else #else
sprintf(tmpname, "%s/zzuf.%i.XXXXXX", tmpdir, (int)getpid()); sprintf(tmpname, "%s/zzuf.%i.XXXXXX%s", tmpdir, (int)getpid(), extension);
int fdout = mkstemp(tmpname); int fdout = mkstemps(tmpname, extlen);
#endif #endif
if (fdout < 0) if (fdout < 0)
{ {