www.pudn.com > eval-1.2.zip > dir.c
#ifdef _WIN32 #include#endif #ifdef LINUX #include #include #include #include int match(char *s, char *pattern) { char a[1024], *p; int r = 1; strcpy(a, pattern); p = strtok(a, "*"); while (p && r) { if (!strstr(s, p)) r = 0; p = strtok(0, "*"); } return r; } #endif #include "dir.h" char **FreeStrAr(char **s, int *l) { int i = 0; for (; i<*l; i++) { free(s[i]); s[i] = 0; } free(s); s = 0; *l = 0; return s; } char **GetSubDirs(int *nD) { char **D = 0, **tmp; #ifdef _WIN32 HANDLE h = 0; WIN32_FIND_DATA d = {0}; if (INVALID_HANDLE_VALUE != (h = FindFirstFile("*", &d))) while (FindNextFile(h, &d)) if (d.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY) if (d.cFileName[0] != '.' && d.cFileName[1] != '.') { if ((tmp = realloc(D, ++*nD * sizeof *D)) == 0) break; D = tmp; if ((D[*nD-1] = malloc(strlen(d.cFileName) + 1)) == 0) break; strcpy(D[*nD-1], d.cFileName); } FindClose(h); #endif return D; } char **GetFiles(int *nF, char *s) { char **F = 0, **tmp; #ifdef _WIN32 HANDLE h = 0; WIN32_FIND_DATA d = {0}; if (INVALID_HANDLE_VALUE != (h = FindFirstFile(s, &d))) { if (d.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY) { F = realloc(F, ++*nF * sizeof *F); F[*nF-1] = malloc((strlen(d.cFileName) + 1)); strcpy(F[*nF-1], d.cFileName); } while (FindNextFile(h, &d)) if (d.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY) { if ((tmp = realloc(F, ++*nF * sizeof *F)) == 0) break; F = tmp; if ((F[*nF-1] = malloc(strlen(d.cFileName) + 1)) == 0) break; strcpy(F[*nF-1], d.cFileName); } } FindClose(h); #endif #ifdef LINUX struct dirent *de; DIR *dir = opendir("."); if (dir) { while (de = readdir(dir)) { if (match(de->d_name, s)) { if (!(tmp = realloc(F, ++*nF * sizeof *F))) { fprintf(stderr, "realloc failed."); return 0; } F = tmp; F[*nF-1] = malloc((strlen(de->d_name) + 1)); strcpy(F[*nF-1], de->d_name); } } } closedir(dir); #endif return F; }