📄 rearj.c
字号:
else if(sw=='+'&&*swptr=='\0') skip_rearj_sw=1; else if(sw=='M') { pick_older=1; timestr_older=swptr; } else if(sw=='N') { pick_newer=1; timestr_newer=swptr; } else if(sw=='Y') { testing_mode=1; testing_marker=swptr; } else error(M_INVALID_SWITCH, arg);}/* Parses REARJ.CFG */static void parse_rearj_cfg(){ #if COMPILER==BCC char *cfg_path; #else char cfg_path[CCHMAXPATH]; #endif char tmp_line[200]; FILE *stream; int i, fakesuffix; #if TARGET!=UNIX #if COMPILER==BCC if((cfg_path=searchpath(cfg_name))==NULL) error(M_CANT_FIND_CONFIG, cfg_name); #else _searchenv(cfg_name, "PATH", cfg_path); if(cfg_path[0]=='\0') error(M_CANT_FIND_CONFIG, cfg_name); #endif #else /* Attempt search in home directory first, then in /etc. */ sprintf(cfg_path, "%s/.%s", getenv("HOME"), cfg_name); if((stream=file_open(cfg_path, m_r))!=NULL) fclose(stream); else sprintf(cfg_path, "/etc/%s", cfg_name); #endif if((stream=file_open(cfg_path, m_r))==NULL) error(M_CANTOPEN, cfg_path); msg_cprintf(H_HL|H_NFMT, M_USING_CONFIG, cfg_path); if(fgets(tmp_line, sizeof(tmp_line), stream)==NULL) total_suffixes=0; else { rewind(stream); for(i=0; i<MAX_SUFFIXES; i++) { /* ASR fix for 2.43, 24/01/2003 - the option strings may be placed before ANY extension command. */ do { /* Extension */ do { if(fgets(tmp_line, sizeof(tmp_line), stream)==NULL) goto no_more_exts; archivers[i].hidden_supported=0; archivers[i].subdirs_supported=0; archivers[i].manual_deletion=0; /* ASR fix */ tokenize_lf(tmp_line); alltrim(tmp_line); } while (tmp_line[0]=='\0'); fakesuffix=1;#ifdef COLOR_OUTPUT if(!strnicmp(tmp_line, "COLORS ", 7)) { if(parse_colors(tmp_line+7)) error(M_INVALID_PARAM_STR, tmp_line); } else#endif if(!strnicmp(tmp_line, "VIRUS ", 6)) { if((extr_cmd_text=strdup(tmp_line+6))==NULL) error(M_OUT_OF_MEMORY); if(strchr(extr_cmd_text, PATHSEP_DEFAULT)==NULL) { msg_cprintf(0, M_NO_ANTIVIRUS_PATH); msg_cprintf(0, M_IGNORED_FOR_COMP); arj_delay(4); } } else fakesuffix=0; } while(fakesuffix); if(strlen(tmp_line)>MAX_SUFFIX) error(M_INVALID_SUFFIX, tmp_line); if((archivers[i].suffix=strdup(tmp_line))==NULL) error(M_OUT_OF_MEMORY); /* Pack command */ if(fgets(tmp_line, sizeof(tmp_line), stream)==NULL) error(M_MISSING_PACK_CMD, archivers[i].suffix); tokenize_lf(tmp_line); alltrim(tmp_line); if(strlen(tmp_line)==0) error(M_INVALID_PACK_CMD, tmp_line); if(strstr(tmp_line, strform)==NULL&&strstr(tmp_line, u_strform)==NULL) error(M_NO_PACK_STRFORM, tmp_line); if((archivers[i].pack_cmd=strdup(tmp_line))==NULL) error(M_OUT_OF_MEMORY); /* Unpack command */ if(fgets(tmp_line, sizeof(tmp_line), stream)==NULL) error(M_MISSING_UNPACK_CMD, archivers[i].suffix); tokenize_lf(tmp_line); alltrim(tmp_line); if(strlen(tmp_line)==0) error(M_INVALID_UNPACK_CMD, tmp_line); if(strstr(tmp_line, strform)==NULL&&strstr(tmp_line, u_strform)==NULL) error(M_NO_UNPACK_STRFORM, tmp_line); if((archivers[i].unpack_cmd=strdup(tmp_line))==NULL) error(M_OUT_OF_MEMORY); /* Option record */ if(fgets(tmp_line, sizeof(tmp_line), stream)==NULL) error(M_MISSING_OPTIONS, archivers[i].suffix); tokenize_lf(tmp_line); alltrim(tmp_line); if(strpbrk(tmp_line, "Aa")!=NULL) archivers[i].hidden_supported=1; if(strpbrk(tmp_line, "Dd")!=NULL) archivers[i].subdirs_supported=1; /* ASR fix for v 2.42.05 (.tar.gz): */ if(strpbrk(tmp_line, "Tt")!=NULL) archivers[i].manual_deletion=1; }no_more_exts: total_suffixes=i; } fclose(stream);}/* atexit routine */static void final_cleanup(void){ if(!cleanup_initiated) { cleanup_initiated=1; if(tmpdir_malloced) release_dir(tmp_dir); /* ASR fix 14/11/2000 */ if(work_dir[0]!='\0') file_chdir(work_dir); if(tmp_dir!=NULL) { if(clear_tmp_dir) unlink_all(tmp_dir); if(!work_directory_assigned) file_rmdir(tmp_dir); if(tmpdir_malloced) { free(tmp_dir); /* malloc'ed */ tmpdir_malloced=0; } } }#ifdef COLOR_OUTPUT scrn_reset();#endif}/* Ctrl+C handler */static void ctrlc_handler(SIGHDLPARAMS){ /* Check if we are able to honor the request. If we aren't, raise the signal again and make a speedy getaway. */ if(ctrlc_busy) raise(SIGINT); else { ctrlc_initiated=1; signal(SIGINT, NULL); /* Restore default Ctrl+C handler */ msg_cprintf(H_SIG, M_BREAK_SIGNALED); exit(REARJ_ERL_WARNING); }}/* Main routine */int main(int argc, char **argv){ int cnv_rc, exit_code; static char rearj_exe[CCHMAXPATH]; int switchar; int arg; char *aptr=NULL; unsigned int i; FILE_COUNT cur_file; char tmp_name[CCHMAXPATH], src_name[CCHMAXPATH]; char *fullpath; long total_gain; char *env_ptr, *env_dup, *env_tail; struct timestamp timestamp; time_t start_time, end_time; unsigned long time_diff;#ifdef COLOR_OUTPUT no_colors=redirected=!is_tty(stdout);#endif msg_cprintf(0, M_REARJ_BANNER, build_date); #ifdef TZ_VAR tzset(); #endif build_crc32_table(); ctrlc_busy=0; lfn_supported=LFN_NOT_SUPPORTED; convert_nested_archives=0; run_precount_cmd=0; run_preunpack_cmd=0; recurse_subdirs=0; help_issued=0; no_file_activity=0; delete_original_archives=0; skip_larger_output=0; cnv_diskette_archives=0; run_extract_cmd=0; skip_rearj_sw=0; logging_enabled=0; internal_archives_only=0; pick_older=0; pick_newer=0; conversion_query=0; chk_integrity=0; overwrite_existing=0; skip_lfn=0; skip_packing=0; skip_timestamping=0; skip_size_check=0; work_directory_assigned=0; testing_mode=0; update_with_backups=0; clear_tmp_dir=0; skip_count=0; target_type=0; total_old_fsize=total_new_fsize=0L; total_files=0; ts_store(&ts_older, OS_SPECIAL, 0L); ts_newer=ts_older; ctrlc_initiated=0; is_registered=0; exit_code=REARJ_ERL_SUCCESS; log_name=rearj_log; default_suffix=-1; backup_extension=backup_ext; #ifndef SKIP_GET_EXE_NAME get_exe_name(rearj_exe); #else get_exe_name(rearj_exe, argv[0]); #endif exe_name=rearj_exe; target_suffix=NULL; suffix_override=NULL; extract_cmd_text=NULL; precount_cmd_text=NULL; preunpack_cmd_text=NULL; tmp_dir=NULL; testing_marker=NULL; acc_nested_suffixes=NULL; work_dir[0]='\0'; timestr_older=timestr_newer=nullstr; flist_init(&flist_exclusion, EXCL_FLIST_SIZE, 0, 0); atexit(final_cleanup); parse_reg_key(); detect_lfns(); is_registered=reg_validation(regdata+REG_KEY1_SHIFT, regdata+REG_KEY2_SHIFT, regdata+REG_NAME_SHIFT, regdata+REG_HDR_SHIFT); if(!is_registered&&!msg_strcmp((FMSG *)regdata+REG_KEY2_SHIFT, M_REG_TYPE)) msg_cprintf(0, M_REGISTERED_TO, regdata+REG_NAME_SHIFT); limit=20; total_suffixes=0; if(signal(SIGINT, ctrlc_handler)==SIG_ERR) error(M_SIGNAL_FAILED); limit=0; if(regdata[REG_NAME_SHIFT]=='\0') is_registered=1; if(!reg_validation(single_spc, single_spc, single_spc, regdata+REG_HDR_SHIFT)) is_registered=2; n_args=0; switchar=get_sw_char(); for(arg=1; arg<argc; arg++) { aptr=argv[arg]; if(aptr[0]==switchar&&aptr[1]=='+'&&aptr[2]=='\0') skip_rearj_sw=0; } if((env_ptr=getenv(rearj_sw))!=NULL) { env_dup=strdup(env_ptr); msg_cprintf(H_HL|H_NFMT, M_USING_REARJ_SW, env_dup); for(env_ptr=env_dup; *env_ptr!='\0'; env_ptr++) if(*env_ptr==' ') *env_ptr='\0'; /* Tokenize by spaces */ env_tail=env_ptr; env_ptr=env_dup; while(env_ptr<env_tail) { while(*env_ptr=='\0') env_ptr++; if(env_ptr<env_tail) { if(switchar=='-') name_to_hdr(env_ptr); if(*env_ptr==switchar) analyze_rearj_sw(aptr); while(*env_ptr!='\0'&&env_ptr<env_tail) env_ptr++; } } } for(arg=1; arg<argc; arg++) { aptr=argv[arg]; if(switchar=='-') name_to_hdr(aptr); if(aptr[0]==switchar) analyze_rearj_sw(aptr); else { if(n_args>=MAX_ARGS) error(M_ARGTABLE_OVERFLOW); arg_ptr[n_args++]=aptr; } } if(skip_lfn) lfn_supported=LFN_NOT_SUPPORTED; if(chk_integrity) exit(check_integrity(rearj_exe)); if(help_issued||n_args==0) { msg_cprintf(0, strform, M_REARJ_COMMANDS); msg_cprintf(0, strform, M_REARJ_RCODES); exit(REARJ_ERL_SUCCESS); } if(delete_original_archives&&testing_mode) error(M_YD_CMD_CONFLICT); if(testing_mode&&testing_marker!=NULL&&!logging_enabled) error(M_LY_CMD); parse_rearj_cfg(); if(run_extract_cmd&&extract_cmd_text==NULL) error(M_NO_V_CMD, cfg_name); if(suffix_override!=NULL) { default_suffix=-1; for(i=0; i<total_suffixes; i++) { if(!strcmp_os(suffix_override, archivers[i].suffix)) default_suffix=i; } if(default_suffix<0) error(M_INVALID_F_SUFFIX, suffix_override); } if(target_suffix!=NULL) { target_type=-1; for(i=limit; i<total_suffixes; i++) { if(!strcmp_os(target_suffix, archivers[i].suffix)) target_type=i; } if(target_type<0) error(M_INVALID_T_SUFFIX, target_suffix); } if(pick_older||pick_newer) { if(timestr_older[0]!='\0') convert_strtime(&ts_older, timestr_older); if(timestr_newer[0]!='\0') convert_strtime(&ts_newer, timestr_newer); if(timestr_older[0]=='\0'||timestr_newer[0]=='\0') { arj_getdate(&x_date); make_timestamp(×tamp, x_date.da_year, x_date.da_mon, x_date.da_day, 0, 0, 0); if(timestr_newer[0]=='\0') ts_newer=timestamp; if(timestr_older[0]=='\0') ts_older=timestamp; } } if(file_getcwd(work_dir)==NULL) error(M_GETCWD_FAILED); if(no_file_activity) msg_cprintf(0, M_SIMULATION_MODE); flist_init(&flist_main, LARGE_FLIST_SIZE, n_args>1, 1); for(i=limit; i<n_args; i++) { flush_kbd(); if(arg_ptr[i][0]=='!') { if(arg_ptr[i][1]=='\0') error(M_NO_LISTFILE); if((liststream=file_open(arg_ptr[i]+1, m_r))==NULL) error(M_CANTOPEN, arg_ptr[i]+1); while(fgets(name_fetch, CCHMAXPATH, liststream)!=NULL) { tokenize_lf(name_fetch); alltrim(name_fetch); if(name_fetch[0]!='\0') { strcpy(tmp_name, name_fetch); if(wild_list(&flist_main, tmp_name, 0, 1, recurse_subdirs, NULL)) break; } } fclose(liststream); } else { strcpy(tmp_name, arg_ptr[i]); if(wild_list(&flist_main, tmp_name, 0, 1, recurse_subdirs, NULL)) break; } } if(flist_main.files==0) { msg_cprintf(0, internal_archives_only?M_NO_FILES_INT:M_NO_FILES); exit(internal_archives_only?REARJ_ERL_SUCCESS:REARJ_ERL_WARNING); } time(&start_time); if(logging_enabled) { if((logstream=file_open(log_name, m_a))==NULL) error(M_CANTOPEN, log_name); arj_gettime(&log_ts); if(testing_marker!=NULL) if(fprintf(logstream, M_LOGENTRY_MARKER, timeseq, archivers[target_type].suffix, testing_marker)<=0) error(M_CANT_WRITE_LOG); if(fprintf(logstream, M_LOGENTRY_HEADER, timeseq, archivers[target_type].suffix)<=0) error(M_CANT_WRITE_LOG); } if(cnv_diskette_archives&&count_files()>0) msg_cprintf(0, M_CWD_MUST_BE_EMPTY); if(tmp_dir==NULL) { /* BUGBUG: this comes from a NetBSD patch. Originally a check for NO_MKDTEMP was suggested, but where are we expected to define it under DOS? -- ASR fix 25/01/2004 */#ifdef HAVE_MKDTEMP tmp_dir=mkdtemp("/tmp/arj.XXXXXX"); /* BUGBUG: hardcoded location? */#else tmp_dir=tmpnam(NULL); if(file_mkdir(tmp_dir)) error(M_CANT_MKDIR, tmp_dir);#endif } else { if(file_chdir(tmp_dir)) error(M_CANT_CHDIR, tmp_dir); if(count_files()>0) error(M_WORK_DIR_NOT_EMPTY, tmp_dir); if(file_chdir(work_dir)) error(M_CANT_CHDIR, work_dir); } if((fullpath=(char *)malloc(CCHMAXPATH))==NULL) error(M_OUT_OF_MEMORY); if(truename(fullpath, tmp_dir)) error(M_CANT_GET_FULL_PATH); tmp_dir=fullpath; clear_tmp_dir=1; tmpdir_malloced=1; /* Introduced by ASR */ for(cur_file=0; cur_file<flist_main.files; cur_file++) { flush_kbd(); if(fetch_keystrokes()) { msg_cprintf(H_PROMPT, M_OK_TO_QUIT); if(query_action()) { msg_cprintf(H_OPER, M_QUITTING); exit(REARJ_ERL_WARNING); } } retrieve_entry(src_name, &flist_main, cur_file); ts_store(×tamp, OS, file_getftime(src_name)); if((!pick_newer||!ts_valid(ts_newer)||ts_cmp(×tamp, &ts_newer)>=0)&& (!pick_older||!ts_valid(ts_older)||ts_cmp(×tamp, &ts_older)<0)) { cnv_rc=convert_archive(src_name); if(cnv_rc!=REARJ_ERL_SUCCESS) { skip_count++; log_as_skipped(src_name, cnv_rc); if(exit_code==REARJ_ERL_SUCCESS&&(!internal_archives_only||cnv_rc!=REARJ_ERL_UNCONFIGURED)) exit_code=cnv_rc; } release_dir(tmp_dir); /* ASR fix 14/11/2000 */ if(file_chdir(work_dir)) error(M_CANT_CHDIR, work_dir); unlink_all(tmp_dir); if(file_rmdir(tmp_dir)) msg_cprintf(H_ERR, M_CANT_RMDIR, tmp_dir); if(file_mkdir(tmp_dir)) error(M_CANT_MKDIR, tmp_dir); } } if(!work_directory_assigned) { if(file_rmdir(tmp_dir)) msg_cprintf(H_ERR, M_CANT_RMDIR, tmp_dir); } if(tmpdir_malloced) { free(tmp_dir); tmpdir_malloced=0; } tmp_dir=NULL; time(&end_time); time_diff=sub_time(end_time, start_time); total_gain=(long)(total_old_fsize-total_new_fsize); msg_cprintf(H_HL|H_NFMT, M_TOTAL_SECONDS, time_diff); msg_cprintf(H_HL|H_NFMT, M_TOTAL_CONVERTED, total_files); msg_cprintf(H_HL|H_NFMT, M_TOTAL_SKIPPED, skip_count); msg_cprintf(H_HL|H_NFMT, M_OLD_SIZE, total_old_fsize); msg_cprintf(H_HL|H_NFMT, M_NEW_SIZE, total_new_fsize); msg_cprintf(H_HL|H_NFMT, M_SAVINGS_SIZE, total_gain); if(logging_enabled) { arj_gettime(&log_ts); if(fprintf(logstream, M_LOGENTRY_FOOTER, timeseq, archivers[target_type].suffix, total_old_fsize, total_new_fsize, total_gain, time_diff)<=0) error(M_CANT_WRITE_LOG); fclose(logstream); } flist_cleanup_proc(&flist_main); flist_cleanup_proc(&flist_exclusion); return((skip_count>0)?exit_code:REARJ_ERL_SUCCESS);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -