Mail Archives: djgpp/2011/12/04/17:45:14
Aha, I didn't imagine that it may go wrong when use tools like redir.
I personally didn't face the problem yet but it may be an issue.
Georg, I fully understand what do you need because I need it for the
same purpose. Here's my code I use of portable function but it rely on
argv[0] so now I know it's not 100% perfect :(
/
****************************************************************************/
/* How to get program execution
directory */
/
****************************************************************************/
/* Created: 21.2.2011 [DJGPP/MINGW32/
LINUX] */
/* Last modified: 21.2.2011 [DJGPP/MINGW32/
LINUX] */
/* Copyright (C) 2011 by
RayeR */
/
****************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#if defined __WIN32__
#include <windows.h>
#elif defined __linux__
#endif
int getexecdir(char *argv0, char *obuffer, int osize)
{
char *last_backslash_in_path;
#ifdef __WIN32__
GetModuleFileName(NULL, obuffer, osize); // get exec path
obuffer[osize-1]=0; // trim to buffer size
#elif defined __linux__
char path[1024]; // auxiliary path
struct stat stat_buf;
strncpy(path, "/proc/self/exe", 1023); // exec path is in this file
while (1)
{
size_t size=readlink(path, obuffer, osize-1);
if (size==(size_t)~0)
{
obuffer[0]=0;
break;
}
else
{
if (size>=osize)
obuffer[osize-1]=0;
else
obuffer[size]=0;
int i=stat(obuffer, &stat_buf);
if (i==-1)
break;
else if (!S_ISLNK(stat_buf.st_mode))
break;
strncpy(obuffer, path, osize-1);
obuffer[osize-1]=0;
}
}
#else
strncpy(obuffer, argv0, osize);
obuffer[osize-1]=0;
#endif
last_backslash_in_path=strrchr(obuffer, '\\'); // find the last
backlash in the exec path
if (last_backslash_in_path==NULL) // if none
last_backslash_in_path=strrchr(obuffer, '/'); // try to find
normal slash in the exec path
if (last_backslash_in_path==NULL)
{
strcpy(obuffer, "./"); // use current directory
return(-1);
}
else // else copy exec path up to
last backslash (included)
last_backslash_in_path[1]=0; // cut off exec name from path
return(0);
}
int main(int argc, char *argv[])
{
char path[1024];
getexecdir(argv[0], path, 1024);
printf("execdir = %s\n", path);
return(0);
}
- Raw text -