Find the answer to your Linux question:
Results 1 to 3 of 3
Hi, I am trying to implement rm - r functionality in C without using "System" method. Below is Code i have written. The program goes into infinite loop and I ...
  1. #1
    Just Joined!
    Join Date
    Sep 2007
    Posts
    2

    Recursive rm implementation in C - Need help

    Hi,

    I am trying to implement rm - r functionality in C without using "System" method.

    Below is Code i have written. The program goes into infinite loop and I am not able to trace the bug.

    Please help.

    #include<stdio.h>
    #include <dirent.h>
    #include <sys/stat.h>

    #include<malloc.h>
    #include<string.h>
    #include <sys/types.h>



    int removedir(char *path)

    {


    int counter =1;


    DIR *pdir = NULL;
    struct dirent *pent = NULL;
    struct stat eStat;
    pdir = opendir(path);


    if ( pdir == NULL)
    {
    return -1;
    }


    while ( (pent=readdir(pdir)) != NULL )
    {

    // printf("\n %s\n" ,pent->d_name);
    if(((pent->d_name)==".")||((pent->d_name)==".."))
    {
    continue;
    }
    else
    {

    stat(pent->d_name, &eStat);
    if(S_ISDIR(eStat.st_mode))
    {
    path=strcat(path,"/");
    path=strcat(path,pent->d_name);
    printf("\n go to directory and remove %s\n",path);
    removedir(path);


    }

    else
    {

    unlink(pent->d_name);
    //printf("Success\n %s", pent->d_name);



    }

    }




    }


    //closedir (pdir);



    if (!rmdir(pent->d_name)) return -1; // delete the directory */



    }






    int main()

    {

    int ret;

    char *path_dir=malloc(256);
    char path[50];
    printf("enter Directory name\n");

    scanf("%s",path);

    strcpy(path_dir,path);

    ret=removedir(path_dir);

    printf("\n %d", ret);

    return 111;

    //else return -1;

    }



    Thanks,
    Ajith

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    In this section of code:
    Code:
    / printf("\n %s\n" ,pent->d_name);
    if(((pent->d_name)==".")||((pent->d_name)==".."))
    {
        continue;
    }
    You continue the loop if the directory is either '.' or '..'; however, the '.' directory is the current directory, so it will continuously loop at that point. I didn't look further, so there may be other problems.

    FWIW, if this is a school/class exercise, we are not supposed to help you, other than in the most general terms to point you in a reasonable direction. Please review and abide by the terms of service for the forums.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  3. #3
    Just Joined!
    Join Date
    Sep 2007
    Posts
    2

    Cool Thanks but I resolved the error

    I just resolved the error .. had done stack trace using gdb..
    Problem was not in "." or ".."
    I was path which was not getting reset if the file is deleted or if trace is coming out of some folder

    Its not school thingy..

    I used to code long time back.. now i m just practising...

    Btw here is revised code if anyone wants it :

    #include<stdio.h>
    #include <dirent.h>
    #include <sys/stat.h>

    #include<malloc.h>
    #include<string.h>
    #include <sys/types.h>
    #include<errno.h>



    int removedir(char path[500])

    {


    int counter =1;


    DIR *pdir = NULL;
    struct dirent *pent = NULL;
    struct stat eStat;
    pdir = opendir(path);
    char x[500];


    if ( pdir == NULL)
    {
    return -1;
    }

    //strcpy(x,path);
    while ( (pent=readdir(pdir)) != NULL )
    {

    // printf("\n %s\n" ,pent->d_name);
    if((strcmp((pent->d_name),".")==0)||(strcmp((pent->d_name),"..")==0))
    {
    printf("Continuing from %s, dname: %s\n",path,pent->d_name);
    continue;

    }
    else
    {
    printf("Entering first non . or .. file%s , dname: %s\n",path,pent->d_name);
    strcpy(x,path);
    printf("Current X :%s\n",x);
    path=strcat(path,"/");
    path=strcat(path,pent->d_name);

    if(stat(path, &eStat))
    {

    printf("ERROR: %s... Meaning it can be a file(Most certainly)\n", strerror(errno));
    //unlink(path);
    //path=x;

    }
    else{


    if(S_ISDIR(eStat.st_mode))
    {

    printf("\n go to directory and remove %s\n",path);
    removedir(path);
    strcpy(path,x);
    //rmdir(path);

    }
    else {

    unlink(path);
    printf("Unlinked %s\n",path);
    strcpy(path,x);
    printf("After Unlinking now path = : %s\n",path);
    //strcpy(path,x);
    }


    }


    }




    }


    //closedir (pdir);



    if (!rmdir(path)) return -1; // delete the directory */



    }






    int main()

    {

    int ret;

    char *path_dir=malloc(500);
    char path[500];
    printf("enter Directory name\n");

    scanf("%s",path);

    strcpy(path_dir,path);

    ret=removedir(path_dir);

    printf("\n %d", ret);

    return 111;

    //else return -1;

    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...