char **filelist = NULL;  // 파일 목록
int filecount = 0;  // 목록의 파일 수

void get_filelist(const char *findspec);  // 해당 파일을 찾는 함수
void free_filelist();  // 목록을 해제
int __cdecl compare(const void *elem1, const void *elem2); // 목록 정렬에 필요한 함수


void get_filelist(const char *findspec)
{
    char drv[_MAX_DRIVE], dir[_MAX_DIR], path[_MAX_PATH];
    _splitpath(findspec, drv, dir, NULL, NULL); // 파일스펙 중의 패스 정보를 빼낸다
    _makepath(path, drv, dir, NULL, NULL);  // 패스를 만들어 둠

    struct _finddata_t fd;
    long handle = _findfirst(findspec, &fd);
    if(handle >= 0) {
        do {
            filelist = (char **)realloc(filelist, sizeof(char *) * (filecount + 1));
            filelist[filecount] = (char *)malloc(strlen(fd.name) + strlen(path) + 1);
            strcat(strcpy(filelist[filecount], path), fd.name);
            filecount++;
            } while(_findnext(handle, &fd) == 0);
        _findclose(handle);
        }
    qsort(filelist, filecount, sizeof(char *), compare);
}


void free_filelist()
{
    if(filelist) {
        for(int i = 0; i < filecount; i++)
            free(filelist[i]);
        free(filelist);
        }
    filelist = NULL;
    filecount = 0;
}

int __cdecl compare(const void *elem1, const void *elem2)
{
    return stricmp(*((char **)elem1), *((char **)elem2));
}