Creating directory with mknod()

大憨熊 提交于 2019-12-11 02:56:52

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!