#include #include #include #include #include "tool.h" /* Test si l'‚criture ou la cr‚ation d'un fichier est permis Retourne != 0 si ok Si le fichier existe pas, v‚rifie si le r‚pertoire existe */ export int file_writeok (const char *fname) { int ret = 0; int type = file_type (fname); if (type == 0){ ret = access (fname,2) != -1; }else if (type == -1){ /* Le fichier n'existe pas, on v‚rifie si on peut ‚crire dans le r‚pertoire */ char path[MAXSIZ_PATH]; path_splitlex (fname,path,NULL); if (path[0] == '\0'){ /* Est-ce qu'on peut ecrire dans le r‚pertoire courant */ ret = access(".",2) != -1; }else if (file_type(path) == 1){ /* Est-ce qu'on peut ecrire dans ce repertoire */ ret = access(path,2) != -1; } } return ret; } /* Change l'attribut d'‚criture d'un fichier. Change uniquement l'attribut de l'usager. Retourne -1 si erreur. */ export int file_setmode (const char *fname, int writeok) { return chmod (fname,writeok ? S_IREAD|S_IWRITE : S_IREAD); } #ifdef TEST static void tst (const char *fname) { printf ("Ecriture %s = %s\n",fname,file_writeok(fname) ? "ok" : "Non"); } int main (int, char *[]) { if (system (">toto; chmod -w toto")!=-1){ tst ("toto"); tst ("toto.1"); tst ("/tmp1/toto.1"); if (file_unlink ("toto") == -1){ printf ("Ne peut detruire toto\n"); system ("chmod +w toto; rm toto"); } tst ("/home/solucor/jacques/vdi/xwin/xwin.old"); tst ("/home/solucor/jacques/vdi/xwin/xwin.h"); } return 0; } #endif