From 48b9e7b931275822b4c463e7047d5a797f8bfdd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Mangano?= <fmang@mg0.fr> Date: Tue, 1 Jan 2013 17:32:20 +0100 Subject: [PATCH] check before overwriting --- opustags.c | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/opustags.c b/opustags.c index 5ac46de..e409845 100644 --- a/opustags.c +++ b/opustags.c @@ -3,6 +3,7 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <unistd.h> #include <ogg/ogg.h> typedef struct { @@ -151,12 +152,16 @@ int write_page(ogg_page *og, FILE *stream){ int main(int argc, char **argv){ const char *path_in, *path_out = NULL; + int overwrite = 0; int c; - while((c = getopt(argc, argv, "o:")) != -1){ + while((c = getopt(argc, argv, "o:y")) != -1){ switch(c){ case 'o': path_out = optarg; break; + case 'y': + overwrite = 1; + break; default: return EXIT_FAILURE; } @@ -166,20 +171,27 @@ int main(int argc, char **argv){ return EXIT_FAILURE; } path_in = argv[optind]; - FILE *in = fopen(path_in, "r"); - if(!in){ - perror("fopen"); - return EXIT_FAILURE; - } FILE *out = NULL; if(path_out != NULL){ + if(!overwrite){ + if(access(path_out, F_OK) == 0){ + fprintf(stderr, "'%s' already exists (use -y to overwrite)\n", path_out); + return EXIT_FAILURE; + } + } out = fopen(path_out, "w"); if(!out){ perror("fopen"); - fclose(in); return EXIT_FAILURE; } } + FILE *in = fopen(path_in, "r"); + if(!in){ + perror("fopen"); + if(out) + fclose(out); + return EXIT_FAILURE; + } ogg_sync_state oy; ogg_stream_state os, enc; ogg_page og;