Compare commits

3 Commits

Author SHA1 Message Date
5df2ddbd89 读取配置文件失败后添加默认的垃圾桶目录 2022-03-24 07:41:34 -03:00
96246e8eb8 优化读取配置文件 2022-03-19 03:40:16 -06:00
daafec68f5 Support Android. 2022-03-11 22:22:11 +08:00
3 changed files with 95 additions and 40 deletions

View File

@@ -1,12 +1,16 @@
#!/bin/bsh
#!/bin/bash
#
# Build MV command
# based on coreutils-8.32
# Based on coreutils-8.32
# Test System: Debian GNU/Linux bookworm/sid (WSL)
#
function init() {
SHELL_FOLDER=$(dirname $(readlink -f "$0"));
set -x
CC=${CROSS_COMPILE}gcc
AR=${CROSS_COMPILE}ar
STRIP=${CROSS_COMPILE}strip
}
function LIB() {
@@ -17,7 +21,7 @@ function LIB() {
echo $i
o=$(echo $i | sed 's/\.c/\.o/');
echo $o;
gcc -I. -I./lib -Ilib -I./lib -g -O2 -c -o lib/${o} lib/${i}
${CC} -I. -I./lib -Ilib -I./lib -g -O2 -c -o lib/${o} lib/${i}
done
cd ${SHELL_FOLDER}/lib;
@@ -26,21 +30,21 @@ function LIB() {
function MV() {
cd ${SHELL_FOLDER};
gcc -I./lib -c -o version.o version.c
ar cr libver.a version.o
gcc -I. -I./lib -g -O2 -c -o remove.o remove.c
gcc -I. -I./lib -g -O2 -c -o copy.o copy.c
gcc -I. -I./lib -g -O2 -c -o cp-hash.o cp-hash.c
gcc -I. -I./lib -g -O2 -c -o extent-scan.o extent-scan.c
gcc -I. -I./lib -g -O2 -c -o force-link.o force-link.c
gcc -I. -I./lib -g -O2 -c -o selinux.o selinux.c
gcc -I. -I./lib -g -O2 -c -o mv.o mv.c
${CC} -I./lib -c -o version.o version.c
${AR} cr libver.a version.o
${CC} -I. -I./lib -g -O2 -c -o remove.o remove.c
${CC} -I. -I./lib -g -O2 -c -o copy.o copy.c
${CC} -I. -I./lib -g -O2 -c -o cp-hash.o cp-hash.c
${CC} -I. -I./lib -g -O2 -c -o extent-scan.o extent-scan.c
${CC} -I. -I./lib -g -O2 -c -o force-link.o force-link.c
${CC} -I. -I./lib -g -O2 -c -o selinux.o selinux.c
${CC} -I. -I./lib -g -O2 -c -o mv.o mv.c
gcc -g -O2 -Wl,--as-needed -o mv \
${CC} -g -O2 -Wl,--as-needed -o mv \
mv.o remove.o copy.o cp-hash.o extent-scan.o force-link.o selinux.o libver.a lib/libcoreutils.a lib/libcoreutils.a \
-lselinux -lacl -lattr -pthread -lpcre2-8 -ldl -static
mv ./mv rm_
$STRIP ./rm_
}

93
mv.c
View File

@@ -22,6 +22,7 @@
#include <sys/types.h>
#include <assert.h>
#include <selinux/selinux.h>
#include <stdlib.h>
#include "system.h"
#include "backupfile.h"
@@ -329,6 +330,8 @@ If you specify more than one of -i, -f, -n, only the final one takes effect.\n\
fputs (VERSION_OPTION_DESCRIPTION, stdout);
printf("\n\033[31mInstead of rm command to prevent mistaken deletion of files\n\
Author: aixiao@aixiao.me\033[0m\n");
printf("\n\033[31m代替rm命令以防止错误删除文件\n\
作者: aixiao@aixiao.me\033[0m\n");
emit_backup_suffix_note ();
@@ -337,6 +340,44 @@ Author: aixiao@aixiao.me\033[0m\n");
exit (status);
}
// 在这里删除字符串里的'\n'
static void delchar(char *str, char c)
{
int i, j;
for (i = j = 0; str[i] != '\0'; i++) {
if (str[i] != c) //判断是否有和待删除字符一样的字符
{
str[j++] = str[i];
}
}
str[j] = '\0'; //字符串结束
}
// 读取配置文件
static char *read_config(char *config_file, char *buffer)
{
char temp_buffer[270];
FILE *fd;
fd = fopen(config_file, "r"); //打开文件
if (fd == NULL) {
return strcpy(buffer, "/tmp/");
}
memset(temp_buffer, 0, 270);
size_t ret = fread(temp_buffer, sizeof(temp_buffer) / sizeof((temp_buffer)[0]), sizeof(*temp_buffer), fd); //读取配置
/*
if (ret != sizeof(*temp_buffer)) {
fprintf(stderr, "fread() failed: %zu\n", ret);
exit(EXIT_FAILURE);
}
*/
delchar(temp_buffer, 10); // 删除'\n'
return strcpy(buffer, temp_buffer);
}
int
main (int argc, char **argv)
{
@@ -352,32 +393,41 @@ main (int argc, char **argv)
char **file;
bool selinux_enabled = (0 < is_selinux_enabled ());
char *argvs[20];
char *argvs[20]; // 复制 *argv[] 到 *argvs[20]
for (int i=0; i<argc; i++) {
argvs[i] = argv[i];
}
char s[270];
FILE *fd;
fd = fopen("/etc/rm_.conf", "r");
if (fd == NULL) {
//char s[] = "/tmp/";
memset(s, 0, 270);
strcpy(s, "/tmp/");
} else {
while(fgets(s, 270, fd) != NULL)
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
{
int len = strlen(s);
s[len-1] = '\0'; //去掉换行符
//printf("%s %d \n", s, len - 1);
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] = s;
char **p9;
p9 = &argvs;
argc = argc + 1;
argvs[argc] = delete_path; // 使argv最后一个参数为垃圾桶目录
char **head_argvs;
head_argvs = &(argvs[0]); // head_argvs指向argvs[0]
argc = argc + 1; // 改变argc数
initialize_main (&argc, &argvs);
set_program_name (argvs[0]);
@@ -457,16 +507,17 @@ main (int argc, char **argv)
usage (EXIT_FAILURE);
}
}
x.interactive = I_ALWAYS_YES;
x.interactive = I_ALWAYS_YES; // 默认使用-f参数
n_files = argc - optind;
file = (p9 + optind);
file = (head_argvs + optind);
/*
// 测试, 打印新的argv参数
int i=0;
while(i < argc)
{
printf("%s\n", p9[i]);
printf("%s\n", head_argvs[i]);
i++;
}
printf("%d\n", argc);

BIN
x64-rm_ Normal file

Binary file not shown.