MCPcopy Create free account
hub / github.com/buke/quickjs-go / js_os_readdir

Function js_os_readdir

deps/quickjs/quickjs-libc.c:3064–3142  ·  view source on GitHub ↗

return [array, errorcode] */

Source from the content-addressed store, hash-verified

3062
3063/* return [array, errorcode] */
3064static JSValue js_os_readdir(JSContext *ctx, JSValueConst this_val,
3065 int argc, JSValueConst *argv)
3066{
3067#ifdef _WIN32
3068 const char *path;
3069 JSValue obj;
3070 int err;
3071 uint32_t len;
3072 HANDLE h;
3073 WIN32_FIND_DATAA d;
3074 char s[1024];
3075
3076 path = JS_ToCString(ctx, argv[0]);
3077 if (!path)
3078 return JS_EXCEPTION;
3079 obj = JS_NewArray(ctx);
3080 if (JS_IsException(obj)) {
3081 JS_FreeCString(ctx, path);
3082 return JS_EXCEPTION;
3083 }
3084 snprintf(s, sizeof(s), "%s/*", path);
3085 JS_FreeCString(ctx, path);
3086 err = 0;
3087 h = FindFirstFileA(s, &d);
3088 if (h == INVALID_HANDLE_VALUE)
3089 err = GetLastError();
3090 if (err)
3091 goto done;
3092 JS_DefinePropertyValueUint32(ctx, obj, 0, JS_NewString(ctx, "."),
3093 JS_PROP_C_W_E);
3094 for (len = 1; FindNextFileA(h, &d); len++) {
3095 JS_DefinePropertyValueUint32(ctx, obj, len,
3096 JS_NewString(ctx, d.cFileName),
3097 JS_PROP_C_W_E);
3098 }
3099 FindClose(h);
3100done:
3101 return make_obj_error(ctx, obj, err);
3102#else
3103 const char *path;
3104 DIR *f;
3105 struct dirent *d;
3106 JSValue obj;
3107 int err;
3108 uint32_t len;
3109
3110 path = JS_ToCString(ctx, argv[0]);
3111 if (!path)
3112 return JS_EXCEPTION;
3113 obj = JS_NewArray(ctx);
3114 if (JS_IsException(obj)) {
3115 JS_FreeCString(ctx, path);
3116 return JS_EXCEPTION;
3117 }
3118 f = opendir(path);
3119 if (!f)
3120 err = errno;
3121 else

Callers

nothing calls this directly

Calls 4

JS_ToCStringFunction · 0.85
JS_IsExceptionFunction · 0.85
JS_NewStringFunction · 0.85
make_obj_errorFunction · 0.85

Tested by

no test coverage detected