/** * This function copies the file 'src' into 'dest'. * Author: Olivier Blanc * Modified by Sébastien Paumier then Gilles Vollant */ void copy_file(const char* dest,const char* src) { int res=af_copy(src,dest); if (res!=0) { if (res==1) { fatal_error("error in writing when copy '%s' to '%s'\n",src,dest); } else fatal_error("error in reading when copy '%s' to '%s'\n",src,dest); } }
int copy_directory_snt_item(const char*dest_snt_dir,const char*src_snd_dir,const char*filename,int mandatory) { char fullname_src[1024]; char fullname_dest[1024]; sprintf(fullname_src,"%s%s",src_snd_dir,filename); sprintf(fullname_dest,"%s%s",dest_snt_dir,filename); int ret_copy = af_copy(fullname_src,fullname_dest); if (!mandatory) return 1; return (ret_copy == 0); }
/** * copy a file */ UNITEX_FUNC int UNITEX_CALL CopyUnitexFile(const char*srcName,const char*dstName) { return af_copy(srcName,dstName); }
int main_DuplicateFile(int argc,char* const argv[]) { if (argc==1) { usage(); return 0; } const char *input_file = NULL; const char *output_file = NULL; int do_delete=0; int do_move=0; int val,index=-1; struct OptVars* vars=new_OptVars(); while (EOF!=(val=getopt_long_TS(argc,argv,optstring_DuplicateFile,lopts_DuplicateFile,&index,vars))) { switch(val) { case 'd': do_delete=1; break; case 'i': if (vars->optarg[0]=='\0') { fatal_error("Empty input argument\n"); } input_file = vars->optarg; break; case 'm': if (vars->optarg[0]=='\0') { fatal_error("Empty move argument\n"); } input_file = vars->optarg; do_move=1; break; case 'h': usage(); return 0; case ':': if (index==-1) fatal_error("Missing argument for option -%c\n",vars->optopt); else fatal_error("Missing argument for option --%s\n",lopts_DuplicateFile[index].name); case '?': if (index==-1) fatal_error("Invalid option -%c\n",vars->optopt); else fatal_error("Invalid option --%s\n",vars->optarg); break; case 'k': case 'q': /* ignore -k and -q parameter instead make error */ break; } index=-1; } if (vars->optind!=argc-1) { fatal_error("Invalid arguments: rerun with --help\n"); } output_file = argv[vars->optind]; if ((input_file==NULL) && (do_delete==0)) { fatal_error("You must specify the input_file file\n"); } if ((input_file!=NULL) && (do_delete==1)) { fatal_error("You cannot specify input_file when delete\n"); } if (output_file==NULL) { fatal_error("You must specify the output_file file\n"); } int result; if (input_file != NULL) { if (do_move == 0) { u_printf("copy file %s to %s\n",input_file,output_file); result=af_copy(input_file,output_file); } else { u_printf("move file %s to %s\n",input_file,output_file); result=af_rename(input_file,output_file); } } else { u_printf("remove file %s\n",output_file); result=af_remove(output_file); } u_printf((result==0) ? "Done.\n" : "Unsucessfull.\n"); return result; }
int main_DuplicateFile(int argc,char* const argv[]) { if (argc==1) { usage(); return SUCCESS_RETURN_CODE; } const char *input_file = NULL; const char *output_file = NULL; int do_delete=0; int do_recursive_delete=0; int do_move=0; int do_make_dir=0; int do_make_dir_parent=0; int val,index=-1; bool only_verify_arguments = false; UnitexGetOpt options; while (EOF!=(val=options.parse_long(argc,argv,optstring_DuplicateFile,lopts_DuplicateFile,&index))) { switch(val) { case 'a': do_make_dir = 1; break; case 'p': do_make_dir_parent = 1; break; case 'd': do_delete = 1; break; case 'r': do_delete = do_recursive_delete = 1; break; case 'i': if (options.vars()->optarg[0]=='\0') { error("Empty input argument\n"); return USAGE_ERROR_CODE; } input_file = options.vars()->optarg; break; case 'm': if (options.vars()->optarg[0]=='\0') { error("Empty move argument\n"); return USAGE_ERROR_CODE; } input_file = options.vars()->optarg; do_move=1; break; case 'V': only_verify_arguments = true; break; case 'h': usage(); return SUCCESS_RETURN_CODE; case ':': index==-1 ? error("Missing argument for option -%c\n",options.vars()->optopt): error("Missing argument for option --%s\n",lopts_DuplicateFile[index].name); return USAGE_ERROR_CODE; case '?': index==-1 ? error("Invalid option -%c\n",options.vars()->optopt) : error("Invalid option --%s\n",options.vars()->optarg); return USAGE_ERROR_CODE; case 'k': case 'q': /* ignore -k and -q parameter instead to raise an error */ break; } index=-1; } if (options.vars()->optind!=argc-1) { error("Invalid arguments: rerun with --help\n"); return USAGE_ERROR_CODE; } output_file = argv[options.vars()->optind]; if ((input_file==NULL) && (do_delete==0) && (do_make_dir==0) && (do_make_dir_parent ==0)) { error("You must specify the input_file file\n"); return USAGE_ERROR_CODE; } if ((input_file!=NULL) && (do_delete==1)) { error("You cannot specify input_file when delete\n"); return USAGE_ERROR_CODE; } if (output_file==NULL) { error("You must specify the output_file file\n"); return USAGE_ERROR_CODE; } if (only_verify_arguments) { // freeing all allocated memory return SUCCESS_RETURN_CODE; } int result = 0; if (input_file != NULL) { if (do_move == 0) { u_printf("copy file %s to %s\n",input_file,output_file); /* af_copy return 0 if success, -1 with reading problem, 1 writing problem */ result=af_copy(input_file,output_file); } else { u_printf("move file %s to %s\n",input_file,output_file); result=af_rename(input_file,output_file); } } else if (do_make_dir != 0) { u_printf("make dir %s\n", output_file); result = mkDirPortable(output_file); } else if (do_make_dir_parent != 0) { u_printf("make dir %s with parent\n", output_file); result = mkDirRecursiveIfNeeded(output_file); } else { if (do_recursive_delete == 0) { u_printf("remove file %s\n",output_file); result=af_remove(output_file); } else { u_printf("remove folder %s\n", output_file); af_remove_folder(output_file); result=0; } } u_printf((result==0) ? "Done.\n" : "Unsucessfull.\n"); return result; }