From eb0837c7c2360ba648df81a9a8f482745a60a6b9 Mon Sep 17 00:00:00 2001 From: Eric Johnson Date: Sat, 6 Jun 2026 00:24:29 -0400 Subject: [PATCH] added check for output file existing and option to override I added an if check to make sure that the output file doesn't already exist. You can use -f or --force to override. If the output file exist and -f or --force are not used, it prints an appropriate error message and exits with fatal(). Updated usage(). --- src/opusenc.c | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/opusenc.c b/src/opusenc.c index 028ad90..1b909e6 100644 --- a/src/opusenc.c +++ b/src/opusenc.c @@ -126,7 +126,7 @@ static void usage(void) printf("The input format can be Wave, AIFF, FLAC, Ogg/FLAC, or raw PCM.\n"); #else printf("The input format can be Wave, AIFF, or raw PCM.\n"); -#endif +#endif printf("\ninput_file can be:\n"); printf(" filename.wav file\n"); printf(" - stdin\n"); @@ -136,6 +136,7 @@ static void usage(void) printf("\nGeneral options:\n"); printf(" -h, --help Show this help\n"); printf(" -V, --version Show version information\n"); + printf(" -f, --force Force overwritng of output file\n"); printf(" --help-picture Show help on attaching album art\n"); printf(" --quiet Enable quiet mode\n"); printf("\nEncoding options:\n"); @@ -395,6 +396,7 @@ int main(int argc, char **argv) }; struct option long_options[] = { + {"force", no_argument, NULL, 0}, {"quiet", no_argument, NULL, 0}, {"bitrate", required_argument, NULL, 0}, {"hard-cbr",no_argument,NULL, 0}, @@ -461,6 +463,7 @@ int main(int argc, char **argv) time_t last_spin=0; int last_spin_len=0; /*Settings*/ + int force=0; int quiet=0; opus_int32 bitrate=-1; opus_int32 rate=48000; @@ -554,7 +557,7 @@ int main(int argc, char **argv) int option_index; const char *optname; - c=getopt_long(argc_utf8, argv_utf8, "hV", long_options, &option_index); + c=getopt_long(argc_utf8, argv_utf8, "hVf", long_options, &option_index); if (c==-1) break; @@ -562,7 +565,9 @@ int main(int argc, char **argv) case 0: optname = long_options[option_index].name; save_cmd = 1; - if (strcmp(optname, "quiet")==0) { + if (strcmp(optname, "force")==0) { + force=1; + } else if (strcmp(optname, "quiet")==0) { quiet=1; save_cmd=0; } else if (strcmp(optname, "bitrate")==0) { @@ -854,6 +859,9 @@ int main(int argc, char **argv) } } break; + case 'f': + force=1; + break; case 'h': usage(); exit(0); @@ -883,6 +891,13 @@ int main(int argc, char **argv) inFile=argv_utf8[optind]; outFile=argv_utf8[optind+1]; + data.fout = fopen_utf8(outFile, "r"); + + if (data.fout && !force) { + fatal("Error: Output file already exist use -f or --force to overwrite\n"); + } else if (data.fout) { + fclose(data.fout); + } if (cline_size > 0) { ret = ope_comments_add(inopt.comments, "ENCODER_OPTIONS", ENCODER_string); if (ret != OPE_OK) {