Skip to content

Commit

Permalink
Implement _stat_r and _fstat_r.
Browse files Browse the repository at this point in the history
xyzz committed Sep 22, 2015
1 parent ccf528d commit 6fed0c7
Showing 1 changed file with 74 additions and 14 deletions.
88 changes: 74 additions & 14 deletions newlib/libc/sys/vita/syscalls.c
Original file line number Diff line number Diff line change
@@ -76,13 +76,6 @@ _fork_r(struct _reent *reent)
return -1;
}

int
_fstat_r(struct _reent *reent, int fd, struct stat *st)
{
reent->_errno = ENOSYS;
return -1;
}

int
_getpid_r(struct _reent *reent)
{
@@ -217,13 +210,6 @@ _readlink_r(struct _reent *reent, const char *path, char *buf, size_t bufsize)
return -1;
}

int
_stat_r(struct _reent *reent, const char *path, struct stat *buf)
{
reent->_errno = EIO;
return -1;
}

int
_unlink_r(struct _reent *reent, const char * path)
{
@@ -260,3 +246,77 @@ _times_r(struct _reent *reent, struct tms *ptms)
ptms->tms_cstime = 0;
return result;
}

struct SceDateTime {
unsigned short year;
unsigned short month;
unsigned short day;
unsigned short hour;
unsigned short minute;
unsigned short second;
unsigned int microsecond;
};

struct SceIoStat {
int st_mode;
unsigned int st_attr;
long long st_size;
struct SceDateTime st_ctime;
struct SceDateTime st_atime;
struct SceDateTime st_mtime;
unsigned st_private[6];
};

enum {
SCE_DIR = 0x1000,
SCE_REG = 0x2000,
SCE_STATFMT = 0xf000
};

#define SCE_ISREG(x) (((x) & SCE_STATFMT) == SCE_REG)
#define SCE_ISDIR(x) (((x) & SCE_STATFMT) == SCE_DIR)

static void
scestat_to_stat(struct SceIoStat *in, struct stat *out) {
memset(out, 0, sizeof(*out));
out->st_size = in->st_size;
if (SCE_ISREG(in->st_mode))
out->st_mode |= _IFREG;
if (SCE_ISDIR(in->st_mode))
out->st_mode |= _IFDIR;
sceRtcGetTime_t(&in->st_atime, &out->st_atime);
sceRtcGetTime_t(&in->st_mtime, &out->st_mtime);
sceRtcGetTime_t(&in->st_ctime, &out->st_ctime);
}

int
_fstat_r(struct _reent *reent, int fd, struct stat *st)
{
struct SceIoStat stat = {0};
int ret;
if ((unsigned)fd > MAX_OPEN_FILES) {
reent->_errno = EINVAL;
return -1;
}
if ((ret = sceIoGetstatByFd(fd_to_scefd[fd], &stat)) < 0) {
reent->_errno = ret & SCE_ERRNO_MASK;
return -1;
}
scestat_to_stat(&stat, st);
reent->_errno = 0;
return 0;
}

int
_stat_r(struct _reent *reent, const char *path, struct stat *st)
{
struct SceIoStat stat = {0};
int ret;
if ((ret = sceIoGetstat(path, &stat)) < 0) {
reent->_errno = ret & SCE_ERRNO_MASK;
return -1;
}
scestat_to_stat(&stat, st);
reent->_errno = 0;
return 0;
}

0 comments on commit 6fed0c7

Please sign in to comment.