(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

@ -42,7 +42,6 @@ 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;
} }
@ -107,7 +106,8 @@ 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) if (*string == c)
return (char *)string; return (char *)string;
string++; string++;
@ -144,13 +144,15 @@ 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++; c = *p++;
spanp = s2; spanp = s2;
do { do
{
if ((sc = *spanp++) == c) if ((sc = *spanp++) == c)
return (p - 1 - s1); return (p - 1 - s1);
} while (sc != 0); }while(sc != 0);
} }
/* NOTREACHED */ /* NOTREACHED */
} }
@ -166,17 +168,19 @@ 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; char *d = dst;
const char *s = src; const char *s = src;
while (*d != 0) while (*d != 0)
d++; d++;
do { do
{
if ((*d = *s++) == 0) if ((*d = *s++) == 0)
break; break;
d++; d++;
} while (--n != 0); }while(--n != 0);
*d = 0; *d = 0;
} }
return (dst); return (dst);
@ -186,29 +190,35 @@ 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; char *d = dst;
const char *s = src; const char *s = src;
do { do
if ((*d++ = *s++) == 0) { {
if ((*d++ = *s++) == 0)
{
/* NUL pad the remaining n-1 bytes */ /* NUL pad the remaining n-1 bytes */
while (--n != 0) while (--n != 0)
*d++ = 0; *d++ = 0;
break; break;
} }
} while (--n != 0); }while(--n != 0);
} }
return (dst); return (dst);
} }
@ -218,7 +228,8 @@ 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;) for (scanp = s2; (sc = *scanp++) != 0;)
if (sc == c) if (sc == c)
return ((char *)(s1 - 1)); return ((char *)(s1 - 1));
@ -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) if (*p == (char) ch)
save = (char *)p; save = (char *)p;
if (!*p) if (!*p)
return(save); return(save);
} }
/* NOTREACHED */
} }
size_t strspn(const char *s1, const char *s2) size_t strspn(const char *s1, const char *s2)
@ -268,7 +279,8 @@ 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) if (strncmp(strpos, substring, strlen(substring)) == 0)
return strpos; return strpos;
@ -293,9 +305,10 @@ 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) if (*end == 0)
return 0; return 0;
@ -305,11 +318,11 @@ char *strtok(char *strToken, const char *strDelimit)
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) if (*start == 0)
@ -321,12 +334,15 @@ char *strtok(char *strToken, const char *strDelimit)
end = start; end = start;
while (*end != 0) { while (*end != 0)
if (strchr(strDelimit, *end)) { {
// if we find a delimiting character if (strchr(strDelimit, *end))
// before the end of the string, then {
// terminate the token and move the end /* if we find a delimiting character
// pointer to the next character * before the end of the string, then
* terminate the token and move the end
* pointer to the next character
*/
*end = 0; *end = 0;
end++; end++;
return start; return start;
@ -334,15 +350,15 @@ char *strtok(char *strToken, const char *strDelimit)
end++; end++;
} }
// reached the end of the string before finding a delimiter /* reached the end of the string before finding a delimiter
// so dont move on to the next character * so dont move on to the next character */
return start; 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')
{ {
@ -369,31 +385,36 @@ char * strtok_r (char *s, const char *delim, char **save_ptr)
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]; c = s[1];
s += 2; s += 2;
base = 16; base = 16;
@ -406,7 +427,8 @@ 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') if (c >= '0' && c <= '9')
c -= '0'; c -= '0';
else if (c >= 'A' && c <= 'Z') else if (c >= 'A' && c <= 'Z')
@ -419,21 +441,26 @@ unsigned long long strtoull(const char * __restrict nptr, char ** __restrict end
break; break;
if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim)) if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
any = -1; any = -1;
else { else
{
any = 1; any = 1;
acc *= base; acc *= base;
acc += c; acc += c;
} }
} }
if (any < 0) { if (any < 0)
{
acc = ULLONG_MAX; 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,7 +53,8 @@ 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));
@ -73,7 +66,8 @@ void _init_ps2_io(void) {
_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,7 +82,7 @@ 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)
@ -98,7 +92,8 @@ int __ps2_acquire_descriptor(void)
__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

@ -161,7 +161,8 @@ 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 */
@ -84,7 +85,7 @@ time_t ps2_time(time_t *t) {
DEC(clocktime.minute), DEC(clocktime.minute),
DEC(clocktime.second)); DEC(clocktime.second));
if(t) if (t)
*t = tim; *t = tim;
return tim; return tim;