Create a New Directory in C

  1. Use the mkdir Function to Create a New Directory
  2. Use the mkdirat Function to Create a New Directory

This article will demonstrate multiple methods about how to create a new directory in C.

Use the mkdir Function to Create a New Directory

mkdir is a POSIX compliant function that can be used to create a new directory. The function takes two arguments - the first of which is the char pointer pointing to the pathname of the newly-created directory, and the second argument specifies the permission bits, which are denoted by a bit-mask and can be set using the predefined macro constants.

In the following example, we declare a constant string variable to be passed as a directory name and specify the S_IRWXU mode bits, which implies that the owner will have read/write/execute permissions on the directory.

#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>

const char *name = "Arbitrary Directory";

int main(void) {
    mkdir(name, S_IRWXU);

    exit(EXIT_SUCCESS);
}

Even though we put mkdir call in a single line code in the previous example, it’s important to implement error checking routines when working with the production level code. At first, we should check that a new directory is created and the later sections of the code can execute successfully if they depend on the given directory. Note that mkdir returns -1 when the error occurs and sets errno accordingly. In this case, we implemented a switch statement to check some error codes and print the corresponding messages to the console.

#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <errno.h>

const char *name = "Arbitrary Directory";

int main(void) {

    errno = 0;
    int ret = mkdir(name, S_IRWXU);
    if (ret == -1) {
        switch (errno) {
            case EACCES :
                printf("the parent directory does not allow write");
                exit(EXIT_FAILURE);
            case EEXIST:
                printf("pathname already exists");
                exit(EXIT_FAILURE);
            case ENAMETOOLONG:
                printf("pathname is too long");
                exit(EXIT_FAILURE);
            default:
                perror("mkdir");
                exit(EXIT_FAILURE);
        }
    }

    exit(EXIT_SUCCESS);
}

Use the mkdirat Function to Create a New Directory

mkdirat is another system call that operates similarly, except that it takes three arguments. The first one is the directory file descriptor that can be retrieved with dirfd system call. Note that this file descriptor is used when the pathname specified as the second argument is a relative. In that case, the path is interpreted relative to the given directory rather than to the current working directory.

mkdirat has the same return type values as the mkdir and errno values can be checked accordingly (see the full list on this page). The following example code attempts to create a new directory in the previous directory to the current one.

#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <sys/stat.h>
#include <errno.h>

const char *name = "Arbitrary Directory";

int main(void) {

    DIR *dir = opendir("../");
    int dfd = dirfd(dir);
    errno = 0;
    int ret = mkdirat(dfd, name, S_IRWXU);
    if (ret == -1) {
        switch (errno) {
            case EACCES :
                printf("the parent directory does not allow write");
                exit(EXIT_FAILURE);
            case EEXIST:
                printf("pathname already exists");
                exit(EXIT_FAILURE);
            case ENAMETOOLONG:
                printf("pathname is too long");
                exit(EXIT_FAILURE);
            default:
                perror("mkdir");
                exit(EXIT_FAILURE);
        }

    }
    closedir(dir);

    exit(EXIT_SUCCESS);
}
Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - C File

  • Read a File in C
  • Write to File in C