Find the answer to your Linux question:
Results 1 to 7 of 7
hey guys... while(dp=readdir(dirp)!=NULL) not working in my code can you help me out...
  1. #1
    Just Joined!
    Join Date
    Jun 2009
    Posts
    20

    while(dp=readdir(dirp)!=NULL) not working

    hey guys...
    while(dp=readdir(dirp)!=NULL) not working in my code
    can you help me out

  2. #2
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,568
    Try this
    while((dp=readdir(dirp))!=NULL)
    If above didn't work,then provide the entire code snippet not a single line,so that people can help you.
    - Lakshmipathi.G
    -------------------
    FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
    First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -------------------

  3. #3
    Just Joined!
    Join Date
    Jun 2009
    Posts
    20

    how to implement recursion in this program

    Hey guys..My task is to scan a directory,its subdirectories if any and so on for .exe files.

    this is my code

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

    static int one (struct dirent *unused)
    {
    return 1;
    }

    int main()
    {
    char y;
    int i=1;
    char *p;
    char* Str=" .exe";
    FILE *fp;
    DIR *dirp;
    struct dirent *dp;
    struct dirent **files;
    int n,cnt;

    /* SCANNING CURRENT DIRECTORY */
    dirp=opendir(".");
    n=scandir(".",&files,one,alphasort);
    printf("%d",n);

    if (n>=0)
    {
    for(cnt=0;cnt<n;++cnt)
    {
    /* SCAN FOR A MALICIOUS FILE */
    p=files[cnt]->d_name;
    if(strstr(p,Str)!=NULL)
    {
    i=0;
    printf("%s is an executable file \n",p);
    }
    }
    }
    else
    {
    perror("Couldn't open the directory");
    return 0;
    }
    if(i==1)
    {
    printf("NO EXECUTABLE FILES FOUND");
    }
    /* scanning subfolders for .exe files */
    }

    I am able to list the exe files within this dorectory.......How do i implement recursive function to traverse through all the subdirectories and so on...

    Please help me guys...

  4. #4
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,568

    Exclamation

    following code snippet taken from "Beginning Linux Programming, 4th Edition
    by Neil Matthew and Richard Stones " iterates though all sub-directories.

    I guess you can modify (add if condition for an exe file)

    #include <unistd.h>
    #include <stdio.h>
    #include <dirent.h>
    #include <string.h>
    #include <sys/stat.h>
    #include <stdlib.h>

    void printdir(char *dir, int depth)
    {
    DIR *dp;
    struct dirent *entry;
    struct stat statbuf;

    if((dp = opendir(dir)) == NULL) {
    fprintf(stderr,"cannot open directory: %s\n", dir);
    return;
    }
    chdir(dir);
    while((entry = readdir(dp)) != NULL) {
    lstat(entry->d_name,&statbuf);
    if(S_ISDIR(statbuf.st_mode)) {
    /* Found a directory, but ignore . and .. */
    if(strcmp(".",entry->d_name) == 0 ||
    strcmp("..",entry->d_name) == 0)
    continue;
    printf("%*s%s/\n",depth,"",entry->d_name);
    /* Recurse at a new indent level */
    printdir(entry->d_name,depth+4);
    }
    else printf("%*s%s\n",depth,"",entry->d_name);
    }
    chdir("..");
    closedir(dp);
    }

    int main()
    {
    printf("Directory scan of /home:\n");
    printdir("/home",0);
    printf("done.\n");

    exit(0);
    }

    - Lakshmipathi.G
    -------------------
    FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
    First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -------------------

  5. #5
    Just Joined!
    Join Date
    Jun 2009
    Posts
    20
    Hey thanks for the post but i'm quite a newbie to C.....so can you please tel me where to add the if (condition),
    in the main function or in the printdir function??

  6. #6
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,568
    in printdir() function

    printdir(entry->d_name,depth+4);
    }
    else printf("%*s%s\n",depth,"",entry->d_name); //Here comes the file name,you can
    //add if condition and check whether file contains .exe part. Hint: check strstr //function with .exe
    }
    chdir("..");
    closedir(dp);
    }
    - Lakshmipathi.G
    -------------------
    FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
    First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -------------------

  7. #7
    Just Joined!
    Join Date
    Jun 2009
    Posts
    20
    Thanks SIR for the help!!!

Posting Permissions

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