diff options
author | Daniel Stenberg <daniel@haxx.se> | 2020-08-24 08:31:36 +0200 |
---|---|---|
committer | Daniel Stenberg <daniel@haxx.se> | 2020-08-24 08:37:10 +0200 |
commit | 6881d2e767d76e4dffb655463fde3c8cb9d257fa (patch) | |
tree | f9cc647bdd2eaf933730447d711257c3bbabb70f /src/tool_cb_wrt.c | |
parent | d71ac6711a4616a851d048cfa82102d1d9e25bdd (diff) | |
download | curl-bagder/output-dir.tar.gz |
curl: add --output-dirbagder/output-dir
Works with --create-dirs and with -J
Add test 3008, 3009, 3011, 3012 and 3013 to verify.
Closes #5637
Diffstat (limited to 'src/tool_cb_wrt.c')
-rw-r--r-- | src/tool_cb_wrt.c | 32 |
1 files changed, 22 insertions, 10 deletions
diff --git a/src/tool_cb_wrt.c b/src/tool_cb_wrt.c index 64b62fefd..6fc51f9a5 100644 --- a/src/tool_cb_wrt.c +++ b/src/tool_cb_wrt.c @@ -39,6 +39,15 @@ #include "memdebug.h" /* keep this as LAST include */ +#ifndef O_BINARY +#define O_BINARY 0 +#endif +#ifdef WIN32 +#define OPENMODE S_IREAD | S_IWRITE +#else +#define OPENMODE S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH +#endif + /* create a local file for writing, return TRUE on success */ bool tool_create_output_file(struct OutStruct *outs, struct OperationConfig *config) @@ -55,21 +64,24 @@ bool tool_create_output_file(struct OutStruct *outs, if(outs->is_cd_filename) { /* don't overwrite existing files */ -#ifndef O_BINARY -#define O_BINARY 0 -#endif - int fd = open(outs->filename, O_CREAT | O_WRONLY | O_EXCL | O_BINARY, -#ifdef WIN32 - S_IREAD | S_IWRITE -#else - S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH -#endif - ); + int fd; + char *name = outs->filename; + char *aname = NULL; + if(config->output_dir) { + aname = aprintf("%s/%s", config->output_dir, name); + if(!aname) { + errorf(global, "out of memory\n"); + return FALSE; + } + name = aname; + } + fd = open(name, O_CREAT | O_WRONLY | O_EXCL | O_BINARY, OPENMODE); if(fd != -1) { file = fdopen(fd, "wb"); if(!file) close(fd); } + free(aname); } else /* open file for writing */ |