35#include <sys/signal.h>
46#define NEWLIB_PORT_AWARE
53extern char __cwd[MAXNAMLEN + 1];
54extern size_t __cwd_len;
55int __path_absolute(
const char *in,
char *out,
int len);
59char __dummy_passwd_loginbuf[16];
61struct passwd __dummy_passwd;
63static void __dummy_passwd_init(
void)
65 strcpy(__dummy_passwd_loginbuf,
"ps2user");
66 __dummy_passwd.pw_name = &__dummy_passwd_loginbuf[0];
67 __dummy_passwd.pw_passwd =
"xxx";
68 __dummy_passwd.pw_uid = 1000;
69 __dummy_passwd.pw_gid = 1000;
70 __dummy_passwd.pw_comment =
"";
71 __dummy_passwd.pw_gecos =
"";
72 __dummy_passwd.pw_dir =
"/";
73 __dummy_passwd.pw_shell =
"";
76static char *__dummy_group_members[2];
77struct group __dummy_group;
80static void __dummy_group_init(
void)
82 __dummy_group.gr_name =
"ps2group";
83 __dummy_group.gr_passwd =
"xxx";
84 __dummy_group.gr_gid = 1000;
85 __dummy_group.gr_mem = &__dummy_group_members[0];
86 __dummy_group_members[0] = &__dummy_passwd_loginbuf[0];
87 __dummy_group_members[1] = NULL;
90int __transform_errno(
int res) {
99int64_t __transform64_errno(int64_t res) {
108#if INT_MAX != 0x7fffffffL
109 #error "INT_MAX != 0x7fffffffL"
112 #error "LONG_MAX not defined"
114#if LONG_MAX != 0x7fffffffL
115 #error "LONG_MAX != 0x7fffffffL"
118#define ct_assert(e) {enum { ct_assert_value = 1/(!!(e)) };}
119void compile_time_check() {
121 ct_assert(
sizeof(
unsigned char)==1);
122 ct_assert(
sizeof(
unsigned short)==2);
123 ct_assert(
sizeof(
unsigned int)==4);
124 ct_assert(
sizeof(
unsigned long)==4);
125 ct_assert(
sizeof(
unsigned long long)==8);
130 typedef unsigned int __glue_ti_check_t
__attribute__((mode(TI)));
131 ct_assert(
sizeof(__glue_ti_check_t)==16);
132 ct_assert(
sizeof(
void *)==4);
135 ct_assert(
sizeof(u8)==1);
136 ct_assert(
sizeof(u16)==2);
137 ct_assert(
sizeof(u32)==4);
138 ct_assert(
sizeof(u64)==8);
139 ct_assert(
sizeof(u128)==16);
142 ct_assert(
sizeof(uint8_t)==1);
143 ct_assert(
sizeof(uint16_t)==2);
144 ct_assert(
sizeof(uint32_t)==4);
145 ct_assert(
sizeof(uint64_t)==8);
149int _open(
const char *buf,
int flags, ...) {
152 _libcglue_fdman_fd_info_t *info;
154 char t_fname[MAXNAMLEN + 1];
156 va_start(alist, flags);
157 mode = va_arg(alist,
int);
160 if(__path_absolute(buf, t_fname,
sizeof(t_fname)) < 0) {
165 if (_libcglue_fdman_path_ops == NULL || _libcglue_fdman_path_ops->open == NULL)
171 fd = __fdman_get_new_descriptor();
178 info = &(__descriptormap[fd]->info);
179 iop_fd = _libcglue_fdman_path_ops->open(info, t_fname, flags, mode);
182 __fdman_release_descriptor(fd);
183 return __transform_errno(iop_fd);
185 __descriptormap[fd]->flags = flags;
190int pipe(
int fildes[2])
200 if (!__IS_FD_VALID(fd)) {
205 if (__descriptormap[fd]->ref_count == 1)
207 _libcglue_fdman_fd_info_t *fdinfo;
209 fdinfo = &(__descriptormap[fd]->info);
210 if (fdinfo->ops != NULL && fdinfo->ops->close != NULL)
212 ret = __transform_errno(fdinfo->ops->close(fdinfo->userdata));
215 __fdman_release_descriptor(fd);
220int _read(
int fd,
void *buf,
size_t nbytes) {
221 if (!__IS_FD_VALID(fd)) {
226 _libcglue_fdman_fd_info_t *fdinfo;
228 fdinfo = &(__descriptormap[fd]->info);
229 if (fdinfo->ops == NULL || fdinfo->ops->read == NULL)
234 return __transform_errno(fdinfo->ops->read(fdinfo->userdata, buf, nbytes));
238int _write(
int fd,
const void *buf,
size_t nbytes) {
239 if (!__IS_FD_VALID(fd)) {
244 _libcglue_fdman_fd_info_t *fdinfo;
246 fdinfo = &(__descriptormap[fd]->info);
247 if (fdinfo->ops == NULL || fdinfo->ops->write == NULL)
252 return __transform_errno(fdinfo->ops->write(fdinfo->userdata, buf, nbytes));
256int _stat(
const char *path,
struct stat *buf) {
257 char dest[MAXNAMLEN + 1];
259 if(__path_absolute(path, dest,
sizeof(dest)) < 0) {
264 if (_libcglue_fdman_path_ops == NULL || _libcglue_fdman_path_ops->stat == NULL)
270 return __transform_errno(_libcglue_fdman_path_ops->stat(dest, buf));
274int lstat(
const char *path,
struct stat *buf) {
275 return stat(path, buf);
279int _fstat(
int fd,
struct stat *buf) {
280 if (!__IS_FD_VALID(fd)) {
285 _libcglue_fdman_fd_info_t *fdinfo;
288 fdinfo = &(__descriptormap[fd]->info);
289 if (fdinfo->ops == NULL || fdinfo->ops->getfilename == NULL)
294 filename = fdinfo->ops->getfilename(fdinfo->userdata);
295 if (filename == NULL)
301 if (_libcglue_fdman_path_ops == NULL || _libcglue_fdman_path_ops->stat == NULL)
307 return __transform_errno(_libcglue_fdman_path_ops->stat(filename, buf));
311int access(
const char *fn,
int flags) {
315 if (s.st_mode & S_IFDIR)
318 if (s.st_mode & S_IWRITE)
327int _fcntl(
int fd,
int cmd, ...)
329 if (!__IS_FD_VALID(fd)) {
338 return __fdman_get_dup_descriptor(fd);
343 return __descriptormap[fd]->flags;
353 va_start (args, cmd);
354 newfl = va_arg(args,
int);
357 __descriptormap[fd]->flags = newfl;
360 _libcglue_fdman_fd_info_t *fdinfo;
362 fdinfo = &(__descriptormap[fd]->info);
364 if (fdinfo->ops != NULL && fdinfo->ops->fcntl_f_setfl != NULL)
366 rv = __transform_errno(fdinfo->ops->fcntl_f_setfl(fdinfo->userdata, newfl));
376 va_start (args, cmd);
377 newfl = va_arg(args,
int);
380 __descriptormap[fd]->flags = newfl;
393int _ioctl(
int fd,
int request,
void *data) {
394 _libcglue_fdman_fd_info_t *fdinfo;
395 fdinfo = libcglue_get_fd_info(fd);
401 if (fdinfo->ops == NULL || fdinfo->ops->ioctl == NULL)
406 return __transform_errno(fdinfo->ops->ioctl(fdinfo->userdata,
request, data));
410int getdents(
int fd,
void *dd_buf,
int count)
416 dirp = (
struct dirent *)dd_buf;
419 _libcglue_fdman_fd_info_t *fdinfo;
421 fdinfo = &(__descriptormap[fd]->info);
423 if (fdinfo->ops != NULL && fdinfo->ops->dread != NULL)
425 rv = __transform_errno(fdinfo->ops->dread(fdinfo->userdata, dirp));
429 return __transform_errno(rv);
430 }
else if (rv == 0) {
434 read +=
sizeof(
struct dirent);
435 dirp->d_reclen =
count;
441off_t _lseek(
int fd, off_t offset,
int whence)
443 if (!__IS_FD_VALID(fd)) {
448 _libcglue_fdman_fd_info_t *fdinfo;
450 fdinfo = &(__descriptormap[fd]->info);
451 if (fdinfo->ops == NULL || fdinfo->ops->lseek == NULL)
456 return __transform_errno(fdinfo->ops->lseek(fdinfo->userdata, offset, whence));
459off64_t lseek64(
int fd, off64_t offset,
int whence)
461 if (!__IS_FD_VALID(fd)) {
466 _libcglue_fdman_fd_info_t *fdinfo;
468 fdinfo = &(__descriptormap[fd]->info);
469 if (fdinfo->ops == NULL || fdinfo->ops->lseek64 == NULL)
474 return __transform64_errno(fdinfo->ops->lseek64(fdinfo->userdata, offset, whence));
477int chdir(
const char *path) {
478 char dest[MAXNAMLEN + 1];
480 if(__path_absolute(path, dest,
sizeof(dest)) < 0) {
485 __cwd_len = snprintf(__cwd,
sizeof(__cwd),
"%.*s",
sizeof(dest) - 1, dest);
486 __cwd_len = (__cwd_len > (
sizeof(__cwd) - 1)) ? (
sizeof(__cwd) - 1) : __cwd_len;
497int _mkdir(
const char *path, mode_t mode)
499 char dest[MAXNAMLEN + 1];
501 if(__path_absolute(path, dest,
sizeof(dest)) < 0) {
506 if (_libcglue_fdman_path_ops == NULL || _libcglue_fdman_path_ops->mkdir == NULL)
512 return __transform_errno(_libcglue_fdman_path_ops->mkdir(dest, mode));
515int mkdir(
const char *path, mode_t mode)
517 return _mkdir(path, mode);
520int rmdir(
const char *path) {
521 char dest[MAXNAMLEN + 1];
523 if(__path_absolute(path, dest,
sizeof(dest)) < 0) {
528 if (_libcglue_fdman_path_ops == NULL || _libcglue_fdman_path_ops->rmdir == NULL)
534 return __transform_errno(_libcglue_fdman_path_ops->rmdir(dest));
538int _link(
const char *old,
const char *new_) {
544int _unlink(
const char *path) {
545 char dest[MAXNAMLEN + 1];
546 if(__path_absolute(path, dest,
sizeof(dest)) < 0) {
551 if (_libcglue_fdman_path_ops == NULL || _libcglue_fdman_path_ops->remove == NULL)
557 return __transform_errno(_libcglue_fdman_path_ops->remove(dest));
561int _rename(
const char *old,
const char *new_) {
562 char oldname[MAXNAMLEN + 1];
563 char newname[MAXNAMLEN + 1];
565 if(__path_absolute(old, oldname,
sizeof(oldname)) < 0) {
570 if(__path_absolute(new_, newname,
sizeof(newname)) < 0) {
575 if (_libcglue_fdman_path_ops == NULL || _libcglue_fdman_path_ops->rename == NULL)
581 return __transform_errno(_libcglue_fdman_path_ops->rename(oldname, newname));
590int getitimer(
int which,
struct itimerval *value)
596int setitimer(
int which,
const struct itimerval *value,
struct itimerval *ovalue)
609 return GetThreadId();
619int _kill(
int pid,
int sig) {
623 return pid == getpid() ? 0 : -1;
625 if (pid == getpid()) {
638int sigprocmask(
int how,
const sigset_t *set, sigset_t *oset)
644int sigaction(
int sig,
const struct sigaction *act,
struct sigaction *oact)
664pid_t _wait(
int *unused) {
670pid_t waitpid(pid_t pid,
int *stat_loc,
int options)
677int _execve(
const char *name,
char *
const argv[],
char *
const env[]) {
682int _system(
const char *command)
691void * _sbrk(
size_t incr) {
692 static void * _heap_ptr = &_end;
693 void *mp, *ret = (
void *)-1;
699 mp = _heap_ptr + incr;
700 if (mp <= EndOfHeap()) {
709int _gettimeofday(
struct timeval *tv,
struct timezone *tz)
722 tv->tv_sec = (time_t)(_libcglue_rtc_get_offset_from_busclk() + ((s64)busclock_sec));
723 tv->tv_usec = busclock_usec;
728 tz->tz_minuteswest = _timezone / 60;
736clock_t _times(
struct tms *buffer) {
742 clk = busclock_sec * CLOCKS_PER_SEC + busclock_usec;
744 if (buffer != NULL) {
745 buffer->tms_utime = clk;
746 buffer->tms_stime = 0;
747 buffer->tms_cutime = 0;
748 buffer->tms_cstime = 0;
754int ftime(
struct timeb *tb) {
758 gettimeofday(&tv, &tz);
760 tb->time = tv.tv_sec;
761 tb->millitm = tv.tv_usec / 1000;
762 tb->timezone = tz.tz_minuteswest;
763 tb->dstflag = tz.tz_dsttime;
768int clock_getres(clockid_t clk_id,
struct timespec *res) {
772 ret = gettimeofday(&tv, NULL);
775 res->tv_sec = tv.tv_sec;
776 res->tv_nsec = tv.tv_usec * 1000;
781int clock_gettime(clockid_t clk_id,
struct timespec *tp) {
785 res = gettimeofday(&tv, NULL);
788 tp->tv_sec = tv.tv_sec;
789 tp->tv_nsec = tv.tv_usec * 1000;
794int clock_settime(clockid_t clk_id,
const struct timespec *tp) {
801ssize_t readv(
int fd,
const struct iovec *iov,
int iovcnt)
807 for (i = 0; i < iovcnt; i += 1)
811 size_cur = read(fd, iov[i].iov_base, iov[i].iov_len);
814 size_sum += size_cur;
815 if (size_cur != iov[i].iov_len)
822ssize_t writev(
int fd,
const struct iovec *iov,
int iovcnt)
828 for (i = 0; i < iovcnt; i += 1)
832 size_cur = write(fd, iov[i].iov_base, iov[i].iov_len);
835 size_sum += size_cur;
836 if (size_cur != iov[i].iov_len)
842int truncate(
const char *path, off_t length)
848 fd = open(path, O_RDONLY);
854 bytes_read = read(fd, &buff, length);
856 if (bytes_read < length) {
861 fd = open (path, O_TRUNC|O_WRONLY);
867 res = write(fd, &buff, length);
872int ftruncate(
int fd, off_t length)
878int _symlink(
const char *target,
const char *linkpath)
880 char dest_target[MAXNAMLEN + 1];
881 char dest_linkpath[MAXNAMLEN + 1];
883 if(__path_absolute(target, dest_target,
sizeof(dest_target)) < 0) {
888 if(__path_absolute(linkpath, dest_linkpath,
sizeof(dest_linkpath)) < 0) {
893 if (_libcglue_fdman_path_ops == NULL || _libcglue_fdman_path_ops->symlink == NULL)
899 return __transform_errno(_libcglue_fdman_path_ops->symlink(dest_target, dest_linkpath));
902int symlink(
const char *target,
const char *linkpath)
904 return _symlink(target, linkpath);
907ssize_t _readlink(
const char *path,
char *buf,
size_t bufsiz)
909 char dest[MAXNAMLEN + 1];
911 if(__path_absolute(path, dest,
sizeof(dest)) < 0) {
916 if (_libcglue_fdman_path_ops == NULL || _libcglue_fdman_path_ops->readlink == NULL)
922 return __transform_errno(_libcglue_fdman_path_ops->readlink(dest, buf, bufsiz));
925ssize_t readlink(
const char *path,
char *buf,
size_t bufsiz)
927 return _readlink(path, buf, bufsiz);
930int utime(
const char *pathname,
const struct utimbuf *times)
937int utimes(
const char *filename,
const struct timeval times[2])
944int fchown(
int fd, uid_t owner, gid_t group)
950ssize_t getrandom(
void *buf,
size_t buflen,
unsigned int flags)
954 arc4random_buf(buf, buflen);
959int _getentropy(
void *buf,
size_t buflen)
973 for (i = 0; i < buflen; i += 1)
976 buf_cur[i] = (u8)(cpu_ticks() & 0xff);
981mode_t umask(mode_t mask)
986int chmod(
const char *pathname, mode_t mode)
992int fchmod(
int fd, mode_t mode)
998int _chown(
const char *path, uid_t owner, gid_t group)
1004int chown(
const char *path, uid_t owner, gid_t group)
1006 return _chown(path, owner, group);
1009static long __pathconf_value(
int name)
1022long pathconf(
const char *path,
int name)
1024 return __pathconf_value(name);
1027long fpathconf(
int fd,
int name)
1029 return __pathconf_value(name);
1037long sysconf(
int name)
1043int tcgetattr(
int fd,
struct termios *tp)
1049int tcsetattr(
int fd,
int opts,
const struct termios *tp)
1058 return __dummy_passwd.pw_name;
1064 return __dummy_passwd.pw_uid;
1069 return __dummy_passwd.pw_uid;
1074 return __dummy_passwd.pw_gid;
1079 return __dummy_passwd.pw_gid;
1083struct passwd *getpwuid(uid_t uid) {
1085 return &__dummy_passwd;
1089struct passwd *getpwnam(
const char *name) {
1091 return &__dummy_passwd;
1100struct group *getgrgid(gid_t gid)
1102 return &__dummy_group;
1105struct group *getgrnam(
const char *nam)
1107 return &__dummy_group;
1110_libcglue_fdman_fd_info_t *libcglue_get_fd_info(
int fd) {
1111 if (!__IS_FD_VALID(fd)) {
1116 return &(__descriptormap[fd]->info);
1119int ps2sdk_get_iop_fd(
int fd)
1121 _libcglue_fdman_fd_info_t *fdinfo;
1122 fdinfo = libcglue_get_fd_info(fd);
1127 if (fdinfo->ops == NULL || fdinfo->ops->getfd == NULL)
1131 return fdinfo->ops->getfd(fdinfo->userdata);
1134char *ps2sdk_get_iop_filename(
int fd)
1136 _libcglue_fdman_fd_info_t *fdinfo;
1137 fdinfo = libcglue_get_fd_info(fd);
1142 if (fdinfo->ops == NULL || fdinfo->ops->getfilename == NULL)
1146 return fdinfo->ops->getfilename(fdinfo->userdata);
1149int _ps2sdk_close(
int fd)
1151 _libcglue_fdman_fd_info_t *fdinfo;
1152 fdinfo = libcglue_get_fd_info(fd);
1157 if (fdinfo->ops == NULL || fdinfo->ops->close == NULL)
1161 return fdinfo->ops->close(fdinfo->userdata);
1164int _ps2sdk_dclose(
int fd)
1166 return _ps2sdk_close(fd);
1169int _ps2sdk_read(
int fd,
void *buf,
int nbytes)
1171 _libcglue_fdman_fd_info_t *fdinfo;
1172 fdinfo = libcglue_get_fd_info(fd);
1177 if (fdinfo->ops == NULL || fdinfo->ops->read == NULL)
1181 return fdinfo->ops->read(fdinfo->userdata, buf, nbytes);
1184int _ps2sdk_lseek(
int fd,
int offset,
int whence)
1186 _libcglue_fdman_fd_info_t *fdinfo;
1187 fdinfo = libcglue_get_fd_info(fd);
1192 if (fdinfo->ops == NULL || fdinfo->ops->lseek == NULL)
1196 return fdinfo->ops->lseek(fdinfo->userdata, offset, whence);
1199int64_t _ps2sdk_lseek64(
int fd, int64_t offset,
int whence)
1201 _libcglue_fdman_fd_info_t *fdinfo;
1202 fdinfo = libcglue_get_fd_info(fd);
1207 if (fdinfo->ops == NULL || fdinfo->ops->lseek64 == NULL)
1211 return fdinfo->ops->lseek64(fdinfo->userdata, offset, whence);
1214int _ps2sdk_write(
int fd,
const void *buf,
int nbytes)
1216 _libcglue_fdman_fd_info_t *fdinfo;
1217 fdinfo = libcglue_get_fd_info(fd);
1222 if (fdinfo->ops == NULL || fdinfo->ops->write == NULL)
1226 return fdinfo->ops->write(fdinfo->userdata, buf, nbytes);
1229int _ps2sdk_ioctl(
int fd,
int request,
void *data)
1231 _libcglue_fdman_fd_info_t *fdinfo;
1232 fdinfo = libcglue_get_fd_info(fd);
1237 if (fdinfo->ops == NULL || fdinfo->ops->ioctl == NULL)
1241 return fdinfo->ops->ioctl(fdinfo->userdata,
request, data);
1244int _ps2sdk_ioctl2(
int fd,
int request,
void *arg,
unsigned int arglen,
void *buf,
unsigned int buflen)
1246 _libcglue_fdman_fd_info_t *fdinfo;
1247 fdinfo = libcglue_get_fd_info(fd);
1252 if (fdinfo->ops == NULL || fdinfo->ops->ioctl == NULL)
1256 return fdinfo->ops->ioctl2(fdinfo->userdata,
request, arg, arglen, buf, buflen);
1259int _ps2sdk_dread(
int fd,
struct dirent *dir)
1261 _libcglue_fdman_fd_info_t *fdinfo;
1262 fdinfo = libcglue_get_fd_info(fd);
1267 if (fdinfo->ops == NULL || fdinfo->ops->dread == NULL)
1271 return fdinfo->ops->dread(fdinfo->userdata, dir);
1276int openat(
int dirfd,
const char *pathname,
int flags, ...)
1283 va_start(args, flags);
1286 int mode = va_arg(args,
int);
1290 return open(pathname, flags, mode);
1293int renameat(
int olddirfd,
const char *oldpath,
int newdirfd,
const char *newpath)
1297 return rename(oldpath, newpath);
1300int fchmodat(
int dirfd,
const char *pathname, mode_t mode,
int flags)
1304 return chmod(pathname, mode);
1307int fstatat(
int dirfd,
const char *pathname,
struct stat *buf,
int flags)
1311 return stat(pathname, buf);
1314int mkdirat(
int dirfd,
const char *pathname, mode_t mode)
1318 return mkdir(pathname, mode);
1321int faccessat(
int dirfd,
const char *pathname,
int mode,
int flags)
1325 return access(pathname, mode);
1328int fchownat(
int dirfd,
const char *pathname, uid_t owner, gid_t group,
int flags)
1332 return chown(pathname, owner, group);
1335int linkat(
int olddirfd,
const char *oldpath,
int newdirfd,
const char *newpath,
int flags) {
1338 return link(oldpath, newpath);
1341int readlinkat(
int dirfd,
const char *pathname,
char *buf,
size_t bufsiz)
1345 return readlink(pathname, buf, bufsiz);
1348int unlinkat(
int dirfd,
const char *pathname,
int flags)
1352 if (flags & AT_REMOVEDIR) {
1353 return rmdir(pathname);
1356 return unlink(pathname);
1362 if (!__IS_FD_VALID(oldfd)) {
1367 return __fdman_get_dup_descriptor(oldfd);
1371int dup2(
int oldfd,
int newfd)
1373 if (!__IS_FD_VALID(oldfd)) {
1378 if (oldfd == newfd) {
1385 if (__descriptormap[newfd]) {
1388 return __fdman_get_dup2_descriptor(oldfd, newfd);
u64 GetTimerSystemTime(void)
u32 count
start sector of fragmented bd/file