Making a New Directory

A C/C++ program may create a directory by calling upon the UNIX mkdir(2) system call. Its synopsis is as follows:

#include <sys/types.h>
#include <sys/stat.h>

int mkdir(const char *path, mode_t mode);

The argument path is the pathname of the new directory that is to be created. All intermediate directory names in the pathname must already exist. Only the last component of the pathname is actually created. The argument mode specifies the permission bits that are to be given to the new directory being created. In most cases, the S_ISGID, S_ISUID, and S_ISVTX bits are silently deleted from the value given in mode. The final permission bits assigned to the new directory are affected by applying the current umask(2) setting.

The function returns 0 when successful or -1 with a code in errno if it fails. A number of possible errors can be returned, but EROFS and EDQUOT are introduced in the following Note.

Note

EROFSā€”Read Only File System An attempt was made to create a directory when the file system has been mounted in read-only mode.

EDQUOT The directory create failed because the user's quota of disk blocks on the containing file system has been exhausted. Alternatively, the user's quota of i-nodes has been exhausted on the file system.


The following example shows how a directory /tmp/my_dir could be created from a C program:

int z;

z = mkdir("/tmp/my_dir",S_IRWXU|S_IRWXG|S_IROTH|S_IXOTH); /* 0775 */
if ( z == -1 )
    /* report error */

The example gives all access to the user and the group, and all others receive only read and execute. The final permissions given to the directory will be determined by the umask(2) that is in effect at the time.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.190.159.10