(ps2 compat files) Cleanups

This commit is contained in:
twinaphex 2020-02-14 13:16:21 +01:00
parent 2d946d0d95
commit ebd3e65f46
4 changed files with 229 additions and 215 deletions

View File

@ -40,10 +40,9 @@ int islower(int c)
int tolower(int ch) int tolower(int ch)
{ {
if(ch >= 'A' && ch <= 'Z') if(ch >= 'A' && ch <= 'Z')
return ('a' + ch - 'A'); return ('a' + ch - 'A');
else return ch;
return ch;
} }
int toupper(int c) int toupper(int c)
@ -107,16 +106,17 @@ char * strcat(char *dest, const char *src)
char *strchr(const char *string, int c) char *strchr(const char *string, int c)
{ {
while (*string) { while (*string)
if (*string == c) {
return (char *)string; if (*string == c)
string++; return (char *)string;
} string++;
}
if (*string == c) if (*string == c)
return (char *)string; return (char *)string;
return NULL; return NULL;
} }
int strcmp(const char *s1, const char *s2) int strcmp(const char *s1, const char *s2)
@ -144,14 +144,16 @@ size_t strcspn(const char *s1, const char *s2)
* Stop as soon as we find any character from s2. Note that there * Stop as soon as we find any character from s2. Note that there
* must be a NUL in s2; it suffices to stop when we find that, too. * must be a NUL in s2; it suffices to stop when we find that, too.
*/ */
for (p = s1;;) { for (p = s1;;)
c = *p++; {
spanp = s2; c = *p++;
do { spanp = s2;
if ((sc = *spanp++) == c) do
return (p - 1 - s1); {
} while (sc != 0); if ((sc = *spanp++) == c)
} return (p - 1 - s1);
}while(sc != 0);
}
/* NOTREACHED */ /* NOTREACHED */
} }
@ -166,51 +168,59 @@ size_t strlen(const char *str)
char * strncat(char *dst, const char *src, size_t n) char * strncat(char *dst, const char *src, size_t n)
{ {
if (n != 0) { if (n != 0)
char *d = dst; {
const char *s = src; char *d = dst;
const char *s = src;
while (*d != 0) while (*d != 0)
d++; d++;
do { do
if ((*d = *s++) == 0) {
break; if ((*d = *s++) == 0)
d++; break;
} while (--n != 0); d++;
*d = 0; }while(--n != 0);
} *d = 0;
return (dst); }
return (dst);
} }
int strncmp(const char *s1, const char *s2, size_t n) int strncmp(const char *s1, const char *s2, size_t n)
{ {
if (n == 0) if (n == 0)
return (0); return (0);
do {
do
{
if (*s1 != *s2++) if (*s1 != *s2++)
return (*(unsigned char *)s1 - *(unsigned char *)--s2); return (*(unsigned char *)s1 - *(unsigned char *)--s2);
if (*s1++ == 0) if (*s1++ == 0)
break; break;
} while (--n != 0); }while (--n != 0);
return (0); return (0);
} }
char * strncpy(char *dst, const char *src, size_t n) char * strncpy(char *dst, const char *src, size_t n)
{ {
if (n != 0) { if (n != 0)
char *d = dst; {
const char *s = src; char *d = dst;
const char *s = src;
do { do
if ((*d++ = *s++) == 0) { {
/* NUL pad the remaining n-1 bytes */ if ((*d++ = *s++) == 0)
while (--n != 0) {
*d++ = 0; /* NUL pad the remaining n-1 bytes */
break; while (--n != 0)
} *d++ = 0;
} while (--n != 0); break;
} }
return (dst); }while(--n != 0);
}
return (dst);
} }
char * strpbrk(const char *s1, const char *s2) char * strpbrk(const char *s1, const char *s2)
@ -218,11 +228,12 @@ char * strpbrk(const char *s1, const char *s2)
const char *scanp; const char *scanp;
int c, sc; int c, sc;
while ((c = *s1++) != 0) { while ((c = *s1++) != 0)
for (scanp = s2; (sc = *scanp++) != 0;) {
if (sc == c) for (scanp = s2; (sc = *scanp++) != 0;)
return ((char *)(s1 - 1)); if (sc == c)
} return ((char *)(s1 - 1));
}
return (NULL); return (NULL);
} }
@ -231,13 +242,13 @@ char * strrchr(const char *p, int ch)
{ {
char *save; char *save;
for (save = NULL;; ++p) { for (save = NULL;; ++p)
if (*p == (char) ch) {
save = (char *)p; if (*p == (char) ch)
if (!*p) save = (char *)p;
return(save); if (!*p)
} return(save);
/* NOTREACHED */ }
} }
size_t strspn(const char *s1, const char *s2) size_t strspn(const char *s1, const char *s2)
@ -268,11 +279,12 @@ char *strstr(const char *string, const char *substring)
strpos = (char *)string; strpos = (char *)string;
while (*strpos != 0) { while (*strpos != 0)
if (strncmp(strpos, substring, strlen(substring)) == 0) {
return strpos; if (strncmp(strpos, substring, strlen(substring)) == 0)
return strpos;
strpos++; strpos++;
} }
return 0; return 0;
@ -280,123 +292,132 @@ char *strstr(const char *string, const char *substring)
size_t strnlen(const char *str, size_t maxlen) size_t strnlen(const char *str, size_t maxlen)
{ {
const char *cp; const char *cp;
for (cp = str; maxlen != 0 && *cp != '\0'; cp++, maxlen--) for (cp = str; maxlen != 0 && *cp != '\0'; cp++, maxlen--)
; ;
return (size_t)(cp - str); return (size_t)(cp - str);
} }
char *strtok(char *strToken, const char *strDelimit) char *strtok(char *strToken, const char *strDelimit)
{ {
static char *start; static char *start;
static char *end; static char *end;
if (strToken != NULL) if (strToken)
start = strToken; start = strToken;
else { else
if (*end == 0) {
return 0; if (*end == 0)
return 0;
start = end; start = end;
} }
if (*start == 0) if (*start == 0)
return 0; return 0;
// Strip out any leading delimiters /* Strip out any leading delimiters */
while (strchr(strDelimit, *start)) { while (strchr(strDelimit, *start))
// If a character from the delimiting string {
// then skip past it /* If a character from the delimiting string
* then skip past it */
start++;
start++; if (*start == 0)
return 0;
}
if (*start == 0) if (*start == 0)
return 0; return 0;
}
if (*start == 0) end = start;
return 0;
end = start; while (*end != 0)
{
if (strchr(strDelimit, *end))
{
/* if we find a delimiting character
* before the end of the string, then
* terminate the token and move the end
* pointer to the next character
*/
*end = 0;
end++;
return start;
}
end++;
}
while (*end != 0) { /* reached the end of the string before finding a delimiter
if (strchr(strDelimit, *end)) { * so dont move on to the next character */
// if we find a delimiting character return start;
// before the end of the string, then
// terminate the token and move the end
// pointer to the next character
*end = 0;
end++;
return start;
}
end++;
}
// reached the end of the string before finding a delimiter
// so dont move on to the next character
return start;
} }
char * strtok_r (char *s, const char *delim, char **save_ptr) char * strtok_r (char *s, const char *delim, char **save_ptr)
{ {
char *end; char *end;
if (s == NULL) if (!s)
s = *save_ptr; s = *save_ptr;
if (*s == '\0') if (*s == '\0')
{ {
*save_ptr = s; *save_ptr = s;
return NULL; return NULL;
} }
/* Scan leading delimiters. */ /* Scan leading delimiters. */
s += strspn (s, delim); s += strspn (s, delim);
if (*s == '\0') if (*s == '\0')
{ {
*save_ptr = s; *save_ptr = s;
return NULL; return NULL;
} }
/* Find the end of the token. */ /* Find the end of the token. */
end = s + strcspn (s, delim); end = s + strcspn (s, delim);
if (*end == '\0') if (*end == '\0')
{ {
*save_ptr = end; *save_ptr = end;
return s; return s;
} }
/* Terminate the token and make *SAVE_PTR point past it. */ /* Terminate the token and make *SAVE_PTR point past it. */
*end = '\0'; *end = '\0';
*save_ptr = end + 1; *save_ptr = end + 1;
return s; return s;
} }
unsigned long long strtoull(const char * __restrict nptr, char ** __restrict endptr, int base) unsigned long long strtoull(const char * __restrict nptr,
char ** __restrict endptr, int base)
{ {
const char *s;
unsigned long long acc;
char c; char c;
unsigned long long acc;
unsigned long long cutoff; unsigned long long cutoff;
int neg, any, cutlim; int neg, any, cutlim;
/* /*
* See strtoq for comments as to the logic used. * See strtoq for comments as to the logic used.
*/ */
s = nptr; const char *s = nptr;
do {
do
{
c = *s++; c = *s++;
} while (isspace((unsigned char)c)); }while(isspace((unsigned char)c));
if (c == '-') { if (c == '-')
{
neg = 1; neg = 1;
c = *s++; c = *s++;
} else { }
else
{
neg = 0; neg = 0;
if (c == '+') if (c == '+')
c = *s++; c = *s++;
} }
if ((base == 0 || base == 16) && if ((base == 0 || base == 16) &&
c == '0' && (*s == 'x' || *s == 'X')) { c == '0' && (*s == 'x' || *s == 'X'))
c = s[1]; {
s += 2; c = s[1];
base = 16; s += 2;
base = 16;
} }
if (base == 0) if (base == 0)
base = c == '0' ? 8 : 10; base = c == '0' ? 8 : 10;
@ -406,34 +427,40 @@ unsigned long long strtoull(const char * __restrict nptr, char ** __restrict end
cutoff = ULLONG_MAX / base; cutoff = ULLONG_MAX / base;
cutlim = ULLONG_MAX % base; cutlim = ULLONG_MAX % base;
for ( ; ; c = *s++) { for ( ; ; c = *s++)
if (c >= '0' && c <= '9') {
c -= '0'; if (c >= '0' && c <= '9')
else if (c >= 'A' && c <= 'Z') c -= '0';
c -= 'A' - 10; else if (c >= 'A' && c <= 'Z')
else if (c >= 'a' && c <= 'z') c -= 'A' - 10;
c -= 'a' - 10; else if (c >= 'a' && c <= 'z')
else c -= 'a' - 10;
break; else
if (c >= base) break;
break; if (c >= base)
if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim)) break;
any = -1; if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
else { any = -1;
any = 1; else
acc *= base; {
acc += c; any = 1;
} acc *= base;
} acc += c;
if (any < 0) { }
acc = ULLONG_MAX; }
if (any < 0)
{
acc = ULLONG_MAX;
errno = ERANGE; errno = ERANGE;
} else if (!any) { }
else if (!any)
{
noconv: noconv:
errno = EINVAL; errno = EINVAL;
} else if (neg) }
else if (neg)
acc = -acc; acc = -acc;
if (endptr != NULL) if (endptr)
*endptr = (char *)(any ? s - 1 : nptr); *endptr = (char *)(any ? s - 1 : nptr);
return (acc); return (acc);
} }
@ -443,17 +470,6 @@ float strtof(const char* str, char** endptr)
return (float) strtod(str, endptr); return (float) strtod(str, endptr);
} }
int link(const char *oldpath, const char *newpath) int link(const char *oldpath, const char *newpath) { return fileXioSymlink(oldpath, newpath); }
{ int unlink(const char *path) { return fileXioRemove(path); }
return fileXioSymlink(oldpath, newpath); int rename(const char *source, const char *dest) { return fileXioRename(source, dest); }
}
int unlink(const char *path)
{
return fileXioRemove(path);
}
int rename(const char *source, const char *dest)
{
return fileXioRename(source, dest);
}

View File

@ -23,15 +23,9 @@ static DescriptorTranslation *__ps2_fdmap[MAX_OPEN_FILES];
static DescriptorTranslation __ps2_fdmap_pool[MAX_OPEN_FILES]; static DescriptorTranslation __ps2_fdmap_pool[MAX_OPEN_FILES];
static int _lock_sema_id = -1; static int _lock_sema_id = -1;
static inline int _lock(void) static inline int _lock(void) { return(WaitSema(_lock_sema_id)); }
{
return(WaitSema(_lock_sema_id));
}
static inline int _unlock(void) static inline int _unlock(void) { return(SignalSema(_lock_sema_id)); }
{
return(SignalSema(_lock_sema_id));
}
static int __ps2_fd_drop(DescriptorTranslation *map) static int __ps2_fd_drop(DescriptorTranslation *map)
{ {
@ -45,9 +39,7 @@ static int __ps2_fd_drop(DescriptorTranslation *map)
memset(map, 0, sizeof(DescriptorTranslation)); memset(map, 0, sizeof(DescriptorTranslation));
} }
else else
{
map->ref_count--; map->ref_count--;
}
_unlock(); _unlock();
return 0; return 0;
@ -61,19 +53,21 @@ int is_fd_valid(int fd)
return (fd >= 0) && (fd < MAX_OPEN_FILES) && (__ps2_fdmap[fd] != NULL); return (fd >= 0) && (fd < MAX_OPEN_FILES) && (__ps2_fdmap[fd] != NULL);
} }
void _init_ps2_io(void) { void _init_ps2_io(void)
{
ee_sema_t sp; ee_sema_t sp;
memset(__ps2_fdmap, 0, sizeof(__ps2_fdmap)); memset(__ps2_fdmap, 0, sizeof(__ps2_fdmap));
memset(__ps2_fdmap_pool, 0, sizeof(__ps2_fdmap_pool)); memset(__ps2_fdmap_pool, 0, sizeof(__ps2_fdmap_pool));
sp.init_count = 1; sp.init_count = 1;
sp.max_count = 1; sp.max_count = 1;
sp.option = 0; sp.option = 0;
_lock_sema_id = CreateSema(&sp); _lock_sema_id = CreateSema(&sp);
} }
void _free_ps2_io(void) { void _free_ps2_io(void)
{
_lock(); _lock();
_unlock(); _unlock();
if(_lock_sema_id >= 0) DeleteSema(_lock_sema_id); if(_lock_sema_id >= 0) DeleteSema(_lock_sema_id);
@ -88,17 +82,18 @@ int __ps2_acquire_descriptor(void)
/* get free descriptor */ /* get free descriptor */
for (fd = 0; fd < MAX_OPEN_FILES; ++fd) for (fd = 0; fd < MAX_OPEN_FILES; ++fd)
{ {
if (__ps2_fdmap[fd] == NULL) if (!__ps2_fdmap[fd])
{ {
/* get free pool */ /* get free pool */
for (i = 0; i < MAX_OPEN_FILES; ++i) for (i = 0; i < MAX_OPEN_FILES; ++i)
{ {
if (__ps2_fdmap_pool[i].ref_count == 0) if (__ps2_fdmap_pool[i].ref_count == 0)
{ {
__ps2_fdmap[fd] = &__ps2_fdmap_pool[i]; __ps2_fdmap[fd] = &__ps2_fdmap_pool[i];
__ps2_fdmap[fd]->ref_count = 1; __ps2_fdmap[fd]->ref_count = 1;
__ps2_fdmap[fd]->current_folder_position = -1; __ps2_fdmap[fd]->current_folder_position = -1;
__ps2_fdmap[fd]->FileEntry = calloc(sizeof(entries), FILEENTRY_SIZE); __ps2_fdmap[fd]->FileEntry =
calloc(sizeof(entries), FILEENTRY_SIZE);
_unlock(); _unlock();
return MAX_OPEN_FILES - fd; return MAX_OPEN_FILES - fd;
} }
@ -115,7 +110,8 @@ int __ps2_release_descriptor(int fd)
{ {
int res = -1; int res = -1;
if (is_fd_valid(fd) && __ps2_fd_drop(__ps2_fdmap[MAX_OPEN_FILES - fd]) >= 0) if (is_fd_valid(fd) &&
__ps2_fd_drop(__ps2_fdmap[MAX_OPEN_FILES - fd]) >= 0)
{ {
_lock(); _lock();
/* Correct fd value */ /* Correct fd value */

View File

@ -160,8 +160,9 @@ enum BootDeviceIDs getBootDeviceID(char *path)
bool waitUntilDeviceIsReady(enum BootDeviceIDs device_id) bool waitUntilDeviceIsReady(enum BootDeviceIDs device_id)
{ {
int openFile = - 1; int openFile = - 1;
int retries = 3; /* just in case we tried a unit that is not working/connected */ /* just in case we tried a unit that is not working/connected */
int retries = 3;
char *rootDevice = rootDevicePath(device_id); char *rootDevice = rootDevicePath(device_id);
while(openFile < 0 && retries > 0) while(openFile < 0 && retries > 0)
@ -178,10 +179,10 @@ bool waitUntilDeviceIsReady(enum BootDeviceIDs device_id)
nopdelay(); nopdelay();
retries--; retries--;
};
if (openFile > 0) {
fileXioDclose(openFile);
} }
if (openFile > 0)
fileXioDclose(openFile);
return openFile >= 0; return openFile >= 0;
} }

View File

@ -54,15 +54,15 @@ static time_t _gmtotime_t (
long passed_seconds_current_day; long passed_seconds_current_day;
time_t seconds_from_1970 = -1; time_t seconds_from_1970 = -1;
if ((yr >= MIN_SUPPORTED_YEAR) || (yr <= MAX_SUPPORTED_YEAR)) { if ((yr >= MIN_SUPPORTED_YEAR) || (yr <= MAX_SUPPORTED_YEAR))
{
passed_years = (long)yr - MIN_SUPPORTED_YEAR; /* Years after 1970 */ passed_years = (long)yr - MIN_SUPPORTED_YEAR; /* Years after 1970 */
/* Calculate days for these years */ /* Calculate days for these years */
passed_days = passed_years * DAYS_YEAR; passed_days = passed_years * DAYS_YEAR;
passed_days += (passed_years >> 2) * (DAYS_YEAR + 1); /* passed leap years */ passed_days += (passed_years >> 2) * (DAYS_YEAR + 1); /* passed leap years */
passed_days += dy + _days[mo - 1]; /* passed days in the year */ passed_days += dy + _days[mo - 1]; /* passed days in the year */
if ( !(yr & 3) && (mo > 2) ) { if ( !(yr & 3) && (mo > 2) )
passed_days++; /* if current year, is a leap year */ passed_days++; /* if current year, is a leap year */
}
passed_seconds_current_day = (((hr * MINS_HOUR) + mn) * SECS_MIN) + sc; passed_seconds_current_day = (((hr * MINS_HOUR) + mn) * SECS_MIN) + sc;
seconds_from_1970 = (passed_days * HOURS_DAY * MINS_HOUR * SECS_MIN) + passed_seconds_current_day; seconds_from_1970 = (passed_days * HOURS_DAY * MINS_HOUR * SECS_MIN) + passed_seconds_current_day;
} }
@ -70,7 +70,8 @@ static time_t _gmtotime_t (
return seconds_from_1970; return seconds_from_1970;
} }
time_t ps2_time(time_t *t) { time_t ps2_time(time_t *t)
{
time_t tim; time_t tim;
sceCdCLOCK clocktime; /* defined in libcdvd.h */ sceCdCLOCK clocktime; /* defined in libcdvd.h */
@ -78,16 +79,16 @@ time_t ps2_time(time_t *t) {
configConvertToLocalTime(&clocktime); configConvertToLocalTime(&clocktime);
tim = _gmtotime_t (DEC(clocktime.year)+ STARTING_YEAR, tim = _gmtotime_t (DEC(clocktime.year)+ STARTING_YEAR,
DEC(clocktime.month), DEC(clocktime.month),
DEC(clocktime.day), DEC(clocktime.day),
DEC(clocktime.hour), DEC(clocktime.hour),
DEC(clocktime.minute), DEC(clocktime.minute),
DEC(clocktime.second)); DEC(clocktime.second));
if(t) if (t)
*t = tim; *t = tim;
return tim; return tim;
} }
/* Protected methods in libc */ /* Protected methods in libc */
@ -134,4 +135,4 @@ size_t strftime(char *s, size_t max, const char *format, const struct tm *tm)
char *setlocale(int category, const char *locale) char *setlocale(int category, const char *locale)
{ {
return NULL; return NULL;
} }