How to Create a New Directory in C

Jinku Hu Feb 02, 2024
  1. Use the mkdir Function to Create a New Directory
  2. Use the mkdirat Function to Create a New Directory
How to Create a New Directory in C

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 <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.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 <dirent.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.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);
}
Author: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

LinkedIn Facebook

Related Article - C File