aiokos aiokos - 2 years ago 87
C Question

Relative File Opening with `open` When Directory Doesn't Exist

I am following the example code in Hacking: The Art of Exploitation and am running into an issue when running the following.

datafile = (char *) ec_malloc(20);
strcpy(datafile, "/tmp/notes");

fd = open(datafile, O_WRONLY|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR);
if (fd == -1)
fatal("in main() while opening file");

The error message is:

[!!] Fatal Error in main() while opening file: No such file or directory

Where the bit before "No such file or directory" is created with the custom

Note that
does not exist in the program's location, but the
flag should account for that (or so I thought). I've tried switching the
segment to
but the result was the same. How can I create directories that don't already exist within the directory where the program is called?

I am using MinGW on Windows 10 and am compiling w/

Answer Source

You say /tmp/notes does not exist "in the program's location." This implies that you are running the program in a directory FOO and expecting it to create a file like FOO/tmp/notes. But what you actually asked for is /tmp/notes, which is an absolute path. You probably want tmp/notes without the leading slash, but that too is a directory plus a file, and open() won't create the directory. So either just use notes as the file to open in the current directory, or use tmp/notes and first call mkdir("tmp").

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download