更优雅

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

94
mv.c
View File

@ -378,6 +378,48 @@ static char *read_config(char *config_file, char *buffer)
return strcpy(buffer, temp_buffer); return strcpy(buffer, temp_buffer);
} }
// 处理参数
int process_argv(int argc, char *argv[], char **argvs)
{
char delete_path[270];
memset(delete_path, 0, 270);
read_config("/etc/rm_.conf", delete_path); // 如果有配置文件存在, 读取配置文件, 如果读取配置文件失败默认使用"/tmp/"目录来当作垃圾桶目录
//printf("%s", delete_path);
char mkdir_s[270];
strcpy(mkdir_s, "mkdir -p ");
strcat(mkdir_s, delete_path);
//printf("%s\n", mkdir_s);
if (!access(delete_path, 0)) {
;//printf("%s EXISITS!\n", delete_path);
}
else
{
printf("%s DOESN'T EXISIT!\n", delete_path);
if (-1 == system("mount -o remount,rw /")) { // 调用mount命令改变根目录读写权限
perror("system mount");
}
if (-1 == system(mkdir_s)) { // 调用mkdir命令创建不存在的垃圾桶目录
perror("system mkdir");
} else {
printf("%s Directory Created Successfully!\n", delete_path);
}
}
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 int
main (int argc, char **argv) main (int argc, char **argv)
{ {
@ -393,41 +435,12 @@ main (int argc, char **argv)
char **file; char **file;
bool selinux_enabled = (0 < is_selinux_enabled ()); bool selinux_enabled = (0 < is_selinux_enabled ());
char *argvs[20]; // 复制 *argv[] 到 *argvs[20] /* 处理 argv */
for (int i=0; i<argc; i++) { char **head_argvs;
argvs[i] = argv[i]; char *argvs[20] = { malloc(sizeof(char *)) };
} process_argv(argc, argv, &(argvs[0]));
head_argvs = &(argvs[0]); // head_argvs指向argvs[0]
char delete_path[270]; argc = argc + 1; // 改变argc数
memset(delete_path, 0, 270);
read_config("/etc/rm_.conf", delete_path); // 如果有配置文件存在, 读取配置文件, 如果读取配置文件失败默认使用"/tmp/"目录来当作垃圾桶目录
//printf("%s", delete_path);
char mkdir_s[270];
strcpy(mkdir_s, "mkdir -p ");
strcat(mkdir_s, delete_path);
//printf("%s\n", mkdir_s);
if (!access(delete_path, 0)) {
;//printf("%s EXISITS!\n", delete_path);
}
else
{
printf("%s DOESN'T EXISIT!\n", delete_path);
if (-1 == system("mount -o remount,rw /")) { // 调用mount命令改变根目录读写权限
perror("system mount");
}
if (-1 == system(mkdir_s)) { // 调用mkdir命令创建不存在的垃圾桶目录
perror("system mkdir");
} else {
printf("%s Directory Created Successfully!\n", delete_path);
}
}
argvs[argc] = delete_path; // 使argv最后一个参数为垃圾桶目录
char **head_argvs;
head_argvs = &(argvs[0]); // head_argvs指向argvs[0]
argc = argc + 1; // 改变argc数
initialize_main (&argc, &argvs); initialize_main (&argc, &argvs);
set_program_name (argvs[0]); set_program_name (argvs[0]);
@ -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)