(svn r1953) Codechange: Tidyup, reduced ridiculous indentation levels, some sprintf()s replced by snprintf()s.

This commit is contained in:
pasky 2005-03-07 00:39:41 +00:00
parent 43fbe3b2da
commit 1c41c61305

50
unix.c
View File

@ -85,16 +85,16 @@ FiosItem *FiosGetSavegameList(int *num, int mode)
fios = FiosAlloc(); fios = FiosAlloc();
fios->type = FIOS_TYPE_PARENT; fios->type = FIOS_TYPE_PARENT;
fios->mtime = 0; fios->mtime = 0;
sprintf(fios->title, ".. (Parent directory)"); strcpy(fios->title, ".. (Parent directory)");
} }
// Show subdirectories first // Show subdirectories first
dir = opendir(_fios_path[0] ? _fios_path : "/"); dir = opendir(_fios_path[0] ? _fios_path : "/");
if (dir != NULL) { if (dir != NULL) {
while ((dirent = readdir(dir))) { while ((dirent = readdir(dir))) {
sprintf (filename, "%s/%s", _fios_path, dirent->d_name); snprintf(filename, MAX_PATH, "%s/%s", _fios_path, dirent->d_name);
if (!stat(filename, &sb)) { if (stat(filename, &sb) || !S_ISDIR(sb.st_mode))
if (S_ISDIR(sb.st_mode)) { continue;
if (dirent->d_name[0] != '.') { if (dirent->d_name[0] != '.') {
fios = FiosAlloc(); fios = FiosAlloc();
fios->mtime = 0; fios->mtime = 0;
@ -103,8 +103,6 @@ FiosItem *FiosGetSavegameList(int *num, int mode)
sprintf(fios->name, "%s/ (Directory)", dirent->d_name); sprintf(fios->name, "%s/ (Directory)", dirent->d_name);
} }
} }
}
}
closedir(dir); closedir(dir);
} }
@ -128,10 +126,13 @@ FiosItem *FiosGetSavegameList(int *num, int mode)
dir = opendir(_fios_path[0] ? _fios_path : "/"); dir = opendir(_fios_path[0] ? _fios_path : "/");
if (dir != NULL) { if (dir != NULL) {
while ((dirent = readdir(dir))) { while ((dirent = readdir(dir))) {
sprintf (filename, "%s/%s", _fios_path, dirent->d_name); char *t;
if (!stat(filename, &sb)) {
if (!S_ISDIR(sb.st_mode)) { snprintf(filename, MAX_PATH, "%s/%s", _fios_path, dirent->d_name);
char *t = strrchr(dirent->d_name, '.'); if (stat(filename, &sb) || S_ISDIR(sb.st_mode))
continue;
t = strrchr(dirent->d_name, '.');
if (t && !strcasecmp(t, ".sav")) { // OpenTTD if (t && !strcasecmp(t, ".sav")) { // OpenTTD
*t = 0; // cut extension *t = 0; // cut extension
fios = FiosAlloc(); fios = FiosAlloc();
@ -139,9 +140,11 @@ FiosItem *FiosGetSavegameList(int *num, int mode)
fios->mtime = sb.st_mtime; fios->mtime = sb.st_mtime;
fios->title[0] = 0; fios->title[0] = 0;
ttd_strlcpy(fios->name, dirent->d_name, sizeof(fios->name)); ttd_strlcpy(fios->name, dirent->d_name, sizeof(fios->name));
} else if (mode == SLD_LOAD_GAME || mode == SLD_LOAD_SCENARIO) { } else if (mode == SLD_LOAD_GAME || mode == SLD_LOAD_SCENARIO) {
int ext = 0; // start of savegame extensions in _old_extensions[] int ext = 0; // start of savegame extensions in _old_extensions[]
if (t && ((ext++, !strcasecmp(t, ".ss1")) || (ext++, !strcasecmp(t, ".sv1")) || (ext++, !strcasecmp(t, ".sv2"))) ) { // TTDLX(Patch) if (t && ((ext++, !strcasecmp(t, ".ss1")) || (ext++, !strcasecmp(t, ".sv1"))
|| (ext++, !strcasecmp(t, ".sv2"))) ) { // TTDLX(Patch)
*t = 0; // cut extension *t = 0; // cut extension
fios = FiosAlloc(); fios = FiosAlloc();
fios->old_extension = ext-1; fios->old_extension = ext-1;
@ -152,8 +155,6 @@ FiosItem *FiosGetSavegameList(int *num, int mode)
} }
} }
} }
}
}
closedir(dir); closedir(dir);
} }
@ -163,6 +164,7 @@ FiosItem *FiosGetSavegameList(int *num, int mode)
} }
// Get a list of scenarios // Get a list of scenarios
// FIXME: Gross code duplication with FiosGetSavegameList()
FiosItem *FiosGetScenarioList(int *num, int mode) FiosItem *FiosGetScenarioList(int *num, int mode)
{ {
FiosItem *fios; FiosItem *fios;
@ -182,9 +184,9 @@ FiosItem *FiosGetScenarioList(int *num, int mode)
dir = opendir(_fios_path[0] ? _fios_path : "/"); dir = opendir(_fios_path[0] ? _fios_path : "/");
if (dir != NULL) { if (dir != NULL) {
while ((dirent = readdir(dir))) { while ((dirent = readdir(dir))) {
sprintf (filename, "%s/%s", _fios_path, dirent->d_name); snprintf(filename, MAX_PATH, "%s/%s", _fios_path, dirent->d_name);
if (!stat(filename, &sb)) { if (stat(filename, &sb) || !S_ISDIR(sb.st_mode))
if (S_ISDIR(sb.st_mode)) { continue;
if (dirent->d_name[0] != '.') { if (dirent->d_name[0] != '.') {
fios = FiosAlloc(); fios = FiosAlloc();
fios->mtime = 0; fios->mtime = 0;
@ -193,8 +195,6 @@ FiosItem *FiosGetScenarioList(int *num, int mode)
sprintf(fios->name, "%s/ (Directory)", dirent->d_name); sprintf(fios->name, "%s/ (Directory)", dirent->d_name);
} }
} }
}
}
closedir(dir); closedir(dir);
} }
@ -209,10 +209,13 @@ FiosItem *FiosGetScenarioList(int *num, int mode)
dir = opendir(_fios_path[0] ? _fios_path : "/"); dir = opendir(_fios_path[0] ? _fios_path : "/");
if (dir != NULL) { if (dir != NULL) {
while ((dirent = readdir(dir))) { while ((dirent = readdir(dir))) {
sprintf (filename, "%s/%s", _fios_path, dirent->d_name); char *t;
if (!stat(filename, &sb)) {
if (!S_ISDIR(sb.st_mode)) { snprintf(filename, MAX_PATH, "%s/%s", _fios_path, dirent->d_name);
char *t = strrchr(dirent->d_name, '.'); if (stat(filename, &sb) || S_ISDIR(sb.st_mode))
continue;
t = strrchr(dirent->d_name, '.');
if (t && !strcasecmp(t, ".scn")) { // OpenTTD if (t && !strcasecmp(t, ".scn")) { // OpenTTD
*t = 0; // cut extension *t = 0; // cut extension
fios = FiosAlloc(); fios = FiosAlloc();
@ -220,6 +223,7 @@ FiosItem *FiosGetScenarioList(int *num, int mode)
fios->mtime = sb.st_mtime; fios->mtime = sb.st_mtime;
fios->title[0] = 0; fios->title[0] = 0;
ttd_strlcpy(fios->name, dirent->d_name, sizeof(fios->name)-3); ttd_strlcpy(fios->name, dirent->d_name, sizeof(fios->name)-3);
} else if (mode == SLD_LOAD_GAME || mode == SLD_LOAD_SCENARIO || mode == SLD_NEW_GAME) { } else if (mode == SLD_LOAD_GAME || mode == SLD_LOAD_SCENARIO || mode == SLD_NEW_GAME) {
int ext = 3; // start of scenario extensions in _old_extensions[] int ext = 3; // start of scenario extensions in _old_extensions[]
if (t && ((ext++, !strcasecmp(t, ".sv0")) || (ext++, !strcasecmp(t, ".ss0"))) ) {// TTDLX(Patch) if (t && ((ext++, !strcasecmp(t, ".sv0")) || (ext++, !strcasecmp(t, ".ss0"))) ) {// TTDLX(Patch)
@ -233,8 +237,6 @@ FiosItem *FiosGetScenarioList(int *num, int mode)
} }
} }
} }
}
}
closedir(dir); closedir(dir);
} }