1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <cerrno>
17 #include <dirent.h>
18 #include <fcntl.h>
19 #include <js_native_api.h>
20 #include <js_native_api_types.h>
21 #include <node_api.h>
22 #include <semaphore.h>
23 #include <stdio_ext.h>
24 #include <sys/eventfd.h>
25 #include <unistd.h>
26
27 #define ERRORNUMBER 0
28 #define SUCCESSRET 1
29 #define PARAM_0 0
30 #define PARAM_5 5
31 #define INIT (-1)
32 #define SUCCESS 0
33
Flushlbf(napi_env env,napi_callback_info info)34 static napi_value Flushlbf(napi_env env, napi_callback_info info)
35 {
36 napi_value result = nullptr;
37 errno = ERRORNUMBER;
38 _flushlbf();
39 int returnValue = ERRORNUMBER;
40 if (errno == ERRORNUMBER) {
41 returnValue = SUCCESSRET;
42 }
43 napi_create_int32(env, returnValue, &result);
44 return result;
45 }
46
Flbf(napi_env env,napi_callback_info info)47 static napi_value Flbf(napi_env env, napi_callback_info info)
48 {
49 napi_value result = nullptr;
50 errno = ERRORNUMBER;
51 int fileDescribe = open("/data/storage/el2/base/files/fzl.txt", O_CREAT);
52 FILE *file = fopen("/data/storage/el2/base/files/fzl.txt", "a+");
53 __flbf(file);
54 int returnValue = ERRORNUMBER;
55 if (errno == ERRORNUMBER) {
56 returnValue = SUCCESSRET;
57 }
58 close(fileDescribe);
59 napi_create_int32(env, returnValue, &result);
60 return result;
61 }
62
Fwriting(napi_env env,napi_callback_info info)63 static napi_value Fwriting(napi_env env, napi_callback_info info)
64 {
65 napi_value result = nullptr;
66 int fileDescribe = open("/data/storage/el2/base/files/fzl.txt", O_CREAT);
67 FILE *file = fopen("/data/storage/el2/base/files/fzl.txt", "a+");
68 int returnValue = __fwriting(file);
69 int backInfo = ERRORNUMBER;
70 if (returnValue == ERRORNUMBER) {
71 backInfo = SUCCESSRET;
72 }
73 close(fileDescribe);
74 napi_create_int32(env, backInfo, &result);
75 return result;
76 }
77
Fpending(napi_env env,napi_callback_info info)78 static napi_value Fpending(napi_env env, napi_callback_info info)
79 {
80 napi_value result = nullptr;
81 int fileDescribe = open("/data/storage/el2/base/files/fzl.txt", O_CREAT);
82 FILE *file = fopen("/data/storage/el2/base/files/fzl.txt", "a+");
83 int returnValue = __fpending(file);
84 int backInfo = ERRORNUMBER;
85 if (returnValue != PARAM_0) {
86 backInfo = SUCCESSRET;
87 }
88 close(fileDescribe);
89 napi_create_int32(env, backInfo, &result);
90 return result;
91 }
92
FBufSize(napi_env env,napi_callback_info info)93 static napi_value FBufSize(napi_env env, napi_callback_info info)
94 {
95 napi_value result = nullptr;
96 int fileDescribe = open("/data/storage/el2/base/files/fzl.txt", O_CREAT);
97 FILE *file = fopen("/data/storage/el2/base/files/fzl.txt", "a+");
98 int returnValue = __fbufsize(file);
99 int backInfo = ERRORNUMBER;
100 if (returnValue != PARAM_0) {
101 backInfo = SUCCESSRET;
102 }
103 close(fileDescribe);
104 napi_create_int32(env, backInfo, &result);
105 return result;
106 }
107
Fpurge(napi_env env,napi_callback_info info)108 static napi_value Fpurge(napi_env env, napi_callback_info info)
109 {
110 napi_value result = nullptr;
111 int fileDescribe = open("/data/storage/el2/base/files/fzl.txt", O_CREAT);
112 FILE *file = fopen("/data/storage/el2/base/files/fzl.txt", "a+");
113 __fpurge(file);
114 int backInfo = ERRORNUMBER;
115 if (errno == ERRORNUMBER) {
116 backInfo = SUCCESSRET;
117 }
118 close(fileDescribe);
119 napi_create_int32(env, backInfo, &result);
120 return result;
121 }
122
Freadable(napi_env env,napi_callback_info info)123 static napi_value Freadable(napi_env env, napi_callback_info info)
124 {
125 napi_value result = nullptr;
126 int fileDescribe = open("/data/storage/el2/base/files/fzl.txt", O_CREAT);
127 FILE *file = fopen("/data/storage/el2/base/files/fzl.txt", "a+");
128 int returnValue = __freadable(file);
129 int backInfo = ERRORNUMBER;
130 if (returnValue != ERRORNUMBER) {
131 backInfo = SUCCESSRET;
132 }
133 close(fileDescribe);
134 napi_create_int32(env, backInfo, &result);
135 return result;
136 }
137
Freading(napi_env env,napi_callback_info info)138 static napi_value Freading(napi_env env, napi_callback_info info)
139 {
140 napi_value result = nullptr;
141 int fileDescribe = open("/data/storage/el2/base/files/fzl.txt", O_CREAT);
142 FILE *file = fopen("/data/storage/el2/base/files/fzl.txt", "a+");
143 int returnValue = __freading(file);
144 int backInfo = ERRORNUMBER;
145 if (returnValue != ERRORNUMBER) {
146 backInfo = SUCCESSRET;
147 }
148 close(fileDescribe);
149 napi_create_int32(env, backInfo, &result);
150 return result;
151 }
152
Fseterr(napi_env env,napi_callback_info info)153 static napi_value Fseterr(napi_env env, napi_callback_info info)
154 {
155 napi_value result = nullptr;
156 napi_create_int32(env, SUCCESS, &result);
157 return result;
158 }
159
Fsetlocking(napi_env env,napi_callback_info info)160 static napi_value Fsetlocking(napi_env env, napi_callback_info info)
161 {
162 napi_value result = nullptr;
163 napi_create_int32(env, SUCCESS, &result);
164 return result;
165 }
166
Fwritable(napi_env env,napi_callback_info info)167 static napi_value Fwritable(napi_env env, napi_callback_info info)
168 {
169 napi_value result = nullptr;
170 int fileDescribe = open("/data/storage/el2/base/files/fzl.txt", O_CREAT);
171 FILE *file = fopen("/data/storage/el2/base/files/fzl.txt", "a+");
172 int returnValue = __fwritable(file);
173 int backInfo = ERRORNUMBER;
174 if (returnValue != ERRORNUMBER) {
175 backInfo = SUCCESSRET;
176 }
177 close(fileDescribe);
178 napi_create_int32(env, backInfo, &result);
179 return result;
180 }
181
DPrintf(napi_env env,napi_callback_info info)182 static napi_value DPrintf(napi_env env, napi_callback_info info)
183 {
184 const char *path = "/data/storage/el2/base/files/Fzl.txt";
185 const char *mode = "r";
186 int backParam = INIT;
187 fopen(path, mode);
188 napi_value result = nullptr;
189 backParam = dprintf(SUCCESS, path);
190 napi_create_int32(env, backParam, &result);
191 return result;
192 }
193
FEof_unlocked(napi_env env,napi_callback_info info)194 static napi_value FEof_unlocked(napi_env env, napi_callback_info info)
195 {
196 const char *path = "/data/storage/el2/base/files/Fzl.txt";
197 const char *mode = "r";
198 int backParam = INIT;
199 FILE *stream = fopen(path, mode);
200 backParam = feof_unlocked(stream);
201 napi_value result = nullptr;
202 napi_create_int32(env, backParam, &result);
203 return result;
204 }
205
FError_unlocked(napi_env env,napi_callback_info info)206 static napi_value FError_unlocked(napi_env env, napi_callback_info info)
207 {
208 const char *path = "/data/storage/el2/base/files/Fzl.txt";
209 const char *mode = "r";
210 int fileDescribe = SUCCESS;
211 FILE *stream = fopen(path, mode);
212 napi_value result = nullptr;
213 if (stream != nullptr) {
214 fileDescribe = ferror_unlocked(stream);
215 napi_create_int32(env, fileDescribe, &result);
216 }
217 napi_create_int32(env, fileDescribe, &result);
218 return result;
219 }
220
FFlush_unlocked(napi_env env,napi_callback_info info)221 static napi_value FFlush_unlocked(napi_env env, napi_callback_info info)
222 {
223 const char *path = "/data/storage/el2/base/files/Fzl.txt";
224 const char *mode = "r";
225 FILE *stream = fopen(path, mode);
226 napi_value result = nullptr;
227 int fileDescribe = SUCCESS;
228 fileDescribe = fflush_unlocked(stream);
229 napi_create_int32(env, fileDescribe, &result);
230 return result;
231 }
232
FGetC_unlocked(napi_env env,napi_callback_info info)233 static napi_value FGetC_unlocked(napi_env env, napi_callback_info info)
234 {
235 const char *path = "/data/storage/el2/base/files/Fzl.txt";
236 const char *mode = "r";
237 FILE *stream = fopen(path, mode);
238 napi_value result = nullptr;
239 int fileDescribe;
240 fileDescribe = fgetc_unlocked(stream);
241 napi_create_int32(env, fileDescribe, &result);
242 return result;
243 }
244
FGetLn(napi_env env,napi_callback_info info)245 static napi_value FGetLn(napi_env env, napi_callback_info info)
246 {
247 int backResult = INIT;
248 const char *path = "/data/storage/el2/base/files/Fzl.txt";
249 const char *mode = "r";
250 char *getResult = nullptr;
251 size_t size_t1 = PARAM_0;
252 FILE *stream = fopen(path, mode);
253 napi_value result = nullptr;
254 getResult = fgetln(stream, &size_t1);
255 if (getResult != nullptr) {
256 getResult = SUCCESS;
257 }
258 napi_create_int32(env, backResult, &result);
259 return result;
260 }
261
FGetS_unlocked(napi_env env,napi_callback_info info)262 static napi_value FGetS_unlocked(napi_env env, napi_callback_info info)
263 {
264 int backResult = INIT;
265 const char *path = "/data/storage/el2/base/files/Fzl.txt";
266 const char *mode = "r";
267 char *getResult = nullptr;
268 char *gets = nullptr;
269 FILE *stream = fopen(path, mode);
270 int fileDescribe = PARAM_0;
271 napi_value result = nullptr;
272 getResult = fgets_unlocked(gets, fileDescribe, stream);
273 if (getResult != nullptr) {
274 getResult = SUCCESS;
275 }
276 napi_create_int32(env, backResult, &result);
277 return result;
278 }
279
FileNo_unlocked(napi_env env,napi_callback_info info)280 static napi_value FileNo_unlocked(napi_env env, napi_callback_info info)
281 {
282 const char *path = "/data/storage/el2/base/files/Fzl.txt";
283 const char *mode = "r";
284 FILE *stream = fopen(path, mode);
285 int backResult;
286 napi_value result = nullptr;
287 backResult = fileno_unlocked(stream);
288 napi_create_int32(env, backResult, &result);
289 return result;
290 }
291
FClose(napi_env env,napi_callback_info info)292 static napi_value FClose(napi_env env, napi_callback_info info)
293 {
294 int backParam;
295 const char *path = "/data/storage/el2/base/files/Fzl.txt";
296 const char *mode = "r";
297 FILE *stream = fopen(path, mode);
298 backParam = fclose(stream);
299 napi_value result = nullptr;
300 napi_create_int32(env, backParam, &result);
301 return result;
302 }
303
FMemOpen(napi_env env,napi_callback_info info)304 static napi_value FMemOpen(napi_env env, napi_callback_info info)
305 {
306 int backParam = INIT;
307 void *buf = nullptr;
308 size_t size = PARAM_5;
309 const char *path = "/data/storage/el2/base/files/Fzl.txt";
310 const char *mode = "r";
311 fopen(path, mode);
312 FILE *newStreeam = nullptr;
313 newStreeam = fmemopen(buf, size, mode);
314 if (newStreeam != nullptr) {
315 backParam = SUCCESS;
316 }
317 napi_value result = nullptr;
318 napi_create_int32(env, backParam, &result);
319 return result;
320 }
321
FlockFile(napi_env env,napi_callback_info info)322 static napi_value FlockFile(napi_env env, napi_callback_info info)
323 {
324 int backParam = INIT;
325 fpos_t pos;
326 const char *path = "/data/storage/el2/base/files/Fzl.txt";
327 const char *mode = "r";
328 FILE *stream = fopen(path, mode);
329 flockfile(stream);
330 backParam = fgetpos(stream, &pos);
331 napi_value result = nullptr;
332 napi_create_int32(env, backParam, &result);
333 return result;
334 }
335
FileNo(napi_env env,napi_callback_info info)336 static napi_value FileNo(napi_env env, napi_callback_info info)
337 {
338 int backParam = INIT;
339 const char *path = "/data/storage/el2/base/files/Fzl.txt";
340 const char *mode = "r";
341 FILE *stream = fopen(path, mode);
342 backParam = fileno(stream);
343 napi_value result = nullptr;
344 napi_create_int32(env, backParam, &result);
345 return result;
346 }
347
FError(napi_env env,napi_callback_info info)348 static napi_value FError(napi_env env, napi_callback_info info)
349 {
350 int backParam;
351 const char *path = "/data/storage/el2/base/files/Fzl.txt";
352 const char *mode = "r";
353 FILE *stream = fopen(path, mode);
354 backParam = ferror(stream);
355 napi_value result = nullptr;
356 napi_create_int32(env, backParam, &result);
357 return result;
358 }
359
FGets(napi_env env,napi_callback_info info)360 static napi_value FGets(napi_env env, napi_callback_info info)
361 {
362 int backParam = INIT;
363 char *getResult = nullptr;
364 char str[256];
365 const char *path = "hidumper -s 1201";
366 const char *mode = "r";
367 FILE *stream = fopen(path, mode);
368 fprintf(stream, "Hello, world!\n");
369 getResult = fgets(str, sizeof(str), stream);
370 if (getResult == nullptr) {
371 backParam = SUCCESS;
372 }
373 napi_value result = nullptr;
374 napi_create_int32(env, backParam, &result);
375 return result;
376 }
377
FGetPos64(napi_env env,napi_callback_info info)378 static napi_value FGetPos64(napi_env env, napi_callback_info info)
379 {
380 int backParam;
381 const char *path = "/data/storage/el2/base/files/Fzl.txt";
382 const char *mode = "r";
383 FILE *stream = fopen(path, mode);
384 fpos_t pos;
385 backParam = fgetpos64(stream, &pos);
386 napi_value result = nullptr;
387 napi_create_int32(env, backParam, &result);
388 return result;
389 }
390
FGetPos(napi_env env,napi_callback_info info)391 static napi_value FGetPos(napi_env env, napi_callback_info info)
392 {
393 int backParam;
394 const char *path = "/data/storage/el2/base/files/Fzl.txt";
395 const char *mode = "r";
396 FILE *stream = fopen(path, mode);
397 fpos_t pos;
398 backParam = fgetpos(stream, &pos);
399 napi_value result = nullptr;
400 napi_create_int32(env, backParam, &result);
401 return result;
402 }
403
FGetC(napi_env env,napi_callback_info info)404 static napi_value FGetC(napi_env env, napi_callback_info info)
405 {
406 const char *path = "/data/storage/el2/base/files/Fzl.txt";
407 const char *mode = "r";
408 FILE *stream = fopen(path, mode);
409 fprintf(stream, "Hello, world!\n");
410 int backParam = INIT;
411 backParam = fgetc(stream);
412 napi_value result = nullptr;
413 napi_create_int32(env, backParam, &result);
414 return result;
415 }
416
FFlush(napi_env env,napi_callback_info info)417 static napi_value FFlush(napi_env env, napi_callback_info info)
418 {
419 int backParam;
420 const char *path = "/data/storage/el2/base/files/Fzl.txt";
421 const char *mode = "r";
422 FILE *stream = fopen(path, mode);
423 backParam = fflush(stream);
424 napi_value result = nullptr;
425 napi_create_int32(env, backParam, &result);
426 return result;
427 }
428
FdOpen(napi_env env,napi_callback_info info)429 static napi_value FdOpen(napi_env env, napi_callback_info info)
430 {
431 int backResult;
432 const char *path = "/data/storage/el2/base/files/Fzl.txt";
433 const char *mode = "r";
434 int fileDescribe = open(path, O_CREAT);
435 FILE *stream = fdopen(fileDescribe, mode);
436 backResult = fclose(stream);
437 napi_value result = nullptr;
438 napi_create_int32(env, backResult, &result);
439 return result;
440 }
441
Init(napi_env env,napi_value exports)442 EXTERN_C_START static napi_value Init(napi_env env, napi_value exports)
443 {
444 napi_property_descriptor desc[] = {
445 {"flushLbf", nullptr, Flushlbf, nullptr, nullptr, nullptr, napi_default, nullptr},
446 {"fWriting", nullptr, Fwriting, nullptr, nullptr, nullptr, napi_default, nullptr},
447 {"fPending", nullptr, Fpending, nullptr, nullptr, nullptr, napi_default, nullptr},
448 {"fPurge", nullptr, Fpurge, nullptr, nullptr, nullptr, napi_default, nullptr},
449 {"fReadable", nullptr, Freadable, nullptr, nullptr, nullptr, napi_default, nullptr},
450 {"fReading", nullptr, Freading, nullptr, nullptr, nullptr, napi_default, nullptr},
451 {"fSetErr", nullptr, Fseterr, nullptr, nullptr, nullptr, napi_default, nullptr},
452 {"fSetLocking", nullptr, Fsetlocking, nullptr, nullptr, nullptr, napi_default, nullptr},
453 {"fWritable", nullptr, Fwritable, nullptr, nullptr, nullptr, napi_default, nullptr},
454 {"fLbf", nullptr, Flbf, nullptr, nullptr, nullptr, napi_default, nullptr},
455 {"fBufSize", nullptr, FBufSize, nullptr, nullptr, nullptr, napi_default, nullptr},
456 {"fclose", nullptr, FClose, nullptr, nullptr, nullptr, napi_default, nullptr},
457 {"fmemopen", nullptr, FMemOpen, nullptr, nullptr, nullptr, napi_default, nullptr},
458 {"flockfile", nullptr, FlockFile, nullptr, nullptr, nullptr, napi_default, nullptr},
459 {"fileno", nullptr, FileNo, nullptr, nullptr, nullptr, napi_default, nullptr},
460 {"ferror", nullptr, FError, nullptr, nullptr, nullptr, napi_default, nullptr},
461 {"fgets", nullptr, FGets, nullptr, nullptr, nullptr, napi_default, nullptr},
462 {"fgetpos", nullptr, FGetPos, nullptr, nullptr, nullptr, napi_default, nullptr},
463 {"fgetpos64", nullptr, FGetPos64, nullptr, nullptr, nullptr, napi_default, nullptr},
464 {"fgetc", nullptr, FGetC, nullptr, nullptr, nullptr, napi_default, nullptr},
465 {"fflush", nullptr, FFlush, nullptr, nullptr, nullptr, napi_default, nullptr},
466 {"fdopen", nullptr, FdOpen, nullptr, nullptr, nullptr, napi_default, nullptr},
467 {"fileno_unlocked", nullptr, FileNo_unlocked, nullptr, nullptr, nullptr, napi_default, nullptr},
468 {"fgets_unlocked", nullptr, FGetS_unlocked, nullptr, nullptr, nullptr, napi_default, nullptr},
469 {"fgetln", nullptr, FGetLn, nullptr, nullptr, nullptr, napi_default, nullptr},
470 {"fgetc_unlocked", nullptr, FGetC_unlocked, nullptr, nullptr, nullptr, napi_default, nullptr},
471 {"fflush_unlocked", nullptr, FFlush_unlocked, nullptr, nullptr, nullptr, napi_default, nullptr},
472 {"ferror_unlocked", nullptr, FError_unlocked, nullptr, nullptr, nullptr, napi_default, nullptr},
473 {"feof_unlocked", nullptr, FEof_unlocked, nullptr, nullptr, nullptr, napi_default, nullptr},
474 {"dprintf", nullptr, DPrintf, nullptr, nullptr, nullptr, napi_default, nullptr},
475 };
476 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
477 return exports;
478 }
479 EXTERN_C_END
480
481 static napi_module demoModule = {
482 .nm_version = 1,
483 .nm_flags = 0,
484 .nm_filename = nullptr,
485 .nm_register_func = Init,
486 .nm_modname = "libstdioextndk",
487 .nm_priv = ((void *)0),
488 .reserved = {0},
489 };
490
RegisterModule(void)491 extern "C" __attribute__((constructor)) void RegisterModule(void) { napi_module_register(&demoModule); }