136 "Operation not permitted",
137 "No such file or directory",
139 "Interrupted system call",
141 "No such device or address",
142 "Argument list too long",
145 "No child processes",
150 "Block device required",
151 "Device or resource busy",
158 "File table overflow",
159 "Too many open files",
163 "No space left on device",
165 "Read-only file system",
168 "Math argument out of domain of func",
169 "Math result not representable",
188 if(fileno >= 0 && fileno <= 2)
215 if ((stream->flags & __SRD) == 0)
218 if ((stream->flags & __SUNGET) != 0) {
219 stream->flags &= ~__SUNGET;
221 return stream->unget;
224 if (stream->flags & __SSTR) {
227 stream->flags |= __SEOF;
238 c = stream->get(stream);
241 stream->flags |= (c == _FDEV_ERR)? __SERR: __SEOF;
269 if(stream != stdout && stream != stderr)
276 if ((stream->flags & __SWR) == 0)
279 if (stream->flags & __SSTR) {
280 if (stream->len < stream->size)
289 ret = stream->put(c, stream);
316 return(
fgetc(stdin));
331 return(
fputc(c,stdout));
381 return(
fputc(c, stream));
398 fgets(
char *str,
int size, FILE *stream)
434 s =
fgets (p, MAXLN, stdin);
438 if (n && p[n - 1] ==
'\n')
452 fputs(
const char *str, FILE *stream)
483 return (
fputc(
'\n',stdout) );
502 if(stream->flags & __SEOF)
519 long offset =
ftell(stream);
540 int fseek(FILE *stream,
long offset,
int whence)
548 ret =
lseek(fn, offset, whence);
568 return (
fseek(stream, (
size_t) *pos, SEEK_SET) );
632 stream->flags |= __SUNGET;
634 if(whence == SEEK_END)
636 else if(whence==SEEK_CUR)
637 position += fh->
fptr;
658 fseek(stream, 0L, SEEK_SET);
724 for(fileno=0; fileno<MAX_FILES; ++
fileno)
726 if (
__iob[fileno] == stream)
747 if(fileno < 0 || fileno >= MAX_FILES)
772 FILE *
fopen(
const char *path,
const char *mode)
793 size_t __wrap_fread(
void *ptr,
size_t size,
size_t nmemb, FILE *stream)
795 size_t count = size * nmemb;
800 ret =
read(fn, ptr, count);
804 return((
size_t) ret);
858 size_t fwrite(
const void *ptr,
size_t size,
size_t nmemb, FILE *stream)
860 size_t count = size * nmemb;
865 ret =
write(fn, ptr, count);
870 return((
size_t) ret);
885 int open(
const char *pathname,
int flags)
906 if((flags & O_ACCMODE) == O_RDWR)
908 else if((flags & O_ACCMODE) == O_RDONLY)
939 res =
f_open(fh, pathname, (
BYTE) (fatfs_modes & 0xff));
959 if((flags & O_ACCMODE) == O_RDWR)
964 stream->flags = _FDEV_SETUP_RW;
966 else if((flags & O_ACCMODE) == O_RDONLY)
971 stream->flags = _FDEV_SETUP_READ;
978 stream->flags = _FDEV_SETUP_WRITE;
1014 char *ptr = (
char *) buf;
1016 stream->flags |= __SUNGET;
1020 ret =
fgetc(stream);
1029 if(stream == stdout || stream == stderr)
1042 res =
f_read(fh, (
void *) buf, bytes, &size);
1048 return ((ssize_t) size);
1063 for(i=0;i<MAX_FILES;++i)
1100 stream->flags |= __SUNGET;
1180 if(stream == stdout || stream == stderr)
1182 char *ptr = (
char *) buf;
1188 ret =
fputc(c, stream);
1209 res =
f_write(fh, buf, bytes, &size);
1215 return ((ssize_t) size);
1221 int ret =
close(fn);
1225 return fopen(filename, mode);
1242 return(
close(fn) );
1295 int fstat(
int fd,
struct stat *buf)
1328 if (strcmp(name,
"/") == 0 || strcmp(name,
".") == 0)
1336 buf->st_mode = S_IFDIR;
1340 res =
f_stat(name, &info);
1347 buf->st_size = info.
fsize;
1349 buf->st_atime = epoch;
1350 buf->st_mtime = epoch;
1351 buf->st_ctime = epoch;
1354 mode = (FATFS_R | FATFS_X);
1372 buf->st_mode = mode;
1382 int utime(
const char *filename,
const struct utimbuf *times)
1386 uint16_t fdate,ftime;
1391 ut = times->modtime;
1407 DWORD fre_clust, fre_sect;
1412 if (res)
return(res);
1415 fre_sect = fre_clust * fs->
csize;
1416 return (int64_t)(fre_sect)*512;
1422 DWORD fre_clust, tot_sect;
1427 if (res)
return(res);
1431 return (int64_t)(tot_sect)*512;
1448 const char *base = str;
1514 int chmod(
const char *pathname, mode_t mode)
1522 if ( !( mode & ( S_IWUSR | S_IWGRP | S_IWOTH)))
1581 int fchmod(
int fd, mode_t mode)
1620 int mkdir(
const char *pathname, mode_t mode)
1632 chmod(pathname, mode);
1648 int rename(
const char *oldpath,
const char *newpath)
1706 int remove(
const char *pathname)
1757 return ((
DIR *) &_dp);
1781 len = strlen(fno.
fname);
1783 _de.d_name[len] = 0;
1784 return( (dirent_t *) &
_de);
1801 stream->flags &= ~__SEOF;
1802 stream->flags &= ~__SERR;
1813 if(stream->flags & __SERR)
1829 const char *ptr =
NULL;
1838 printf(
"%s: %s\n", s, ptr);
1892 if (put == 0 &&
get == 0)
1895 if ((s =
calloc(1,
sizeof(FILE))) == 0)
1898 s->flags = __SMALLOC;
2013 fh = (
FIL *) fdev_get_udata(stream);
2021 if( res !=
FR_OK || size != 1)
2024 stream->flags |= __SEOF;
2057 if(res !=
FR_OK || size != 1)
2100 fh = (
FIL *) fdev_get_udata(stream);
2108 if( res !=
FR_OK || size != 1)
2111 stream->flags |= __SEOF;
2216 for(i=0;i<MAX_FILES;++i)
2221 if( fh == (
FIL *) fdev_get_udata(stream) )
2236 int mon [] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }, y, m, i;
2237 const unsigned MINUTE = 60;
2238 const unsigned HOUR = 60*MINUTE;
2239 const unsigned DAY = 24*HOUR;
2240 const unsigned YEAR = 365*DAY;
2242 if (t->tm_year < 70) {
2246 n = t->tm_year + 1900 - 1;
2247 epoch = (t->tm_year - 70) * YEAR +
2248 ((n / 4 - n / 100 + n / 400) - (1969 / 4 - 1969 / 100 + 1969 / 400)) * DAY;
2250 y = t->tm_year + 1900;
2253 for(i = 0; i < t->tm_mon; i++) {
2254 epoch += mon [m] * DAY;
2255 if(m == 1 && y % 4 == 0 && (y % 100 != 0 || y % 400 == 0))
2264 epoch += (t->tm_mday - 1) * DAY;
2265 epoch += t->tm_hour * HOUR + t->tm_min * MINUTE + t->tm_sec;
2287 memset(&tp, 0,
sizeof(
struct tm));
2289 tp.tm_sec = (time << 1) & 0x3e;
2290 tp.tm_min = ((time >> 5) & 0x3f);
2291 tp.tm_hour = ((time >> 11) & 0x1f);
2292 tp.tm_mday = (date & 0x1f);
2293 tp.tm_mon = ((date >> 5) & 0x0f) - 1;
2294 tp.tm_year = ((date >> 9) & 0x7f) + 80;
2309 struct tm *t = gmtime((time_t *) &epoch);
2312 *date = ((uint16_t)(t->tm_year - 80) << 9)
2313 | (((uint16_t)t->tm_mon+1) << 5)
2314 | (((uint16_t)t->tm_mday));
2316 *time = ((uint16_t)t->tm_hour << 11)
2317 | ((uint16_t)t->tm_min << 5)
2318 | ((uint16_t)t->tm_sec >> 1);
2344 if( stream ==
NULL )
2347 fh = fdev_get_udata(stream);
2384 fh = fdev_get_udata(stream);
2391 if(stream->buf !=
NULL && stream->flags & __SMALLOC)
2416 for(i=0;i<MAX_FILES;++i)
2422 stream = (FILE *)
calloc(
sizeof(FILE),1);
2437 fdev_set_udata(stream, (
void *) fh);
2477 if(modecmp(mode,
"r") || modecmp(mode,
"rb"))
2482 if(modecmp(mode,
"r+") || modecmp(mode,
"r+b" ) || modecmp(mode,
"rb+" ))
2484 flag = O_RDWR | O_TRUNC;
2487 if(modecmp(mode,
"w") || modecmp(mode,
"wb"))
2489 flag = O_WRONLY | O_CREAT | O_TRUNC;
2492 if(modecmp(mode,
"w+") || modecmp(mode,
"w+b" ) || modecmp(mode,
"wb+" ))
2494 flag = O_RDWR | O_CREAT | O_TRUNC;
2497 if(modecmp(mode,
"a") || modecmp(mode,
"ab"))
2499 flag = O_WRONLY | O_CREAT | O_APPEND;
2502 if(modecmp(mode,
"a+") || modecmp(mode,
"a+b" ) || modecmp(mode,
"ab+" ))
2504 flag = O_RDWR | O_CREAT | O_APPEND;
2547 for(i = 0; i < len; i++) {
int printf(const char *fmt,...)
int syncfs(int fd)
POSIX Sync pending file changes and metadata for specified fileno.
int fileno(FILE *stream)
Convert POSIX stream pointer to POSIX fileno (index of __iob[])
char * baseext(char *str)
File extention of a file name. NOT POSIX.
int rename(const char *oldpath, const char *newpath)
POSIX rename a file by name.
FRESULT f_closedir(DIR *dp)
int vasprintf(char **strp, const char *fmt, va_list ap)
FRESULT f_lseek(FIL *fp, FSIZE_t ofs)
void rewind(FILE *stream)
POSIX rewind file to the beginning.
dirent_t * readdir(DIR *dirp)
int ferror(FILE *stream)
ferror reports if the stream has an error flag set
int open(const char *pathname, int flags)
POSIX Open a file with integer mode flags.
int fgetpos(FILE *stream, size_t *pos)
POSIX get position of file stream.
static time_t replace_mktime(const struct tm *t)
int fatfs_putc(char c, FILE *stream)
Private FatFs function called by fputc() to put a byte from file stream NOT POSIX open() assigns stre...
char * __wrap_strerror_r(int errnum, char *buf, size_t buflen)
POSIX strerror_r() - convert POSIX errno to text with user message.
FRESULT f_unlink(const TCHAR *path)
int fatfs_to_errno(FRESULT Result)
Convert FafFs error result to POSIX errno. NOT POSIX.
char * strerror(int errnum)
POSIX strerror() - convert POSIX errno to text with user message.
int utime(const char *filename, const struct utimbuf *times)
Set Modification and Access time of a file.
int dirname(char *str)
POSIX directory name of a filename. Return the index of the last '/' character.
ssize_t write(int fd, const void *buf, size_t count)
POSIX Write count bytes from *buf to fileno fd.
int posix_fopen_modes_to_open(const char *mode)
Convert POSIX fopen mode to POSIX open mode flags. NOT POSIX.
int rmdir(const char *pathname)
POSIX delete a directory.
FRESULT f_chdir(const TCHAR *path)
FRESULT f_chmod(const TCHAR *path, BYTE attr, BYTE mask)
int new_file_descriptor(void)
Allocate a POSIX FILE descriptor. NOT POSIX.
char * getcwd(char *pathname, int len)
POSIX get current working directory.
int fatfs_getc(FILE *stream)
Formt SD card.
void * calloc(size_t nmemb, size_t size)
int putchar(int c)
put a character to stdout See fdevopen() sets stream->put get for TTY devices
time_t fat_time_to_unix(uint16_t date, uint16_t time)
Convert FatFs file date and time to POSIX epoch seconds. NOT POSIX.
FRESULT f_truncate(FIL *fp)
FILE * fdevopen(int(*put)(char, FILE *), int(*get)(FILE *))
Device open functions.
int fatfs_to_fileno(FIL *fh)
Convert FatFS file handle to POSIX fileno. NOT POSIX.
int fsetpos(FILE *stream, size_t *pos)
POSIX set position of file stream.
void perror(const char *s)
POSIX perror() - convert POSIX errno to text with user message.
long ftell(FILE *stream)
POSIX file position of open stream.
int free_file_descriptor(int fileno)
Free POSIX fileno FILE descriptor. NOT POSIX.
int feof(FILE *stream)
feof reports if the stream is at EOF
ssize_t read(int fd, void *buf, size_t count)
POSIX read count bytes from *buf to fileno fd.
FRESULT f_readdir(DIR *dp, FILINFO *fno)
FRESULT f_read(FIL *fp, void *buff, UINT btr, UINT *br)
int isatty(int fileno)
Test POSIX fileno if it is a Serial Console/TTY.
FRESULT f_getfree(const TCHAR *path, DWORD *nclst, FATFS **fatfs)
void unix_time_to_fat(time_t epoch, uint16_t *date, uint16_t *time)
Convert Linux POSIX time_t to FAT32 date and time. NOT POSIX.
int puts(const char *str)
put a string to stdout See fdevopen() sets stream->put get for TTY devices
int putc(int c, FILE *stream)
Un-Get byte from a TTY device or FatFs file stream.
int __wrap_fclose(FILE *stream)
POSIX close a file stream.
int getchar()
functions normally defined as macros
int close(int fileno)
POSIX Close a file with fileno handel.
FRESULT f_stat(const TCHAR *path, FILINFO *fno)
void sync(void)
POSIX Sync all pending file changes and metadata on ALL files.
int unlink(const char *pathname)
POSIX delete a file.
int fputs(const char *str, FILE *stream)
put a string to stdout See fdevopen() sets stream->put get for TTY devices
off_t lseek(int fileno, off_t position, int whence)
POSIX seek to file position.
int mkdir(const char *pathname, mode_t mode)
POSIX make a directory.
int fseek(FILE *stream, long offset, int whence)
POSIX seek to file possition.
int ftruncate(int fd, off_t length)
POSIX truncate open file to length.
static dirent_t _de
POSIX opendir.
FRESULT f_opendir(DIR *dp, const TCHAR *path)
FIL * fileno_to_fatfs(int fileno)
Convert POSIX fileno to FatFS handle NOT POSIX.
FILE * fileno_to_stream(int fileno)
Convert POSIX fileno to POSIX FILE stream pointer. NOT POSIX.
int stat(const char *name, struct stat *buf)
Display struct stat, from POSIX stat(0 or fstat(), in ASCII. NOT POSIX.
FILE * fopen(const char *path, const char *mode)
POSIX Open a file with path name and ascii file mode string.
int chdir(const char *pathname)
POSIX change directory.
int closedir(DIR *dirp)
POSIX closedir.
int fgetc(FILE *stream)
Get byte from a TTY device or FatFs file stream open() or fopen() sets stream->get = fatfs_getc() for...
int chmod(const char *pathname, mode_t mode)
POSIX chmod function - change file access permission Unfortunately file f_open modes and f_chmod mode...
FRESULT f_utime(const TCHAR *path, const FILINFO *fno)
FRESULT f_mkdir(const TCHAR *path)
int errno
Note: fdevopen assigns stdin,stdout,stderr.
int fprintf(FILE *fp, const char *fmt,...)
fprintf character write function
int truncate(const char *path, off_t length)
POSIX truncate named file to length.
void uint32_t uint32_t uint32_t flag
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
POSIX write nmemb elements from buf, size bytes each, to the stream fd.
const char * sys_errlist[]
POSIX error messages for each errno value.
int fputc(int c, FILE *stream)
Put a byte to TTY device or FatFs file stream open() or fopen() sets stream->put = fatfs_outc() for F...
FILE * __iob[MAX_FILES]
POSIX fileno to POSIX FILE stream table.
DIR * opendir(const char *pathdir)
FILE * __wrap_freopen(const char *filename, const char *mode, FILE *stream)
FRESULT f_rename(const TCHAR *path_old, const TCHAR *path_new)
char * fgets(char *str, int size, FILE *stream)
get a string from stdin See fdevopen() sets stream->put get for TTY devices
static DIR _dp
POSIX opendir.
FRESULT f_write(FIL *fp, const void *buff, UINT btw, UINT *bw)
FRESULT f_getcwd(TCHAR *buff, UINT len)
char * basename(const char *str)
POSIX Basename of filename.
void clearerr(FILE *stream)
size_t __wrap_fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
POSIX read nmemb elements from buf, size bytes each, to the stream fd.
void clrerror(FILE *stream)
clrerror resets stream EOF and error flags
FRESULT f_open(FIL *fp, const TCHAR *path, BYTE mode)