(PS2) Cleanups

This commit is contained in:
twinaphex 2020-02-14 17:43:00 +01:00
parent d6e7ae3769
commit c4f69b1eb7
3 changed files with 181 additions and 177 deletions

View File

@ -33,75 +33,76 @@ int islower(int c)
if ((c < 'a') || (c > 'z')) if ((c < 'a') || (c > 'z'))
return 0; return 0;
// passed both criteria, so it /* passed both criteria, so it
// is a lower case alpha char * is a lower case alpha char */
return 1; return 1;
} }
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');
return ch; return ch;
} }
int toupper(int c) int toupper(int c)
{ {
if (islower(c)) if (islower(c))
c -= 32; c -= 32;
return c; return c;
} }
int memcmp(const void *s1, const void *s2, unsigned int length) int memcmp(const void *s1, const void *s2, unsigned int length)
{ {
const char *a = s1; const char *a = s1;
const char *b = s2; const char *b = s2;
while (length--) { while (length--)
if (*a++ != *b++) {
return 1; if (*a++ != *b++)
} return 1;
}
return 0; return 0;
} }
void * memcpy (void *dest, const void *src, size_t len) void * memcpy (void *dest, const void *src, size_t len)
{ {
char *d = dest; char *d = dest;
const char *s = src; const char *s = src;
while (len--) while (len--)
*d++ = *s++; *d++ = *s++;
return dest; return dest;
} }
void * memset (void *dest, int val, size_t len) void * memset (void *dest, int val, size_t len)
{ {
unsigned char *ptr = dest; unsigned char *ptr = dest;
while (len-- > 0) while (len-- > 0)
*ptr++ = val; *ptr++ = val;
return dest; return dest;
} }
int sprintf (char *s, const char *format, ...) int sprintf (char *s, const char *format, ...)
{ {
va_list arg; va_list arg;
int done; int done;
va_start (arg, format); va_start (arg, format);
done = vsprintf (s, format, arg); done = vsprintf (s, format, arg);
va_end (arg); va_end (arg);
return done; return done;
} }
char * strcat(char *dest, const char *src) char * strcat(char *dest, const char *src)
{ {
size_t i,j; size_t i,j;
for (i = 0; dest[i] != '\0'; i++) for (i = 0; dest[i] != '\0'; i++)
; ;
for (j = 0; src[j] != '\0'; j++) for (j = 0; src[j] != '\0'; j++)
dest[i+j] = src[j]; dest[i+j] = src[j];
dest[i+j] = '\0'; dest[i+j] = '\0';
return dest; return dest;
} }
char *strchr(const char *string, int c) 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) int strcmp(const char *s1, const char *s2)
{ {
while (*s1 == *s2++) while (*s1 == *s2++)
if (*s1++ == 0) if (*s1++ == 0)
return (0); return (0);
return (*(unsigned char *)s1 - *(unsigned char *)--s2); return (*(unsigned char *)s1 - *(unsigned char *)--s2);
} }
char * strcpy(char *to, const char *from) char * strcpy(char *to, const char *from)
{ {
char *save = to; char *save = to;
for (; (*to = *from) != '\0'; ++from, ++to); for (; (*to = *from) != '\0'; ++from, ++to);
return(save); return(save);
} }
size_t strcspn(const char *s1, const char *s2) size_t strcspn(const char *s1, const char *s2)
{ {
const char *p, *spanp; const char *p, *spanp;
char c, sc; char c, sc;
/* /*
* 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
{ {
@ -154,16 +155,16 @@ size_t strcspn(const char *s1, const char *s2)
return (p - 1 - s1); return (p - 1 - s1);
}while(sc != 0); }while(sc != 0);
} }
/* NOTREACHED */ /* NOTREACHED */
} }
size_t strlen(const char *str) size_t strlen(const char *str)
{ {
const char *s; const char *s;
for (s = str; *s; ++s) for (s = str; *s; ++s)
; ;
return (s - str); return (s - str);
} }
char * strncat(char *dst, const char *src, size_t n) 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) if (n != 0)
{ {
char *d = dst; char *d = dst;
const char *s = src; const char *s = src;
do do
@ -225,16 +226,16 @@ char * strncpy(char *dst, const char *src, size_t n)
char * strpbrk(const char *s1, const char *s2) 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));
} }
return (NULL); return (NULL);
} }
/* Do not link to strrchr() from libc */ /* 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, unsigned long long strtoull(const char * __restrict nptr,
char ** __restrict endptr, int base) char ** __restrict endptr, int base)
{ {
char c; char c;
unsigned long long acc; 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.
*/ */
const char *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;
} }
if (base == 0) if (base == 0)
base = c == '0' ? 8 : 10; base = c == '0' ? 8 : 10;
acc = any = 0; acc = any = 0;
if (base < 2 || base > 36) if (base < 2 || base > 36)
goto noconv; goto noconv;
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';
@ -448,21 +449,21 @@ unsigned long long strtoull(const char * __restrict nptr,
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) if (endptr)
*endptr = (char *)(any ? s - 1 : nptr); *endptr = (char *)(any ? s - 1 : nptr);
return (acc); return (acc);
} }
float strtof(const char* str, char** endptr) float strtof(const char* str, char** endptr)

View File

@ -19,29 +19,31 @@ static int comp_entries_by_filename(const void *elem1, const void *elem2)
return strcmp(((entries*)elem1)->filename, ((entries*)elem2)->filename); 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(); int cdmode = sceCdGetDiskType();
switch (cdmode) { switch (cdmode)
case SCECdPSCD: {
case SCECdPSCDDA: case SCECdPSCD:
case SCECdPS2CD: case SCECdPSCDDA:
case SCECdPS2CDDA: case SCECdPS2CD:
case SCECdPS2DVD: case SCECdPS2CDDA:
case SCECdCDDA: case SCECdPS2DVD:
case SCECdDVDV: case SCECdCDDA:
return 1; case SCECdDVDV:
case SCECdNODISC: return 1;
case SCECdDETCT: case SCECdNODISC:
case SCECdDETCTCD: case SCECdDETCT:
case SCECdDETCTDVDS: case SCECdDETCTCD:
case SCECdDETCTDVDD: case SCECdDETCTDVDS:
case SCECdUNKNOWN: case SCECdDETCTDVDD:
case SCECdIllegalMedia: case SCECdUNKNOWN:
default: case SCECdIllegalMedia:
return 0; default:
} return 0;
}
} }
static u64 cd_Timer(void) static u64 cd_Timer(void)
@ -60,15 +62,18 @@ static int prepareCDVD(void)
u64 wait_start; u64 wait_start;
int cdmode = sceCdGetDiskType(); int cdmode = sceCdGetDiskType();
if (sceCdGetDiskType() <= SCECdUNKNOWN) { if (sceCdGetDiskType() <= SCECdUNKNOWN)
{
wait_start = cd_Timer(); wait_start = cd_Timer();
while ((cd_Timer() < wait_start + 500) && !ps2_cdDiscValid()) { while ((cd_Timer() < wait_start + 500) && !ps2_cdDiscValid())
{
if (cdmode == SCECdNODISC) if (cdmode == SCECdNODISC)
return 0; return 0;
} }
if (cdmode == SCECdNODISC) if (cdmode == SCECdNODISC)
return 0; return 0;
if ((cdmode < SCECdPSCD) || (cdmode > SCECdPS2DVD)) { if ((cdmode < SCECdPSCD) || (cdmode > SCECdPS2DVD))
{
ps2_cdStop(); ps2_cdStop();
return 0; return 0;
} }
@ -86,50 +91,55 @@ static int listcdvd(const char *path, entries *FileEntry)
int first_file_index; int first_file_index;
strcpy(dir, &path[5]); strcpy(dir, &path[5]);
// Directories first...
/* Directories first... */
CDVD_FlushCache(); 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( if (TocEntryList[i].fileProperties & 0x02 && (!strcmp(
TocEntryList[i].filename, ".") || !strcmp( TocEntryList[i].filename, ".") || !strcmp(
TocEntryList[i].filename, ".."))) TocEntryList[i].filename, "..")))
continue; // Skip pseudopaths "." and ".." continue;
FileEntry[t].dircheck = 1; FileEntry[t].dircheck = 1;
strcpy(FileEntry[t].filename, TocEntryList[i].filename); strcpy(FileEntry[t].filename, TocEntryList[i].filename);
t++; t++;
if (t >= FILEENTRY_SIZE - 2) { if (t >= FILEENTRY_SIZE - 2)
break; break;
}
} }
qsort(FileEntry, t, sizeof(entries), comp_entries_by_filename); qsort(FileEntry, t, sizeof(entries), comp_entries_by_filename);
first_file_index = t; first_file_index = t;
// Now files only /* Now files only */
CDVD_FlushCache(); CDVD_FlushCache();
n = CDVD_GetDir(dir, NULL, CDVD_GET_FILES_ONLY, TocEntryList, FILEENTRY_SIZE, dir); 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( if (TocEntryList[i].fileProperties & 0x02 && (!strcmp(
TocEntryList[i].filename, ".") || !strcmp( TocEntryList[i].filename, ".") || !strcmp(
TocEntryList[i].filename, ".."))) TocEntryList[i].filename, "..")))
continue; // Skip pseudopaths "." and ".." continue;
FileEntry[t].dircheck = 0; FileEntry[t].dircheck = 0;
strcpy(FileEntry[t].filename, TocEntryList[i].filename); strcpy(FileEntry[t].filename, TocEntryList[i].filename);
t++; t++;
if (t >= FILEENTRY_SIZE - 2) { if (t >= FILEENTRY_SIZE - 2)
break; 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; return t;
} }
@ -138,15 +148,17 @@ static int fileXioCDDread(int fd, iox_dirent_t *dirent)
{ {
DescriptorTranslation *descriptor = __ps2_fd_grab(fd); 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); 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; dirent->stat.mode = FIO_S_IFDIR;
} else { else
dirent->stat.mode = FIO_S_IFREG; dirent->stat.mode = FIO_S_IFREG;
}
descriptor->current_folder_position++; descriptor->current_folder_position++;
} else { }
else
{
descriptor->current_folder_position = 0; descriptor->current_folder_position = 0;
return 0; return 0;
} }
@ -156,47 +168,38 @@ static int fileXioCDDread(int fd, iox_dirent_t *dirent)
static int fileXioCDDopen(const char *name) static int fileXioCDDopen(const char *name)
{ {
int fd = -1; if (prepareCDVD())
if (prepareCDVD()){ {
fd = __ps2_acquire_descriptor(); int fd = __ps2_acquire_descriptor();
DescriptorTranslation *descriptor = __ps2_fd_grab(fd); DescriptorTranslation *descriptor = __ps2_fd_grab(fd);
descriptor->current_folder_position = 0; descriptor->current_folder_position = 0;
descriptor->items = listcdvd(name, descriptor->FileEntry); descriptor->items = listcdvd(name, descriptor->FileEntry);
return fd;
} }
return fd; return -1;
} }
int ps2fileXioDopen(const char *name) int ps2fileXioDopen(const char *name)
{ {
enum BootDeviceIDs deviceID = getBootDeviceID((char *)name); enum BootDeviceIDs deviceID = getBootDeviceID((char *)name);
int fd = -1; if (deviceID == BOOT_DEVICE_CDFS)
if (deviceID == BOOT_DEVICE_CDFS) { return fileXioCDDopen(name);
fd = fileXioCDDopen(name); return fileXioDopen(name);
} else {
fd = fileXioDopen(name);
}
return fd;
} }
int ps2fileXioDread(int fd, iox_dirent_t *dirent) int ps2fileXioDread(int fd, iox_dirent_t *dirent)
{ {
if (is_fd_valid(fd)) { if (is_fd_valid(fd))
return fileXioCDDread(fd, dirent); return fileXioCDDread(fd, dirent);
} else { return fileXioDread(fd, dirent);
return fileXioDread(fd, dirent);
}
} }
int ps2fileXioDclose(int fd) int ps2fileXioDclose(int fd)
{ {
int ret = -19; if (is_fd_valid(fd))
if (is_fd_valid(fd)) { return __ps2_release_descriptor(fd);
ret = __ps2_release_descriptor(fd); else if (fd > 0)
} else if (fd > 0) { return fileXioDclose(fd);
ret = fileXioDclose(fd); return -19;
}
return ret;
} }

View File

@ -49,22 +49,22 @@ static time_t _gmtotime_t (
int sc int sc
) )
{ {
int passed_years;
long passed_days;
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 */ long passed_seconds_current_day;
int 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; long 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;
} }
return seconds_from_1970; return seconds_from_1970;