27 #include <sys/timeb.h>
28 #include <sys/times.h>
29 #include <sys/utime.h>
32 #include <sys/signal.h>
33 #include <sys/types.h>
34 #include <sys/random.h>
44 #define NEWLIB_PORT_AWARE
46 #include "ps2sdkapi.h"
47 #include "timer_alarm.h"
51 extern char __cwd[MAXNAMLEN + 1];
52 extern size_t __cwd_len;
53 int __path_absolute(
const char *in,
char *out,
int len);
57 #ifdef F___dummy_passwd
58 char __dummy_passwd_loginbuf[16];
60 struct passwd __dummy_passwd;
62 static void __dummy_passwd_init(
void)
64 strcpy(__dummy_passwd_loginbuf,
"ps2user");
65 __dummy_passwd.pw_name = &__dummy_passwd_loginbuf[0];
66 __dummy_passwd.pw_passwd =
"xxx";
67 __dummy_passwd.pw_uid = 1000;
68 __dummy_passwd.pw_gid = 1000;
69 __dummy_passwd.pw_comment =
"";
70 __dummy_passwd.pw_gecos =
"";
71 __dummy_passwd.pw_dir =
"/";
72 __dummy_passwd.pw_shell =
"";
75 extern char __dummy_passwd_loginbuf[16];
76 extern struct passwd __dummy_passwd;
79 #ifdef F___dummy_group
80 static char *__dummy_group_members[2];
81 struct group __dummy_group;
84 static void __dummy_group_init(
void)
86 __dummy_group.gr_name =
"ps2group";
87 __dummy_group.gr_passwd =
"xxx";
88 __dummy_group.gr_gid = 1000;
89 __dummy_group.gr_mem = &__dummy_group_members[0];
90 __dummy_group_members[0] = &__dummy_passwd_loginbuf[0];
91 __dummy_group_members[1] = NULL;
94 extern struct group __dummy_group;
97 #ifdef F___transform_errno
98 int __transform_errno(
int res) {
107 int __transform_errno(
int res);
110 #ifdef F___transform64_errno
111 int64_t __transform64_errno(int64_t res) {
120 int64_t __transform64_errno(int64_t res);
123 #if INT_MAX != 0x7fffffffL
124 #error "INT_MAX != 0x7fffffffL"
127 #error "LONG_MAX not defined"
129 #if LONG_MAX != 0x7fffffffL
130 #error "LONG_MAX != 0x7fffffffL"
133 #define ct_assert(e) {enum { ct_assert_value = 1/(!!(e)) };}
134 #ifdef F_compile_time_check
135 void compile_time_check() {
137 ct_assert(
sizeof(
unsigned char)==1);
138 ct_assert(
sizeof(
unsigned short)==2);
139 ct_assert(
sizeof(
unsigned int)==4);
140 ct_assert(
sizeof(
unsigned long)==4);
141 ct_assert(
sizeof(
unsigned long long)==8);
142 ct_assert(
sizeof(
unsigned int __attribute__(( mode(TI) )))==16);
143 ct_assert(
sizeof(
void *)==4);
146 ct_assert(
sizeof(u8)==1);
147 ct_assert(
sizeof(u16)==2);
148 ct_assert(
sizeof(u32)==4);
149 ct_assert(
sizeof(u64)==8);
150 ct_assert(
sizeof(u128)==16);
153 ct_assert(
sizeof(uint8_t)==1);
154 ct_assert(
sizeof(uint16_t)==2);
155 ct_assert(
sizeof(uint32_t)==4);
156 ct_assert(
sizeof(uint64_t)==8);
162 int _open(
const char *buf,
int flags, ...) {
167 char t_fname[MAXNAMLEN + 1];
169 va_start(alist, flags);
170 mode = va_arg(alist,
int);
173 if(__path_absolute(buf, t_fname,
sizeof(t_fname)) < 0) {
178 if (_libcglue_fdman_path_ops == NULL || _libcglue_fdman_path_ops->open == NULL)
184 fd = __fdman_get_new_descriptor();
191 info = &(__descriptormap[fd]->info);
192 iop_fd = _libcglue_fdman_path_ops->open(
info, t_fname, flags, mode);
195 __fdman_release_descriptor(fd);
196 return __transform_errno(iop_fd);
198 __descriptormap[fd]->flags = flags;
205 int pipe(
int fildes[2])
217 if (!__IS_FD_VALID(fd)) {
222 if (__descriptormap[fd]->ref_count == 1)
226 fdinfo = &(__descriptormap[fd]->info);
227 if (fdinfo->ops != NULL && fdinfo->ops->close != NULL)
229 ret = __transform_errno(fdinfo->ops->close(fdinfo->userdata));
232 __fdman_release_descriptor(fd);
239 int _read(
int fd,
void *buf,
size_t nbytes) {
240 if (!__IS_FD_VALID(fd)) {
247 fdinfo = &(__descriptormap[fd]->info);
248 if (fdinfo->ops == NULL || fdinfo->ops->read == NULL)
253 return __transform_errno(fdinfo->ops->read(fdinfo->userdata, buf, nbytes));
259 int _write(
int fd,
const void *buf,
size_t nbytes) {
260 if (!__IS_FD_VALID(fd)) {
267 fdinfo = &(__descriptormap[fd]->info);
268 if (fdinfo->ops == NULL || fdinfo->ops->write == NULL)
273 return __transform_errno(fdinfo->ops->write(fdinfo->userdata, buf, nbytes));
279 int _stat(
const char *path,
struct stat *buf) {
280 char dest[MAXNAMLEN + 1];
282 if(__path_absolute(path, dest,
sizeof(dest)) < 0) {
287 if (_libcglue_fdman_path_ops == NULL || _libcglue_fdman_path_ops->stat == NULL)
293 return __transform_errno(_libcglue_fdman_path_ops->stat(dest, buf));
299 int lstat(
const char *path,
struct stat *buf) {
300 return stat(path, buf);
306 int _fstat(
int fd,
struct stat *buf) {
307 if (!__IS_FD_VALID(fd)) {
315 fdinfo = &(__descriptormap[fd]->info);
316 if (fdinfo->ops == NULL || fdinfo->ops->getfilename == NULL)
321 filename = fdinfo->ops->getfilename(fdinfo->userdata);
322 if (filename == NULL)
328 if (_libcglue_fdman_path_ops == NULL || _libcglue_fdman_path_ops->stat == NULL)
334 return __transform_errno(_libcglue_fdman_path_ops->stat(filename, buf));
340 int access(
const char *fn,
int flags) {
344 if (s.st_mode & S_IFDIR)
347 if (s.st_mode & S_IWRITE)
358 int _fcntl(
int fd,
int cmd, ...)
360 if (!__IS_FD_VALID(fd)) {
369 return __fdman_get_dup_descriptor(fd);
374 return __descriptormap[fd]->flags;
384 va_start (args, cmd);
385 newfl = va_arg(args,
int);
388 __descriptormap[fd]->flags = newfl;
393 fdinfo = &(__descriptormap[fd]->info);
395 if (fdinfo->ops != NULL && fdinfo->ops->fcntl_f_setfl != NULL)
397 rv = __transform_errno(fdinfo->ops->fcntl_f_setfl(fdinfo->userdata, newfl));
407 va_start (args, cmd);
408 newfl = va_arg(args,
int);
411 __descriptormap[fd]->flags = newfl;
426 int _ioctl(
int fd,
int request,
void *data) {
428 fdinfo = libcglue_get_fd_info(fd);
434 if (fdinfo->ops == NULL || fdinfo->ops->ioctl == NULL)
439 return __transform_errno(fdinfo->ops->ioctl(fdinfo->userdata,
request, data));
445 int getdents(
int fd,
void *dd_buf,
int count)
451 dirp = (
struct dirent *)dd_buf;
456 fdinfo = &(__descriptormap[fd]->info);
458 if (fdinfo->ops != NULL && fdinfo->ops->dread != NULL)
460 rv = __transform_errno(fdinfo->ops->dread(fdinfo->userdata, dirp));
464 return __transform_errno(rv);
465 }
else if (rv == 0) {
469 read +=
sizeof(
struct dirent);
470 dirp->d_reclen =
count;
478 off_t _lseek(
int fd, off_t offset,
int whence)
480 if (!__IS_FD_VALID(fd)) {
487 fdinfo = &(__descriptormap[fd]->info);
488 if (fdinfo->ops == NULL || fdinfo->ops->lseek == NULL)
493 return __transform_errno(fdinfo->ops->lseek(fdinfo->userdata, offset, whence));
498 off64_t lseek64(
int fd, off64_t offset,
int whence)
500 if (!__IS_FD_VALID(fd)) {
507 fdinfo = &(__descriptormap[fd]->info);
508 if (fdinfo->ops == NULL || fdinfo->ops->lseek64 == NULL)
513 return __transform64_errno(fdinfo->ops->lseek64(fdinfo->userdata, offset, whence));
518 int chdir(
const char *path) {
519 char dest[MAXNAMLEN + 1];
521 if(__path_absolute(path, dest,
sizeof(dest)) < 0) {
526 __cwd_len = snprintf(__cwd,
sizeof(__cwd),
"%.*s",
sizeof(dest) - 1, dest);
527 __cwd_len = (__cwd_len > (
sizeof(__cwd) - 1)) ? (
sizeof(__cwd) - 1) : __cwd_len;
542 int _mkdir(
const char *path, mode_t mode)
544 char dest[MAXNAMLEN + 1];
546 if(__path_absolute(path, dest,
sizeof(dest)) < 0) {
551 if (_libcglue_fdman_path_ops == NULL || _libcglue_fdman_path_ops->mkdir == NULL)
557 return __transform_errno(_libcglue_fdman_path_ops->mkdir(dest, mode));
560 int _mkdir(
const char *path, mode_t mode);
564 int mkdir(
const char *path, mode_t mode)
566 return _mkdir(path, mode);
569 int mkdir(
const char *path, mode_t mode);
573 int rmdir(
const char *path) {
574 char dest[MAXNAMLEN + 1];
576 if(__path_absolute(path, dest,
sizeof(dest)) < 0) {
581 if (_libcglue_fdman_path_ops == NULL || _libcglue_fdman_path_ops->rmdir == NULL)
587 return __transform_errno(_libcglue_fdman_path_ops->rmdir(dest));
593 int _link(
const char *old,
const char *new_) {
601 int _unlink(
const char *path) {
602 char dest[MAXNAMLEN + 1];
603 if(__path_absolute(path, dest,
sizeof(dest)) < 0) {
608 if (_libcglue_fdman_path_ops == NULL || _libcglue_fdman_path_ops->remove == NULL)
614 return __transform_errno(_libcglue_fdman_path_ops->remove(dest));
620 int _rename(
const char *old,
const char *new_) {
621 char oldname[MAXNAMLEN + 1];
622 char newname[MAXNAMLEN + 1];
624 if(__path_absolute(old, oldname,
sizeof(oldname)) < 0) {
629 if(__path_absolute(new_, newname,
sizeof(newname)) < 0) {
634 if (_libcglue_fdman_path_ops == NULL || _libcglue_fdman_path_ops->rename == NULL)
640 return __transform_errno(_libcglue_fdman_path_ops->rename(oldname, newname));
653 int getitimer(
int which,
struct itimerval *value)
661 int setitimer(
int which,
const struct itimerval *value,
struct itimerval *ovalue)
669 int sched_yield(
void)
678 return GetThreadId();
692 int _kill(
int pid,
int sig) {
693 #if 0 // needs to be tested first
696 return pid == getpid() ? 0 : -1;
698 if (pid == getpid()) {
713 int sigprocmask(
int how,
const sigset_t *set, sigset_t *oset)
721 int sigaction(
int sig,
const struct sigaction *act,
struct sigaction *oact)
747 pid_t _wait(
int *unused) {
755 pid_t waitpid(pid_t pid,
int *stat_loc,
int options)
764 int _execve(
const char *name,
char *
const argv[],
char *
const env[]) {
771 int _system(
const char *command)
782 void * _sbrk(
size_t incr) {
783 static void * _heap_ptr = &_end;
784 void *mp, *ret = (
void *)-1;
790 mp = _heap_ptr + incr;
791 if (mp <= EndOfHeap()) {
800 #ifdef F__gettimeofday
802 int _gettimeofday(
struct timeval *tv,
struct timezone *tz)
814 TimerBusClock2USec(GetTimerSystemTime(), &busclock_sec, &busclock_usec);
815 tv->tv_sec = (time_t)(_libcglue_rtc_get_offset_from_busclk() + ((s64)busclock_sec));
816 tv->tv_usec = busclock_usec;
821 tz->tz_minuteswest = _timezone / 60;
831 clock_t _times(
struct tms *buffer) {
836 TimerBusClock2USec(GetTimerSystemTime(), &busclock_sec, &busclock_usec);
837 clk = busclock_sec * CLOCKS_PER_SEC + busclock_usec;
839 if (buffer != NULL) {
840 buffer->tms_utime = clk;
841 buffer->tms_stime = 0;
842 buffer->tms_cutime = 0;
843 buffer->tms_cstime = 0;
851 int ftime(
struct timeb *tb) {
855 gettimeofday(&tv, &tz);
857 tb->time = tv.tv_sec;
858 tb->millitm = tv.tv_usec / 1000;
859 tb->timezone = tz.tz_minuteswest;
860 tb->dstflag = tz.tz_dsttime;
866 #ifdef F_clock_getres
867 int clock_getres(clockid_t clk_id,
struct timespec *res) {
871 ret = gettimeofday(&tv, NULL);
874 res->tv_sec = tv.tv_sec;
875 res->tv_nsec = tv.tv_usec * 1000;
881 #ifdef F_clock_gettime
882 int clock_gettime(clockid_t clk_id,
struct timespec *tp) {
886 res = gettimeofday(&tv, NULL);
889 tp->tv_sec = tv.tv_sec;
890 tp->tv_nsec = tv.tv_usec * 1000;
896 #ifdef F_clock_settime
897 int clock_settime(clockid_t clk_id,
const struct timespec *tp) {
906 ssize_t readv(
int fd,
const struct iovec *iov,
int iovcnt)
912 for (i = 0; i < iovcnt; i += 1)
916 size_cur = read(fd, iov[i].iov_base, iov[i].iov_len);
919 size_sum += size_cur;
920 if (size_cur != iov[i].iov_len)
929 ssize_t writev(
int fd,
const struct iovec *iov,
int iovcnt)
935 for (i = 0; i < iovcnt; i += 1)
939 size_cur = write(fd, iov[i].iov_base, iov[i].iov_len);
942 size_sum += size_cur;
943 if (size_cur != iov[i].iov_len)
951 int truncate(
const char *path, off_t length)
957 fd = open(path, O_RDONLY);
963 bytes_read = read(fd, &buff, length);
965 if (bytes_read < length) {
970 fd = open (path, O_TRUNC|O_WRONLY);
976 res = write(fd, &buff, length);
983 int ftruncate(
int fd, off_t length)
991 int _symlink(
const char *target,
const char *linkpath)
993 char dest_target[MAXNAMLEN + 1];
994 char dest_linkpath[MAXNAMLEN + 1];
996 if(__path_absolute(target, dest_target,
sizeof(dest_target)) < 0) {
1001 if(__path_absolute(linkpath, dest_linkpath,
sizeof(dest_linkpath)) < 0) {
1006 if (_libcglue_fdman_path_ops == NULL || _libcglue_fdman_path_ops->symlink == NULL)
1012 return __transform_errno(_libcglue_fdman_path_ops->symlink(dest_target, dest_linkpath));
1015 int _symlink(
const char *target,
const char *linkpath);
1019 int symlink(
const char *target,
const char *linkpath)
1021 return _symlink(target, linkpath);
1026 ssize_t _readlink(
const char *path,
char *buf,
size_t bufsiz)
1028 char dest[MAXNAMLEN + 1];
1030 if(__path_absolute(path, dest,
sizeof(dest)) < 0) {
1035 if (_libcglue_fdman_path_ops == NULL || _libcglue_fdman_path_ops->readlink == NULL)
1041 return __transform_errno(_libcglue_fdman_path_ops->readlink(dest, buf, bufsiz));
1044 ssize_t _readlink(
const char *path,
char *buf,
size_t bufsiz);
1048 ssize_t readlink(
const char *path,
char *buf,
size_t bufsiz)
1050 return _readlink(path, buf, bufsiz);
1055 int utime(
const char *pathname,
const struct utimbuf *times)
1064 int utimes(
const char *filename,
const struct timeval times[2])
1073 int fchown(
int fd, uid_t owner, gid_t group)
1081 ssize_t getrandom(
void *buf,
size_t buflen,
unsigned int flags)
1085 arc4random_buf(buf, buflen);
1090 #ifdef F__getentropy
1092 int _getentropy(
void *buf,
size_t buflen)
1106 for (i = 0; i < buflen; i += 1)
1109 buf_cur[i] = (u8)(cpu_ticks() & 0xff);
1116 mode_t umask(mode_t mask)
1123 int chmod(
const char *pathname, mode_t mode)
1131 int fchmod(
int fd, mode_t mode)
1139 int _chown(
const char *path, uid_t owner, gid_t group)
1145 int _chown(
const char *path, uid_t owner, gid_t group);
1149 int chown(
const char *path, uid_t owner, gid_t group)
1151 return _chown(path, owner, group);
1156 long pathconf(
const char *path,
int name)
1164 long fpathconf(
int fd,
int name)
1179 long sysconf(
int name)
1187 int tcgetattr(
int fd,
struct termios *tp)
1195 int tcsetattr(
int fd,
int opts,
const struct termios *tp)
1204 char *getlogin(
void)
1206 return __dummy_passwd.pw_name;
1214 return __dummy_passwd.pw_uid;
1221 return __dummy_passwd.pw_uid;
1228 return __dummy_passwd.pw_gid;
1235 return __dummy_passwd.pw_gid;
1241 struct passwd *getpwuid(uid_t uid) {
1243 return &__dummy_passwd;
1249 struct passwd *getpwnam(
const char *name) {
1251 return &__dummy_passwd;
1264 struct group *getgrgid(gid_t gid)
1266 return &__dummy_group;
1271 struct group *getgrnam(
const char *nam)
1273 return &__dummy_group;
1277 #ifdef F_libcglue_get_fd_info
1279 if (!__IS_FD_VALID(fd)) {
1284 return &(__descriptormap[fd]->info);
1288 #ifdef F_ps2sdk_get_iop_fd
1289 int ps2sdk_get_iop_fd(
int fd)
1292 fdinfo = libcglue_get_fd_info(fd);
1297 if (fdinfo->ops == NULL || fdinfo->ops->getfd == NULL)
1301 return fdinfo->ops->getfd(fdinfo->userdata);
1305 #ifdef F_ps2sdk_get_iop_filename
1306 char *ps2sdk_get_iop_filename(
int fd)
1309 fdinfo = libcglue_get_fd_info(fd);
1314 if (fdinfo->ops == NULL || fdinfo->ops->getfilename == NULL)
1318 return fdinfo->ops->getfilename(fdinfo->userdata);
1322 #ifdef F__ps2sdk_close
1323 int _ps2sdk_close(
int fd)
1326 fdinfo = libcglue_get_fd_info(fd);
1331 if (fdinfo->ops == NULL || fdinfo->ops->close == NULL)
1335 return fdinfo->ops->close(fdinfo->userdata);
1339 #ifdef F__ps2sdk_dclose
1340 int _ps2sdk_dclose(
int fd)
1342 return _ps2sdk_close(fd);
1346 #ifdef F__ps2sdk_read
1347 int _ps2sdk_read(
int fd,
void *buf,
int nbytes)
1350 fdinfo = libcglue_get_fd_info(fd);
1355 if (fdinfo->ops == NULL || fdinfo->ops->read == NULL)
1359 return fdinfo->ops->read(fdinfo->userdata, buf, nbytes);
1363 #ifdef F__ps2sdk_lseek
1364 int _ps2sdk_lseek(
int fd,
int offset,
int whence)
1367 fdinfo = libcglue_get_fd_info(fd);
1372 if (fdinfo->ops == NULL || fdinfo->ops->lseek == NULL)
1376 return fdinfo->ops->lseek(fdinfo->userdata, offset, whence);
1380 #ifdef F__ps2sdk_lseek64
1381 int64_t _ps2sdk_lseek64(
int fd, int64_t offset,
int whence)
1384 fdinfo = libcglue_get_fd_info(fd);
1389 if (fdinfo->ops == NULL || fdinfo->ops->lseek64 == NULL)
1393 return fdinfo->ops->lseek64(fdinfo->userdata, offset, whence);
1397 #ifdef F__ps2sdk_write
1398 int _ps2sdk_write(
int fd,
const void *buf,
int nbytes)
1401 fdinfo = libcglue_get_fd_info(fd);
1406 if (fdinfo->ops == NULL || fdinfo->ops->write == NULL)
1410 return fdinfo->ops->write(fdinfo->userdata, buf, nbytes);
1414 #ifdef F__ps2sdk_ioctl
1415 int _ps2sdk_ioctl(
int fd,
int request,
void *data)
1418 fdinfo = libcglue_get_fd_info(fd);
1423 if (fdinfo->ops == NULL || fdinfo->ops->ioctl == NULL)
1427 return fdinfo->ops->ioctl(fdinfo->userdata,
request, data);
1431 #ifdef F__ps2sdk_ioctl2
1432 int _ps2sdk_ioctl2(
int fd,
int request,
void *arg,
unsigned int arglen,
void *buf,
unsigned int buflen)
1435 fdinfo = libcglue_get_fd_info(fd);
1440 if (fdinfo->ops == NULL || fdinfo->ops->ioctl == NULL)
1444 return fdinfo->ops->ioctl2(fdinfo->userdata,
request, arg, arglen, buf, buflen);
1448 #ifdef F__ps2sdk_dread
1449 int _ps2sdk_dread(
int fd,
struct dirent *dir)
1452 fdinfo = libcglue_get_fd_info(fd);
1457 if (fdinfo->ops == NULL || fdinfo->ops->dread == NULL)
1461 return fdinfo->ops->dread(fdinfo->userdata, dir);
1468 int openat(
int dirfd,
const char *pathname,
int flags, ...)
1475 va_start(args, flags);
1478 int mode = va_arg(args,
int);
1482 return open(pathname, flags, mode);
1487 int renameat(
int olddirfd,
const char *oldpath,
int newdirfd,
const char *newpath)
1491 return rename(oldpath, newpath);
1496 int fchmodat(
int dirfd,
const char *pathname, mode_t mode,
int flags)
1500 return chmod(pathname, mode);
1505 int fstatat(
int dirfd,
const char *pathname,
struct stat *buf,
int flags)
1509 return stat(pathname, buf);
1514 int mkdirat(
int dirfd,
const char *pathname, mode_t mode)
1518 return mkdir(pathname, mode);
1523 int faccessat(
int dirfd,
const char *pathname,
int mode,
int flags)
1527 return access(pathname, mode);
1532 int fchownat(
int dirfd,
const char *pathname, uid_t owner, gid_t group,
int flags)
1536 return chown(pathname, owner, group);
1541 int linkat(
int olddirfd,
const char *oldpath,
int newdirfd,
const char *newpath,
int flags) {
1544 return link(oldpath, newpath);
1549 int readlinkat(
int dirfd,
const char *pathname,
char *buf,
size_t bufsiz)
1553 return readlink(pathname, buf, bufsiz);
1558 int unlinkat(
int dirfd,
const char *pathname,
int flags)
1562 if (flags & AT_REMOVEDIR) {
1563 return rmdir(pathname);
1566 return unlink(pathname);
1574 if (!__IS_FD_VALID(oldfd)) {
1579 return __fdman_get_dup_descriptor(oldfd);
1585 int dup2(
int oldfd,
int newfd)
1587 if (!__IS_FD_VALID(oldfd)) {
1592 if (oldfd == newfd) {
1599 if (__descriptormap[newfd]) {
1602 return __fdman_get_dup2_descriptor(oldfd, newfd);