gtpc2m4rC/C++ Language Support User's Guide

mkdir-Make a Directory

This function creates a directory.

Format

#include <sys/stat.h>
int mkdir(char *pathname, mode_t mode);

pathname
The name of the new directory.

mode
The access mode for the new directory.

This function creates a new, empty directory called pathname. The file permission bits in mode are modified by the file creation mask of the process and then used to set the file permission bits of the directory being created. For more information about the file creation mask, see umask-Set the File Mode Creation Mask; for information about the file permission bits, see chmod-Change the Mode of a File or Directory.

If the S_ISGID mode bit is set OFF in the parent directory, the group ID of the new directory is set to the group ID of the process; otherwise, the group ID of the new directory is set to the group ID of the parent directory.

The mkdir function sets the modification time for the new directory. It also sets the modification time for the directory that contains the new directory.

TPF deviation from POSIX

The TPF system does not support the atime (access time) or ctime (change time) time stamp.

If pathname names a symbolic link, mkdir fails.

Normal Return

If successful, the mkdir function returns a value of 0.

Error Return

If unsuccessful, the mkdir function does not create a directory; it returns a value of -1 and sets errno to one of the following:

EACCES
The process did not have search permission on some component of pathname, or did not have write permission on the parent directory of the directory to be created.

EEXIST
Either the named file refers to a symbolic link or there is already a file or directory with the given pathname.

ELOOP
A loop exists in symbolic links. This error is issued if the number of symbolic links found while resolving the pathname argument is greater than POSIX_SYMLOOP.

EMLINK
The link count of the parent directory has already reached LINK_MAX (defined in the limits.h header file).

ENAMETOOLONG
pathname is longer than PATH_MAX characters or some component of pathname is longer than NAME_MAX characters. For symbolic links, the length of the path name string substituted for a symbolic link exceeds PATH_MAX.

ENOENT
Some component of pathname does not exist or pathname is an empty string.

ENOSPC
The file system does not have enough space to contain a new directory or the parent directory cannot be extended.

ENOTDIR
A component of the pathname prefix is not a directory.

Programming Considerations

 The TPF system does not support creating, updating, or deleting files in 1052 or UTIL state. Special files may or may not be writable in 1052 or UTIL state depending on the device driver implementation. 

Because the mkdir function requires an update to an existing directory file and the creation of a new directory file, new directories cannot be created in 1052 or UTIL state.

Examples

The following example creates a new directory.

#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
 
main() {
  char new_dir[]="new.dir";
 
  if (mkdir(new_dir, S_IRWXU|S_IRGRP|S_IXGRP) != 0)
    perror("mkdir() error");
  else if (chdir(new_dir) != 0)
    perror("first chdir() error");
  else if (chdir("..") != 0)
    perror("second chdir() error");
  else if (rmdir(new_dir) != 0)
    perror("rmdir() error");
  else
    puts("success!");
}

Related Information

See Appendix E, Programming Support for the TPF File System for more information about TPF File System C Functions.