txlyre 21 uur geleden
bovenliggende
commit
ff12f6aee6
1 gewijzigde bestanden met toevoegingen van 52 en 4 verwijderingen
  1. 52 4
      qic.c

+ 52 - 4
qic.c

@@ -5,6 +5,31 @@
 #include <string.h>
 #include <time.h>
 
+#if defined(_WIN32) || defined(_WIN64)
+#  define ON_WINDOWS
+#  include <windows.h>
+#else
+#  define ON_POSIX
+#  include <sys/stat.h>
+#endif
+
+int is_directory(char *path)
+{
+#ifdef ON_WINDOWS
+  DWORD attributes = GetFileAttributesA(path);
+  if (attributes == INVALID_FILE_ATTRIBUTES)
+    return 0;
+
+  return (attributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
+#else
+  struct stat statbuf;
+  if (stat(path, &statbuf) != 0)
+    return 0;
+
+  return S_ISDIR(statbuf.st_mode);
+#endif
+}
+
 #define QIC_VERSION "qic v01 " __DATE__
 
 size_t GID = 0;
@@ -4958,6 +4983,8 @@ void compile_into(char *source, buffer_t *gbuf, buffer_t *buf, list_t *ctx,
 int require_once(buffer_t *gbuf, buffer_t *buf, list_t *ctx, table_t *ltab,
                  list_t *lstk, list_t *sstk, list_t *lbl, char *path)
 {
+  char tmp[512];
+          
   char *source = NULL;
 
   if (is_required(path))
@@ -4972,10 +4999,23 @@ int require_once(buffer_t *gbuf, buffer_t *buf, list_t *ctx, table_t *ltab,
       break;
     }
   }
-
+  
   if (!source)
   {
-    FILE *fd = fopen(path, "rb");
+    FILE *fd;
+    
+    if (is_directory(path)) {
+      snprintf(tmp, sizeof(tmp), "%s/main.qi", path);
+
+      fd = fopen(tmp, "rb");
+      if (fd) {
+        path = strdup(tmp);
+        
+        goto pass;
+      }
+    }
+
+    fd = fopen(path, "rb");
     if (!fd)
     {
       char *qipath = getenv("QIPATH");
@@ -4986,7 +5026,6 @@ int require_once(buffer_t *gbuf, buffer_t *buf, list_t *ctx, table_t *ltab,
 
       while (n)
       {
-        char tmp[512];
         snprintf(tmp, sizeof(tmp), "%s/%s", n, path);
 
         fd = fopen(tmp, "rb");
@@ -5001,6 +5040,7 @@ int require_once(buffer_t *gbuf, buffer_t *buf, list_t *ctx, table_t *ltab,
         return -1;
     }
 
+pass:
     path = realpath(path, NULL);
 
     if (is_required(path)) {
@@ -7793,6 +7833,8 @@ int main(int argc, char **argv)
 
   genmathlib();
 
+  char tmp[512];
+  
   char *out;
   char *main = NULL;
   char *outf = NULL;
@@ -7889,7 +7931,13 @@ int main(int argc, char **argv)
     out = compile_file("<stdin>", stdin, required);
   else
   {
-    FILE *fd = fopen(main, "rb");
+    FILE *fd;
+    if (is_directory(main)) {
+      snprintf(tmp, sizeof(tmp), "%s/main.qi", main);
+
+      fd = fopen(tmp, "rb");
+    } else fd = fopen(main, "rb");
+    
     if (!fd)
     {
       fprintf(stderr, "fatal: unable to open file: '%s'\n", main);