linux下利用系统函数实现rm -rf的功能!
2015-11-24 21:41
751 查看
刚学习linux下的文件和目录函数,利用这些写个linux的rm -rf功能模块。希望大家多多点评!!!
#include<stdio.h> #include<sys/types.h> #include<dirent.h> #include<stdlib.h> void removedir(char* dir); void removefile(char *name)//remove file and empty dir { DIR *d; if ((d = opendir(name)) == NULL) { printf("file:%s is moved!\n", name); unlink(name); } else if ((rmdir(name)) == 0) printf("dir:%s is moved!\n", name); else { printf("dir name:%s\n", name); removedir(name); } } void removedir(char* dir)//check the dir { struct dirent *dp; DIR *d; char bb[100] = ""; d = opendir(dir); getcwd(bb, 100); while((dp = readdir(d)) != NULL) { if (strcmp(dp->d_name, ".") == 0 || strcmp(dp->d_name, "..") == 0) continue; chdir(dir); printf("\nfind :%s\n", dp->d_name); removefile(dp->d_name); } closedir(d); chdir(bb); removefile(dir); printf("%s is moved!\n", dir); } int main(int argc, char* argv[]) { if (argc < 2) { printf("./app file or dir!\n"); exit(1); } removefile(argv[1]); printf("remove successful\n!"); return 0; }
相关文章推荐
- Linux下Vsftp配置及详解
- Linux进程冻结技术
- Linux/Cent OS 6.5 编译,安装obfuscator-llvm-3.6.1
- Linux重定向:将标准输出stdout和标准错误stderr重定向到一个文件
- rhel-server-7.1更换yum源(CentOS7)
- Linux命令集锦
- 编写linux静态库
- CentOS Linux 上安装svn服务器
- Linux学习笔记4
- Linux 常用命令
- Linux.BackDoor.Gates/Chikdos Attack And Defense Analysis
- Linux下基于net-snmp的Agent扩展开发
- 用 Aptik 备份你的 Linux 桌面的应用和设置
- Linux实验二报告
- Linux常用命令大全
- 自定义Linux 内核驱动模块的编译
- Linux ldconfig命令
- linux 进线程间通信
- 在linux的环境下 连接深澜校园网的方法
- Linux学习 - Ubuntu 14.04配置Opencv环境