diff --git a/cobj/cobj.c b/cobj/cobj.c index 48cafbad..b86d65b7 100644 --- a/cobj/cobj.c +++ b/cobj/cobj.c @@ -1048,12 +1048,28 @@ static int process_command_line(const int argc, char *argv[]) { case 'o': /* -o : the directory where class files are stored */ /* -class-file-dir : the directory where class files are stored */ + if (optarg == NULL || *optarg == '\0') { + fprintf(stderr, "Error: Missing directory path argument\n"); + exit(1); + } + if (stat(optarg, &st) != 0 || !(S_ISDIR(st.st_mode))) { + fprintf(stderr, "Error: '%s' is not a valid directory\n", optarg); + exit(1); + } output_name = strdup(optarg); break; case 'j': /* -j : the directory where java files are stored */ /* -java-source-dir : the directory where java files are stored */ + if (optarg == NULL || *optarg == '\0') { + fprintf(stderr, "Error: Missing directory path argument\n"); + exit(1); + } + if (stat(optarg, &st) != 0 || !(S_ISDIR(st.st_mode))) { + fprintf(stderr, "Error: '%s' is not a valid directory\n", optarg); + exit(1); + } java_source_dir = strdup(optarg); break; diff --git a/tests/command-line-options.src/file-path.at b/tests/command-line-options.src/file-path.at index cd9e000b..b00039e0 100644 --- a/tests/command-line-options.src/file-path.at +++ b/tests/command-line-options.src/file-path.at @@ -122,4 +122,42 @@ AT_CHECK([test ! -e class_dir/prog.class]) AT_CHECK([test ! -e prog.java]) AT_CHECK([rm -f *.java *.class class_dir/* java_dir/*]) +AT_CLEANUP + + +AT_SETUP([file-path options with non-existent directory]) + +AT_DATA([prog.cbl], [ + IDENTIFICATION DIVISION. + PROGRAM-ID. prog. + DATA DIVISION. + WORKING-STORAGE SECTION. + PROCEDURE DIVISION. + DISPLAY "HELLO". +]) + +AT_CHECK([${COBJ} -j java_dir prog.cbl], [1], [], +[Error: 'java_dir' is not a valid directory +]) + +AT_CHECK([${COBJ} -java-source-dir=java_dir prog.cbl], [1], [], +[Error: 'java_dir' is not a valid directory +]) + +AT_CHECK([${COBJ} -java-source-dir= prog.cbl], [1], [], +[Error: Missing directory path argument +]) + +AT_CHECK([${COBJ} -o class_dir prog.cbl], [1], [], +[Error: 'class_dir' is not a valid directory +]) + +AT_CHECK([${COBJ} -class-file-dir=class_dir prog.cbl], [1], [], +[Error: 'class_dir' is not a valid directory +]) + +AT_CHECK([${COBJ} -class-file-dir= prog.cbl], [1], [], +[Error: Missing directory path argument +]) + AT_CLEANUP \ No newline at end of file