/* 13/07/88 : Jacques Gelinas Manipulation de path */ #include #include #include "vfs.h" #include /* Determine si un caractŠre est un s‚parateur de path */ int path_issep (char carac) { return (carac == '/'); } /* extrait le nom d'un fichier d'un path */ void path_filename (const char *path, char *name) { path_splitlex (path,NULL,name); } /* Ajoute conditionnellement le separateur a la fin d'un path ret peut etre identique a path */ void path_addback (char const *path, char *ret) { int last; if (ret != path) strcpy (ret,path); last = strlen(path)-1; if (last < 0){ strcpy (ret,"./"); }else if (path[last] != '/'){ ret [last+1] = '/'; ret [last+2] = '\0'; } } /* Elimine conditionnellement le separateur a la fin d'un path ret peut etre identique a path */ void path_stripsep (char const *path, char *ret) { int last = strlen(path)-1; if (ret != path) strcpy (ret,path); if (last > 0 && path[last] == '/'){ ret [last] = '\0'; } } /* Decompose un path lexicalement path peut etre identique a pathfile path peut etre NULL file peut etre identique a pathfile file peut etre NULL */ void path_splitlex (char const *pathfile, char *path, char *file) { char tmppath[PATH_MAX]; char *pt; if (path == NULL) path = tmppath; strcpy (path,pathfile); pt = path+strlen(path)-1; while (pt >= path && *pt != '/') pt--; pt++; if (file != NULL) strcpy (file,pt); *pt = '\0'; path_stripsep (path,path); } /* Decompose un path en reconnaissant la nature de chaque element du path /dir/dir/dir --> /dir/dir/dir * /dir/dir/file --> /dir/dir file */ void path_split (char const *path, char *prmpath, char *file) { char *newpath = prmpath; int ftype = file_type (path); if (ftype == 0 || ftype == -1 || strchr(path,'*')!=NULL || strchr(path,'?')!=NULL){ /* path est un fichier ou n'est pas un directory */ char const *pt = path+strlen(path)-1; while (pt >= path && *pt !='/') pt--; if (pt < path){ /* pas de '/' dans le path, donc le path est seulement un nom de fichier ou wildcard */ strcpy (newpath,""); strcpy (file,path); }else{ strcpy (file,pt+1); /* La derniere section est un fichier ou wild, le debut est le path */ while (path <= pt) *newpath++ = *path++; *newpath = '\0'; } }else if (ftype == 1){ strcpy (newpath,path); strcpy (file,"*"); } if (*prmpath == '\0'){ char curpath[PATH_MAX]; int ok = path_getcwd (curpath,sizeof(curpath)-1); assert (ok != -1); strcpy (prmpath,curpath); } path_stripsep (prmpath,prmpath); } /* Comme path_split, excepte que la partie fichier peut comporter plusieurs wildcard separe par des virgules ex : c:\prj\tool\*.c,*.asm */ int path_parmsplit (char const *path, char *newpath, char *wild[]) { static char file[PATH_MAX]; int nbf; path_split (path,newpath,file); nbf = wild_split (file,wild); return (nbf); } #ifdef TEST static void split (char *path) { char newpath[PATH_MAX]; char fname[PATH_MAX]; path_split (path,newpath,fname); printf ("%s -> '%s' '%s'\n",path,newpath,fname); } static void splitp (char *path) { char newpath[PATH_MAX]; char *fname[100]; int i=0; path_parmsplit (path,newpath,fname); printf ("%s -> '%s' ",path,newpath); while (fname[i]!=NULL) printf ("%s ",fname[i++]); printf ("\n"); } static void addback (char *p1,char *p2) { path_addback (p1,p2); printf ("%s : %s --> %s\n",p1==p2?"P1==P2":"P1!=P2",p1,p2); } void main (void) { char p1[10],p2[10]; strcpy (p1,"allo"); addback (p1,p2); addback (p1,p1); addback (p1,p2); addback (p1,p1); printf ("\n******* SPLIT ********\n"); split ("*.*"); split ("/allo/*.*"); split ("/*.*"); split ("/usr/prj1/tool"); split ("/usr/prj1/tool/*.c"); split ("/usr/prj1/tool/makefile"); printf ("\n******* PARMSPLIT ********\n"); splitp ("/allo/*.*"); splitp ("/*.*"); splitp ("*.*"); splitp ("/usr/prj1/tool"); splitp ("/usr/prj1/tool/*.c"); splitp ("/usr/prj1/tool/makefile"); splitp ("/usr/prj1/tool/makefile,*.c"); } #endif