--- libaitio/src/aitio.c 2010/02/23 22:54:52 1.1 +++ libaitio/src/aitio.c 2010/09/10 12:39:41 1.2 @@ -3,12 +3,15 @@ * by Michael Pounov * * $Author: misho $ -* $Id: aitio.c,v 1.1 2010/02/23 22:54:52 misho Exp $ +* $Id: aitio.c,v 1.2 2010/09/10 12:39:41 misho Exp $ * *************************************************************************/ #include "global.h" +int io_Debug; + + #pragma GCC visibility push(hidden) int io_Errno; @@ -302,3 +305,52 @@ char *ioRegexReplace(const char *csRegex, const char * return str; } + + +/* + * ioMkDir() Function for racursive directory creation and validation + * @csDir = Full directory path + * @mode = Mode for directory creation if missing dir + * return: -1 error, 0 directory path exist, >0 created missing dirs +*/ +int +ioMkDir(const char *csDir, int mode) +{ + char *str, *s, *pbrk, szOld[MAXPATHLEN] = { 0 }; + register int cx = -1; + + if (!csDir) + return cx; + + str = strdup(csDir); + if (!str) { + LOGERR; + return cx; + } + + getcwd(szOld, MAXPATHLEN); + if (*str == '/') + chdir("/"); + + for (cx = 0, s = strtok_r(str, "/", &pbrk); s; s = strtok_r(NULL, "/", &pbrk)) { + if (mkdir(s, mode) == -1) { + if (errno != EEXIST) { + LOGERR; + cx = -1; + goto end; + } + } else + cx++; + + if (chdir(s) == -1) { + LOGERR; + cx = -1; + goto end; + } + } +end: + chdir(szOld); + free(str); + return cx; +} +