tags:

views:

66

answers:

4

I am taking a file path from a user. I need to then access that file, and create it if it does not exist. I also need to create any intermediate directories if they don't exist as well.

For example, if my directory looks like:

mydir
|
+---subdir
|   |
|   +-- FileA
|
+---File1

I might receive mydir/subdir/FileA and would then access the relevant file. However, I might also receive mydir/NewDir/FileB.

How can I do this using Unix system calls in C? I have tried using open() with the (O_RDWR | O_CREAT) flags, but this didn't work out.

+3  A: 

I think this answer covers your question:

http://stackoverflow.com/questions/2336242/recursive-mkdir-system-call-on-unix

In short, you must create the subdirs yourself.

Aaron H.
+1  A: 

If you are allowed to use it, OS calls can be wrapped inside system() call. e.g. for listing files system( "ls" );

To create a directory including the intermediate ones, use mkdir with -p

mkdir -p mydir/NewDir/

(AFAIK, it doesn't hurt if NewDir already existed.)

To create an empty file, use touch

touch mydir/NewDir/FileB

Followup: I wrote this quick proof-of-concept program and tested in cygwin.

#include <stdlib.h>
#include <string.h>

int main() {

    char const * dir = "dir/subdir";
    char const * file = "file";

    char mkdirCmd[ 80 ] = { 0 };
    strcat( mkdirCmd, "mkdir -p " );
    strcat( mkdirCmd, dir );

    char touchCmd[ 80 ] = { 0 };
    strcat( touchCmd, "touch " );
    strcat( touchCmd, dir );
    strcat( touchCmd, "/" );
    strcat( touchCmd, file );

    system( mkdirCmd );
    system( touchCmd );

    return 0;
}

Unit test:

$ ls
mkdir.c  mkdir.exe*

$ ./mkdir.exe 

$ ls
dir/  mkdir.c  mkdir.exe*

$ ls -R dir
dir:
subdir/

dir/subdir:
file
ArunSaha
The `-p` flag is not universally available. Alas.
dmckee
@dmckee: I didn't know that, always found it available in the systems I have worked, linux, solaris, cygwin. It's listed in the opengroup spec: http://www.opengroup.org/onlinepubs/9699919799/utilities/mkdir.html
ArunSaha
Uh....ah...er...I wish to amend that statement. The `-p` flag as not universally available in the past, and may not exist on older installations. Yeah that's it. I would swear that I worked on such a system within the last couple of years, but can't for the life of me figure out which it was.
dmckee
@dmckee: Ah, I see; good that *now* it's available on most systems.
ArunSaha
A: 

Unfortunately there is no recursive 'makedir' so I'm scared that you might have to do it yourself.

But I think its gonna be easy doing it

function recursiveCreate(filepath) split the filepath by slash if last part create file else create directory end

Now you have your function :) using it will make you happier than using some UNIX defined function :)

ShyamLovesToCode
A: 

When I wrote the initial shell for HelenOS, I had to implement many of the standard core utilities as built in commands, such as cd, mkdir, cat, touch, etc.

Here is my implementation of mkdir (you only need one function), which you can pretty much use as-is (BSD licensed).

It first checks to see if the parent directory exists, if not, it tokenizes the desired path and creates all parent directories. That is (basically) your only option in most cases.

Disclaimer, I wrote the entire shell in one month, it was needed quickly for interactive testing of more important things such as dynamic loading, debugging and interaction with various file system drivers.

Tim Post