Skip to content

Commit a0de729

Browse files
committed
Added missing _stat function, fixed dos date/time conversion
1 parent a3f7f79 commit a0de729

File tree

1 file changed

+39
-7
lines changed

1 file changed

+39
-7
lines changed

kernel/syscalls.c

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <unistd.h>
2020
#include <stdint.h>
2121
#include <string.h>
22+
#include <strings.h>
2223
#include <errno.h>
2324
#include <fcntl.h>
2425
#include <sys/stat.h>
@@ -37,7 +38,7 @@ static struct emmc_block_dev *emmc_dev;
3738
#define MMC 0 /* Map MMC/SD card to drive number 0 */
3839
#define USB 1 /* Map USB drive to drive number 1 */
3940

40-
#define MAX_DESCRIPTORS 256
41+
#define MAX_DESCRIPTORS 64
4142
#define RESERVED_DESCRIPTORS 3
4243

4344
static FATFS fat_fs[_VOLUMES];
@@ -98,6 +99,37 @@ static int FRESULT_to_errno(FRESULT rc) {
9899
return EIO;
99100
}
100101

102+
int _stat(const char *file, struct stat *st) {
103+
FILINFO fno;
104+
struct tm ltm;
105+
106+
FRESULT rc = f_stat(file, &fno);
107+
108+
if (rc == FR_OK) {
109+
ltm.tm_sec = (fno.ftime & 0x1f) << 1;
110+
ltm.tm_min = (fno.ftime & 0x3e0) >> 5;
111+
ltm.tm_hour = (fno.ftime & 0xf800) >> 11;
112+
ltm.tm_mday = (fno.fdate & 0x1f);
113+
ltm.tm_mon = ((fno.fdate & 0x1e0) >> 5) - 1;
114+
ltm.tm_year = ((fno.fdate & 0xfe00) >> 9) + 80;
115+
116+
st->st_dev = 0;
117+
st->st_ino = 0;
118+
st->st_mode = 0;
119+
st->st_nlink = 0;
120+
st->st_uid = 0;
121+
st->st_gid = 0;
122+
st->st_rdev = 0;
123+
st->st_size = fno.fsize;
124+
st->st_atime = st->st_mtime = st->st_ctime = mktime(&ltm);
125+
st->st_blksize = 0;
126+
st->st_blocks = 0;
127+
}
128+
errno = FRESULT_to_errno(rc);
129+
130+
return rc == FR_OK ? 0 : -1;
131+
}
132+
101133
int _fstat(int file, struct stat *st) {
102134
if (file < RESERVED_DESCRIPTORS || file >= MAX_DESCRIPTORS || file_descriptors[file] == NULL) {
103135
errno = EBADF;
@@ -578,12 +610,12 @@ DWORD get_fattime (void)
578610
time_t now = time(NULL);
579611
struct tm *ltm = localtime(&now);
580612

581-
return ((DWORD)(ltm->tm_year - 80) << 25)
582-
| ((DWORD)ltm->tm_mon << 21)
583-
| ((DWORD)ltm->tm_mday << 16)
584-
| ((DWORD)ltm->tm_hour << 11)
585-
| ((DWORD)ltm->tm_min << 5)
586-
| ((DWORD)ltm->tm_sec >> 1);
613+
return ((DWORD)(ltm->tm_year - 80) << 25)
614+
| ((DWORD)(ltm->tm_mon + 1) << 21)
615+
| ((DWORD)ltm->tm_mday << 16)
616+
| ((DWORD)ltm->tm_hour << 11)
617+
| ((DWORD)ltm->tm_min << 5)
618+
| ((DWORD)ltm->tm_sec >> 1);
587619
}
588620
#endif
589621

0 commit comments

Comments
 (0)