30#include <sys/random.h>
40#define NEWLIB_PORT_AWARE
43#include "timer_alarm.h"
47extern char __cwd[MAXNAMLEN + 1];
48extern size_t __cwd_len;
49int __path_absolute(
const char *in,
char *out,
int len);
53#ifdef F___dummy_passwd
55struct passwd __dummy_passwd = {
"ps2_user",
"xxx", 1000, 1000,
"",
"",
"/",
"" };
57extern struct passwd __dummy_passwd;
60#ifdef F___transform_errno
61int __transform_errno(
int res) {
70int __transform_errno(
int res);
73#ifdef F___transform64_errno
74int64_t __transform64_errno(int64_t res) {
83int64_t __transform64_errno(int64_t res);
86#if INT_MAX != 0x7fffffffL
87 #error "INT_MAX != 0x7fffffffL"
90 #error "LONG_MAX not defined"
92#if LONG_MAX != 0x7fffffffL
93 #error "LONG_MAX != 0x7fffffffL"
96#define ct_assert(e) {enum { ct_assert_value = 1/(!!(e)) };}
97#ifdef F_compile_time_check
98void compile_time_check() {
100 ct_assert(
sizeof(
unsigned char)==1);
101 ct_assert(
sizeof(
unsigned short)==2);
102 ct_assert(
sizeof(
unsigned int)==4);
103 ct_assert(
sizeof(
unsigned long)==4);
104 ct_assert(
sizeof(
unsigned long long)==8);
105 ct_assert(
sizeof(
unsigned int __attribute__(( mode(TI) )))==16);
106 ct_assert(
sizeof(
void *)==4);
109 ct_assert(
sizeof(u8)==1);
110 ct_assert(
sizeof(u16)==2);
111 ct_assert(
sizeof(u32)==4);
112 ct_assert(
sizeof(u64)==8);
113 ct_assert(
sizeof(u128)==16);
116 ct_assert(
sizeof(uint8_t)==1);
117 ct_assert(
sizeof(uint16_t)==2);
118 ct_assert(
sizeof(uint32_t)==4);
119 ct_assert(
sizeof(uint64_t)==8);
124int _open(
const char *buf,
int flags, ...) {
129 char t_fname[MAXNAMLEN + 1];
131 va_start(alist, flags);
132 mode = va_arg(alist,
int);
135 if(__path_absolute(buf, t_fname, MAXNAMLEN) < 0) {
140 if (_libcglue_fdman_path_ops == NULL || _libcglue_fdman_path_ops->open == NULL)
146 fd = __fdman_get_new_descriptor();
153 info = &(__descriptormap[fd]->info);
154 iop_fd = _libcglue_fdman_path_ops->open(
info, t_fname, flags, mode);
157 __fdman_release_descriptor(fd);
158 return __transform_errno(iop_fd);
160 __descriptormap[fd]->flags = flags;
169 if (!__IS_FD_VALID(fd)) {
174 if (__descriptormap[fd]->ref_count == 1)
178 fdinfo = &(__descriptormap[fd]->info);
179 if (fdinfo->ops != NULL && fdinfo->ops->close != NULL)
181 ret = __transform_errno(fdinfo->ops->close(fdinfo->userdata));
184 __fdman_release_descriptor(fd);
190int _read(
int fd,
void *buf,
size_t nbytes) {
191 if (!__IS_FD_VALID(fd)) {
198 fdinfo = &(__descriptormap[fd]->info);
199 if (fdinfo->ops == NULL || fdinfo->ops->read == NULL)
204 return __transform_errno(fdinfo->ops->read(fdinfo->userdata, buf, nbytes));
209int _write(
int fd,
const void *buf,
size_t nbytes) {
210 if (!__IS_FD_VALID(fd)) {
217 fdinfo = &(__descriptormap[fd]->info);
218 if (fdinfo->ops == NULL || fdinfo->ops->write == NULL)
223 return __transform_errno(fdinfo->ops->write(fdinfo->userdata, buf, nbytes));
228int _stat(
const char *path,
struct stat *buf) {
229 char dest[MAXNAMLEN + 1];
231 if(__path_absolute(path, dest, MAXNAMLEN) < 0) {
236 if (_libcglue_fdman_path_ops == NULL || _libcglue_fdman_path_ops->stat == NULL)
242 return __transform_errno(_libcglue_fdman_path_ops->stat(dest, buf));
247int lstat(
const char *path,
struct stat *buf) {
248 return stat(path, buf);
253int _fstat(
int fd,
struct stat *buf) {
254 if (!__IS_FD_VALID(fd)) {
262 fdinfo = &(__descriptormap[fd]->info);
263 if (fdinfo->ops == NULL || fdinfo->ops->getfilename == NULL)
268 filename = fdinfo->ops->getfilename(fdinfo->userdata);
269 if (filename == NULL)
275 if (_libcglue_fdman_path_ops == NULL || _libcglue_fdman_path_ops->stat == NULL)
281 return __transform_errno(_libcglue_fdman_path_ops->stat(filename, buf));
286int access(
const char *fn,
int flags) {
290 if (s.st_mode & S_IFDIR)
293 if (s.st_mode & S_IWRITE)
303int _fcntl(
int fd,
int cmd, ...)
305 if (!__IS_FD_VALID(fd)) {
314 return __fdman_get_dup_descriptor(fd);
319 return __descriptormap[fd]->flags;
329 va_start (args, cmd);
330 newfl = va_arg(args,
int);
333 __descriptormap[fd]->flags = newfl;
338 fdinfo = &(__descriptormap[fd]->info);
340 if (fdinfo->ops != NULL && fdinfo->ops->fcntl_f_setfl != NULL)
342 rv = __transform_errno(fdinfo->ops->fcntl_f_setfl(fdinfo->userdata, newfl));
352 va_start (args, cmd);
353 newfl = va_arg(args,
int);
356 __descriptormap[fd]->flags = newfl;
368int getdents(
int fd,
void *dd_buf,
int count)
374 dirp = (
struct dirent *)dd_buf;
379 fdinfo = &(__descriptormap[fd]->info);
381 if (fdinfo->ops != NULL && fdinfo->ops->dread != NULL)
383 rv = __transform_errno(fdinfo->ops->dread(fdinfo->userdata, dirp));
387 return __transform_errno(rv);
388 }
else if (rv == 0) {
392 read +=
sizeof(
struct dirent);
393 dirp->d_reclen =
count;
402off_t _lseek(
int fd, off_t offset,
int whence)
404 if (!__IS_FD_VALID(fd)) {
411 fdinfo = &(__descriptormap[fd]->info);
412 if (fdinfo->ops == NULL || fdinfo->ops->lseek == NULL)
417 return __transform_errno(fdinfo->ops->lseek(fdinfo->userdata, offset, whence));
422off64_t lseek64(
int fd, off64_t offset,
int whence)
424 if (!__IS_FD_VALID(fd)) {
431 fdinfo = &(__descriptormap[fd]->info);
432 if (fdinfo->ops == NULL || fdinfo->ops->lseek64 == NULL)
437 return __transform64_errno(fdinfo->ops->lseek64(fdinfo->userdata, offset, whence));
442int chdir(
const char *path) {
443 char dest[MAXNAMLEN + 1];
445 if(__path_absolute(path, dest, MAXNAMLEN) < 0) {
450 strncpy(__cwd, dest,
sizeof(__cwd));
451 __cwd_len = strnlen(__cwd,
sizeof(__cwd));
457int mkdir(
const char *path, mode_t mode) {
458 char dest[MAXNAMLEN + 1];
460 if(__path_absolute(path, dest, MAXNAMLEN) < 0) {
465 if (_libcglue_fdman_path_ops == NULL || _libcglue_fdman_path_ops->mkdir == NULL)
471 return __transform_errno(_libcglue_fdman_path_ops->mkdir(dest, mode));
476int rmdir(
const char *path) {
477 char dest[MAXNAMLEN + 1];
479 if(__path_absolute(path, dest, MAXNAMLEN) < 0) {
484 if (_libcglue_fdman_path_ops == NULL || _libcglue_fdman_path_ops->rmdir == NULL)
490 return __transform_errno(_libcglue_fdman_path_ops->rmdir(dest));
495int _link(
const char *old,
const char *
new) {
502int _unlink(
const char *path) {
503 char dest[MAXNAMLEN + 1];
504 if(__path_absolute(path, dest, MAXNAMLEN) < 0) {
509 if (_libcglue_fdman_path_ops == NULL || _libcglue_fdman_path_ops->remove == NULL)
515 return __transform_errno(_libcglue_fdman_path_ops->remove(dest));
520int _rename(
const char *old,
const char *
new) {
521 char oldname[MAXNAMLEN + 1];
522 char newname[MAXNAMLEN + 1];
524 if(__path_absolute(old, oldname, MAXNAMLEN) < 0) {
529 if(__path_absolute(
new, newname, MAXNAMLEN) < 0) {
534 if (_libcglue_fdman_path_ops == NULL || _libcglue_fdman_path_ops->rename == NULL)
540 return __transform_errno(_libcglue_fdman_path_ops->rename(oldname, newname));
546 return GetThreadId();
551int _kill(
int pid,
int sig) {
555 return pid == getpid() ? 0 : -1;
557 if (pid == getpid()) {
578pid_t _wait(
int *unused) {
585int _execve(
const char *name,
char *
const argv[],
char *
const env[]) {
592void * _sbrk(
size_t incr) {
593 static void * _heap_ptr = &_end;
594 void *mp, *ret = (
void *)-1;
600 mp = _heap_ptr + incr;
601 if (mp <= EndOfHeap()) {
610#ifdef F__gettimeofday
611int _gettimeofday(
struct timeval *tv,
struct timezone *tz)
623 TimerBusClock2USec(GetTimerSystemTime(), &busclock_sec, &busclock_usec);
624 tv->tv_sec = (time_t)(_ps2sdk_rtc_offset_from_busclk + ((s64)busclock_sec));
625 tv->tv_usec = busclock_usec;
630 tz->tz_minuteswest = _timezone / 60;
639clock_t _times(
struct tms *buffer) {
640 clock_t clk = GetTimerSystemTime() / (kBUSCLK / (1000 * 1000));
642 if (buffer != NULL) {
643 buffer->tms_utime = clk;
644 buffer->tms_stime = 0;
645 buffer->tms_cutime = 0;
646 buffer->tms_cstime = 0;
654int ftime(
struct timeb *tb) {
658 gettimeofday(&tv, &tz);
660 tb->time = tv.tv_sec;
661 tb->millitm = tv.tv_usec / 1000;
662 tb->timezone = tz.tz_minuteswest;
663 tb->dstflag = tz.tz_dsttime;
670int clock_getres(clockid_t clk_id,
struct timespec *res) {
674 ret = gettimeofday(&tv, NULL);
677 res->tv_sec = tv.tv_sec;
678 res->tv_nsec = tv.tv_usec * 1000;
684#ifdef F_clock_gettime
685int clock_gettime(clockid_t clk_id,
struct timespec *tp) {
689 res = gettimeofday(&tv, NULL);
692 tp->tv_sec = tv.tv_sec;
693 tp->tv_nsec = tv.tv_usec * 1000;
699#ifdef F_clock_settime
700int clock_settime(clockid_t clk_id,
const struct timespec *tp) {
708int truncate(
const char *path, off_t length)
714 fd = open(path, O_RDONLY);
720 bytes_read = read(fd, &buff, length);
722 if (bytes_read < length) {
727 fd = open (path, O_TRUNC|O_WRONLY);
733 res = write(fd, &buff, length);
740int symlink(
const char *target,
const char *linkpath)
742 char dest_target[MAXNAMLEN + 1];
743 char dest_linkpath[MAXNAMLEN + 1];
745 if(__path_absolute(target, dest_target, MAXNAMLEN) < 0) {
750 if(__path_absolute(linkpath, dest_linkpath, MAXNAMLEN) < 0) {
755 if (_libcglue_fdman_path_ops == NULL || _libcglue_fdman_path_ops->symlink == NULL)
761 return __transform_errno(_libcglue_fdman_path_ops->symlink(dest_target, dest_linkpath));
766ssize_t readlink(
const char *path,
char *buf,
size_t bufsiz)
768 char dest[MAXNAMLEN + 1];
770 if(__path_absolute(path, dest, MAXNAMLEN) < 0) {
775 if (_libcglue_fdman_path_ops == NULL || _libcglue_fdman_path_ops->readlink == NULL)
781 return __transform_errno(_libcglue_fdman_path_ops->readlink(dest, buf, bufsiz));
786int utime(
const char *pathname,
const struct utimbuf *times)
795int fchown(
int fd, uid_t owner, gid_t group)
803ssize_t getrandom(
void *buf,
size_t buflen,
unsigned int flags)
807 arc4random_buf(buf, buflen);
813int _getentropy(
void *buf,
size_t buflen)
827 for (i = 0; i < buflen; i += 1)
830 buf_cur[i] = (u8)(cpu_ticks() & 0xff);
845int chmod(
const char *pathname, mode_t mode)
853int fchmod(
int fd, mode_t mode)
861long int pathconf(
const char *path,
int name)
877 return __dummy_passwd.pw_uid;
883 return __dummy_passwd.pw_uid;
888struct passwd *getpwuid(uid_t uid) {
890 return &__dummy_passwd;
895struct passwd *getpwnam(
const char *name) {
897 return &__dummy_passwd;
901#ifdef F_libcglue_get_fd_info
903 if (!__IS_FD_VALID(fd)) {
908 return &(__descriptormap[fd]->info);
912#ifdef F_ps2sdk_get_iop_fd
913int ps2sdk_get_iop_fd(
int fd)
916 fdinfo = libcglue_get_fd_info(fd);
921 if (fdinfo->ops == NULL || fdinfo->ops->getfd == NULL)
925 return fdinfo->ops->getfd(fdinfo->userdata);
929#ifdef F_ps2sdk_get_iop_filename
930char *ps2sdk_get_iop_filename(
int fd)
933 fdinfo = libcglue_get_fd_info(fd);
938 if (fdinfo->ops == NULL || fdinfo->ops->getfilename == NULL)
942 return fdinfo->ops->getfilename(fdinfo->userdata);
946#ifdef F__ps2sdk_close
947int _ps2sdk_close(
int fd)
950 fdinfo = libcglue_get_fd_info(fd);
955 if (fdinfo->ops == NULL || fdinfo->ops->close == NULL)
959 return fdinfo->ops->close(fdinfo->userdata);
963#ifdef F__ps2sdk_dclose
964int _ps2sdk_dclose(
int fd)
966 return _ps2sdk_close(fd);
971int _ps2sdk_read(
int fd,
void *buf,
int nbytes)
974 fdinfo = libcglue_get_fd_info(fd);
979 if (fdinfo->ops == NULL || fdinfo->ops->read == NULL)
983 return fdinfo->ops->read(fdinfo->userdata, buf, nbytes);
987#ifdef F__ps2sdk_lseek
988int _ps2sdk_lseek(
int fd,
int offset,
int whence)
991 fdinfo = libcglue_get_fd_info(fd);
996 if (fdinfo->ops == NULL || fdinfo->ops->lseek == NULL)
1000 return fdinfo->ops->lseek(fdinfo->userdata, offset, whence);
1004#ifdef F__ps2sdk_lseek64
1005int64_t _ps2sdk_lseek64(
int fd, int64_t offset,
int whence)
1008 fdinfo = libcglue_get_fd_info(fd);
1013 if (fdinfo->ops == NULL || fdinfo->ops->lseek64 == NULL)
1017 return fdinfo->ops->lseek64(fdinfo->userdata, offset, whence);
1021#ifdef F__ps2sdk_write
1022int _ps2sdk_write(
int fd,
const void *buf,
int nbytes)
1025 fdinfo = libcglue_get_fd_info(fd);
1030 if (fdinfo->ops == NULL || fdinfo->ops->write == NULL)
1034 return fdinfo->ops->write(fdinfo->userdata, buf, nbytes);
1038#ifdef F__ps2sdk_ioctl
1039int _ps2sdk_ioctl(
int fd,
int request,
void *data)
1042 fdinfo = libcglue_get_fd_info(fd);
1047 if (fdinfo->ops == NULL || fdinfo->ops->ioctl == NULL)
1051 return fdinfo->ops->ioctl(fdinfo->userdata,
request, data);
1055#ifdef F__ps2sdk_ioctl2
1056int _ps2sdk_ioctl2(
int fd,
int request,
void *arg,
unsigned int arglen,
void *buf,
unsigned int buflen)
1059 fdinfo = libcglue_get_fd_info(fd);
1064 if (fdinfo->ops == NULL || fdinfo->ops->ioctl == NULL)
1068 return fdinfo->ops->ioctl2(fdinfo->userdata,
request, arg, arglen, buf, buflen);
1072#ifdef F__ps2sdk_dread
1073int _ps2sdk_dread(
int fd,
struct dirent *dir)
1076 fdinfo = libcglue_get_fd_info(fd);
1081 if (fdinfo->ops == NULL || fdinfo->ops->dread == NULL)
1085 return fdinfo->ops->dread(fdinfo->userdata, dir);
1092int openat(
int dirfd,
const char *pathname,
int flags, ...)
1099 va_start(args, flags);
1102 int mode = va_arg(args,
int);
1106 return open(pathname, flags, mode);
1111int renameat(
int olddirfd,
const char *oldpath,
int newdirfd,
const char *newpath)
1115 return rename(oldpath, newpath);
1120int fchmodat(
int dirfd,
const char *pathname, mode_t mode,
int flags)
1124 return chmod(pathname, mode);
1129int fstatat(
int dirfd,
const char *pathname,
struct stat *buf,
int flags)
1133 return stat(pathname, buf);
1138int mkdirat(
int dirfd,
const char *pathname, mode_t mode)
1142 return mkdir(pathname, mode);
1147int faccessat(
int dirfd,
const char *pathname,
int mode,
int flags)
1151 return access(pathname, mode);
1156int fchownat(
int dirfd,
const char *pathname, uid_t owner, gid_t group,
int flags)
1160 return chown(pathname, owner, group);
1165int linkat(
int olddirfd,
const char *oldpath,
int newdirfd,
const char *newpath,
int flags) {
1168 return link(oldpath, newpath);
1173int readlinkat(
int dirfd,
const char *pathname,
char *buf,
size_t bufsiz)
1177 return readlink(pathname, buf, bufsiz);
1182int unlinkat(
int dirfd,
const char *pathname,
int flags)
1186 if (flags & AT_REMOVEDIR) {
1187 return rmdir(pathname);
1190 return unlink(pathname);
1198 if (!__IS_FD_VALID(oldfd)) {
1203 return __fdman_get_dup_descriptor(oldfd);
1208int dup2(
int oldfd,
int newfd)
1210 if (!__IS_FD_VALID(oldfd)) {
1215 if (oldfd == newfd) {
1222 if (__descriptormap[newfd]) {
1225 return __fdman_get_dup2_descriptor(oldfd, newfd);
u32 count
start sector of fragmented bd/file