4040/** Include header files to avoid implicit declarations (not allowed on LLVM) */
4141#include <ctype.h>
4242#include <sys/types.h>
43+ #include <dirent.h>
44+ #include <errno.h>
4345
4446// UNIX specific headers (non-Windows)
4547#if defined(__unix__ ) || defined(__APPLE__ )
@@ -61,6 +63,7 @@ static long mcstartdate = 0; /* start simulation time */
6163static int mcdisable_output_files = 0 ; /* --no-output-files */
6264mcstatic int mcgravitation = 0 ; /* use gravitation flag, for PROP macros */
6365mcstatic int mcusedefaults = 0 ; /* assume default value for all parameters */
66+ mcstatic int mcappend = 0 ; /* flag to allow append mode on datasets/directories */
6467mcstatic int mcdotrace = 0 ; /* flag for --trace and messages for DISPLAY */
6568mcstatic int mcnexus_embed_idf = 0 ; /* flag to embed xml-formatted IDF file for Mantid */
6669#pragma acc declare create ( mcdotrace )
@@ -2731,15 +2734,24 @@ mcuse_dir(char *dir)
27312734#ifdef USE_MPI
27322735 if (mpi_node_rank == mpi_node_root ) {
27332736#endif
2737+ int exists = 0 ;
2738+ DIR * handle = opendir (dirname );
2739+ if (handle ) {
2740+ /* Directory exists. */
2741+ closedir (handle );
2742+ exists = 1 ;
2743+ }
27342744 if (mkdir (dirname , 0777 )) {
27352745#ifndef DANSE
2736- fprintf (stderr , "Error: unable to create directory '%s' (mcuse_dir)\n" , dir );
2737- fprintf (stderr , "(Maybe the directory already exists?)\n" );
2746+ if (!mcappend ) {
2747+ fprintf (stderr , "Error: unable to create directory '%s' (mcuse_dir)\n" , dir );
2748+ fprintf (stderr , "(Maybe the directory already exists?)\n" );
27382749#endif
27392750#ifdef USE_MPI
2740- MPI_Abort (MPI_COMM_WORLD , -1 );
2751+ MPI_Abort (MPI_COMM_WORLD , -1 );
27412752#endif
2742- exit (-1 );
2753+ exit (-1 );
2754+ }
27432755 }
27442756#ifdef USE_MPI
27452757 }
@@ -4492,6 +4504,7 @@ mchelp(char *pgmname)
44924504" -s SEED --seed=SEED Set random seed (must be != 0)\n"
44934505" -n COUNT --ncount=COUNT Set number of particles to simulate.\n"
44944506" -d DIR --dir=DIR Put all data files in directory DIR.\n"
4507+ " -a --append Append data files to those in directory DIR.\n"
44954508" -t --trace Enable trace of " MCCODE_PARTICLE "s through instrument.\n"
44964509" (Use -t=2 or --trace=2 for modernised mcdisplay rendering)\n"
44974510" -g --gravitation Enable gravitation for all trajectories.\n"
@@ -4745,6 +4758,10 @@ mcparseoptions(int argc, char *argv[])
47454758 usedir = & argv [i ][2 ];
47464759 else if (!strcmp ("--dir" , argv [i ]) && (i + 1 ) < argc )
47474760 usedir = argv [++ i ];
4761+ else if (!strncmp ("-a" , argv [i ], 2 ))
4762+ mcappend = 1 ;
4763+ else if (!strcmp ("--append" , argv [i ]))
4764+ mcappend = 1 ;
47484765 else if (!strncmp ("--dir=" , argv [i ], 6 ))
47494766 usedir = & argv [i ][6 ];
47504767 else if (!strcmp ("-h" , argv [i ]))
0 commit comments