diff --git a/integration_tests/run_tests.py b/integration_tests/run_tests.py index 1359a79031..f6bdc92ab8 100755 --- a/integration_tests/run_tests.py +++ b/integration_tests/run_tests.py @@ -20,6 +20,7 @@ "test_numpy_01.py", "test_numpy_02.py", "test_random.py", + "test_os.py", "test_builtin.py", "test_builtin_abs.py", "test_builtin_bool.py", diff --git a/integration_tests/test_os.py b/integration_tests/test_os.py new file mode 100644 index 0000000000..fea7230893 --- /dev/null +++ b/integration_tests/test_os.py @@ -0,0 +1,14 @@ +from ltypes import i64 +from os import (open, read, close) + +def test(): + path: str + path = "integration_tests/test_os.py" + fd: i64 + n: i64 + fd = open(path, "r") + n = 100 + print(read(fd, n)) + close(fd) + +test() diff --git a/src/runtime/impure/lfortran_intrinsics.c b/src/runtime/impure/lfortran_intrinsics.c index aeb59f5ebd..9cedaa2a97 100644 --- a/src/runtime/impure/lfortran_intrinsics.c +++ b/src/runtime/impure/lfortran_intrinsics.c @@ -786,3 +786,38 @@ LFORTRAN_API void _lfortran_dp_rand_num(double *x) { srand(time(0)); *x = rand() / (double) RAND_MAX; } + +LFORTRAN_API int64_t _lpython_open(char *path, char *flags) +{ + FILE *fd; + fd = fopen(path, flags); + if (!fd) + { + printf("Error in opening the file!\n"); + perror(path); + exit(1); + } + return (int64_t)fd; +} + +LFORTRAN_API char* _lpython_read(int64_t fd, int64_t n) +{ + char *c = (char *) calloc(n, sizeof(char)); + if (fd < 0) + { + printf("Error in reading the file!\n"); + exit(1); + } + int x = fread(c, 1, n, (FILE*)fd); + c[x] = '\0'; + return c; +} + +LFORTRAN_API void _lpython_close(int64_t fd) +{ + if (fclose((FILE*)fd) != 0) + { + printf("Error in closing the file!\n"); + exit(1); + } +} diff --git a/src/runtime/impure/lfortran_intrinsics.h b/src/runtime/impure/lfortran_intrinsics.h index 2438eca410..8492f3db60 100644 --- a/src/runtime/impure/lfortran_intrinsics.h +++ b/src/runtime/impure/lfortran_intrinsics.h @@ -160,6 +160,9 @@ LFORTRAN_API void _lfortran_i64sys_clock( uint64_t *count, int64_t *rate, int64_t *max); LFORTRAN_API void _lfortran_sp_rand_num(float *x); LFORTRAN_API void _lfortran_dp_rand_num(double *x); +LFORTRAN_API int64_t _lpython_open(char *path, char *flags); +LFORTRAN_API char* _lpython_read(int64_t fd, int64_t n); +LFORTRAN_API void _lpython_close(int64_t fd); #ifdef __cplusplus } diff --git a/src/runtime/os.py b/src/runtime/os.py new file mode 100644 index 0000000000..8d1050e3f9 --- /dev/null +++ b/src/runtime/os.py @@ -0,0 +1,32 @@ +from ltypes import i32, i64, ccall + +def open(path: str, flag: str) -> i64: + """ + Returns the file descriptor for the newly opened file + """ + return _lpython_open(path, flag) + +@ccall +def _lpython_open(path: str, flag: str) -> i64: + pass + +def read(fd: i64, n: i64) -> str: + """ + Reads at most `n` bytes from file descriptor + """ + return _lpython_read(fd, n) + +@ccall +def _lpython_read(fd: i64, n: i64) -> str: + pass + +def close(fd: i64): + """ + Closes the file descriptor + """ + _lpython_close(fd) + return + +@ccall +def _lpython_close(fd: i64): + pass