Re: [AD] alternate entry point for allegro programs

[ Thread Index | Date Index | More lists.liballeg.org/allegro-developers Archives ]


> Considering that using argv[0] can be unreliable at any rate, using this 
if 
> proc fs is available may not be a bad idea anyway (this can be done using 
> a configure check at compile time, I guess...).
> I think I'll go ahead and prepare a patch for that regardless.

Attached. Moderately tested on SuSE Linux 9.0 running kernel 2.4.21.
I haven't eliminated the need for END_OF_MAIN() on Linux yet, although I 
guess I could now. Note that with this patch theresult is already 
significantly better behaved than without it. Hacking the following code 
onto the beginning of tests/test.c (after allegro_init())

{
   char temp[1024];

   get_executable_name(temp, 1024);
   printf("%s\n", temp);
}

produces the output 
/home/allegro/Allegro/cvs/mainline/alleg_work/tests/test
with the patch and
tests/test
without.

My main concern is with the readlink function, which is neither ISO nor 
POSIX, but BSD 4.4. I don't think this matters much in practice.

Evert
--- allegro/src/unix/usystem.c	2004-07-28 19:56:56.000000000 +0200
+++ alleg_work/src/unix/usystem.c	2004-08-01 14:19:57.000000000 +0200
@@ -240,12 +240,33 @@
 #ifndef ALLEGRO_MACOSX
 
 /* _unix_get_executable_name:
- *  Return full path to the current executable.
+ *  Return full path to the current executable, use proc fs if available.
  */
 void _unix_get_executable_name(char *output, int size)
 {
+   char linkname[1024];
+   char filename[1024];
+   struct stat buf;
    char *path;
-
+   pid_t pid;
+   int len;
+   
+   /* get symolic link to executable from proc fs */
+   pid = getpid();
+   uszprintf (filename, sizeof(filename), "/proc/%d/exe", pid);
+   do_uconvert (filename, U_CURRENT, linkname, U_ASCII, size);
+   
+   if (stat (linkname, &buf) == 0) {
+      len = readlink (linkname, filename, sizeof(filename)-1);
+      if (len>-1) {
+	 filename[len] = '\0';
+         
+	 do_uconvert (filename, U_ASCII, output, U_CURRENT, size);
+	 return;
+      }
+   }
+   
+   /* Cannot stat file or resolve symlink, use argv[0] instead */
    /* If argv[0] has no explicit path, but we do have $PATH, search there */
    if (!strchr (__crt0_argv[0], '/') && (path = getenv("PATH"))) {
       char *start = path, *end = path, *buffer = NULL, *temp;


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/