/*
Gavin SATHAN
20th February 2015
*/
#include
#include
#include
#include
#include
void DELETE_FOLDER_RECURSIVELY(char * path_name);
void main(){
mkdir("./DEL",0777);
mkdir("./DEL/F1",0777);
mkdir("./DEL/F2",0777);
mkdir("./DEL/F3",0777);
mkdir("./DEL/F3/F3_1",0777);
mkdir("./DEL/F3/F3_2",0777);
int fd=open("./DEL/F2/file1.txt",O_RDONLY|O_CREAT,0777);
close(fd);
fd=open("./DEL/F3/F3_1/file2.txt",O_RDONLY|O_CREAT,0777);
close(fd);
fd=open("./DEL/F3/F3_1/file3.txt",O_RDONLY|O_CREAT,0777);
close(fd);
fd=open("./DEL/F3/F3_2/file4.txt",O_RDONLY|O_CREAT,0777);
close(fd);
fd=open("./DEL/F3/file5.txt",O_RDONLY|O_CREAT,0777);
close(fd);
fd=open("./DEL/file5.txt",O_RDONLY|O_CREAT,0777);
close(fd);
DELETE_FOLDER_RECURSIVELY("./DEL");
}//main
void DELETE_FOLDER_RECURSIVELY(char * path_name){
printf("\n\nInstructed to delete %s", path_name);
DIR * directory_ptr=opendir(path_name);
struct dirent * dp=NULL;
char current[1024]=".";
char parent[1024]="..";
char full_path[1024];
printf("%s",current);
while((dp=readdir(directory_ptr))!=NULL)
{
char arg1[1024];
strcpy(full_path,path_name);
strcat(full_path,"/");
strcpy(arg1,dp->d_name);
if ((strcmp(arg1,current)!=0)&&(strcmp(arg1,parent)!=0)){
strcat(full_path,arg1);
if(dp->d_type!=DT_DIR){
printf("\n\tGot a file, name is %s\n",full_path);
int success=unlink(full_path);
if (success==0){
printf("\t\tDeletion Successful!!");
}
else{
printf("\t\tDeletion Failed!!");
}
}//if DT_DIR
else{
printf("\n\tGot a Directory, name is %s\n",full_path);
DELETE_FOLDER_RECURSIVELY(full_path);
}
}//if
}//while
printf("\nThe Directory %s is now Empty.... \n\tProceeding with the Delete\n\n",path_name);
rmdir(path_name);
}//fn
No comments:
Post a Comment