now this is the code i have written for recursively deleting the contents of a folder.
i am going to change it so that given a dirname it will not delete dirname but will delete other than dirname.
there are reasons why i used tree data structure.
for example :-
there is a folder fdt, say it contents subfolders sd1,sd2,sd3. say sd1 has subfolder ssd1.
now if i use gftp to delete contents od fdt it deletes in following seq.
remove /fdt/sd1/ssd1
remove /fdt/sd3
remove /fdt/sd2
remove /fdt/sd1
now i want to delete in same manner but yet to acheive it. i think tree data structure will help me in this like if i calculate depth of the tree and then start deleting from highest depth. or like this ...
please go through it and give suggestions.
mehul.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#define Error(Str) FatalError(Str)
#define FatalError(Str) fprintf(stderr, "%s\n", Str), exit(1)
struct treenode
{
struct treenode *father; ----> not using now
char data[257];
struct treenode *son;
struct treenode *next;
};
typedef struct treenode *NODEPTR;
NODEPTR rfCreateTreeNode(NODEPTR,char *,int);
NODEPTR rfCreateDirTree(NODEPTR,char *,int);
void rfPrintDirTree(NODEPTR);
int rfDeleteDirTree(NODEPTR);
int main(void)
{
NODEPTR tree = NULL;
int retStatus;
tree = rfCreateTreeNode(tree,"/usr/kalinga/ftp_delete_test1",-1);
tree = rfCreateDirTree(tree,"/usr/kalinga/ftp_delete_test1",-1);
rfPrintDirTree(tree);
retStatus = rfDeleteDirTree(tree);
return 0;
}//end of main()
NODEPTR rfCreateTreeNode(NODEPTR tree,char *value,int status)
{
if(tree == NULL)
{
//printf("1\n");
tree = (NODEPTR)malloc(sizeof(struct treenode));
if(tree == NULL)
FatalError("Out of space!!!");
else
tree->son = tree->next = tree->father = NULL;
strcpy(tree->data,value);
}//end of if(tree == NULL)
if(status == 1)
{
tree->son = rfCreateTreeNode(tree->son,value,-1);
tree->son->father = tree;
tree = tree->son;
}//end of if(status == 1)
if(status == 0)
{
while(tree->next != NULL)
tree = tree->next;
tree->next = rfCreateTreeNode(tree->next,value,-1);
tree->next->father = tree->father;
tree = tree->next;
}//end of if(status == 0)
return tree;
}//end of rfCreateTree()
NODEPTR rfCreateDirTree(NODEPTR dirTree,char *dirName,int iStatus)
{
DIR *dp;
struct dirent *d;
struct stat st;
static int fiCnt1 = 0;
int fiCnt2 = 0;
char filename[257];
if((dp=opendir(dirName)) == NULL)
{
printf ("ERROR unable to open %s directory, at line %d of file %s.\n",
dirName,__LINE__, __FILE__);
return NULL;
}//end of if((dp=opendir(dirName)) == NULL)
if(fiCnt1)
dirTree = rfCreateTreeNode(dirTree,dirName,iStatus);
while((d=readdir(dp))!= NULL)
{
if(!(strcmp(d->d_name,".")) || !(strcmp(d->d_name,"..")))
continue;
sprintf(filename,"%s/%s",dirName,d->d_name);
if(lstat(filename,&st) < 0)
return NULL;
if((st.st_mode & S_IFMT) == S_IFDIR)
{
fiCnt1++;
if(fiCnt2)
{
if(dirTree->son != NULL)
dirTree = rfCreateDirTree(dirTree->son,filename,0);
}//end of if(fiCnt2)
else
dirTree = rfCreateDirTree(dirTree,filename,1);
fiCnt2++;
}//end of if((st.st_mode & S_IFMT) == S_IFDIR)
}//end of while((d=readdir(dp))!= NULL)
if(dirTree->father != NULL)
dirTree = dirTree->father;
return dirTree;
}//end of rfCreateDirTree()
void rfPrintDirTree(NODEPTR tree)
{
if(tree != NULL)
{
rfPrintDirTree(tree->son);
printf("%s\n",tree->data);
rfPrintDirTree(tree->next);
}//end of if(tree != NULL)
}//end of fPrintTree()
int rfDeleteDirTree(NODEPTR tree)
{
DIR *dp;
struct dirent *d;
struct stat st;
char filename[257];
char cmd[263];
if(tree != NULL)
{
rfDeleteDirTree(tree->son);
if((dp=opendir(tree->data)) == NULL)
{
printf ("ERROR unable to open %s directory, at line %d of file %s.\n",
tree->data,__LINE__, __FILE__);
return -1;
}//end of if((dp=opendir(tree->data)) == NULL)
while((d=readdir(dp))!= NULL)
{
if(!(strcmp(d->d_name,".")) || !(strcmp(d->d_name,"..")))
continue;
sprintf(filename,"%s/%s",tree->data,d->d_name);
if(lstat(filename,&st) < 0)
return -1;
if((st.st_mode & S_IFMT) == S_IFDIR)
sprintf(cmd,"rmdir %s",filename);
else
sprintf(cmd,"rm %s",filename);
printf("Deleting %s...\n",filename);
if(system(cmd) != 0)
{
printf ("ERROR when called system, at line %d of file %s.\n",
__LINE__, __FILE__);
return -1;
}//end of if(system(cmd) != 0)
}//end of while((d=readdir(dp))!= NULL)
rfDeleteDirTree(tree->next);
}//end of if(tree != NULL)
return 0;
}//end of fPrintTree()*/