00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "VMDDir.h"
00022 #include <string.h>
00023 #include <stdlib.h>
00024
00025 #define VMD_FILENAME_MAX 1024
00026
00027 #if defined(_MSC_VER)
00028
00029
00030
00031 VMDDIR * vmd_opendir(const char * filename) {
00032 VMDDIR * d;
00033 char dirname[VMD_FILENAME_MAX];
00034
00035 strcpy(dirname, filename);
00036 strcat(dirname, "\\*");
00037 d = (VMDDIR *) malloc(sizeof(VMDDIR));
00038 if (d != NULL) {
00039 d->h = FindFirstFile(dirname, &(d->fd));
00040 if (d->h == ((HANDLE)(-1))) {
00041 free(d);
00042 return NULL;
00043 }
00044 }
00045 return d;
00046 }
00047
00048 char * vmd_readdir(VMDDIR * d) {
00049 if (FindNextFile(d->h, &(d->fd))) {
00050 return d->fd.cFileName;
00051 }
00052 return NULL;
00053 }
00054
00055 void vmd_closedir(VMDDIR * d) {
00056 if (d->h != NULL) {
00057 FindClose(d->h);
00058 }
00059 free(d);
00060 }
00061
00062
00063 int vmd_file_is_executable(const char * filename) {
00064 FILE * fp = NULL;
00065 char *newfilename = (char *) malloc(strlen(filename)+1);
00066
00067 if (newfilename != NULL) {
00068 char *ns = newfilename;
00069 const char *s = filename;
00070
00071
00072 while ((*s) != '\0') {
00073 if ((*s) != '\"') {
00074 *ns = *s;
00075 ns++;
00076 }
00077 s++;
00078 }
00079 *ns = '\0';
00080
00081 fp=fopen(newfilename, "rb");
00082 free(newfilename);
00083
00084 if (fp != NULL) {
00085 fclose(fp);
00086 return 1;
00087 }
00088 }
00089
00090 return 0;
00091 }
00092
00093 #else
00094
00095
00096
00097 #include <sys/types.h>
00098 #include <sys/stat.h>
00099
00100 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 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 void vmd_closedir(VMDDIR * d) {
00125 if (d->d != NULL) {
00126 closedir(d->d);
00127 }
00128 free(d);
00129 }
00130
00131 int vmd_file_is_executable(const char * filename) {
00132 struct stat buf;
00133 memset(&buf, 0, sizeof(buf));
00134
00135 if (!stat(filename, &buf) &&
00136 ((buf.st_mode & S_IXUSR) ||
00137 (buf.st_mode & S_IXGRP) ||
00138 (buf.st_mode & S_IXOTH)) &&
00139 (buf.st_mode & S_IFREG)) {
00140 return 1;
00141 }
00142
00143 return 0;
00144 }
00145
00146 #endif
00147
00148
00149
00150