Ce n'est p-ê pas parfait (en fait ça ne l'est pas, en tout cas sur un Unixoïde c possible d'avoir un ":" dans un nom de répertoire dans le PATH, à condition de mettre des guillemets autour), mais voilà ce que je fais dans GTC :
char *gtcpath=alloca(strlen(argv[0])+40);
strcpy(gtcpath,argv[0]);
if (strrchr(gtcpath,DIR_SEPARATOR)) strrchr(gtcpath,DIR_SEPARATOR)[1]=0; // strip 'gtc.exe'
else {
#if 0
gtcpath=""; // 'gtc.exe' is in the current folder... (this assumption is OK under Win9x)
#else
// things don't stop here as WinXP sets argv[0] to be 'gtc.exe' if it is in the
// PATH environment variable... (and the same goes for Unixoids)
char *path=getenv("PATH");
while ((gtcpath="",path)) {
char *limit=strchr(path,ENVPATH_SEPARATOR),*base_limit=limit; char *buf;
FILE *fp;
if (!limit) limit=path+strlen(path);
if (path[0]=='"' && path[limit-path-1]=='"')
path++,limit--;
gtcpath=alloca(limit-path+2);
memcpy(gtcpath,path,limit-path); gtcpath[limit-path]=DIR_SEPARATOR; gtcpath[limit-path+1]=0;
buf=alloca(strlen(gtcpath)+20);
sprintf(buf,"%sgtc.exe",gtcpath);
if ((fp=fopen(buf,"rb"))) {
fclose(fp);
break;
}
#ifndef _WIN32
sprintf(buf,"%sgtc",gtcpath);
if ((fp=fopen(buf,"rb"))) {
fclose(fp);
break;
}
#endif
path = base_limit ? base_limit+1 : NULL;
}
#endif
}
avec
#ifdef PC
#ifdef _WIN32
#define DIR_SEPARATOR '\'
#define ENVPATH_SEPARATOR ';'
#else
#ifdef UNIXOID
#define DIR_SEPARATOR '/'
#define ENVPATH_SEPARATOR ':'
#else
#error Please define a directory and a $PATH separator for your platform.
#endif
#endif
#endif