#include #include #include #include "ostool.h" #include "commun.h" /* recherche un fichier ou un repertoire en revenant vers la racine Si le fichier n'est dans le path fournis, remonte d'un repertoire et essai. Retourne 0 si ok, -1 si erreur. */ export int file_findanc ( const char *path, /* path ou on pense trouver le fichier */ /* "" -> path courant */ const char *nom, /* nom du fichier ou du repertoire */ char *filepath) /* contiendra le path trouve pour ce fichier */ /* peut ˆtre NULL */ { int ret = -1; char wpath[MAXSIZ_PATH]; char tmpfpath[MAXSIZ_PATH]; if (filepath == NULL) filepath = tmpfpath; path_addback (path,wpath); if (path_setabs (wpath,wpath)!=-1){ while (1){ path_make (wpath,nom,filepath); if (file_type(filepath) >= 0){ ret = 0; break; }else if(path_isroot(wpath)){ break; }else{ char dummy[MAXSIZ_NAME]; int len = strlen(wpath); path_stripsep (wpath,wpath); path_splitlex (wpath,wpath,dummy); if ((int)strlen(wpath)==len) break; } } } return (ret); } /* recherche un fichier ou un repertoire en revenant vers la racine Si le fichier n'est pas dans le path fournis, remonte d'un repertoire et essai. Retourne 0 si ok, -1 si erreur. */ export int file_findancp ( const char *fpath, /* path du fichier ou r‚pertoire cherch‚ */ char *fname) /* path o— il … ‚t‚ trouv‚ */ { char path[MAXSIZ_PATH]; char name[MAXSIZ_NAME]; path_splitlex (fpath,path,name); return (file_findanc(path,name,fname)); } #ifdef TEST static void tst1 (char *path, char *file) { char newpath[200]; int ret = file_findanc (path,file,newpath); printf ("Cherche %s %s\n",path,file); if (ret != -1){ printf (" trouve %s\n",newpath); }else{ printf (" Ne trouve pas\n"); } } static void tst (char *path) { tst1 (path,"file2.c"); tst1 (path,"rules.mak"); tst1 (path,"autoexec.bat"); tst1 (path,"absent"); } void main() { tst (""); tst ("c:\\prj\\tool"); tst ("c:."); tst ("c:"); tst (".."); tst ("..\\"); tst ("c:.\\"); } #endif