本文共 1115 字,大约阅读时间需要 3 分钟。
- #include <sys/types.h>
- #include <dirent.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/stat.h>
- #include <unistd.h>
- #define BUF_LEN 1024
-
-
-
-
-
-
-
-
-
- void rm(char * name)
- {
- DIR *dir;
- struct dirent *read_dir;
- struct stat st;
- char buf[BUF_LEN];
-
- if(lstat(name, &st) < 0)
- {
- fprintf(stderr, "Lstat Error!/n");
- exit(1);
- }
-
- if(S_ISDIR(st.st_mode))
- {
- if((dir = opendir(name)) == NULL)
- {
- fprintf(stderr, "remove [%s] faild/n", name);
- exit(1);
- }
-
- while((read_dir = readdir(dir)) != NULL)
- {
- if(strcmp(read_dir->d_name, ".") == 0 ||
- strcmp(read_dir->d_name, "..") == 0)
- continue;
- sprintf(buf, "%s/%s", name, read_dir->d_name);
- rm(buf);
- }
- }
- printf("rm :%s/n", name);
- if(remove(name) < 0)
- {
- fprintf(stderr, "remove [%s] faild/n", name);
- }
- }
-
- int main(int argc, char **argv)
- {
- if(argc < 1)
- {
- fprintf(stderr, "Usage <%s><file>/n", argv[0]);
- }
- rm(argv[1]);
-
- return 0;
- }
转载地址:http://vuhrb.baihongyu.com/