bool Config::saveJoystickConfig()
{
+ bool ret = true;
char filename[PATH_MAX];
snprintf(filename, sizeof filename, "%sjoystick.cfg", engine->userHomeDirectory);
if (!fp)
{
- debug(("WARNING: Couldn't save joystick config\n"));
+ debug(("WARNING: Couldn't save joystick config: %s\n", strerror(errno)));
return false;
}
-
- fwrite(&joystick, sizeof(Joystick), 1, fp);
-
+
+ if (fwrite(&joystick, sizeof(Joystick), 1, fp) != 1)
+ {
+ debug(("WARNING: Couldn't save joystick config: %s\n", strerror(errno)));
+ ret = false;
+ }
+
fclose(fp);
- return true;
+ return ret;
}
bool Config::loadKeyConfig()
bool Config::saveKeyConfig()
{
+ bool ret = true;
char filename[PATH_MAX];
snprintf(filename, sizeof filename, "%skeyboard.cfg", engine->userHomeDirectory);
if (!fp)
{
+ debug(("WARNING: Couldn't save keyboard config: %s\n", strerror(errno)));
return false;
}
-
- fwrite(&keyboard, sizeof(keyboard), 1, fp);
-
+
+ if (fwrite(&keyboard, sizeof(keyboard), 1, fp) != 1)
+ {
+ debug(("WARNING: Couldn't save keyboard config: %s\n", strerror(errno)));
+ ret = false;
+ }
+
fclose(fp);
- return true;
+ return ret;
}
void Config::restoreKeyDefaults()
*/
bool Engine::unpack(const char *filename, int fileType)
{
+ bool ret = true;
+
if (fileType == PAK_DATA)
{
delete[] dataBuffer;
return false;
}
- fwrite(binaryBuffer, 1, pak.getUncompressedSize(), fp);
+ if (fwrite(binaryBuffer, 1, pak.getUncompressedSize(), fp) != pak.getUncompressedSize())
+ {
+ printf("Fatal Error: could not write to %s: %s", tempPath, strerror(errno));
+ ret = false;
+ }
fclose(fp);
}
- debug(("unpack() : Loaded %s (%d)\n", filename, pak.getUncompressedSize()));
+ debug(("unpack() : Loaded %s (%d), ret: %d\n", filename, pak.getUncompressedSize(), (int)ret));
- return true;
+ return ret;
}
bool Engine::loadData(const char *filename)
{
+ bool ret = true;
+
delete[] dataBuffer;
dataBuffer = NULL;
dataBuffer = new unsigned char[fSize + 1];
- fread(dataBuffer, 1, fSize, fp);
+ if (fread(dataBuffer, 1, fSize, fp) != (size_t)fSize)
+ ret = false;
+
dataBuffer[fSize] = 0;
fclose(fp);
- debug(("loadData() : Loaded %s (%d)\n", filename, fSize));
+ debug(("loadData() : Loaded %s (%d), ret: %d\n", filename, fSize, (int)ret));
- return true;
+ return ret;
}
void Engine::reportFontFailure()
}
fseek(pak, (-sizeof(Uint32)) * 2, SEEK_END);
- fread(&listPos, sizeof(Uint32), 1, pak);
- fread(&numberOfFiles, sizeof(Uint32), 1, pak);
+ if (fread(&listPos, sizeof(Uint32), 1, pak) != 1)
+ {
+ fclose(pak);
+ showPakErrorAndExit();
+ }
+ if (fread(&numberOfFiles, sizeof(Uint32), 1, pak) != 1)
+ {
+ fclose(pak);
+ showPakErrorAndExit();
+ }
debug(("Pak : File list resides at %d\n", (int)listPos));
debug(("Pak : Number of files are %d\n", (int)numberOfFiles));
input = new unsigned char[(int)(currentFile->cSize * 1.01) + 12];
*buffer = new unsigned char[currentFile->fSize + 1];
- fread(input, 1, currentFile->cSize, pak);
+ if (fread(input, 1, currentFile->cSize, pak) != currentFile->cSize)
+ {
+ fclose(pak);
+ showPakErrorAndExit();
+ }
uLongf fSize = (uLongf)currentFile->fSize;
{
save();
rewind(fp);
- fwrite(&header, sizeof(ReplayDataHeader), 1, fp);
+ int size = fwrite(&header, sizeof(ReplayDataHeader), 1, fp);
+ if (size != 1)
+ {
+ printf("Error saving replay data: %s\n", strerror(errno));
+ exit(1);
+ }
}
if (replayMode != REPLAY_MODE::NONE)
swapHeaderEndians();
- fwrite(&header, sizeof(ReplayDataHeader), 1, fp);
+ int size = fwrite(&header, sizeof(ReplayDataHeader), 1, fp);
+ if (size != 1)
+ {
+ printf("Error writing replay data header: %s\n", strerror(errno));
+ replayMode = REPLAY_MODE::NONE;
+ fclose(fp);
+ fp = NULL;
+ return;
+ }
reset();
}
*/
+#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
return true;
}
- fscanf(fp, "%10f %10d", &version, &release);
+ if (fscanf(fp, "%10f %10d", &version, &release) != 2)
+ {
+ rtn = true;
+ }
debug(("Version = %.2f - Expected %.2f\n", version, VERSION));
debug(("Release = %d - Expected %d\n", release, RELEASE));
rtn = true;
}
- fscanf(fp, "%10d %10d %10d %10d %10d %10d %10d", &engine.fullScreen, &game.musicVol, &game.soundVol, &game.output, &game.brightness, &engine.extremeAvailable, &game.gore);
+ if (fscanf(fp, "%10d %10d %10d %10d %10d %10d %10d", &engine.fullScreen, &game.musicVol, &game.soundVol, &game.output, &game.brightness, &engine.extremeAvailable, &game.gore) != 7)
+ {
+ rtn = true;
+ }
fclose(fp);
return 0;
}
- fscanf(fp, "%19s", privateKey);
+ if (fscanf(fp, "%19s", privateKey) != 1)
+ {
+ graphics.showMedalMessage(-1, "Medal Key file corrupt - Online functions disabled");
+ SDL_mutexV(medalServer.lock);
+ fclose(fp);
+ return 0;
+ }
fclose(fp);
while (true)
{
- fgets(line, 1024, fp);
-
+ if (!fgets(line, 1024, fp)) {
+ fclose(fp);
+ graphics.showErrorAndExit("Unexpected end of file reading save data", "");
+ }
+
sscanf(line, "%*c %[^\"] %*c %*c %[^\"] %*c %d %d", string[0], string[1], ¶m[0], ¶m[1]);
data = new Data();
while (true)
{
- fgets(line, 1024, fp);
-
+ if (!fgets(line, 1024, fp)) {
+ fclose(fp);
+ graphics.showErrorAndExit("Unexpected end of file reading save data", "");
+ }
+
sscanf(line, "%[^\n\r]", string[0]);
strlcpy(stageName, string[0], sizeof stageName);
{
break;
}
-
- fgets(line, 1024, fp);
+
+ if (!fgets(line, 1024, fp)) {
+ fclose(fp);
+ graphics.showErrorAndExit("Unexpected end of file reading save data", "");
+ }
sscanf(line, "%d", &numberOfLines);
debug(("Read %s with %d lines.\n", stageName, numberOfLines));
for (int i = 0 ; i < numberOfLines ; i++)
{
persistData = new PersistData();
-
- fgets(line, 1024, fp);
-
+
+ if (!fgets(line, 1024, fp)) {
+ fclose(fp);
+ graphics.showErrorAndExit("Unexpected end of file reading save data", "");
+ }
+
strlcpy(persistData->data, line, sizeof persistData->data);
//debug(("Read %d: %s", i, persistData->data));
graphics.showErrorAndExit("File write error whilst saving game", "");
}
- fwrite(&game, sizeof(Game), 1, fp);
+ if (fwrite(&game, sizeof(Game), 1, fp) != 1)
+ {
+ fclose(fp);
+ graphics.showErrorAndExit("File write error whilst saving game", strerror(errno));
+ }
fclose(fp);
fileData[files].set(filename, fSize, cSize, ftell(pak));
- fwrite(output, 1, cSize, pak);
+ if (fwrite(output, 1, cSize, pak) != cSize)
+ {
+ fprintf(stderr, "Error writing to pakfile: %s\n", strerror(errno));
+ fclose(pak);
+ exit(1);
+ }
files++;
}
pak = fopen(argv[argc - 1], "wb");
+ if (!pak)
+ {
+ fprintf(stderr, "Error opening %s: %s\n", argv[argc - 1], strerror(errno));
+ return 1;
+ }
for (int i = 1 ; i < (argc - 1) ; i++)
{
break;
}
- fwrite(&fileData[i], sizeof(FileData), 1, pak);
+ if (fwrite(&fileData[i], sizeof(FileData), 1, pak) != 1)
+ {
+ fprintf(stderr, "Error writing to %s: %s\n", argv[argc - 1], strerror(errno));
+ fclose(pak);
+ return 1;
+ }
}
unsigned int numberOfFiles = totalFiles;
- fwrite(&pos, sizeof(unsigned int), 1, pak);
- fwrite(&numberOfFiles, sizeof(unsigned int), 1, pak);
+ if (fwrite(&pos, sizeof(unsigned int), 1, pak) != 1)
+ {
+ fprintf(stderr, "Error writing to %s: %s\n", argv[argc - 1], strerror(errno));
+ fclose(pak);
+ return 1;
+ }
+ if (fwrite(&numberOfFiles, sizeof(unsigned int), 1, pak) != 1)
+ {
+ fprintf(stderr, "Error writing to %s: %s\n", argv[argc - 1], strerror(errno));
+ fclose(pak);
+ return 1;
+ }
fclose(pak);