LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to simulate "mkdir -p /home/blah1/blah2/blah3" in "c" where only /home exist (http://www.linuxquestions.org/questions/linux-newbie-8/how-to-simulate-mkdir-p-home-blah1-blah2-blah3-in-c-where-only-home-exist-759487/)

platinumedge 10-03-2009 07:48 PM

how to simulate "mkdir -p /home/blah1/blah2/blah3" in "c" where only /home exist
 
I'm trying to make use of mkdir(char *pathname, S_IRWXU) to create the directories.

but it only creates one directory at a time. so I have to separate the tokens for "/home/blah1/blah2/blah3" as "home blah1 blah2 blah3" using delimiter "/", but it is again hectic to create such directory structure.

I know Linux command "mkdir -p /home/blah1/blah2/blah3" would create all the sub-directories if it doesn't exist.

how to achieve the same "mkdir -p /home/blah1/blah2/blah3" in "c" where only /home exist

I'm using Redhat Linux 9 on Intel board 915GLVG

lutusp 10-03-2009 08:32 PM

Quote:

Originally Posted by platinumedge (Post 3706593)
I'm trying to make use of mkdir(char *pathname, S_IRWXU) to create the directories.

but it only creates one directory at a time. so I have to separate the tokens for "/home/blah1/blah2/blah3" as "home blah1 blah2 blah3" using delimiter "/", but it is again hectic to create such directory structure.

I know Linux command "mkdir -p /home/blah1/blah2/blah3" would create all the sub-directories if it doesn't exist.

how to achieve the same "mkdir -p /home/blah1/blah2/blah3" in "c" where only /home exist

I'm using Redhat Linux 9 on Intel board 915GLVG

Break up the path and repeat mkdir() as required until you get to the target.

manu-tm 10-03-2009 10:01 PM

here is an emulation of mkdir -p:

#include <stdio.h>
#include <string.h>
#include <sys/stat.h>

int mkdir2(char *pathname)
{
char pathname2[512 + 1];
int i;

if (strlen(pathname) > 512)
return 1;
else {
for (i = 0; i <= strlen(pathname); i++)
if (pathname[i] == '/' || pathname[i] == '\0') {
strncpy(pathname2, pathname, i);
pathname2[i] = '\0';
mkdir(pathname2, S_IRWXU);
}
return 0;
}
}

int main(int argc, char *argv[])
{
if (argc == 2) {
mkdir2(argv[1]);
return 0;
}
else {
printf("Usage: mkdir2 pathname\n");
return 1;
}
}

manu-tm 10-03-2009 10:04 PM

sorry, better like this:

Code:

#include <stdio.h>
#include <string.h>
#include <sys/stat.h>

int mkdir2(char *pathname)
{
        char        pathname2[512 + 1];
        int        i;

        if (strlen(pathname) > 512)
                return 1;
        else {
                for (i = 0; i <= strlen(pathname); i++)
                        if (pathname[i] == '/' || pathname[i] == '\0') {
                                strncpy(pathname2, pathname, i);
                                pathname2[i] = '\0';
                                mkdir(pathname2, S_IRWXU);
                        }
                return 0;
        }
}

int main(int argc, char *argv[])
{
        if (argc == 2) {
                mkdir2(argv[1]);
                return 0;
        }
        else {
                printf("Usage: mkdir2 pathname\n");
                return 1;
        }
}


platinumedge 10-04-2009 07:28 PM

thanks a lot
 
thanks a lot for your help manu-tm! I really appreciate your efforts. thanks once again


All times are GMT -5. The time now is 10:23 PM.