File: paths.c

package info (click to toggle)
hatari 2.5.0%2Bdfsg-1
  • links: PTS
  • area: main
  • in suites: trixie
  • size: 12,744 kB
  • sloc: ansic: 164,630; cpp: 8,685; python: 6,251; objc: 1,899; asm: 1,742; sh: 1,668; javascript: 146; makefile: 86; xml: 32
file content (351 lines) | stat: -rw-r--r-- 8,573 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
  Hatari - paths.c

  This file is distributed under the GNU General Public License, version 2
  or at your option any later version. Read the file gpl.txt for details.

  Set up the various path strings.
*/
const char Paths_fileid[] = "Hatari paths.c";

#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>

#include "main.h"
#include "file.h"
#include "paths.h"
#include "str.h"

#if defined(WIN32) && !defined(mkdir)
#define mkdir(name,mode) mkdir(name)
#endif  /* WIN32 */

#if defined(__APPLE__)
	#define HATARI_HOME_DIR "Library/Application Support/Hatari"
#elif defined(WIN32)
	#define HATARI_HOME_DIR "AppData\\Local\\Hatari"
#else
	#define HATARI_HOME_DIR ".config/hatari"
#endif

static char *sWorkingDir;     /* Working directory */
static char *sDataDir;        /* Directory where data files of Hatari can be found */
static char *sUserHomeDir;    /* User's home directory ($HOME) */
static char *sHatariHomeDir;  /* Hatari's home directory ($HOME/.hatari/) */
static char *sScreenShotDir;  /* Directory to use for screenshots */

/**
 * Return pointer to current working directory string
 */
const char *Paths_GetWorkingDir(void)
{
	return sWorkingDir;
}

/**
 * Return pointer to data directory string
 */
const char *Paths_GetDataDir(void)
{
	return sDataDir;
}

/**
 * Return pointer to user's home directory string
 */
const char *Paths_GetUserHome(void)
{
	return sUserHomeDir;
}

/**
 * Return pointer to Hatari's home directory string
 */
const char *Paths_GetHatariHome(void)
{
	return sHatariHomeDir;
}

/**
 * Return pointer to screenshot directory string
 */
const char *Paths_GetScreenShotDir(void)
{
	return sScreenShotDir;
}

/**
 * Set new screenshot directory location
 */
void Paths_SetScreenShotDir(const char *sNewDir)
{
	Str_Free(sScreenShotDir);
	sScreenShotDir = Str_Dup(sNewDir);
}

/**
 * Explore the PATH environment variable to see where our executable is
 * installed.
 */
static void Paths_GetExecDirFromPATH(const char *argv0, char *pExecDir, int nMaxLen)
{
	char *pPathEnv;
	char *pAct;
	char *pTmpName;
	const char *pToken;

	/* Get the PATH environment string */
	pPathEnv = getenv("PATH");
	if (!pPathEnv)
		return;
	/* Duplicate the string because strtok destroys it later */
	pPathEnv = strdup(pPathEnv);
	if (!pPathEnv)
		return;

	pTmpName = malloc(FILENAME_MAX);
	if (!pTmpName)
	{
		perror("Paths_GetExecDirFromPATH");
		free(pPathEnv);
		return;
	}

	/* If there is a semicolon in the PATH, we assume it is the PATH
	 * separator token (like on Windows), otherwise we use a colon. */
	if (strchr((pPathEnv), ';'))
		pToken = ";";
	else
		pToken = ":";

	pAct = strtok (pPathEnv, pToken);
	while (pAct)
	{
		snprintf(pTmpName, FILENAME_MAX, "%s%c%s",
		         pAct, PATHSEP, argv0);
		if (File_Exists(pTmpName))
		{
			/* Found the executable - so use the corresponding path: */
			Str_Copy(pExecDir, pAct, nMaxLen);
			break;
		}
		pAct = strtok (NULL, pToken);
	}

	free(pPathEnv);
	free(pTmpName);
}


/**
 * Locate the directory where the hatari executable resides
 */
static char *Paths_InitExecDir(const char *argv0)
{
	char *psExecDir;  /* Path string where the hatari executable can be found */

	/* Allocate memory for storing the path string of the executable */
	psExecDir = malloc(FILENAME_MAX);
	if (!psExecDir)
	{
		fprintf(stderr, "Out of memory (Paths_Init)\n");
		exit(-1);
	}

	/* Determine the bindir...
	 * Start with empty string, then try to use OS specific functions,
	 * and finally analyze the PATH variable if it has not been found yet. */
	psExecDir[0] = '\0';

#if defined(__linux__)
	{
		int i;
		/* On Linux, we can analyze the symlink /proc/self/exe */
		i = readlink("/proc/self/exe", psExecDir, FILENAME_MAX-1);
		if (i > 0)
		{
			char *p;
			psExecDir[i] = '\0';
			p = strrchr(psExecDir, '/');    /* Search last slash */
			if (p)
				*p = 0;                     /* Strip file name from path */
		}
	}
//#elif defined(WIN32)
//	/* On Windows we can use GetModuleFileName for getting the exe path */
//	GetModuleFileName(NULL, psExecDir, FILENAME_MAX);
#endif

	/* If we do not have the execdir yet, analyze argv[0] and the PATH: */
	if (psExecDir[0] == 0)
	{
		if (strchr(argv0, PATHSEP) == NULL)
		{
			/* No separator in argv[0], we have to explore PATH... */
			Paths_GetExecDirFromPATH(argv0, psExecDir, FILENAME_MAX);
		}
		else
		{
			/* There was a path separator in argv[0], so let's assume a
			 * relative or absolute path to the current directory in argv[0] */
			char *p;
			Str_Copy(psExecDir, argv0, FILENAME_MAX);
			p = strrchr(psExecDir, PATHSEP);  /* Search last slash */
			if (p)
				*p = 0;                       /* Strip file name from path */
		}
	}

	return psExecDir;
}


/**
 * Initialize the users home directory string
 * and Hatari's home directory (~/.config/hatari)
 */
static void Paths_InitHomeDirs(void)
{
	char *psHome;

	psHome = getenv("HOME");
	if (psHome)
	{
		sUserHomeDir = Str_Dup(psHome);
	}
#if defined(WIN32)
	else
	{
		char *psDrive;
		int len = 0;
		/* Windows home path? */
		psDrive = getenv("HOMEDRIVE");
		if (psDrive)
			len = strlen(psDrive);
		psHome = getenv("HOMEPATH");
		if (psHome)
			len += strlen(psHome);
		if (len > 0)
		{
			sUserHomeDir = Str_Alloc(len);
			if (psDrive)
				strcpy(sUserHomeDir, psDrive);
			if (psHome)
				strcat(sUserHomeDir, psHome);
		}
	}
#endif
	if (!sUserHomeDir)
	{
		/* $HOME not set, so let's use current working dir as home */
		sUserHomeDir = Str_Dup(sWorkingDir);
		sHatariHomeDir = Str_Dup(sWorkingDir);
		return;
	}

	sHatariHomeDir = Str_Alloc(strlen(sUserHomeDir) + 1 + strlen(HATARI_HOME_DIR));

	/* Try to use a private hatari directory in the users home directory */
	sprintf(sHatariHomeDir, "%s%c%s", sUserHomeDir, PATHSEP, HATARI_HOME_DIR);
	if (File_DirExists(sHatariHomeDir))
	{
		return;
	}
	/* Try legacy location ~/.hatari */
	sprintf(sHatariHomeDir, "%s%c.hatari", sUserHomeDir, PATHSEP);
	if (File_DirExists(sHatariHomeDir))
	{
		return;
	}

	/* Hatari home directory does not exists yet...
	 * ... so let's try to create it: */
#if !defined(__APPLE__) && !defined(WIN32)
	sprintf(sHatariHomeDir, "%s%c.config", sUserHomeDir, PATHSEP);
	if (!File_DirExists(sHatariHomeDir))
	{
		/* ~/.config does not exist yet, create it first */
		if (mkdir(sHatariHomeDir, 0700) != 0)
		{
			perror("Failed to create ~/.config directory");
		}
	}
#endif
	sprintf(sHatariHomeDir, "%s%c%s", sUserHomeDir, PATHSEP, HATARI_HOME_DIR);
	if (mkdir(sHatariHomeDir, 0750) != 0)
	{
		/* Failed to create, so use user's home dir instead */
		strcpy(sHatariHomeDir, sUserHomeDir);
	}
}


/**
 * Initialize directory names
 *
 * The datadir will be initialized relative to the bindir (where the executable
 * has been installed to). This means a lot of additional effort since we first
 * have to find out where the executable is. But thanks to this effort, we get
 * a relocatable package (we don't have any absolute path names in the program)!
 */
void Paths_Init(const char *argv0)
{
	char *psExecDir;  /* Path string where the hatari executable can be found */

	/* Init working directory string */
	sWorkingDir = malloc(FILENAME_MAX);
	if (!sWorkingDir || getcwd(sWorkingDir, FILENAME_MAX) == NULL)
	{
		/* This should never happen... just in case... */
		sWorkingDir = Str_Dup(".");
	}

	/* Init the user's home directory string */
	Paths_InitHomeDirs();

	/* Init screenshot directory string */
#if !defined(__APPLE__)
	sScreenShotDir = Str_Dup(sWorkingDir);
#else
	sScreenShotDir = Paths_GetMacScreenShotDir();
	if (!sScreenShotDir)
	{
		/* Failsafe, but should not be able to happen */
		sScreenShotDir = Str_Dup(sWorkingDir);
	}
#endif

	/* Get the directory where the executable resides */
	psExecDir = Paths_InitExecDir(argv0);

	/* Now create the datadir path name from the bindir path name: */
	sDataDir = Str_Alloc(FILENAME_MAX);
	if (psExecDir && strlen(psExecDir) > 0)
	{
		sprintf(sDataDir, "%s%c%s", psExecDir, PATHSEP, BIN2DATADIR);
	}
	else
	{
		/* bindir could not be determined, let's assume datadir is relative
		 * to current working directory... */
		strcpy(sDataDir, BIN2DATADIR);
	}

	/* And finally make a proper absolute path out of datadir: */
	File_MakeAbsoluteName(sDataDir);

	free(psExecDir);

	/* fprintf(stderr, " WorkingDir = %s\n DataDir = %s\n UserHomeDir = %s\n HatariHomeDir = %s\n ScrenShotDir = %s\n",
	        sWorkingDir, sDataDir, sUserHomeDir, sHatariHomeDir, sScreenShotDir); */
}

void Paths_UnInit(void)
{
	Str_Free(sWorkingDir);
	Str_Free(sDataDir);
	Str_Free(sUserHomeDir);
	Str_Free(sHatariHomeDir);
	Str_Free(sScreenShotDir);
}