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 # Build MV command
# based on coreutils-8.32 # Based on coreutils-8.32
# Test System: Debian GNU/Linux bookworm/sid (WSL)
# #
function init() { function init() {
SHELL_FOLDER=$(dirname $(readlink -f "$0")); SHELL_FOLDER=$(dirname $(readlink -f "$0"));
set -x set -x
CC=${CROSS_COMPILE}gcc
AR=${CROSS_COMPILE}ar
STRIP=${CROSS_COMPILE}strip
} }
function LIB() { function LIB() {
@@ -17,7 +21,7 @@ function LIB() {
echo $i echo $i
o=$(echo $i | sed 's/\.c/\.o/'); o=$(echo $i | sed 's/\.c/\.o/');
echo $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 done
cd ${SHELL_FOLDER}/lib; cd ${SHELL_FOLDER}/lib;
@@ -26,21 +30,21 @@ function LIB() {
function MV() { function MV() {
cd ${SHELL_FOLDER}; cd ${SHELL_FOLDER};
gcc -I./lib -c -o version.o version.c ${CC} -I./lib -c -o version.o version.c
ar cr libver.a version.o ${AR} cr libver.a version.o
gcc -I. -I./lib -g -O2 -c -o remove.o remove.c ${CC} -I. -I./lib -g -O2 -c -o remove.o remove.c
gcc -I. -I./lib -g -O2 -c -o copy.o copy.c ${CC} -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 ${CC} -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 ${CC} -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 ${CC} -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 ${CC} -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. -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 \ 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 -lselinux -lacl -lattr -pthread -lpcre2-8 -ldl -static
mv ./mv rm_ mv ./mv rm_
$STRIP ./rm_
} }

99
mv.c
View File

@@ -22,6 +22,7 @@
#include <sys/types.h> #include <sys/types.h>
#include <assert.h> #include <assert.h>
#include <selinux/selinux.h> #include <selinux/selinux.h>
#include <stdlib.h>
#include "system.h" #include "system.h"
#include "backupfile.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); fputs (VERSION_OPTION_DESCRIPTION, stdout);
printf("\n\033[31mInstead of rm command to prevent mistaken deletion of files\n\ printf("\n\033[31mInstead of rm command to prevent mistaken deletion of files\n\
Author: aixiao@aixiao.me\033[0m\n"); Author: aixiao@aixiao.me\033[0m\n");
printf("\n\033[31m代替rm命令以防止错误删除文件\n\
作者: aixiao@aixiao.me\033[0m\n");
emit_backup_suffix_note (); emit_backup_suffix_note ();
@@ -337,6 +340,44 @@ Author: aixiao@aixiao.me\033[0m\n");
exit (status); 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 int
main (int argc, char **argv) main (int argc, char **argv)
{ {
@@ -352,32 +393,41 @@ 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]; char *argvs[20]; // 复制 *argv[] 到 *argvs[20]
for (int i=0; i<argc; i++) { for (int i=0; i<argc; i++) {
argvs[i] = argv[i]; argvs[i] = argv[i];
} }
char s[270]; char delete_path[270];
FILE *fd; memset(delete_path, 0, 270);
fd = fopen("/etc/rm_.conf", "r"); read_config("/etc/rm_.conf", delete_path); // 如果有配置文件存在, 读取配置文件, 如果读取配置文件失败默认使用"/tmp/"目录来当作垃圾桶目录
if (fd == NULL) { //printf("%s", delete_path);
//char s[] = "/tmp/";
memset(s, 0, 270); char mkdir_s[270];
strcpy(s, "/tmp/"); strcpy(mkdir_s, "mkdir -p ");
} else { strcat(mkdir_s, delete_path);
while(fgets(s, 270, fd) != NULL) //printf("%s\n", mkdir_s);
{
int len = strlen(s); if (!access(delete_path, 0)) {
s[len-1] = '\0'; //去掉换行符 ;//printf("%s EXISITS!\n", delete_path);
//printf("%s %d \n", s, len - 1); }
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最后一个参数为垃圾桶目录
argvs[argc] = s; char **head_argvs;
char **p9; head_argvs = &(argvs[0]); // head_argvs指向argvs[0]
p9 = &argvs; argc = argc + 1; // 改变argc数
argc = argc + 1;
initialize_main (&argc, &argvs); initialize_main (&argc, &argvs);
set_program_name (argvs[0]); set_program_name (argvs[0]);
@@ -457,20 +507,21 @@ main (int argc, char **argv)
usage (EXIT_FAILURE); usage (EXIT_FAILURE);
} }
} }
x.interactive = I_ALWAYS_YES; x.interactive = I_ALWAYS_YES; // 默认使用-f参数
n_files = argc - optind; n_files = argc - optind;
file = (p9 + optind); file = (head_argvs + optind);
/* /*
// 测试, 打印新的argv参数
int i=0; int i=0;
while(i < argc) while(i < argc)
{ {
printf("%s\n", p9[i]); printf("%s\n", head_argvs[i]);
i++; i++;
} }
printf("%d\n", argc); printf("%d\n", argc);
*/ */
if (n_files <= !target_directory) if (n_files <= !target_directory)
{ {
if (n_files <= 0) if (n_files <= 0)

BIN
x64-rm_ Normal file

Binary file not shown.