]> SAFE projects GIT repository - jmp/mailleur/commitdiff
adding procedure rou_do_mkpdir
authorJean-Marc Pigeon (Delson) <jmp@safe.ca>
Mon, 23 Jun 2025 14:21:20 +0000 (10:21 -0400)
committerJean-Marc Pigeon (Delson) <jmp@safe.ca>
Mon, 23 Jun 2025 14:21:20 +0000 (10:21 -0400)
lib/subrou.c
lib/subrou.h

index 47914786e22f97ed06a078afcbc47ca2f0b47ae7..061c82f9bb67bffc0dc0a69a4512d8a26231cabe 100644 (file)
@@ -4,6 +4,7 @@
 /*     Module for low level subroutine                 */
 /*                                                     */
 /********************************************************/
+#include        <sys/stat.h>
 #include        <sys/time.h>
 #include        <dirent.h>
 #include        <errno.h>
@@ -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
+}
+/*
+\f
+*/
+/********************************************************/
+/*                                                      */
 /*     Procedure to return the time difference between */
 /*     current real-time at nano-second level and      */
 /*      time reference.                                 */
index b5ec7fa2fe965ba6fe9a96d6133a2e0a786cb05a..90d256e7e832031d5c9d43e1659f677214d85b20 100644 (file)
@@ -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);