Просмотр исходного кода

【修复】在mdk环境下编译错误和文件读取错误

zyh 7 лет назад
Родитель
Сommit
427a320b03
2 измененных файлов с 25 добавлено и 30 удалено
  1. 2 3
      rtthread-port/jerry_module.c
  2. 23 27
      rtthread-port/jerry_util.c

+ 2 - 3
rtthread-port/jerry_module.c

@@ -15,7 +15,6 @@
 #define PATH_MAX    256
 #endif
 
-char *strdup(const char *);
 char *getcwd(char *buf, size_t size);
 
 typedef jerry_value_t (*module_init_func_t)(void);
@@ -28,7 +27,7 @@ char *js_module_dirname(char *path)
 
     if (!path || !*path) return NULL;
 
-    s = strdup(path);
+    s = rt_strdup(path);
     if (!s) return NULL;
 
     i = strlen(s)-1;
@@ -88,7 +87,7 @@ char *js_module_normalize_path(const char *directory, const char *filename)
     }
     else
     {
-        fullpath = strdup(filename); /* copy string */
+        fullpath = rt_strdup(filename); /* copy string */
 
         if (fullpath == NULL)
             return NULL;

+ 23 - 27
rtthread-port/jerry_util.c

@@ -182,41 +182,37 @@ void js_value_dump(jerry_value_t value)
         printf("what?");
     }
 }
-
+#include <dfs_posix.h>
 int js_read_file(const char* filename, char **script)
 {
     FILE *fp;
     int length = 0;
-
+    struct stat statbuf;
     if (!filename || !script) return 0;
 
-    fp = fopen(filename, "rb");
-    if (fp)
-    {
-        fseek(fp, 0, SEEK_END);
-        length = ftell(fp);
-        fseek(fp, 0, SEEK_SET);
+    stat(filename, &statbuf);
+    length = statbuf.st_size;
 
-        if (length)
-        {
-            char *script_str = (char*) malloc (length + 1);
-            if (script_str)
-            {
-                script_str[length] = '\0';
-                if (fread(script_str, length, 1, fp) == 1)
-                {
-                    *script = script_str;
-                }
-                else
-                {
-                    printf("read failed!\n");
-                }
-            }
-            else length = 0;
-        }
-        fclose(fp);
-    }
+    if(!length) return 0;
 
+    *script = (char *)rt_malloc(length + 1);
+    if(!(*script)) retrun 0;
+    (*script)[length] = '\0';
+    fp = fopen(filename,"rb");
+    if(!fp) 
+    {
+        rt_free(*script);
+        *script = RT_NULL;
+        return 0;
+    }
+    if(fread(*script,length,1,fp)) != 1)
+    {
+        length = 0;
+        rt_free(*script);
+        *script = RT_NULL;
+        printf("read failed!\n");
+    }
+    fclose(fp);
     return length;
 }