Issue information

Issue ID
#4739
Status
Fixed
Severity
None
Started
Hercules Elf Bot
Feb 2, 2011 18:13
Last Post
Hercules Elf Bot
Feb 2, 2011 18:13
Confirmation
N/A

Hercules Elf Bot - Feb 2, 2011 18:13

Originally posted by [b]xazax[/b]
http://www.eathena.ws/board/index.php?autocom=bugtracker&showbug=4739

CODE
void findfile(const char *p, const char *pat, void (func)(const char*))
{    
    DIR* dir;                    // pointer to the scanned directory.
    struct dirent* entry;        // pointer to one directory entry.
    struct stat dir_stat;       // used by stat().
    char tmppath[MAX_DIR_PATH+1];
    char path[MAX_DIR_PATH+1]= ".";
    const char *pattern = (pat==NULL)? "" : pat;
    if(p!=NULL) strcpy(path,p);

    // open the directory for reading
    dir = opendir( checkpath(path, path) );
    if (!dir) {
        ShowError("Cannot read directory '%s'\n", path);
        return;
    }

    // scan the directory, traversing each sub-directory
    // matching the pattern for each file name.
    while ((entry = readdir(dir))) {
        // skip the "." and ".." entries.
        if (strcmp(entry->d_name, ".") == 0)
            continue;
        if (strcmp(entry->d_name, "..") == 0)
            continue;

        sprintf(tmppath,"%s%c%s",path, PATHSEP, entry->d_name);

        // check if the pattern matchs.
        if (entry->d_name && strstr(entry->d_name, pattern)) {
            func( tmppath );
        }
        // check if it is a directory.
        if (stat(tmppath, &dir_stat) == -1) {
            ShowError("stat error %s\n': ", tmppath);
            continue;
        }
        // is this a directory?
        if (S_ISDIR(dir_stat.st_mode)) {
            // decent recursivly
            findfile(tmppath, pat, func);
        }
    }//end while
}


missing: closedir(dir);