Skip to content

Commit

Permalink
Use standard C file IO
Browse files Browse the repository at this point in the history
  • Loading branch information
certik committed Mar 29, 2022
1 parent eac54bb commit 94b9196
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 deletions.
18 changes: 7 additions & 11 deletions src/runtime/impure/lfortran_intrinsics.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@
#include <time.h>
#include <float.h>
#include <limits.h>
#include <fcntl.h>

#ifndef _WIN32
#include <unistd.h>
#endif

#include "lfortran_intrinsics.h"

Expand Down Expand Up @@ -792,16 +787,17 @@ LFORTRAN_API void _lfortran_dp_rand_num(double *x) {
*x = rand() / (double) RAND_MAX;
}

LFORTRAN_API int64_t _lpython_open(char *path, int32_t flags)
LFORTRAN_API int64_t _lpython_open(char *path, char *flags)
{
int64_t fd = open(path, flags);
if (fd < 0)
FILE *fd;
fd = fopen(path, flags);
if (!fd)
{
printf("Error in opening the file!\n");
perror(path);
exit(1);
}
return fd;
return (int64_t)fd;
}

LFORTRAN_API char* _lpython_read(int64_t fd, int64_t n)
Expand All @@ -812,14 +808,14 @@ LFORTRAN_API char* _lpython_read(int64_t fd, int64_t n)
printf("Error in reading the file!\n");
exit(1);
}
int x = read(fd, c, n);
int x = fread(c, 1, n, (FILE*)fd);
c[x] = '\0';
return c;
}

LFORTRAN_API void _lpython_close(int64_t fd)
{
if (close(fd) < 0)
if (fclose((FILE*)fd) != 0)
{
printf("Error in closing the file!\n");
exit(1);
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/impure/lfortran_intrinsics.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ 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, int32_t flags);
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);

Expand Down

0 comments on commit 94b9196

Please sign in to comment.