问题
I need to create a directory using mknod()
(use of mkdir()
is not allow in my case), I would call the program from a certain directory and introduce the path where the new dir should be created inside the previous one.
Ex: If I'm /home/user/test/
and inside test
there is /level1/
, I want to create the directory level2
inside level1
, so I would pass the argument /level1/level2/
I have the following code that works when I create a pipe,but when I change the mode to S_IFDIR
, it doesn't do anything.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
void main(int argc,char *argv[]){
int status;
if ((status = mknod(argv[1], S_IFDIR,0)) == 1){
printf("error\n" );
}
exit(0);
}
回答1:
mknod
is normally used for creating device nodes (special directories).
However, some OSs do support creating a regular directory with mknod
(QNX).
Did you check the man page for mknod on the OS you're using? I am quite sure that S_IFDIR
is a non-portable option for mknod
.
来源:https://stackoverflow.com/questions/26811662/creating-directory-with-mknod