1 #include "creaSystem.h"
7 #include <windows.h> /* GetModuleFileName */
11 #ifdef __APPLE__ /* assume this is OSX */
12 #include <sys/param.h>
13 #include <mach-o/dyld.h> /* _NSGetExecutablePath : must add -framework
14 CoreFoundation to link line */
17 # define PATH_MAX MAXPATHLEN
22 # define PATH_MAX 2048
38 int System::HasTTY() { return false; }
43 return isatty(fileno(stdin));
47 int System::GetAppPath(char *pname, size_t pathsize)
50 /* Oddly, the readlink(2) man page says no NULL is appended. */
51 /* So you have to do it yourself, based on the return value: */
52 pathsize --; /* Preserve a space to add the trailing NULL */
53 long result = readlink("/proc/self/exe", pname, pathsize);
56 pname[result] = 0; /* add the #@!%ing NULL */
58 if ((access(pname, 0) == 0))
59 return 0; /* file exists, return OK */
60 /*else name doesn't seem to exist, return FAIL (falls
66 long result = GetModuleFileName(NULL, pname, pathsize);
69 /* fix up the dir slashes... */
70 int len = strlen(pname);
72 for (idx = 0; idx < len; idx++)
74 if (pname[idx] == '\\') pname[idx] = '/';
77 for (idx = len-1; idx >=0 ; idx--)
79 if (pname[idx] == '/')
86 if ((access(pname, 0) == 0))
87 return 0; /* file exists, return OK */
88 /*else name doesn't seem to exist, return FAIL (falls
94 char *p = getexecname();
97 /* According to the Sun manpages, getexecname will
98 "normally" return an */
99 /* absolute path - BUT might not... AND that IF it is not,
101 /* getcwd() will "usually" be the correct thing... Urgh!
104 /* check pathname is absolute (begins with a / ???) */
105 if (p[0] == '/') /* assume this means we have an
108 strncpy(pname, p, pathsize);
109 if ((access(pname, 0) == 0))
110 return 0; /* file exists, return OK */
112 else /* if not, prepend getcwd() then check if file
115 getcwd(pname, pathsize);
116 long result = strlen(pname);
117 strncat(pname, "/", (pathsize - result));
119 strncat(pname, p, (pathsize - result));
121 if ((access(pname, 0) == 0))
122 return 0; /* file exists, return OK */
123 /*else name doesn't seem to exist, return FAIL
129 #ifdef MACOSX /* assume this is OSX */
131 from http://www.hmug.org/man/3/NSModule.html
133 extern int _NSGetExecutablePath(char *buf, unsigned long
136 _NSGetExecutablePath copies the path of the executable
137 into the buffer and returns 0 if the path was successfully
138 copied in the provided buffer. If the buffer is not large
139 enough, -1 is returned and the expected buffer size is
140 copied in *bufsize. Note that _NSGetExecutablePath will
141 return "a path" to the executable not a "real path" to the
142 executable. That is the path may be a symbolic link and
143 not the real file. And with deep directories the total
144 bufsize needed could be more than MAXPATHLEN.
148 char *given_path = (char*)malloc(MAXPATHLEN * 2);
149 if (!given_path) return status;
151 uint32_t npathsize = MAXPATHLEN * 2;
152 long result = _NSGetExecutablePath(given_path, &npathsize);
154 { /* OK, we got something - now try and resolve the real path...
156 if (realpath(given_path, pname) != NULL)
158 if ((access(pname, 0) == 0))
159 status = 0; /* file exists, return OK */
166 return -1; /* Path Lookup Failed */