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)
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)
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 d->h = FindFirstFile(dirname, &(d->fd));
00060 if (d->h == ((HANDLE)(-1))) {
00061 free(d);
00062 return NULL;
00063 }
00064 }
00065 return d;
00066 }
00067
00068 static char * vmd_readdir(VMDDIR * d) {
00069 if (FindNextFile(d->h, &(d->fd))) {
00070 return d->fd.cFileName;
00071 }
00072 return NULL;
00073 }
00074
00075 static void vmd_closedir(VMDDIR * d) {
00076 if (d->h != NULL) {
00077 FindClose(d->h);
00078 }
00079 free(d);
00080 }
00081
00082
00083 static int vmd_file_is_executable(const char * filename) {
00084 FILE * fp;
00085 if ((fp=fopen(filename, "rb")) != NULL) {
00086 fclose(fp);
00087 return 1;
00088 }
00089
00090 return 0;
00091 }
00092
00093 #else
00094
00095
00096
00097 #include <sys/types.h>
00098 #include <sys/stat.h>
00099
00100 static VMDDIR * vmd_opendir(const char * filename) {
00101 VMDDIR * d;
00102
00103 d = (VMDDIR *) malloc(sizeof(VMDDIR));
00104 if (d != NULL) {
00105 d->d = opendir(filename);
00106 if (d->d == NULL) {
00107 free(d);
00108 return NULL;
00109 }
00110 }
00111
00112 return d;
00113 }
00114
00115 static char * vmd_readdir(VMDDIR * d) {
00116 struct dirent * p;
00117 if ((p = readdir(d->d)) != NULL) {
00118 return p->d_name;
00119 }
00120
00121 return NULL;
00122 }
00123
00124 static void vmd_closedir(VMDDIR * d) {
00125 if (d->d != NULL) {
00126 closedir(d->d);
00127 }
00128 free(d);
00129 }
00130
00131
00132 static int vmd_file_is_executable(const char * filename) {
00133 struct stat buf;
00134 if (!stat(filename, &buf)) {
00135 if (buf.st_mode & S_IXUSR ||
00136 buf.st_mode & S_IXGRP ||
00137 buf.st_mode & S_IXOTH) {
00138 return 1;
00139 }
00140 }
00141 return 0;
00142 }
00143
00144 #endif
00145
00146
00147
00148