commit 00f2bbf79ad5aaa0609843879bfd7131f458926a
parent 9334b1ae918dadaef0d73bd137bac2265bb0a495
Author: Vincent Forest <vincent.forest@meso-star.com>
Date: Thu, 26 Apr 2018 19:41:16 +0200
Handle the -f option in the test_nc program
Diffstat:
| M | src/test_nc.c | | | 78 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------- |
1 file changed, 63 insertions(+), 15 deletions(-)
diff --git a/src/test_nc.c b/src/test_nc.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 2 /* getopt support */
+#define _POSIX_C_SOURCE 200112L /* getopt/close support */
#include <rsys/rsys.h>
#include <rsys/math.h>
@@ -22,7 +22,9 @@
#include <alloca.h>
#include <netcdf.h>
#include <string.h>
-#include <unistd.h>
+#include <unistd.h> /* getopt */
+#include <fcntl.h> /* open */
+#include <sys/stat.h> /* S_IRUSR & S_IWUSR */
/*******************************************************************************
* Command arguments
@@ -84,7 +86,7 @@ args_init(struct args* args, const int argc, char** argv)
res_T res = RES_OK;
ASSERT(args && argc && argv);
- while((opt = getopt(argc, argv, "chi:o:q")) != -1) {
+ while((opt = getopt(argc, argv, "cfhi:o:q")) != -1) {
switch(opt) {
case 'c': args->check = 1; break;
case 'f': args->force_overwrite = 1; break;
@@ -113,6 +115,8 @@ args_init(struct args* args, const int argc, char** argv)
goto error;
}
+ if(args->no_output) args->output = NULL;
+
exit:
return res;
error:
@@ -139,7 +143,7 @@ static void
grid_release(struct grid* grid)
{
CHK(grid);
- if(grid->vxsz_z) free(grid->vxsz_z);
+ if(grid->vxsz_z) mem_rm(grid->vxsz_z);
}
/*******************************************************************************
@@ -227,6 +231,54 @@ sizeof_nctype(const nc_type type)
* Helper functions
******************************************************************************/
static res_T
+open_output_stream(const char* path, const int force_overwrite, FILE** stream)
+{
+ int fd = -1;
+ FILE* fp = NULL;
+ res_T res = RES_OK;
+ ASSERT(path);
+
+ if(force_overwrite) {
+ fp = fopen(path, "w");
+ if(!fp) {
+ fprintf(stderr, "Could not open the output file '%s'.\n", path);
+ goto error;
+ }
+ } else {
+ fd = open(path, O_CREAT|O_WRONLY|O_EXCL|O_TRUNC, S_IRUSR|S_IWUSR);
+ if(fd >= 0) {
+ fp = fdopen(fd, "w");
+ if(fp == NULL) {
+ fprintf(stderr, "Could not open the output file '%s'.\n", path);
+ goto error;
+ }
+ } else if(errno == EEXIST) {
+ fprintf(stderr,
+ "The output file '%s' already exists. Use -f to overwrite it.\n", path);
+ goto error;
+ } else {
+ fprintf(stderr,
+ "Unexpected error while opening the output file `'%s'.\n", path);
+ goto error;
+ }
+ }
+
+exit:
+ *stream = fp;
+ return res;
+error:
+ res = RES_IO_ERR;
+ if(fp) {
+ CHK(fclose(fp) == 0);
+ fp = NULL;
+ } else if(fd >= 0) {
+ CHK(close(fd) == 0);
+ }
+ goto exit;
+}
+
+
+static res_T
setup_definition
(int nc, int32_t* nx, int32_t* ny, int32_t* nz, int32_t* ntimes)
{
@@ -601,17 +653,8 @@ main(int argc, char** argv)
if(args.quit) goto exit;
if(args.output) {
- if(access(args.output, F_OK) == 0) {
- fprintf(stderr, "The OUTPUT file '%s' already exists.\n", args.output);
- res = RES_IO_ERR;
- goto error;
- }
- stream = fopen(args.output, "w");
- if(!stream) {
- fprintf(stderr, "Could not create the OUTPUT file '%s'.\n", args.output);
- res = RES_IO_ERR;
- goto error;
- }
+ res = open_output_stream(args.output, args.force_overwrite, &stream);
+ if(res != RES_OK) goto error;
}
err_nc = nc_open(args.input, NC_WRITE|NC_MMAP, &nc);
@@ -653,6 +696,11 @@ main(int argc, char** argv)
exit:
grid_release(&grid);
if(nc != INVALID_ID) NC(close(nc));
+ if(mem_allocated_size() != 0) {
+ fprintf(stderr, "Memory leaks: %lu Bytes\n",
+ (unsigned long)mem_allocated_size());
+ err = -1;
+ }
return err;
error:
err = -1;