From b5ce1584a71b5a56b035af01c604c89e7b5506b5 Mon Sep 17 00:00:00 2001 From: "Jean-Marc Pigeon (Delson)" Date: Mon, 23 Jun 2025 10:21:20 -0400 Subject: [PATCH] adding procedure rou_do_mkpdir --- lib/subrou.c | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/subrou.h | 3 ++ 2 files changed, 80 insertions(+) diff --git a/lib/subrou.c b/lib/subrou.c index 4791478..061c82f 100644 --- a/lib/subrou.c +++ b/lib/subrou.c @@ -4,6 +4,7 @@ /* Module for low level subroutine */ /* */ /********************************************************/ +#include #include #include #include @@ -156,6 +157,82 @@ return taille; */ /********************************************************/ /* */ +/* Procedure to create directory with all up */ +/* directories if they are missing. */ +/* Return true if successfull. */ +/* */ +/********************************************************/ +PUBLIC _Bool rou_do_mkpdir(char *dirpath) + +{ +#define OPEP "subrou.c:rou_do_mkpdir," +_Bool done; +int taille; +char *ptr; +char locdir[PATH_MAX]; +int phase;; +_Bool proceed; + +done=false; +(void) strcpy(locdir,dirpath); +ptr=(char *)0; +taille=strlen(locdir); +phase=0; +proceed=(taille>0); +while (proceed==true) { + switch (phase) { + case 0 : //make sur directory is starting with / + if (locdir[0]!='/') { + (void) rou_alert(0,"%s, <%s> is not a directory name (Bug?)",OPEP,locdir); + phase=999; //Trouble No need to go further! + } + break; + case 1 : //cleaning directory ending buy '/' + taille--; + if ((taille>1)&&(locdir[taille]=='/')) + locdir[taille]='\000'; + break; + case 2 : //is directory existing? + DIR *dirp; + + if ((dirp=opendir(locdir))!=(DIR *)0) { + (void) closedir(dirp); + done=true; + phase=999; //No need to go further + } + break; + case 3 : //is directory up existing? + if ((ptr=strrchr(locdir,'/'))!=(char *)0) { + *ptr='\000'; + if (rou_do_mkpdir(locdir)==false) + phase=999; //unable to create up directory! + } + break; + case 4 : //creating the requested directory + if (mkdir(dirpath,0755)<0) { + (void) rou_alert(0,"%s, unable to create directory <%s> (error=<%s>)", + OPEP,dirpath,strerror(errno)); + phase=999; //unable to create directory! + } + break; + case 5 : //everything fine + done=true; + break; + default : //SAFE Guard + proceed=false; + break; + } + phase++; + } +return done; + +#undef OPEP +} +/* + +*/ +/********************************************************/ +/* */ /* Procedure to return the time difference between */ /* current real-time at nano-second level and */ /* time reference. */ diff --git a/lib/subrou.h b/lib/subrou.h index b5ec7fa..90d256e 100644 --- a/lib/subrou.h +++ b/lib/subrou.h @@ -46,6 +46,9 @@ extern int rou_vasprintf(char **str,const char *fmt,va_list ap); //procedure to assign memory according a format and parameter list extern int rou_asprintf(char **str,const char *fmt,...); +//procedure to create needed subdirectory +extern _Bool rou_do_mkpdir(char *dirpath); + //procedure to get the time difference between the current //time and a TIMESPEC starttime extern unsigned rou_getdifftime(TIMESPEC *start); -- 2.47.3