1/*
2 * Unix support routines for PhysicsFS.
3 *
4 * Please see the file LICENSE.txt in the source's root directory.
5 *
6 * This file written by Ryan C. Gordon.
7 */
8
9#define __PHYSICSFS_INTERNAL__
10#include "physfs_platforms.h"
11
12#ifdef PHYSFS_PLATFORM_UNIX
13
14#include <ctype.h>
15#include <unistd.h>
16#include <stdlib.h>
17#include <sys/types.h>
18#include <pwd.h>
19#include <sys/stat.h>
20#include <sys/param.h>
21#include <dirent.h>
22#include <time.h>
23#include <errno.h>
24#include <limits.h>
25
26#if PHYSFS_NO_CDROM_SUPPORT
27#elif PHYSFS_PLATFORM_LINUX
28# define PHYSFS_HAVE_MNTENT_H 1
29#elif defined __CYGWIN__
30# define PHYSFS_HAVE_MNTENT_H 1
31#elif PHYSFS_PLATFORM_SOLARIS
32# define PHYSFS_HAVE_SYS_MNTTAB_H 1
33#elif PHYSFS_PLATFORM_BSD
34# define PHYSFS_HAVE_SYS_UCRED_H 1
35#else
36# warning No CD-ROM support included. Either define your platform here,
37# warning or define PHYSFS_NO_CDROM_SUPPORT=1 to confirm this is intentional.
38#endif
39
40#ifdef PHYSFS_HAVE_SYS_UCRED_H
41# ifdef PHYSFS_HAVE_MNTENT_H
42# undef PHYSFS_HAVE_MNTENT_H /* don't do both... */
43# endif
44# include <sys/mount.h>
45# include <sys/ucred.h>
46#endif
47
48#ifdef PHYSFS_HAVE_MNTENT_H
49#include <mntent.h>
50#endif
51
52#ifdef PHYSFS_HAVE_SYS_MNTTAB_H
53#include <sys/mnttab.h>
54#endif
55
56#ifdef PHYSFS_PLATFORM_FREEBSD
57#include <sys/sysctl.h>
58#endif
59
60
61#include "physfs_internal.h"
62
63int __PHYSFS_platformInit(void)
64{
65 return 1; /* always succeed. */
66} /* __PHYSFS_platformInit */
67
68
69void __PHYSFS_platformDeinit(void)
70{
71 /* no-op */
72} /* __PHYSFS_platformDeinit */
73
74
75void __PHYSFS_platformDetectAvailableCDs(PHYSFS_StringCallback cb, void *data)
76{
77#if (defined PHYSFS_NO_CDROM_SUPPORT)
78 /* no-op. */
79
80#elif (defined PHYSFS_HAVE_SYS_UCRED_H)
81 int i;
82 struct statfs *mntbufp = NULL;
83 int mounts = getmntinfo(&mntbufp, MNT_NOWAIT);
84
85 for (i = 0; i < mounts; i++)
86 {
87 int add_it = 0;
88
89 if (strcmp(mntbufp[i].f_fstypename, "iso9660") == 0)
90 add_it = 1;
91 else if (strcmp( mntbufp[i].f_fstypename, "cd9660") == 0)
92 add_it = 1;
93
94 /* add other mount types here */
95
96 if (add_it)
97 cb(data, mntbufp[i].f_mntonname);
98 } /* for */
99
100#elif (defined PHYSFS_HAVE_MNTENT_H)
101 FILE *mounts = NULL;
102 struct mntent *ent = NULL;
103
104 mounts = setmntent("/etc/mtab", "r");
105 BAIL_IF(mounts == NULL, PHYSFS_ERR_IO, /*return void*/);
106
107 while ( (ent = getmntent(mounts)) != NULL )
108 {
109 int add_it = 0;
110 if (strcmp(ent->mnt_type, "iso9660") == 0)
111 add_it = 1;
112 else if (strcmp(ent->mnt_type, "udf") == 0)
113 add_it = 1;
114
115 /* !!! FIXME: these might pick up floppy drives, right? */
116 else if (strcmp(ent->mnt_type, "auto") == 0)
117 add_it = 1;
118 else if (strcmp(ent->mnt_type, "supermount") == 0)
119 add_it = 1;
120
121 /* add other mount types here */
122
123 if (add_it)
124 cb(data, ent->mnt_dir);
125 } /* while */
126
127 endmntent(mounts);
128
129#elif (defined PHYSFS_HAVE_SYS_MNTTAB_H)
130 FILE *mounts = fopen(MNTTAB, "r");
131 struct mnttab ent;
132
133 BAIL_IF(mounts == NULL, PHYSFS_ERR_IO, /*return void*/);
134 while (getmntent(mounts, &ent) == 0)
135 {
136 int add_it = 0;
137 if (strcmp(ent.mnt_fstype, "hsfs") == 0)
138 add_it = 1;
139
140 /* add other mount types here */
141
142 if (add_it)
143 cb(data, ent.mnt_mountp);
144 } /* while */
145
146 fclose(mounts);
147#endif
148} /* __PHYSFS_platformDetectAvailableCDs */
149
150
151/*
152 * See where program (bin) resides in the $PATH specified by (envr).
153 * returns a copy of the first element in envr that contains it, or NULL
154 * if it doesn't exist or there were other problems. PHYSFS_SetError() is
155 * called if we have a problem.
156 *
157 * (envr) will be scribbled over, and you are expected to allocator.Free() the
158 * return value when you're done with it.
159 */
160static char *findBinaryInPath(const char *bin, char *envr)
161{
162 size_t alloc_size = 0;
163 char *exe = NULL;
164 char *start = envr;
165 char *ptr;
166
167 assert(bin != NULL);
168 assert(envr != NULL);
169
170 do
171 {
172 size_t size;
173 size_t binlen;
174
175 ptr = strchr(start, ':'); /* find next $PATH separator. */
176 if (ptr)
177 *ptr = '\0';
178
179 binlen = strlen(bin);
180 size = strlen(start) + binlen + 2;
181 if (size >= alloc_size)
182 {
183 char *x = (char *) allocator.Realloc(exe, size);
184 if (!x)
185 {
186 if (exe != NULL)
187 allocator.Free(exe);
188 BAIL(PHYSFS_ERR_OUT_OF_MEMORY, NULL);
189 } /* if */
190
191 alloc_size = size;
192 exe = x;
193 } /* if */
194
195 /* build full binary path... */
196 strcpy(exe, start);
197 if ((exe[0] == '\0') || (exe[strlen(exe) - 1] != '/'))
198 strcat(exe, "/");
199 strcat(exe, bin);
200
201 if (access(exe, X_OK) == 0) /* Exists as executable? We're done. */
202 {
203 exe[(size - binlen) - 1] = '\0'; /* chop off filename, leave '/' */
204 return exe;
205 } /* if */
206
207 start = ptr + 1; /* start points to beginning of next element. */
208 } while (ptr != NULL);
209
210 if (exe != NULL)
211 allocator.Free(exe);
212
213 return NULL; /* doesn't exist in path. */
214} /* findBinaryInPath */
215
216
217static char *readSymLink(const char *path)
218{
219 ssize_t len = 64;
220 ssize_t rc = -1;
221 char *retval = NULL;
222
223 while (1)
224 {
225 char *ptr = (char *) allocator.Realloc(retval, (size_t) len);
226 if (ptr == NULL)
227 break; /* out of memory. */
228 retval = ptr;
229
230 rc = readlink(path, retval, len);
231 if (rc == -1)
232 break; /* not a symlink, i/o error, etc. */
233
234 else if (rc < len)
235 {
236 retval[rc] = '\0'; /* readlink doesn't null-terminate. */
237 return retval; /* we're good to go. */
238 } /* else if */
239
240 len *= 2; /* grow buffer, try again. */
241 } /* while */
242
243 if (retval != NULL)
244 allocator.Free(retval);
245 return NULL;
246} /* readSymLink */
247
248
249char *__PHYSFS_platformCalcBaseDir(const char *argv0)
250{
251 char *retval = NULL;
252 const char *envr = NULL;
253
254 /* Try to avoid using argv0 unless forced to. Try system-specific stuff. */
255
256 #if defined(PHYSFS_PLATFORM_FREEBSD)
257 {
258 char fullpath[PATH_MAX];
259 size_t buflen = sizeof (fullpath);
260 int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
261 if (sysctl(mib, 4, fullpath, &buflen, NULL, 0) != -1)
262 retval = __PHYSFS_strdup(fullpath);
263 }
264 #endif
265
266 /* If there's a Linux-like /proc filesystem, you can get the full path to
267 * the current process from a symlink in there.
268 */
269
270 if (!retval && (access("/proc", F_OK) == 0))
271 {
272 retval = readSymLink("/proc/self/exe");
273 if (!retval) retval = readSymLink("/proc/curproc/file");
274 if (!retval) retval = readSymLink("/proc/curproc/exe");
275 if (!retval) retval = readSymLink("/proc/self/path/a.out");
276 if (retval == NULL)
277 {
278 /* older kernels don't have /proc/self ... try PID version... */
279 const unsigned long long pid = (unsigned long long) getpid();
280 char path[64];
281 const int rc = (int) snprintf(path,sizeof(path),"/proc/%llu/exe",pid);
282 if ( (rc > 0) && (rc < sizeof(path)) )
283 retval = readSymLink(path);
284 } /* if */
285 } /* if */
286
287 #if defined(PHYSFS_PLATFORM_SOLARIS)
288 if (!retval) /* try getexecname() if /proc didn't pan out. This may not be an absolute path! */
289 {
290 const char *path = getexecname();
291 if ((path != NULL) && (path[0] == '/')) /* must be absolute path... */
292 retval = __PHYSFS_strdup(path);
293 } /* if */
294 #endif
295
296 if (retval != NULL) /* chop off filename. */
297 {
298 char *ptr = strrchr(retval, '/');
299 if (ptr != NULL)
300 *(ptr+1) = '\0';
301 else /* shouldn't happen, but just in case... */
302 {
303 allocator.Free(retval);
304 retval = NULL;
305 } /* else */
306 } /* if */
307
308 /* No /proc/self/exe, etc, but we have an argv[0] we can parse? */
309 if ((retval == NULL) && (argv0 != NULL))
310 {
311 /* fast path: default behaviour can handle this. */
312 if (strchr(argv0, '/') != NULL)
313 return NULL; /* higher level parses out real path from argv0. */
314
315 /* If there's no dirsep on argv0, then look through $PATH for it. */
316 envr = getenv("PATH");
317 if (envr != NULL)
318 {
319 char *path = (char *) __PHYSFS_smallAlloc(strlen(envr) + 1);
320 BAIL_IF(!path, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
321 strcpy(path, envr);
322 retval = findBinaryInPath(argv0, path);
323 __PHYSFS_smallFree(path);
324 } /* if */
325 } /* if */
326
327 if (retval != NULL)
328 {
329 /* try to shrink buffer... */
330 char *ptr = (char *) allocator.Realloc(retval, strlen(retval) + 1);
331 if (ptr != NULL)
332 retval = ptr; /* oh well if it failed. */
333 } /* if */
334
335 return retval;
336} /* __PHYSFS_platformCalcBaseDir */
337
338
339char *__PHYSFS_platformCalcPrefDir(const char *org, const char *app)
340{
341 /*
342 * We use XDG's base directory spec, even if you're not on Linux.
343 * This isn't strictly correct, but the results are relatively sane
344 * in any case.
345 *
346 * https://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
347 */
348 const char *envr = getenv("XDG_DATA_HOME");
349 const char *append = "/";
350 char *retval = NULL;
351 size_t len = 0;
352
353 if (!envr)
354 {
355 /* You end up with "$HOME/.local/share/Game Name 2" */
356 envr = __PHYSFS_getUserDir();
357 BAIL_IF_ERRPASS(!envr, NULL); /* oh well. */
358 append = ".local/share/";
359 } /* if */
360
361 len = strlen(envr) + strlen(append) + strlen(app) + 2;
362 retval = (char *) allocator.Malloc(len);
363 BAIL_IF(!retval, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
364 snprintf(retval, len, "%s%s%s/", envr, append, app);
365 return retval;
366} /* __PHYSFS_platformCalcPrefDir */
367
368#endif /* PHYSFS_PLATFORM_UNIX */
369
370/* end of physfs_platform_unix.c ... */
371
372