Merge pull request #12 from edmcman/feature/retain_file_extension_in_copy_mode

Retain file extension in copy mode.
This commit is contained in:
Sam Hocevar 2016-02-07 14:00:25 +01:00
commit dd94449e75

View File

@ -47,6 +47,8 @@
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
#include <signal.h> #include <signal.h>
#include <libgen.h>
#include <alloca.h>
#if defined HAVE_SYS_TIME_H #if defined HAVE_SYS_TIME_H
# include <sys/time.h> # include <sys/time.h>
#endif #endif
@ -732,7 +734,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)
{ {
@ -740,12 +742,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)
{ {