00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include <stdio.h>
00019 #include <stdlib.h>
00020
00021 #if defined(_MSC_VER) || defined(__MINGW32__)
00022 #include <windows.h>
00023
00024 typedef struct {
00025 HANDLE h;
00026 WIN32_FIND_DATA fd;
00027 } VMDDIR;
00028
00029 #else
00030 #include <dirent.h>
00031
00032 typedef struct {
00033 DIR * d;
00034 } VMDDIR;
00035 #endif
00036
00037
00038
00039 static VMDDIR * vmd_opendir(const char *);
00040 static char * vmd_readdir(VMDDIR *);
00041 static void vmd_closedir(VMDDIR *);
00042 static int vmd_file_is_executable(const char * filename);
00043
00044
00045 #define VMD_FILENAME_MAX 1024
00046
00047 #if defined(_MSC_VER) || defined(__MINGW32__)
00048
00049
00050
00051 static VMDDIR * vmd_opendir(const char * filename) {
00052 VMDDIR * d;
00053 char dirname[VMD_FILENAME_MAX];
00054
00055 strcpy(dirname, filename);
00056 strcat(dirname, "\\*");
00057 d = (VMDDIR *) malloc(sizeof(VMDDIR));
00058 if (d != NULL) {
00059 #if _MSC_VER < 1400 || !(defined(UNICODE) || defined(_UNICODE))
00060 d->h = FindFirstFile(dirname, &(d->fd));
00061 #else
00062 wchar_t szBuff[VMD_FILENAME_MAX];
00063 swprintf(szBuff, L"%p", dirname);
00064 d->h = FindFirstFile(szBuff, &(d->fd));
00065 #endif
00066 if (d->h == ((HANDLE)(-1))) {
00067 free(d);
00068 return NULL;
00069 }
00070 }
00071 return d;
00072 }
00073
00074 static char * vmd_readdir(VMDDIR * d) {
00075 if (FindNextFile(d->h, &(d->fd))) {
00076 #if _MSC_VER < 1400 || !(defined(UNICODE) || defined(_UNICODE))
00077 return d->fd.cFileName;
00078 #else
00079 int len = wcslen(d->fd.cFileName);
00080 char* ascii = new char[len + 1];
00081 wcstombs(ascii, d->fd.cFileName, len);
00082 return ascii;
00083 #endif
00084 }
00085 return NULL;
00086 }
00087
00088 static void vmd_closedir(VMDDIR * d) {
00089 if (d->h != NULL) {
00090 FindClose(d->h);
00091 }
00092 free(d);
00093 }
00094
00095
00096 static int vmd_file_is_executable(const char * filename) {
00097 FILE * fp;
00098 if ((fp=fopen(filename, "rb")) != NULL) {
00099 fclose(fp);
00100 return 1;
00101 }
00102
00103 return 0;
00104 }
00105
00106 #else
00107
00108
00109
00110 #include <sys/types.h>
00111 #include <sys/stat.h>
00112
00113 static VMDDIR * vmd_opendir(const char * filename) {
00114 VMDDIR * d;
00115
00116 d = (VMDDIR *) malloc(sizeof(VMDDIR));
00117 if (d != NULL) {
00118 d->d = opendir(filename);
00119 if (d->d == NULL) {
00120 free(d);
00121 return NULL;
00122 }
00123 }
00124
00125 return d;
00126 }
00127
00128 static char * vmd_readdir(VMDDIR * d) {
00129 struct dirent * p;
00130 if ((p = readdir(d->d)) != NULL) {
00131 return p->d_name;
00132 }
00133
00134 return NULL;
00135 }
00136
00137 static void vmd_closedir(VMDDIR * d) {
00138 if (d->d != NULL) {
00139 closedir(d->d);
00140 }
00141 free(d);
00142 }
00143
00144
00145 static int vmd_file_is_executable(const char * filename) {
00146 struct stat buf;
00147 if (!stat(filename, &buf)) {
00148 if (buf.st_mode & S_IXUSR ||
00149 buf.st_mode & S_IXGRP ||
00150 buf.st_mode & S_IXOTH) {
00151 return 1;
00152 }
00153 }
00154 return 0;
00155 }
00156
00157 #endif
00158
00159
00160
00161