I want to create a file on linux system in my program, say /a/b/c/test, currently I only have dir /a exist and I want to do it in my program, is there any function that can create file as well as missing dirs (that is, create b/c/test) in one deal? open, fopen, mkdir all seem can't work if there are missing dirs along the path. Thank you.
From strace mkdir -p a/b/c
:
mkdir("a", 0751) = 0
open("a", O_RDONLY|O_NOCTTY|O_NONBLOCK|O_LARGEFILE|O_DIRECTORY|O_NOFOLLOW) = 3
fchdir(3) = 0
close(3) = 0
mkdir("b", 0751) = 0
open("b", O_RDONLY|O_NOCTTY|O_NONBLOCK|O_LARGEFILE|O_DIRECTORY|O_NOFOLLOW) = 3
fchdir(3) = 0
close(3) = 0
mkdir("c", 0751) = 0
In other words, you have to call mkdir()
yourself, in a loop per directory, then create the file. This is how the mkdir
exe does it (in C). Why not run the mkdir -p /a/b/c
command with execve
? With C stuff set up for the call properly, of course.