commit 4c14df24c32aed73e90a7e5a7bbe3019739df0cb
parent 2c4db0843749d074ad8a1d681a2e9ae00ae56a64
Author: Vincent Forest <vincent.forest@meso-star.com>
Date: Wed, 8 Feb 2017 13:57:29 +0100
Add the '-f' option
Do not prompt before overwriting the output file.
Diffstat:
4 files changed, 33 insertions(+), 17 deletions(-)
diff --git a/doc/cli b/doc/cli
@@ -2,6 +2,7 @@ solstice
-b # Output binary per receiver hits
-d <date-list>:<pos-on-earth>
-D <sun-dir-list>
+ -f # Force output overwrite
-h # Short help and exit
-O # Switch in dump mode
-o OUTPUT # defaulting to stdout if not defined
diff --git a/src/solstice.c b/src/solstice.c
@@ -13,7 +13,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. */
-#define _POSIX_C_SOURCE 200112L /* fdopen support */
+#define _POSIX_C_SOURCE 200112L /* close support */
#include "solstice.h"
#include "solstice_c.h"
@@ -379,30 +379,38 @@ prompt_yes_no(void)
}
static FILE*
-open_output_stream(const char* name)
+open_output_stream(const char* name, const int force_overwriting)
{
int fd = -1;
FILE* fp = NULL;
ASSERT(name);
- fd = open(name, O_CREAT|O_WRONLY|O_EXCL|O_TRUNC, S_IRUSR|S_IWUSR);
-
- if(fd >= 0) {
- fp = fdopen(fd, "w");
- if(fp == NULL) goto error;
- } else if(errno == EEXIST) {
- fprintf(stderr, "The output file `%s' already exist. Overwrite it? ", name);
- if(!prompt_yes_no()) return NULL;
-
- fd = open(name, O_CREAT|O_WRONLY|O_TRUNC, S_IRUSR|S_IRUSR);
- if(fd >= 0 && !(fp = fdopen(fd, "w"))) goto error;
+ if(force_overwriting) {
+ fp = fopen(name, "w");
+ if(!fp) goto error;
+ } else {
+ fd = open(name, O_CREAT|O_WRONLY|O_EXCL|O_TRUNC, S_IRUSR|S_IWUSR);
+ if(fd >= 0) {
+ fp = fdopen(fd, "w");
+ if(fp == NULL) goto error;
+ } else if(errno == EEXIST) {
+ fprintf(stderr, "The output file `%s' already exist. Overwrite it? ", name);
+ if(!prompt_yes_no()) return NULL;
+
+ fd = open(name, O_CREAT|O_WRONLY|O_TRUNC, S_IRUSR|S_IRUSR);
+ if(fd >= 0 && !(fp = fdopen(fd, "w"))) goto error;
+ }
}
exit:
return fp;
error:
- if(fd >= 0) CHECK(close(fd), 0);
- fp = NULL;
+ if(fp) {
+ CHECK(fclose(fp), 0);
+ fp = NULL;
+ } else if(fd >= 0) {
+ CHECK(close(fd), 0);
+ }
goto exit;
}
@@ -466,7 +474,8 @@ solstice_init
if(!args->output_filename) {
solstice->output = stdout;
} else {
- solstice->output = open_output_stream(args->output_filename);
+ solstice->output = open_output_stream
+ (args->output_filename, args->force_overwriting);
if(!solstice->output) {
fprintf(stderr, "Could not open the output file `%s'.\n",
args->output_filename);
diff --git a/src/solstice_args.c b/src/solstice_args.c
@@ -44,6 +44,9 @@ print_help(const char* program)
printf(
" -D <dirs> list of sun directions.\n");
printf(
+" -f do not prompt before overwriting the output file submitted\n"
+" with the '-o' option.\n");
+ printf(
" -H output hit-on-receiver data (binary format).\n");
printf(
" -h display this help and exit.\n");
@@ -303,11 +306,12 @@ solstice_args_init(struct solstice_args* args, const int argc, char** argv)
*args = SOLSTICE_ARGS_DEFAULT;
optind = 0;
- while((opt = getopt(argc, argv, "D:Hhn:O:o:qR:r:t:")) != -1) {
+ while((opt = getopt(argc, argv, "D:fHhn:O:o:qR:r:t:")) != -1) {
switch(opt) {
case 'D':
res = parse_sun_dir_list(optarg, args);
break;
+ case 'f': args->force_overwriting = 1; break;
case 'H': args->output_hits = 1; break;
case 'h':
print_help(argv[0]);
diff --git a/src/solstice_args.h.in b/src/solstice_args.h.in
@@ -47,6 +47,7 @@ struct solstice_args {
unsigned long height;
} img;
+ int force_overwriting;
int dump_obj;
int rendering;
int output_hits; /* Output the per receiver hits */
@@ -79,6 +80,7 @@ static const struct solstice_args SOLSTICE_ARGS_NULL = SOLSTICE_ARGS_NULL__;
@SOLSTICE_ARGS_DEFAULT_IMG_HEIGHT@ \
}, \
\
+ 0, /* Force overwriting */ \
0, /* Dump */ \
0, /* Rendering */ \
0, /* Output hits */ \