Remove some static length strings
There are too many MAX_PATH_CHARACTERS length string. make Initial_directory, Data_directory, Config_directory dynamically allocated.
This commit is contained in:
165
src/setup.c
165
src/setup.c
@@ -33,6 +33,10 @@
|
||||
#include <windows.h>
|
||||
#ifdef _MSC_VER
|
||||
#include <direct.h>
|
||||
#define strdup _strdup
|
||||
#if _MSC_VER < 1900
|
||||
#define snprintf _snprintf
|
||||
#endif
|
||||
#endif
|
||||
#elif defined(__macosx__)
|
||||
#import <CoreFoundation/CoreFoundation.h>
|
||||
@@ -53,6 +57,7 @@
|
||||
#include "io.h"
|
||||
#include "setup.h"
|
||||
#include "global.h"
|
||||
#include "gfx2log.h"
|
||||
|
||||
#ifndef PATH_MAX
|
||||
// This is a random default value ...
|
||||
@@ -70,13 +75,15 @@ int Create_ConfigDirectory(const char * config_dir)
|
||||
|
||||
// Determine which directory contains the executable.
|
||||
// IN: Main's argv[0], some platforms need it, some don't.
|
||||
// OUT: Write into program_dir. Trailing / or \ is kept.
|
||||
// OUT: program_dir. Trailing / or \ is kept.
|
||||
// Note : in fact this is only used to check for the datafiles and fonts in
|
||||
// this same directory.
|
||||
void Set_program_directory(const char * argv0, char * program_dir)
|
||||
char * Get_program_directory(const char * argv0)
|
||||
{
|
||||
char * program_dir;
|
||||
// MacOSX
|
||||
#if defined(__macosx__)
|
||||
program_dir = malloc(MAXPATHLEN);
|
||||
CFURLRef url = CFBundleCopyBundleURL(CFBundleGetMainBundle());
|
||||
(void)argv0; // unused
|
||||
CFURLGetFileSystemRepresentation(url,true,(UInt8*)program_dir,MAXPATHLEN);
|
||||
@@ -87,19 +94,19 @@ void Set_program_directory(const char * argv0, char * program_dir)
|
||||
// AmigaOS and alike: hard-coded volume name.
|
||||
#elif defined(__amigaos4__) || defined(__AROS__) || defined(__MORPHOS__) || defined(__amigaos__)
|
||||
(void)argv0; // unused
|
||||
strcpy(program_dir,"PROGDIR:");
|
||||
program_dir = strdup("PROGDIR:");
|
||||
#elif defined(__MINT__)
|
||||
static char path[1024]={0};
|
||||
char currentDrive='A';
|
||||
char currentDrive;
|
||||
|
||||
(void)argv0; // unused
|
||||
currentDrive=currentDrive+Dgetdrv();
|
||||
|
||||
Dgetpath(path,0);
|
||||
sprintf(program_dir,"%c:\%s",currentDrive,path);
|
||||
// Append trailing slash
|
||||
strcat(program_dir,PATH_SEPARATOR);
|
||||
currentDrive = 'A' + Dgetdrv();
|
||||
Dgetpath(path, 0);
|
||||
program_dir = malloc(4 + strlen(path) + 1);
|
||||
sprintf(program_dir,"%c:\\%s%s", currentDrive, path, PATH_SEPARATOR);
|
||||
#elif defined(__ANDROID__)
|
||||
(void)argv0; // unused
|
||||
progam_dir = malloc(MAX_PATH_CHARACTERS);
|
||||
getcwd(program_dir, MAX_PATH_CHARACTERS);
|
||||
strcat(program_dir, "/");
|
||||
// Linux: argv[0] unreliable
|
||||
@@ -109,54 +116,65 @@ void Set_program_directory(const char * argv0, char * program_dir)
|
||||
char path[PATH_MAX];
|
||||
if (readlink("/proc/self/exe", path, sizeof(path)) >= 0)
|
||||
{
|
||||
program_dir = malloc(strlen(path) + 1);
|
||||
Extract_path(program_dir, path);
|
||||
return;
|
||||
return program_dir;
|
||||
}
|
||||
}
|
||||
}
|
||||
program_dir = malloc(strlen(argv0) + 1);
|
||||
Extract_path(program_dir, argv0);
|
||||
|
||||
// Others: The part of argv[0] before the executable name.
|
||||
// Keep the last \ or /.
|
||||
// On Windows, Mingw32 already provides the full path in all cases.
|
||||
#else
|
||||
program_dir = malloc(strlen(argv0) + 1);
|
||||
Extract_path(program_dir, argv0);
|
||||
#endif
|
||||
return program_dir;
|
||||
}
|
||||
|
||||
// Determine which directory contains the read-only data.
|
||||
// IN: The directory containing the executable
|
||||
// OUT: Write into data_dir. Trailing / or \ is kept.
|
||||
void Set_data_directory(const char * program_dir, char * data_dir)
|
||||
/// Determine which directory contains the read-only data.
|
||||
/// @param program_dir The directory containing the executable
|
||||
/// @return the path. Trailing / or \ is kept.
|
||||
char * Get_data_directory(const char * program_dir)
|
||||
{
|
||||
const char * to_append;
|
||||
// On all platforms, data is relative to the executable's directory
|
||||
strcpy(data_dir,program_dir);
|
||||
// On MacOSX, it is stored in a special folder:
|
||||
#if defined(__macosx__)
|
||||
strcat(data_dir,"Contents/Resources/");
|
||||
// On GP2X, AROS and Android, executable is not in bin/
|
||||
// On MacOSX, it is stored in a special folder:
|
||||
to_append = "Contents/Resources/";
|
||||
#elif defined (__GP2X__) || defined (__gp2x__) || defined (__WIZ__) || defined (__CAANOO__) || defined(GCWZERO) || defined(__AROS__) || defined(__ANDROID__)
|
||||
//strcat(data_dir,"share/grafx2/");
|
||||
strcat(data_dir, "data/");
|
||||
//on tos, the same directory is used for everything
|
||||
// On GP2X, AROS and Android, executable is not in bin/
|
||||
to_append = "data/";
|
||||
#elif defined (__MINT__)
|
||||
strcpy(data_dir, program_dir);
|
||||
//on switch, we store everything in the SD card in /switch/grafx2
|
||||
//on tos, the same directory is used for everything
|
||||
return strdup(program_dir);
|
||||
#elif defined(__SWITCH__)
|
||||
strcpy(data_dir,"/switch/grafx2/");
|
||||
// Haiku provides us with an API to find it.
|
||||
//on switch, we store everything in the SD card in /switch/grafx2
|
||||
return strdup("/switch/grafx2/");
|
||||
#elif defined(__HAIKU__)
|
||||
if (find_path(Set_data_directory, B_FIND_PATH_DATA_DIRECTORY, "grafx2/", data_dir, PATH_MAX) != B_OK)
|
||||
// Haiku provides us with an API to find it.
|
||||
char * data_dir = malloc(PATH_MAX);
|
||||
if (find_path(Get_data_directory, B_FIND_PATH_DATA_DIRECTORY, "grafx2/", data_dir, PATH_MAX) == B_OK)
|
||||
{
|
||||
return data_dir;
|
||||
}
|
||||
else
|
||||
{
|
||||
// If the program is not installed, find_path will fail. Try from local dir then.
|
||||
strcat(data_dir,"../share/grafx2/");
|
||||
free(data_dir);
|
||||
to_append = "../share/grafx2/";
|
||||
}
|
||||
|
||||
#elif defined(WIN32)
|
||||
strcat(data_dir,"..\\share\\grafx2\\");
|
||||
// All other targets, program is in a "bin" subdirectory
|
||||
to_append = "..\\share\\grafx2\\";
|
||||
#else
|
||||
strcat(data_dir,"../share/grafx2/");
|
||||
// All other targets, program is in a "bin" subdirectory
|
||||
to_append = "../share/grafx2/";
|
||||
#endif
|
||||
|
||||
return Filepath_append_to_dir(program_dir, to_append);
|
||||
}
|
||||
|
||||
// Determine which directory should store the user's configuration.
|
||||
@@ -170,37 +188,39 @@ void Set_data_directory(const char * program_dir, char * data_dir)
|
||||
// own directory.
|
||||
// IN: The directory containing the executable
|
||||
// OUT: Write into config_dir. Trailing / or \ is kept.
|
||||
void Set_config_directory(const char * program_dir, char * config_dir)
|
||||
char * Get_config_directory(const char * program_dir)
|
||||
{
|
||||
// AmigaOS4 provides the PROGIR: alias to the directory where the executable is.
|
||||
// AmigaOS4 provides the PROGDIR: alias to the directory where the executable is.
|
||||
#if defined(__amigaos4__) || defined(__AROS__)
|
||||
strcpy(config_dir,"PROGDIR:");
|
||||
return strdup("PROGDIR:");
|
||||
// GP2X
|
||||
#elif defined(__GP2X__) || defined(__WIZ__) || defined(__CAANOO__)
|
||||
// On the GP2X, the program is installed to the sdcard, and we don't want to mess with the system tree which is
|
||||
// on an internal flash chip. So, keep these settings locals.
|
||||
strcpy(config_dir,program_dir);
|
||||
return strdup(program_dir);
|
||||
// For TOS we store everything in the program dir
|
||||
#elif defined(__MINT__)
|
||||
strcpy(config_dir,program_dir);
|
||||
return strdup(program_dir);
|
||||
//on switch, we store everything in the SD card in /switch/grafx2
|
||||
#elif defined(__SWITCH__)
|
||||
strcpy(config_dir,"/switch/grafx2/");
|
||||
// For all other platforms, there is some kind of settigns dir to store this.
|
||||
return strdup("/switch/grafx2/");
|
||||
// For all other platforms, there is some kind of settings dir to store this.
|
||||
#else
|
||||
char filename[MAX_PATH_CHARACTERS];
|
||||
char * config_dir;
|
||||
size_t len;
|
||||
char * filename;
|
||||
#ifdef GCWZERO
|
||||
strcpy(config_dir, "/media/home/.grafx2/");
|
||||
config_dir = strdup("/media/home/.grafx2/");
|
||||
#elif defined(__macosx__)
|
||||
// On all the remaining targets except OSX, the executable is in ./bin
|
||||
config_dir = strdup(program_dir);
|
||||
#else
|
||||
// In priority: check root directory
|
||||
strcpy(config_dir, program_dir);
|
||||
len = strlen(program_dir) + 4;
|
||||
config_dir = malloc(len);
|
||||
snprintf(config_dir, len, "%s%s", program_dir, "../");
|
||||
#endif
|
||||
// On all the remaining targets except OSX, the executable is in ./bin
|
||||
#if !defined(__macosx__)
|
||||
strcat(config_dir, "../");
|
||||
#endif
|
||||
strcpy(filename, config_dir);
|
||||
strcat(filename, CONFIG_FILENAME);
|
||||
|
||||
filename = Filepath_append_to_dir(config_dir, CONFIG_FILENAME);
|
||||
|
||||
if (File_exists(filename))
|
||||
{
|
||||
@@ -230,7 +250,7 @@ void Set_config_directory(const char * program_dir, char * config_dir)
|
||||
#elif defined(__MINT__)
|
||||
const char* Config_SubDir = "";
|
||||
printf("GFX2.CFG not found in %s\n",filename);
|
||||
strcpy(config_parent_dir, config_dir);
|
||||
config_parent_dir = strdup(config_dir);
|
||||
#else
|
||||
// ~/.config/grafx2
|
||||
const char* Config_SubDir;
|
||||
@@ -238,45 +258,52 @@ void Set_config_directory(const char * program_dir, char * config_dir)
|
||||
if (config_parent_dir)
|
||||
Config_SubDir = "grafx2";
|
||||
else {
|
||||
Config_SubDir = ".config/grafx2";
|
||||
config_parent_dir = getenv("HOME");
|
||||
Config_SubDir = ".config/grafx2";
|
||||
config_parent_dir = getenv("HOME");
|
||||
}
|
||||
#endif
|
||||
Portable_Installation_Detected = 0;
|
||||
|
||||
if (config_parent_dir && config_parent_dir[0]!='\0')
|
||||
{
|
||||
int size = strlen(config_parent_dir);
|
||||
strcpy(config_dir, config_parent_dir);
|
||||
size_t size = strlen(config_parent_dir);
|
||||
free(config_dir);
|
||||
len = size + strlen(Config_SubDir) + strlen(PATH_SEPARATOR) + 1;
|
||||
if (config_parent_dir[size-1] != '\\' && config_parent_dir[size-1] != '/')
|
||||
{
|
||||
strcat(config_dir,PATH_SEPARATOR);
|
||||
}
|
||||
strcat(config_dir,Config_SubDir);
|
||||
if (Directory_exists(config_dir))
|
||||
{
|
||||
// Répertoire trouvé, ok
|
||||
strcat(config_dir,PATH_SEPARATOR);
|
||||
len += strlen(PATH_SEPARATOR);
|
||||
config_dir = malloc(len);
|
||||
snprintf(config_dir, len, "%s%s%s%s",
|
||||
config_parent_dir, PATH_SEPARATOR,
|
||||
Config_SubDir, PATH_SEPARATOR);
|
||||
}
|
||||
else
|
||||
{
|
||||
config_dir = malloc(len);
|
||||
snprintf(config_dir, len, "%s%s%s",
|
||||
config_parent_dir,
|
||||
Config_SubDir, PATH_SEPARATOR);
|
||||
}
|
||||
if (!Directory_exists(config_dir))
|
||||
{
|
||||
// Tentative de création
|
||||
if (!Create_ConfigDirectory(config_dir))
|
||||
{
|
||||
// Réussi
|
||||
strcat(config_dir,PATH_SEPARATOR);
|
||||
}
|
||||
else
|
||||
if (Create_ConfigDirectory(config_dir) < 0)
|
||||
{
|
||||
// Echec: on se rabat sur le repertoire de l'executable.
|
||||
strcpy(config_dir,program_dir);
|
||||
Portable_Installation_Detected = 1;
|
||||
free(config_dir);
|
||||
#if defined(__macosx__)
|
||||
strcat(config_dir, "../");
|
||||
len = strlen(program_dir) + 4;
|
||||
config_dir = malloc(len);
|
||||
snprintf(config_dir, len, "%s%s", program_dir, "../");
|
||||
#else
|
||||
config_dir = strdup(program_dir);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
free(filename);
|
||||
return config_dir;
|
||||
#endif
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user