1.从命令行传参删除多个目录
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <unistd.h>
#include <sys/stat.h>
void my_rm(const char*argv)
{
DIR * dir = opendir(argv);
if(NULL==dir)
{
perror("打开目录失败");
return;
}
while(1){
struct dirent* entry = readdir(dir);
if(NULL==entry)
{
break;
}
if(strcmp(entry->d_name,".")== 0 || strcmp(entry->d_name,"..")== 0 ){
continue;
}
char*pathname=malloc(1024);
snprintf(pathname,1024,"%s/%s",argv,entry->d_name);
if(entry->d_type==DT_DIR){
my_rm(pathname);
rmdir(pathname);
}
else{
unlink(pathname);
}
free(pathname);
}
rmdir(argv);
}
int main(int argc, const char *argv[])
{
int i;
for(i=1;i<argc;i++){
my_rm(argv[i]);
}
return 0;
}