-1
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>


int main(int argc, const char *argv[])
{
    char* original_path = "/home/userxxx/somedir/newdir";



    if (mkdir(original_path,0777) == -1) {
        perror(argv[0]);
        exit(EXIT_FAILURE);
    }



    return 0;
}

Please remember, this is not the system call, so i cannot use -p to create parent directories, i have to use the posix function , that should also create the parent and intermediate directories if they dont exist.

User89
  • 1
  • 2

1 Answers1

0

You can solve this using a recursive technique: in pseudocode

createFolder(folder)
{
    if folder.exists return // blocks the recursion
    createFolder(folder.parent)
    folder.makefolder // this will make a folder one level at a time
}

It has the attractive feature that a call to the function on a folder that exists is a no-op.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483