lua-users home
lua-l archive

Re: loading lua scripts relative to an app's path

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Sat, 7 Oct 2006 03:37:07 -0700
"Wesley Smith" <wesley.hoke@gmail.com> wrote:
> So that works for windows, but is there a POSIX or another OS agnostic
> method for doing this?
I don't know about one, but it would be nice to have such a feature.
Some time ago I did some experiments, and you can do the following in
MacOSX, it will return the path of the executable image:
 #include <mach-o/dyld.h>
 #include <string.h>
 char* get_exepath(void)
 {
 size_t sz = 0;
 char *buf;
 char *sl;
 _NSGetExecutablePath(NULL, &sz);
 buf = (char*) malloc(++sz);
 _NSGetExecutablePath(buf, &sz);
 sl = strrchr(buf, '/');
 *(sl + 1) = '0円';
 return buf;
 }
Under Linux and maybe other systems with a similar "/proc" layout you
can use the following (of course, this needs "/proc" mounted!):
 #include <unistd.h>
 #include <string.h>
 #include <errno.h>
 char* get_exepath(void)
 {
 size_t sz = 100;
 char sym[1024];
 char *buf = (char*) malloc(sz);
 char *sl;
 snprintf(sym, "/proc/%i/exe", getpid());
 for (;;) {
 int ret = readlink(sym, buf, sz);
 if (ret == -1) return NULL;
 else if (ret < sz) {
 buf[ret] = '0円';
 break;
 }
 else {
 sz += 50;
 buf = (char*) realloc(buf, sz);
 }
 }
 sl = strrchr(buf, '/');
 *(sl + 1) = '0円';
 return buf;
 }
Just my two cents, hope this is useful for you ;-)
-- 
Futility Factor: No experiment is ever a complete failure - it can
always serve as a negative example.
-- 
"When I die, I want a tombstone that says "GAME OVER" - Ton Richters

Attachment: signature.asc
Description: PGP signature


AltStyle によって変換されたページ (->オリジナル) /