(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')
return ('a' + ch - 'A');
else
return ch;
}
@ -107,7 +106,8 @@ char * strcat(char *dest, const char *src)
char *strchr(const char *string, int c)
{
while (*string) {
while (*string)
{
if (*string == c)
return (char *)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
* 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;
do {
do
{
if ((sc = *spanp++) == c)
return (p - 1 - s1);
} while (sc != 0);
}while(sc != 0);
}
/* NOTREACHED */
}
@ -166,17 +168,19 @@ size_t strlen(const char *str)
char * strncat(char *dst, const char *src, size_t n)
{
if (n != 0) {
if (n != 0)
{
char *d = dst;
const char *s = src;
while (*d != 0)
d++;
do {
do
{
if ((*d = *s++) == 0)
break;
d++;
} while (--n != 0);
}while(--n != 0);
*d = 0;
}
return (dst);
@ -186,29 +190,35 @@ int strncmp(const char *s1, const char *s2, size_t n)
{
if (n == 0)
return (0);
do {
do
{
if (*s1 != *s2++)
return (*(unsigned char *)s1 - *(unsigned char *)--s2);
if (*s1++ == 0)
break;
} while (--n != 0);
}while (--n != 0);
return (0);
}
char * strncpy(char *dst, const char *src, size_t n)
{
if (n != 0) {
if (n != 0)
{
char *d = dst;
const char *s = src;
do {
if ((*d++ = *s++) == 0) {
do
{
if ((*d++ = *s++) == 0)
{
/* NUL pad the remaining n-1 bytes */
while (--n != 0)
*d++ = 0;
break;
}
} while (--n != 0);
}while(--n != 0);
}
return (dst);
}
@ -218,7 +228,8 @@ char * strpbrk(const char *s1, const char *s2)
const char *scanp;
int c, sc;
while ((c = *s1++) != 0) {
while ((c = *s1++) != 0)
{
for (scanp = s2; (sc = *scanp++) != 0;)
if (sc == c)
return ((char *)(s1 - 1));
@ -231,13 +242,13 @@ char * strrchr(const char *p, int ch)
{
char *save;
for (save = NULL;; ++p) {
for (save = NULL;; ++p)
{
if (*p == (char) ch)
save = (char *)p;
if (!*p)
return(save);
}
/* NOTREACHED */
}
size_t strspn(const char *s1, const char *s2)
@ -268,7 +279,8 @@ char *strstr(const char *string, const char *substring)
strpos = (char *)string;
while (*strpos != 0) {
while (*strpos != 0)
{
if (strncmp(strpos, substring, strlen(substring)) == 0)
return strpos;
@ -293,9 +305,10 @@ char *strtok(char *strToken, const char *strDelimit)
static char *start;
static char *end;
if (strToken != NULL)
if (strToken)
start = strToken;
else {
else
{
if (*end == 0)
return 0;
@ -305,11 +318,11 @@ char *strtok(char *strToken, const char *strDelimit)
if (*start == 0)
return 0;
// Strip out any leading delimiters
while (strchr(strDelimit, *start)) {
// If a character from the delimiting string
// then skip past it
/* Strip out any leading delimiters */
while (strchr(strDelimit, *start))
{
/* If a character from the delimiting string
* then skip past it */
start++;
if (*start == 0)
@ -321,12 +334,15 @@ char *strtok(char *strToken, const char *strDelimit)
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
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;
@ -334,15 +350,15 @@ char *strtok(char *strToken, const char *strDelimit)
end++;
}
// reached the end of the string before finding a delimiter
// so dont move on to the next character
/* 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 *end;
if (s == NULL)
if (!s)
s = *save_ptr;
if (*s == '\0')
{
@ -369,31 +385,36 @@ char * strtok_r (char *s, const char *delim, char **save_ptr)
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;
unsigned long long acc;
unsigned long long cutoff;
int neg, any, cutlim;
/*
* See strtoq for comments as to the logic used.
*/
s = nptr;
do {
const char *s = nptr;
do
{
c = *s++;
} while (isspace((unsigned char)c));
if (c == '-') {
}while(isspace((unsigned char)c));
if (c == '-')
{
neg = 1;
c = *s++;
} else {
}
else
{
neg = 0;
if (c == '+')
c = *s++;
}
if ((base == 0 || base == 16) &&
c == '0' && (*s == 'x' || *s == 'X')) {
c == '0' && (*s == 'x' || *s == 'X'))
{
c = s[1];
s += 2;
base = 16;
@ -406,7 +427,8 @@ unsigned long long strtoull(const char * __restrict nptr, char ** __restrict end
cutoff = ULLONG_MAX / base;
cutlim = ULLONG_MAX % base;
for ( ; ; c = *s++) {
for ( ; ; c = *s++)
{
if (c >= '0' && c <= '9')
c -= '0';
else if (c >= 'A' && c <= 'Z')
@ -419,21 +441,26 @@ unsigned long long strtoull(const char * __restrict nptr, char ** __restrict end
break;
if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
any = -1;
else {
else
{
any = 1;
acc *= base;
acc += c;
}
}
if (any < 0) {
if (any < 0)
{
acc = ULLONG_MAX;
errno = ERANGE;
} else if (!any) {
}
else if (!any)
{
noconv:
errno = EINVAL;
} else if (neg)
}
else if (neg)
acc = -acc;
if (endptr != NULL)
if (endptr)
*endptr = (char *)(any ? s - 1 : nptr);
return (acc);
}
@ -443,17 +470,6 @@ float strtof(const char* str, char** endptr)
return (float) strtod(str, endptr);
}
int link(const char *oldpath, const char *newpath)
{
return fileXioSymlink(oldpath, newpath);
}
int unlink(const char *path)
{
return fileXioRemove(path);
}
int rename(const char *source, const char *dest)
{
return fileXioRename(source, dest);
}
int link(const char *oldpath, const char *newpath) { return fileXioSymlink(oldpath, newpath); }
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 int _lock_sema_id = -1;
static inline int _lock(void)
{
return(WaitSema(_lock_sema_id));
}
static inline int _lock(void) { return(WaitSema(_lock_sema_id)); }
static inline int _unlock(void)
{
return(SignalSema(_lock_sema_id));
}
static inline int _unlock(void) { return(SignalSema(_lock_sema_id)); }
static int __ps2_fd_drop(DescriptorTranslation *map)
{
@ -45,9 +39,7 @@ static int __ps2_fd_drop(DescriptorTranslation *map)
memset(map, 0, sizeof(DescriptorTranslation));
}
else
{
map->ref_count--;
}
_unlock();
return 0;
@ -61,7 +53,8 @@ int is_fd_valid(int fd)
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;
memset(__ps2_fdmap, 0, sizeof(__ps2_fdmap));
@ -73,7 +66,8 @@ void _init_ps2_io(void) {
_lock_sema_id = CreateSema(&sp);
}
void _free_ps2_io(void) {
void _free_ps2_io(void)
{
_lock();
_unlock();
if(_lock_sema_id >= 0) DeleteSema(_lock_sema_id);
@ -88,7 +82,7 @@ int __ps2_acquire_descriptor(void)
/* get free descriptor */
for (fd = 0; fd < MAX_OPEN_FILES; ++fd)
{
if (__ps2_fdmap[fd] == NULL)
if (!__ps2_fdmap[fd])
{
/* get free pool */
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]->ref_count = 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();
return MAX_OPEN_FILES - fd;
}
@ -115,7 +110,8 @@ int __ps2_release_descriptor(int fd)
{
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();
/* Correct fd value */

View File

@ -161,7 +161,8 @@ enum BootDeviceIDs getBootDeviceID(char *path)
bool waitUntilDeviceIsReady(enum BootDeviceIDs device_id)
{
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);
while(openFile < 0 && retries > 0)
@ -178,10 +179,10 @@ bool waitUntilDeviceIsReady(enum BootDeviceIDs device_id)
nopdelay();
retries--;
};
if (openFile > 0) {
fileXioDclose(openFile);
}
if (openFile > 0)
fileXioDclose(openFile);
return openFile >= 0;
}

View File

@ -54,15 +54,15 @@ static time_t _gmtotime_t (
long passed_seconds_current_day;
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 */
/* Calculate days for these years */
passed_days = passed_years * DAYS_YEAR;
passed_days += (passed_years >> 2) * (DAYS_YEAR + 1); /* passed leap years */
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_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;
}
@ -70,7 +70,8 @@ static time_t _gmtotime_t (
return seconds_from_1970;
}
time_t ps2_time(time_t *t) {
time_t ps2_time(time_t *t)
{
time_t tim;
sceCdCLOCK clocktime; /* defined in libcdvd.h */
@ -84,7 +85,7 @@ time_t ps2_time(time_t *t) {
DEC(clocktime.minute),
DEC(clocktime.second));
if(t)
if (t)
*t = tim;
return tim;