From c4f69b1eb775546cfe61d2ae9cf8fa0a4964ebcf Mon Sep 17 00:00:00 2001 From: twinaphex Date: Fri, 14 Feb 2020 17:43:00 +0100 Subject: [PATCH] (PS2) Cleanups --- ps2/compat_files/compat_ctype.c | 205 ++++++++++++++++---------------- ps2/compat_files/fileXio_cdvd.c | 135 +++++++++++---------- ps2/compat_files/time.c | 18 +-- 3 files changed, 181 insertions(+), 177 deletions(-) diff --git a/ps2/compat_files/compat_ctype.c b/ps2/compat_files/compat_ctype.c index d7b91d0f17..1766833c05 100644 --- a/ps2/compat_files/compat_ctype.c +++ b/ps2/compat_files/compat_ctype.c @@ -33,75 +33,76 @@ int islower(int c) if ((c < 'a') || (c > 'z')) return 0; - // passed both criteria, so it - // is a lower case alpha char + /* passed both criteria, so it + * is a lower case alpha char */ return 1; } int tolower(int ch) { - if(ch >= 'A' && ch <= 'Z') + if (ch >= 'A' && ch <= 'Z') return ('a' + ch - 'A'); return ch; } int toupper(int c) { - if (islower(c)) - c -= 32; + if (islower(c)) + c -= 32; - return c; + return c; } int memcmp(const void *s1, const void *s2, unsigned int length) { - const char *a = s1; - const char *b = s2; + const char *a = s1; + const char *b = s2; - while (length--) { - if (*a++ != *b++) - return 1; - } + while (length--) + { + if (*a++ != *b++) + return 1; + } - return 0; + return 0; } void * memcpy (void *dest, const void *src, size_t len) { - char *d = dest; - const char *s = src; - while (len--) - *d++ = *s++; - return dest; + char *d = dest; + const char *s = src; + while (len--) + *d++ = *s++; + return dest; } void * memset (void *dest, int val, size_t len) { - unsigned char *ptr = dest; - while (len-- > 0) - *ptr++ = val; - return dest; + unsigned char *ptr = dest; + while (len-- > 0) + *ptr++ = val; + return dest; } int sprintf (char *s, const char *format, ...) { - va_list arg; - int done; - va_start (arg, format); - done = vsprintf (s, format, arg); - va_end (arg); - return done; + va_list arg; + int done; + va_start (arg, format); + done = vsprintf (s, format, arg); + va_end (arg); + return done; } char * strcat(char *dest, const char *src) { - size_t i,j; - for (i = 0; dest[i] != '\0'; i++) - ; - for (j = 0; src[j] != '\0'; j++) - dest[i+j] = src[j]; - dest[i+j] = '\0'; - return dest; + size_t i,j; + for (i = 0; dest[i] != '\0'; i++) + ; + for (j = 0; src[j] != '\0'; j++) + dest[i+j] = src[j]; + dest[i+j] = '\0'; + return dest; } char *strchr(const char *string, int c) @@ -121,32 +122,32 @@ char *strchr(const char *string, int c) int strcmp(const char *s1, const char *s2) { - while (*s1 == *s2++) - if (*s1++ == 0) - return (0); - return (*(unsigned char *)s1 - *(unsigned char *)--s2); + while (*s1 == *s2++) + if (*s1++ == 0) + return (0); + return (*(unsigned char *)s1 - *(unsigned char *)--s2); } char * strcpy(char *to, const char *from) { - char *save = to; + char *save = to; - for (; (*to = *from) != '\0'; ++from, ++to); - return(save); + for (; (*to = *from) != '\0'; ++from, ++to); + return(save); } size_t strcspn(const char *s1, const char *s2) { - const char *p, *spanp; - char c, sc; + const char *p, *spanp; + char c, sc; - /* - * 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;;) + /* + * 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;;) { - c = *p++; + c = *p++; spanp = s2; do { @@ -154,16 +155,16 @@ size_t strcspn(const char *s1, const char *s2) return (p - 1 - s1); }while(sc != 0); } - /* NOTREACHED */ + /* NOTREACHED */ } size_t strlen(const char *str) { const char *s; - for (s = str; *s; ++s) - ; - return (s - str); + for (s = str; *s; ++s) + ; + return (s - str); } char * strncat(char *dst, const char *src, size_t n) @@ -206,7 +207,7 @@ char * strncpy(char *dst, const char *src, size_t n) { if (n != 0) { - char *d = dst; + char *d = dst; const char *s = src; do @@ -225,16 +226,16 @@ char * strncpy(char *dst, const char *src, size_t n) char * strpbrk(const char *s1, const char *s2) { - const char *scanp; - int c, sc; + 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)); } - return (NULL); + return (NULL); } /* Do not link to strrchr() from libc */ @@ -388,46 +389,46 @@ char * strtok_r (char *s, const char *delim, char **save_ptr) unsigned long long strtoull(const char * __restrict nptr, char ** __restrict endptr, int base) { - char c; - unsigned long long acc; - unsigned long long cutoff; - int neg, any, cutlim; - /* - * See strtoq for comments as to the logic used. - */ - const char *s = nptr; + char c; + unsigned long long acc; + unsigned long long cutoff; + int neg, any, cutlim; + /* + * See strtoq for comments as to the logic used. + */ + const char *s = nptr; - do + do { - c = *s++; - }while(isspace((unsigned char)c)); - if (c == '-') + c = *s++; + }while(isspace((unsigned char)c)); + if (c == '-') { - neg = 1; - c = *s++; - } + neg = 1; + c = *s++; + } else { - neg = 0; - if (c == '+') - c = *s++; - } - if ((base == 0 || base == 16) && - c == '0' && (*s == 'x' || *s == 'X')) + neg = 0; + if (c == '+') + c = *s++; + } + if ((base == 0 || base == 16) && + c == '0' && (*s == 'x' || *s == 'X')) { - c = s[1]; - s += 2; - base = 16; - } - if (base == 0) - base = c == '0' ? 8 : 10; - acc = any = 0; - if (base < 2 || base > 36) - goto noconv; + c = s[1]; + s += 2; + base = 16; + } + if (base == 0) + base = c == '0' ? 8 : 10; + acc = any = 0; + if (base < 2 || base > 36) + goto noconv; - cutoff = ULLONG_MAX / base; - cutlim = ULLONG_MAX % base; - for ( ; ; c = *s++) + cutoff = ULLONG_MAX / base; + cutlim = ULLONG_MAX % base; + for ( ; ; c = *s++) { if (c >= '0' && c <= '9') c -= '0'; @@ -448,21 +449,21 @@ unsigned long long strtoull(const char * __restrict nptr, acc += c; } } - if (any < 0) + if (any < 0) { - acc = ULLONG_MAX; - errno = ERANGE; - } + acc = ULLONG_MAX; + errno = ERANGE; + } else if (!any) { noconv: - errno = EINVAL; - } + errno = EINVAL; + } else if (neg) - acc = -acc; - if (endptr) - *endptr = (char *)(any ? s - 1 : nptr); - return (acc); + acc = -acc; + if (endptr) + *endptr = (char *)(any ? s - 1 : nptr); + return (acc); } float strtof(const char* str, char** endptr) diff --git a/ps2/compat_files/fileXio_cdvd.c b/ps2/compat_files/fileXio_cdvd.c index e82b66f10d..66ca035ad2 100644 --- a/ps2/compat_files/fileXio_cdvd.c +++ b/ps2/compat_files/fileXio_cdvd.c @@ -19,29 +19,31 @@ static int comp_entries_by_filename(const void *elem1, const void *elem2) return strcmp(((entries*)elem1)->filename, ((entries*)elem2)->filename); } -static int ps2_cdDiscValid(void) //returns 1 if disc valid, else returns 0 +/* returns 1 if disc valid, else returns 0 */ +static int ps2_cdDiscValid(void) { int cdmode = sceCdGetDiskType(); - switch (cdmode) { - case SCECdPSCD: - case SCECdPSCDDA: - case SCECdPS2CD: - case SCECdPS2CDDA: - case SCECdPS2DVD: - case SCECdCDDA: - case SCECdDVDV: - return 1; - case SCECdNODISC: - case SCECdDETCT: - case SCECdDETCTCD: - case SCECdDETCTDVDS: - case SCECdDETCTDVDD: - case SCECdUNKNOWN: - case SCECdIllegalMedia: - default: - return 0; - } + switch (cdmode) + { + case SCECdPSCD: + case SCECdPSCDDA: + case SCECdPS2CD: + case SCECdPS2CDDA: + case SCECdPS2DVD: + case SCECdCDDA: + case SCECdDVDV: + return 1; + case SCECdNODISC: + case SCECdDETCT: + case SCECdDETCTCD: + case SCECdDETCTDVDS: + case SCECdDETCTDVDD: + case SCECdUNKNOWN: + case SCECdIllegalMedia: + default: + return 0; + } } static u64 cd_Timer(void) @@ -60,15 +62,18 @@ static int prepareCDVD(void) u64 wait_start; int cdmode = sceCdGetDiskType(); - if (sceCdGetDiskType() <= SCECdUNKNOWN) { + if (sceCdGetDiskType() <= SCECdUNKNOWN) + { wait_start = cd_Timer(); - while ((cd_Timer() < wait_start + 500) && !ps2_cdDiscValid()) { + while ((cd_Timer() < wait_start + 500) && !ps2_cdDiscValid()) + { if (cdmode == SCECdNODISC) return 0; } if (cdmode == SCECdNODISC) return 0; - if ((cdmode < SCECdPSCD) || (cdmode > SCECdPS2DVD)) { + if ((cdmode < SCECdPSCD) || (cdmode > SCECdPS2DVD)) + { ps2_cdStop(); return 0; } @@ -86,50 +91,55 @@ static int listcdvd(const char *path, entries *FileEntry) int first_file_index; strcpy(dir, &path[5]); - // Directories first... + + /* Directories first... */ CDVD_FlushCache(); - n = CDVD_GetDir(dir, NULL, CDVD_GET_DIRS_ONLY, TocEntryList, FILEENTRY_SIZE, dir); + n = CDVD_GetDir(dir, NULL, CDVD_GET_DIRS_ONLY, + TocEntryList, FILEENTRY_SIZE, dir); - for (i = 0; i < n; i++) { + for (i = 0; i < n; i++) + { + /* Skip pseudopaths "." and ".." */ if (TocEntryList[i].fileProperties & 0x02 && (!strcmp( - TocEntryList[i].filename, ".") || !strcmp( + TocEntryList[i].filename, ".") || !strcmp( TocEntryList[i].filename, ".."))) - continue; // Skip pseudopaths "." and ".." + continue; FileEntry[t].dircheck = 1; strcpy(FileEntry[t].filename, TocEntryList[i].filename); t++; - if (t >= FILEENTRY_SIZE - 2) { + if (t >= FILEENTRY_SIZE - 2) break; - } } qsort(FileEntry, t, sizeof(entries), comp_entries_by_filename); first_file_index = t; - // Now files only + /* Now files only */ CDVD_FlushCache(); n = CDVD_GetDir(dir, NULL, CDVD_GET_FILES_ONLY, TocEntryList, FILEENTRY_SIZE, dir); - for (i = 0; i < n; i++) { + for (i = 0; i < n; i++) + { + /* Skip pseudopaths "." and ".." */ if (TocEntryList[i].fileProperties & 0x02 && (!strcmp( - TocEntryList[i].filename, ".") || !strcmp( + TocEntryList[i].filename, ".") || !strcmp( TocEntryList[i].filename, ".."))) - continue; // Skip pseudopaths "." and ".." + continue; FileEntry[t].dircheck = 0; strcpy(FileEntry[t].filename, TocEntryList[i].filename); t++; - if (t >= FILEENTRY_SIZE - 2) { + if (t >= FILEENTRY_SIZE - 2) break; - } } - qsort(FileEntry + first_file_index, t - first_file_index, sizeof(entries), comp_entries_by_filename); + qsort(FileEntry + first_file_index, t - first_file_index, + sizeof(entries), comp_entries_by_filename); return t; } @@ -138,15 +148,17 @@ static int fileXioCDDread(int fd, iox_dirent_t *dirent) { DescriptorTranslation *descriptor = __ps2_fd_grab(fd); - if (descriptor && descriptor->current_folder_position < descriptor->items) { + if (descriptor && descriptor->current_folder_position < descriptor->items) + { strcpy(dirent->name, descriptor->FileEntry[descriptor->current_folder_position].filename); - if (descriptor->FileEntry[descriptor->current_folder_position].dircheck) { + if (descriptor->FileEntry[descriptor->current_folder_position].dircheck) dirent->stat.mode = FIO_S_IFDIR; - } else { + else dirent->stat.mode = FIO_S_IFREG; - } descriptor->current_folder_position++; - } else { + } + else + { descriptor->current_folder_position = 0; return 0; } @@ -156,47 +168,38 @@ static int fileXioCDDread(int fd, iox_dirent_t *dirent) static int fileXioCDDopen(const char *name) { - int fd = -1; - if (prepareCDVD()){ - fd = __ps2_acquire_descriptor(); + if (prepareCDVD()) + { + int fd = __ps2_acquire_descriptor(); DescriptorTranslation *descriptor = __ps2_fd_grab(fd); descriptor->current_folder_position = 0; descriptor->items = listcdvd(name, descriptor->FileEntry); + return fd; } - return fd; + return -1; } int ps2fileXioDopen(const char *name) { enum BootDeviceIDs deviceID = getBootDeviceID((char *)name); - int fd = -1; - if (deviceID == BOOT_DEVICE_CDFS) { - fd = fileXioCDDopen(name); - } else { - fd = fileXioDopen(name); - } - - return fd; + if (deviceID == BOOT_DEVICE_CDFS) + return fileXioCDDopen(name); + return fileXioDopen(name); } int ps2fileXioDread(int fd, iox_dirent_t *dirent) { - if (is_fd_valid(fd)) { + if (is_fd_valid(fd)) return fileXioCDDread(fd, dirent); - } else { - return fileXioDread(fd, dirent); - } + return fileXioDread(fd, dirent); } int ps2fileXioDclose(int fd) { - int ret = -19; - if (is_fd_valid(fd)) { - ret = __ps2_release_descriptor(fd); - } else if (fd > 0) { - ret = fileXioDclose(fd); - } - - return ret; + if (is_fd_valid(fd)) + return __ps2_release_descriptor(fd); + else if (fd > 0) + return fileXioDclose(fd); + return -19; } diff --git a/ps2/compat_files/time.c b/ps2/compat_files/time.c index 47518ff614..5bdf1642bf 100644 --- a/ps2/compat_files/time.c +++ b/ps2/compat_files/time.c @@ -49,22 +49,22 @@ static time_t _gmtotime_t ( int sc ) { - int passed_years; - long passed_days; - long passed_seconds_current_day; time_t seconds_from_1970 = -1; if ((yr >= MIN_SUPPORTED_YEAR) || (yr <= MAX_SUPPORTED_YEAR)) { - passed_years = (long)yr - MIN_SUPPORTED_YEAR; /* Years after 1970 */ + long passed_seconds_current_day; + int 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) ) + long 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)) 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; + seconds_from_1970 = (passed_days * HOURS_DAY * MINS_HOUR * SECS_MIN) + passed_seconds_current_day; } return seconds_from_1970;