32#include <sys/signal.h>
34#include <sys/random.h>
44#define NEWLIB_PORT_AWARE
47#include "timer_alarm.h"
51extern char __cwd[MAXNAMLEN + 1];
52extern size_t __cwd_len;
53int __path_absolute(
const char *in,
char *out,
int len);
57#ifdef F___dummy_passwd
58char __dummy_passwd_loginbuf[16] =
"ps2user";
60struct passwd __dummy_passwd = { &__dummy_passwd_loginbuf[0],
"xxx", 1000, 1000,
"",
"",
"/",
"" };
62extern char __dummy_passwd_loginbuf[16];
63extern struct passwd __dummy_passwd;
67static char *__dummy_group_members[2] = {&__dummy_passwd_loginbuf[0], NULL};
68struct group __dummy_group = {
"ps2group",
"xxx", 1000, &__dummy_group_members[0]};
70extern struct group __dummy_group;
73#ifdef F___transform_errno
74int __transform_errno(
int res) {
83int __transform_errno(
int res);
86#ifdef F___transform64_errno
87int64_t __transform64_errno(int64_t res) {
96int64_t __transform64_errno(int64_t res);
99#if INT_MAX != 0x7fffffffL
100 #error "INT_MAX != 0x7fffffffL"
103 #error "LONG_MAX not defined"
105#if LONG_MAX != 0x7fffffffL
106 #error "LONG_MAX != 0x7fffffffL"
109#define ct_assert(e) {enum { ct_assert_value = 1/(!!(e)) };}
110#ifdef F_compile_time_check
111void compile_time_check() {
113 ct_assert(
sizeof(
unsigned char)==1);
114 ct_assert(
sizeof(
unsigned short)==2);
115 ct_assert(
sizeof(
unsigned int)==4);
116 ct_assert(
sizeof(
unsigned long)==4);
117 ct_assert(
sizeof(
unsigned long long)==8);
118 ct_assert(
sizeof(
unsigned int __attribute__(( mode(TI) )))==16);
119 ct_assert(
sizeof(
void *)==4);
122 ct_assert(
sizeof(u8)==1);
123 ct_assert(
sizeof(u16)==2);
124 ct_assert(
sizeof(u32)==4);
125 ct_assert(
sizeof(u64)==8);
126 ct_assert(
sizeof(u128)==16);
129 ct_assert(
sizeof(uint8_t)==1);
130 ct_assert(
sizeof(uint16_t)==2);
131 ct_assert(
sizeof(uint32_t)==4);
132 ct_assert(
sizeof(uint64_t)==8);
138int _open(
const char *buf,
int flags, ...) {
143 char t_fname[MAXNAMLEN + 1];
145 va_start(alist, flags);
146 mode = va_arg(alist,
int);
149 if(__path_absolute(buf, t_fname, MAXNAMLEN) < 0) {
154 if (_libcglue_fdman_path_ops == NULL || _libcglue_fdman_path_ops->open == NULL)
160 fd = __fdman_get_new_descriptor();
167 info = &(__descriptormap[fd]->info);
168 iop_fd = _libcglue_fdman_path_ops->open(
info, t_fname, flags, mode);
171 __fdman_release_descriptor(fd);
172 return __transform_errno(iop_fd);
174 __descriptormap[fd]->flags = flags;
181int pipe(
int fildes[2])
193 if (!__IS_FD_VALID(fd)) {
198 if (__descriptormap[fd]->ref_count == 1)
202 fdinfo = &(__descriptormap[fd]->info);
203 if (fdinfo->ops != NULL && fdinfo->ops->close != NULL)
205 ret = __transform_errno(fdinfo->ops->close(fdinfo->userdata));
208 __fdman_release_descriptor(fd);
215int _read(
int fd,
void *buf,
size_t nbytes) {
216 if (!__IS_FD_VALID(fd)) {
223 fdinfo = &(__descriptormap[fd]->info);
224 if (fdinfo->ops == NULL || fdinfo->ops->read == NULL)
229 return __transform_errno(fdinfo->ops->read(fdinfo->userdata, buf, nbytes));
235int _write(
int fd,
const void *buf,
size_t nbytes) {
236 if (!__IS_FD_VALID(fd)) {
243 fdinfo = &(__descriptormap[fd]->info);
244 if (fdinfo->ops == NULL || fdinfo->ops->write == NULL)
249 return __transform_errno(fdinfo->ops->write(fdinfo->userdata, buf, nbytes));
255int _stat(
const char *path,
struct stat *buf) {
256 char dest[MAXNAMLEN + 1];
258 if(__path_absolute(path, dest, MAXNAMLEN) < 0) {
263 if (_libcglue_fdman_path_ops == NULL || _libcglue_fdman_path_ops->stat == NULL)
269 return __transform_errno(_libcglue_fdman_path_ops->stat(dest, buf));
275int lstat(
const char *path,
struct stat *buf) {
276 return stat(path, buf);
282int _fstat(
int fd,
struct stat *buf) {
283 if (!__IS_FD_VALID(fd)) {
291 fdinfo = &(__descriptormap[fd]->info);
292 if (fdinfo->ops == NULL || fdinfo->ops->getfilename == NULL)
297 filename = fdinfo->ops->getfilename(fdinfo->userdata);
298 if (filename == NULL)
304 if (_libcglue_fdman_path_ops == NULL || _libcglue_fdman_path_ops->stat == NULL)
310 return __transform_errno(_libcglue_fdman_path_ops->stat(filename, buf));
316int access(
const char *fn,
int flags) {
320 if (s.st_mode & S_IFDIR)
323 if (s.st_mode & S_IWRITE)
334int _fcntl(
int fd,
int cmd, ...)
336 if (!__IS_FD_VALID(fd)) {
345 return __fdman_get_dup_descriptor(fd);
350 return __descriptormap[fd]->flags;
360 va_start (args, cmd);
361 newfl = va_arg(args,
int);
364 __descriptormap[fd]->flags = newfl;
369 fdinfo = &(__descriptormap[fd]->info);
371 if (fdinfo->ops != NULL && fdinfo->ops->fcntl_f_setfl != NULL)
373 rv = __transform_errno(fdinfo->ops->fcntl_f_setfl(fdinfo->userdata, newfl));
383 va_start (args, cmd);
384 newfl = va_arg(args,
int);
387 __descriptormap[fd]->flags = newfl;
402int _ioctl(
int fd,
int request,
void *data) {
404 fdinfo = libcglue_get_fd_info(fd);
410 if (fdinfo->ops == NULL || fdinfo->ops->ioctl == NULL)
415 return __transform_errno(fdinfo->ops->ioctl(fdinfo->userdata,
request, data));
421int getdents(
int fd,
void *dd_buf,
int count)
427 dirp = (
struct dirent *)dd_buf;
432 fdinfo = &(__descriptormap[fd]->info);
434 if (fdinfo->ops != NULL && fdinfo->ops->dread != NULL)
436 rv = __transform_errno(fdinfo->ops->dread(fdinfo->userdata, dirp));
440 return __transform_errno(rv);
441 }
else if (rv == 0) {
445 read +=
sizeof(
struct dirent);
446 dirp->d_reclen =
count;
454off_t _lseek(
int fd, off_t offset,
int whence)
456 if (!__IS_FD_VALID(fd)) {
463 fdinfo = &(__descriptormap[fd]->info);
464 if (fdinfo->ops == NULL || fdinfo->ops->lseek == NULL)
469 return __transform_errno(fdinfo->ops->lseek(fdinfo->userdata, offset, whence));
474off64_t lseek64(
int fd, off64_t offset,
int whence)
476 if (!__IS_FD_VALID(fd)) {
483 fdinfo = &(__descriptormap[fd]->info);
484 if (fdinfo->ops == NULL || fdinfo->ops->lseek64 == NULL)
489 return __transform64_errno(fdinfo->ops->lseek64(fdinfo->userdata, offset, whence));
494int chdir(
const char *path) {
495 char dest[MAXNAMLEN + 1];
497 if(__path_absolute(path, dest, MAXNAMLEN) < 0) {
502 strncpy(__cwd, dest,
sizeof(__cwd));
503 __cwd_len = strnlen(__cwd,
sizeof(__cwd));
518int _mkdir(
const char *path, mode_t mode)
520 char dest[MAXNAMLEN + 1];
522 if(__path_absolute(path, dest, MAXNAMLEN) < 0) {
527 if (_libcglue_fdman_path_ops == NULL || _libcglue_fdman_path_ops->mkdir == NULL)
533 return __transform_errno(_libcglue_fdman_path_ops->mkdir(dest, mode));
536int _mkdir(
const char *path, mode_t mode);
540int mkdir(
const char *path, mode_t mode)
542 return _mkdir(path, mode);
545int mkdir(
const char *path, mode_t mode);
549int rmdir(
const char *path) {
550 char dest[MAXNAMLEN + 1];
552 if(__path_absolute(path, dest, MAXNAMLEN) < 0) {
557 if (_libcglue_fdman_path_ops == NULL || _libcglue_fdman_path_ops->rmdir == NULL)
563 return __transform_errno(_libcglue_fdman_path_ops->rmdir(dest));
569int _link(
const char *old,
const char *
new) {
577int _unlink(
const char *path) {
578 char dest[MAXNAMLEN + 1];
579 if(__path_absolute(path, dest, MAXNAMLEN) < 0) {
584 if (_libcglue_fdman_path_ops == NULL || _libcglue_fdman_path_ops->remove == NULL)
590 return __transform_errno(_libcglue_fdman_path_ops->remove(dest));
596int _rename(
const char *old,
const char *
new) {
597 char oldname[MAXNAMLEN + 1];
598 char newname[MAXNAMLEN + 1];
600 if(__path_absolute(old, oldname, MAXNAMLEN) < 0) {
605 if(__path_absolute(
new, newname, MAXNAMLEN) < 0) {
610 if (_libcglue_fdman_path_ops == NULL || _libcglue_fdman_path_ops->rename == NULL)
616 return __transform_errno(_libcglue_fdman_path_ops->rename(oldname, newname));
629int getitimer(
int which,
struct itimerval *value)
637int setitimer(
int which,
const struct itimerval *value,
struct itimerval *ovalue)
654 return GetThreadId();
668int _kill(
int pid,
int sig) {
672 return pid == getpid() ? 0 : -1;
674 if (pid == getpid()) {
689int sigprocmask(
int how,
const sigset_t *set, sigset_t *oset)
697int sigaction(
int sig,
const struct sigaction *act,
struct sigaction *oact)
723pid_t _wait(
int *unused) {
731pid_t waitpid(pid_t pid,
int *stat_loc,
int options)
740int _execve(
const char *name,
char *
const argv[],
char *
const env[]) {
747int _system(
const char *command)
758void * _sbrk(
size_t incr) {
759 static void * _heap_ptr = &_end;
760 void *mp, *ret = (
void *)-1;
766 mp = _heap_ptr + incr;
767 if (mp <= EndOfHeap()) {
776#ifdef F__gettimeofday
778int _gettimeofday(
struct timeval *tv,
struct timezone *tz)
790 TimerBusClock2USec(GetTimerSystemTime(), &busclock_sec, &busclock_usec);
791 tv->tv_sec = (time_t)(_ps2sdk_rtc_offset_from_busclk + ((s64)busclock_sec));
792 tv->tv_usec = busclock_usec;
797 tz->tz_minuteswest = _timezone / 60;
807clock_t _times(
struct tms *buffer) {
812 TimerBusClock2USec(GetTimerSystemTime(), &busclock_sec, &busclock_usec);
813 clk = busclock_sec * CLOCKS_PER_SEC + busclock_usec;
815 if (buffer != NULL) {
816 buffer->tms_utime = clk;
817 buffer->tms_stime = 0;
818 buffer->tms_cutime = 0;
819 buffer->tms_cstime = 0;
827int ftime(
struct timeb *tb) {
831 gettimeofday(&tv, &tz);
833 tb->time = tv.tv_sec;
834 tb->millitm = tv.tv_usec / 1000;
835 tb->timezone = tz.tz_minuteswest;
836 tb->dstflag = tz.tz_dsttime;
843int clock_getres(clockid_t clk_id,
struct timespec *res) {
847 ret = gettimeofday(&tv, NULL);
850 res->tv_sec = tv.tv_sec;
851 res->tv_nsec = tv.tv_usec * 1000;
857#ifdef F_clock_gettime
858int clock_gettime(clockid_t clk_id,
struct timespec *tp) {
862 res = gettimeofday(&tv, NULL);
865 tp->tv_sec = tv.tv_sec;
866 tp->tv_nsec = tv.tv_usec * 1000;
872#ifdef F_clock_settime
873int clock_settime(clockid_t clk_id,
const struct timespec *tp) {
882ssize_t readv(
int fd,
const struct iovec *iov,
int iovcnt)
888 for (i = 0; i < iovcnt; i += 1)
892 size_cur = read(fd, iov[i].iov_base, iov[i].iov_len);
895 size_sum += size_cur;
896 if (size_cur != iov[i].iov_len)
905ssize_t writev(
int fd,
const struct iovec *iov,
int iovcnt)
911 for (i = 0; i < iovcnt; i += 1)
915 size_cur = write(fd, iov[i].iov_base, iov[i].iov_len);
918 size_sum += size_cur;
919 if (size_cur != iov[i].iov_len)
927int truncate(
const char *path, off_t length)
933 fd = open(path, O_RDONLY);
939 bytes_read = read(fd, &buff, length);
941 if (bytes_read < length) {
946 fd = open (path, O_TRUNC|O_WRONLY);
952 res = write(fd, &buff, length);
959int ftruncate(
int fd, off_t length)
967int _symlink(
const char *target,
const char *linkpath)
969 char dest_target[MAXNAMLEN + 1];
970 char dest_linkpath[MAXNAMLEN + 1];
972 if(__path_absolute(target, dest_target, MAXNAMLEN) < 0) {
977 if(__path_absolute(linkpath, dest_linkpath, MAXNAMLEN) < 0) {
982 if (_libcglue_fdman_path_ops == NULL || _libcglue_fdman_path_ops->symlink == NULL)
988 return __transform_errno(_libcglue_fdman_path_ops->symlink(dest_target, dest_linkpath));
991int _symlink(
const char *target,
const char *linkpath);
995int symlink(
const char *target,
const char *linkpath)
997 return _symlink(target, linkpath);
1002ssize_t _readlink(
const char *path,
char *buf,
size_t bufsiz)
1004 char dest[MAXNAMLEN + 1];
1006 if(__path_absolute(path, dest, MAXNAMLEN) < 0) {
1011 if (_libcglue_fdman_path_ops == NULL || _libcglue_fdman_path_ops->readlink == NULL)
1017 return __transform_errno(_libcglue_fdman_path_ops->readlink(dest, buf, bufsiz));
1020ssize_t _readlink(
const char *path,
char *buf,
size_t bufsiz);
1024ssize_t readlink(
const char *path,
char *buf,
size_t bufsiz)
1026 return _readlink(path, buf, bufsiz);
1031int utime(
const char *pathname,
const struct utimbuf *times)
1040int utimes(
const char *filename,
const struct timeval times[2])
1049int fchown(
int fd, uid_t owner, gid_t group)
1057ssize_t getrandom(
void *buf,
size_t buflen,
unsigned int flags)
1061 arc4random_buf(buf, buflen);
1068int _getentropy(
void *buf,
size_t buflen)
1082 for (i = 0; i < buflen; i += 1)
1085 buf_cur[i] = (u8)(cpu_ticks() & 0xff);
1092mode_t umask(mode_t mask)
1099int chmod(
const char *pathname, mode_t mode)
1107int fchmod(
int fd, mode_t mode)
1115int _chown(
const char *path, uid_t owner, gid_t group)
1121int _chown(
const char *path, uid_t owner, gid_t group);
1125int chown(
const char *path, uid_t owner, gid_t group)
1127 return _chown(path, owner, group);
1132long pathconf(
const char *path,
int name)
1140long fpathconf(
int fd,
int name)
1155long sysconf(
int name)
1163int tcgetattr(
int fd,
struct termios *tp)
1171int tcsetattr(
int fd,
int opts,
const struct termios *tp)
1182 return __dummy_passwd.pw_name;
1190 return __dummy_passwd.pw_uid;
1197 return __dummy_passwd.pw_uid;
1204 return __dummy_passwd.pw_gid;
1211 return __dummy_passwd.pw_gid;
1217struct passwd *getpwuid(uid_t uid) {
1219 return &__dummy_passwd;
1225struct passwd *getpwnam(
const char *name) {
1227 return &__dummy_passwd;
1240struct group *getgrgid(gid_t gid)
1242 return &__dummy_group;
1247struct group *getgrnam(
const char *nam)
1249 return &__dummy_group;
1253#ifdef F_libcglue_get_fd_info
1255 if (!__IS_FD_VALID(fd)) {
1260 return &(__descriptormap[fd]->info);
1264#ifdef F_ps2sdk_get_iop_fd
1265int ps2sdk_get_iop_fd(
int fd)
1268 fdinfo = libcglue_get_fd_info(fd);
1273 if (fdinfo->ops == NULL || fdinfo->ops->getfd == NULL)
1277 return fdinfo->ops->getfd(fdinfo->userdata);
1281#ifdef F_ps2sdk_get_iop_filename
1282char *ps2sdk_get_iop_filename(
int fd)
1285 fdinfo = libcglue_get_fd_info(fd);
1290 if (fdinfo->ops == NULL || fdinfo->ops->getfilename == NULL)
1294 return fdinfo->ops->getfilename(fdinfo->userdata);
1298#ifdef F__ps2sdk_close
1299int _ps2sdk_close(
int fd)
1302 fdinfo = libcglue_get_fd_info(fd);
1307 if (fdinfo->ops == NULL || fdinfo->ops->close == NULL)
1311 return fdinfo->ops->close(fdinfo->userdata);
1315#ifdef F__ps2sdk_dclose
1316int _ps2sdk_dclose(
int fd)
1318 return _ps2sdk_close(fd);
1322#ifdef F__ps2sdk_read
1323int _ps2sdk_read(
int fd,
void *buf,
int nbytes)
1326 fdinfo = libcglue_get_fd_info(fd);
1331 if (fdinfo->ops == NULL || fdinfo->ops->read == NULL)
1335 return fdinfo->ops->read(fdinfo->userdata, buf, nbytes);
1339#ifdef F__ps2sdk_lseek
1340int _ps2sdk_lseek(
int fd,
int offset,
int whence)
1343 fdinfo = libcglue_get_fd_info(fd);
1348 if (fdinfo->ops == NULL || fdinfo->ops->lseek == NULL)
1352 return fdinfo->ops->lseek(fdinfo->userdata, offset, whence);
1356#ifdef F__ps2sdk_lseek64
1357int64_t _ps2sdk_lseek64(
int fd, int64_t offset,
int whence)
1360 fdinfo = libcglue_get_fd_info(fd);
1365 if (fdinfo->ops == NULL || fdinfo->ops->lseek64 == NULL)
1369 return fdinfo->ops->lseek64(fdinfo->userdata, offset, whence);
1373#ifdef F__ps2sdk_write
1374int _ps2sdk_write(
int fd,
const void *buf,
int nbytes)
1377 fdinfo = libcglue_get_fd_info(fd);
1382 if (fdinfo->ops == NULL || fdinfo->ops->write == NULL)
1386 return fdinfo->ops->write(fdinfo->userdata, buf, nbytes);
1390#ifdef F__ps2sdk_ioctl
1391int _ps2sdk_ioctl(
int fd,
int request,
void *data)
1394 fdinfo = libcglue_get_fd_info(fd);
1399 if (fdinfo->ops == NULL || fdinfo->ops->ioctl == NULL)
1403 return fdinfo->ops->ioctl(fdinfo->userdata,
request, data);
1407#ifdef F__ps2sdk_ioctl2
1408int _ps2sdk_ioctl2(
int fd,
int request,
void *arg,
unsigned int arglen,
void *buf,
unsigned int buflen)
1411 fdinfo = libcglue_get_fd_info(fd);
1416 if (fdinfo->ops == NULL || fdinfo->ops->ioctl == NULL)
1420 return fdinfo->ops->ioctl2(fdinfo->userdata,
request, arg, arglen, buf, buflen);
1424#ifdef F__ps2sdk_dread
1425int _ps2sdk_dread(
int fd,
struct dirent *dir)
1428 fdinfo = libcglue_get_fd_info(fd);
1433 if (fdinfo->ops == NULL || fdinfo->ops->dread == NULL)
1437 return fdinfo->ops->dread(fdinfo->userdata, dir);
1444int openat(
int dirfd,
const char *pathname,
int flags, ...)
1451 va_start(args, flags);
1454 int mode = va_arg(args,
int);
1458 return open(pathname, flags, mode);
1463int renameat(
int olddirfd,
const char *oldpath,
int newdirfd,
const char *newpath)
1467 return rename(oldpath, newpath);
1472int fchmodat(
int dirfd,
const char *pathname, mode_t mode,
int flags)
1476 return chmod(pathname, mode);
1481int fstatat(
int dirfd,
const char *pathname,
struct stat *buf,
int flags)
1485 return stat(pathname, buf);
1490int mkdirat(
int dirfd,
const char *pathname, mode_t mode)
1494 return mkdir(pathname, mode);
1499int faccessat(
int dirfd,
const char *pathname,
int mode,
int flags)
1503 return access(pathname, mode);
1508int fchownat(
int dirfd,
const char *pathname, uid_t owner, gid_t group,
int flags)
1512 return chown(pathname, owner, group);
1517int linkat(
int olddirfd,
const char *oldpath,
int newdirfd,
const char *newpath,
int flags) {
1520 return link(oldpath, newpath);
1525int readlinkat(
int dirfd,
const char *pathname,
char *buf,
size_t bufsiz)
1529 return readlink(pathname, buf, bufsiz);
1534int unlinkat(
int dirfd,
const char *pathname,
int flags)
1538 if (flags & AT_REMOVEDIR) {
1539 return rmdir(pathname);
1542 return unlink(pathname);
1550 if (!__IS_FD_VALID(oldfd)) {
1555 return __fdman_get_dup_descriptor(oldfd);
1561int dup2(
int oldfd,
int newfd)
1563 if (!__IS_FD_VALID(oldfd)) {
1568 if (oldfd == newfd) {
1575 if (__descriptormap[newfd]) {
1578 return __fdman_get_dup2_descriptor(oldfd, newfd);
u32 count
start sector of fragmented bd/file