2727#include <stdio.h>
2828#include <stdlib.h>
2929#include <string.h>
30+ #include <fcntl.h>
3031
3132#include "py/mpstate.h"
3233#include "py/mphal.h"
@@ -100,22 +101,26 @@ void prompt_read_history(void) {
100101 vstr_t vstr ;
101102 vstr_init (& vstr , 50 );
102103 vstr_printf (& vstr , "%s/.micropython.history" , home );
103- FILE * fp = fopen (vstr_null_terminated_str (& vstr ), "r" );
104- if (fp != NULL ) {
104+ int fd = open (vstr_null_terminated_str (& vstr ), O_RDONLY );
105+ if (fd != -1 ) {
105106 vstr_reset (& vstr );
106107 for (;;) {
107- int c = fgetc (fp );
108- if (c == EOF || c == '\n' ) {
108+ char c ;
109+ int sz = read (fd , & c , 1 );
110+ if (sz < 0 ) {
111+ break ;
112+ }
113+ if (sz == 0 || c == '\n' ) {
109114 readline_push_history (vstr_null_terminated_str (& vstr ));
110- if (c == EOF ) {
115+ if (sz == 0 ) {
111116 break ;
112117 }
113118 vstr_reset (& vstr );
114119 } else {
115120 vstr_add_byte (& vstr , c );
116121 }
117122 }
118- fclose ( fp );
123+ close ( fd );
119124 }
120125 vstr_clear (& vstr );
121126 }
@@ -133,16 +138,18 @@ void prompt_write_history(void) {
133138 vstr_t vstr ;
134139 vstr_init (& vstr , 50 );
135140 vstr_printf (& vstr , "%s/.micropython.history" , home );
136- FILE * fp = fopen (vstr_null_terminated_str (& vstr ), "w" );
137- if (fp != NULL ) {
141+ int fd = open (vstr_null_terminated_str (& vstr ), O_CREAT | O_TRUNC | O_WRONLY , 0644 );
142+ if (fd != -1 ) {
138143 for (int i = MP_ARRAY_SIZE (MP_STATE_PORT (readline_hist )) - 1 ; i >= 0 ; i -- ) {
139144 const char * line = MP_STATE_PORT (readline_hist )[i ];
140145 if (line != NULL ) {
141- fwrite (line , 1 , strlen (line ), fp );
142- fputc ('\n' , fp );
146+ int res ;
147+ res = write (fd , line , strlen (line ));
148+ res = write (fd , "\n" , 1 );
149+ (void )res ;
143150 }
144151 }
145- fclose ( fp );
152+ close ( fd );
146153 }
147154 }
148155 #elif MICROPY_USE_READLINE == 2
0 commit comments