更优雅

This commit is contained in:
aixiao 2022-04-15 14:54:27 +08:00
parent 494e3724ed
commit cefd122c19

64
mv.c
View File

@ -378,26 +378,9 @@ static char *read_config(char *config_file, char *buffer)
return strcpy(buffer, temp_buffer); return strcpy(buffer, temp_buffer);
} }
int // 处理参数
main (int argc, char **argv) int process_argv(int argc, char *argv[], char **argvs)
{ {
int c;
bool ok;
bool make_backups = false;
char const *backup_suffix = NULL;
char *version_control_string = NULL;
struct cp_options x;
char *target_directory = NULL;
bool no_target_directory = false;
int n_files;
char **file;
bool selinux_enabled = (0 < is_selinux_enabled ());
char *argvs[20]; // 复制 *argv[] 到 *argvs[20]
for (int i=0; i<argc; i++) {
argvs[i] = argv[i];
}
char delete_path[270]; char delete_path[270];
memset(delete_path, 0, 270); memset(delete_path, 0, 270);
read_config("/etc/rm_.conf", delete_path); // 如果有配置文件存在, 读取配置文件, 如果读取配置文件失败默认使用"/tmp/"目录来当作垃圾桶目录 read_config("/etc/rm_.conf", delete_path); // 如果有配置文件存在, 读取配置文件, 如果读取配置文件失败默认使用"/tmp/"目录来当作垃圾桶目录
@ -424,8 +407,38 @@ main (int argc, char **argv)
} }
} }
argvs[argc] = delete_path; // 使argv最后一个参数为垃圾桶目录 for (int i=0; i<argc; i++)
{
argvs[i] = argv[i];
}
char *s = malloc(270);
strcpy(s, delete_path);
argvs[argc] = s;
return 0;
}
int
main (int argc, char **argv)
{
int c;
bool ok;
bool make_backups = false;
char const *backup_suffix = NULL;
char *version_control_string = NULL;
struct cp_options x;
char *target_directory = NULL;
bool no_target_directory = false;
int n_files;
char **file;
bool selinux_enabled = (0 < is_selinux_enabled ());
/* 处理 argv */
char **head_argvs; char **head_argvs;
char *argvs[20] = { malloc(sizeof(char *)) };
process_argv(argc, argv, &(argvs[0]));
head_argvs = &(argvs[0]); // head_argvs指向argvs[0] head_argvs = &(argvs[0]); // head_argvs指向argvs[0]
argc = argc + 1; // 改变argc数 argc = argc + 1; // 改变argc数
@ -512,16 +525,7 @@ x.interactive = I_ALWAYS_YES; // 默认使用-f参数
n_files = argc - optind; n_files = argc - optind;
file = (head_argvs + optind); file = (head_argvs + optind);
/*
// 测试, 打印新的argv参数
int i=0;
while(i < argc)
{
printf("%s\n", head_argvs[i]);
i++;
}
printf("%d\n", argc);
*/
if (n_files <= !target_directory) if (n_files <= !target_directory)
{ {
if (n_files <= 0) if (n_files <= 0)