#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2002
    Location
    Istanbul / Turkey
    Posts
    2
    Rep Power
    0

    finding a file in disk with C/C++


    i'm trying to code a function which will be responsible for doing the following thing as fast as possible and with minimum resources;

    i'll send it a file name like "foo.bar" and a directory like "/home/dummy " (to let recursion if needed) and it'll search the directory and all subdirectories and return a char ** as a result which will contain the file locations, such as "/home/dummy/bin/foo.bar" etc..

    To do this i have some thing in my mind but i am scared to use it because of my past experience with protected mode and assembly.. i'm decideing to use int 21h with function 4Eh to check if file exists or not..

    In oder hand i know that i may use fopen to text if file exists but it'll be quite slow for my purpose with that function.

    And finding subdirectories still remains obscure.. the code have to be portable and this closes all of my narrow escapes..

    I need something similar to Midnight Commander's "search for files" feature.. But i dont know how it'd be as fast as MC is, or how ro code such a thing in a limited time.

    What i need is documentation or your ideas about how do it efficient.. i'd prefer C but C++ is wellcome.

    Can anyone lead me to find out how i can code that function?
  2. #2
  3. /(bb|[^b]{2})/

    Join Date
    Nov 2001
    Location
    Somewhere in the great unknown
    Posts
    5,163
    Rep Power
    792
    Have you checked out the dirent.h file? It deals with the majority of director handling.
  4. #3
  5. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,625
    Rep Power
    4247
    Are you coding this for DOS or *NIX. The reason I'm asking is because your path names indicate an *NIX environment, but the INT 21h statement indicates a DOS environment. Also, I'm curious exactly how much programming you've done in assembly for DOS, because a moderately experienced programmer would know that INT21h is the system call mechanism for DOS alone and doesn't apply to other OSs.

    Anyways, assuming you have a *NIX environment, here's some C code that I wrote to go down thru a directory tree and do things to files. I've only snipped certain sections that you can easily figure out for yourself.
    Code:
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <dirent.h>
    #include <unistd.h>
    #include <string.h>
    
    void crawldir(char *currdir);
    void StripFile(char *filename, long filesize);
    char tempname[MAXNAMLEN];
    
    int main(int argc, char **argv) {
      if (argc < 2) {
        fprintf(stderr, "Usage: %s startdir\n", argv[0]);
        fprintf(stderr, "E.g. %s /var/www/foo\n", argv[0]);
        fprintf(stderr, "Description:\t This program ... <snip>.\n");
        fprintf(stderr, "\t\t It recursively crawls its way up startdir. \n");
        return 0;
      }
      crawldir(argv[1]);
    }
    
    void crawldir(char *currdir)
    {
      /* Open the directory */
      DIR *dir;
      struct dirent *de;
      struct stat st;
    
      chdir (currdir);
      printf("Current dir is %s\n", getwd(tempname));
      dir = opendir(".");
      while (de = readdir(dir)) {
        lstat(de->d_name, &st);
        if (S_ISDIR(st.st_mode)) {
          if ((strcmp(de->d_name, ".") != 0) && (strcmp(de->d_name, "..") != 0)) {
            crawldir(de->d_name); 
          }
        } 
        else {
          StripFile(de->d_name, st.st_size);
        }
      }
      closedir(dir);
      printf("Switching back one dir level\n");
      chdir("..");
    }
    
    void StripFile(char *filename, long filesize) {
     ...<snip>... your code comes here
    }
    Hope this helps!
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2002
    Location
    Istanbul / Turkey
    Posts
    2
    Rep Power
    0
    yeah, you're right.. havent done much assembly coding before.

    i think your example will do much of what i have been looking for.. thank you for your help. :)
  8. #5
  9. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,625
    Rep Power
    4247
    Edited the above code to remove hardcoded array sizes and use constants.

IMN logo majestic logo threadwatch logo seochat tools logo