mkdir()--Make Directory


  Syntax
 #include <sys/stat.h>

 int mkdir(const char *path, mode_t mode);  
  Service Program Name: QP0LLIB1

  Default Public Authority: *USE

  Threadsafe: Conditional; see Usage Notes.

The mkdir() function creates a new, empty directory whose name is defined by path. The file permission bits in mode are modified by the file creation mask of the job and then used to set the file permission bits of the directory being created.

For more information on the permission bits in mode see chmod()--Change File Authorizations. For more information on the file creation mask, see umask()--Set Authorization Mask for Job.

The owner ID of the new directory is set to the effective user ID (uid) of the job. If the directory is being created in the "root" (/), QOpenSys, and user-defined file systems, the following applies. If the S_ISGID bit of the parent directory is off, the group ID (GID) is set to the effective GID of the thread creating the directory. If the S_ISGID bit of the parent directory is on, the group ID (GID) of the new directory is set to the GID of the parent directory. For all other file systems, the group ID (GID) of the new directory is set to the GID of the parent directory.

mkdir() sets the access, change, modification, and creation times for the new directory. It also sets the change and modification times for the directory that contains the new directory (parent directory).

The link count of the parent directory link count is increased by one. The link count of the new directory is set to 2. The new directory also contains an entry for "dot" (.) and "dot-dot" (..).

If path names a symbolic link, the symbolic link is not followed, and mkdir() fails with the [EEXIST] error.

If bits in mode other than the file permission bits are set, mkdir() fails with the [EINVAL] error.


Parameters

path
(Input) A pointer to the null-terminated path name of the directory to be created.

This parameter is assumed to be represented in the CCSID (coded character set identifier) currently in effect for the job. If the CCSID of the job is 65535, this parameter is assumed to be represented in the default CCSID of the job.

The name of the new directory is assumed to be represented in the language and country or region currently in effect for the process.

See QlgMkdir()--Make Directory for a description and an example of supplying the path in any CCSID.


mode
(Input) Permission bits for the new directory. The S_ISVTX bit may also be specified when creating the directory.

See chmod()--Change File Authorizations for details on the values that can be specified for mode.


Authorities

Note: Adopted authority is not used.

Authorization Required for mkdir() (excluding QSYS.LIB, Independent ASP QSYS.LIB, and QDLS)

Object Referred to Authority Required errno
Each directory in the path name preceding the directory to be created. *X EACCES
Parent directory of directory to be created *WX EACCES

Authorization Required for mkdir() in the QSYS.LIB and independent ASP QSYS.LIB File Systems

Object Referred to Authority Required errno
Each directory in the path name preceding the directory to be created. *X EACCES
Parent directory of directory to be created (when the directory being created is a database file) *X and *ADD EACCES

Authorization Required for mkdir() in the QDLS File System

Object Referred to Authority Required errno
Each directory in the path name preceding the directory to be created. *X EACCES
Parent directory of directory to be created *CHANGE EACCES

Return Value

0
mkdir() was successful. The directory was created.
-1
mkdir() was not successful. The directory was not created. The errno global variable is set to indicate the error.

Error Conditions

If mkdir() is not successful, errno usually indicates one of the following errors. Under some conditions, errno could indicate an error other than those listed here.

Error condition Additional information
[EACCES]

If you are accessing a remote file through the Network File System, update operations to file permissions at the server are not reflected at the client until updates to data that is stored locally by the Network File System take place. (Several options on the Add Mounted File System (ADDMFS) command determine the time between refresh operations of local data.) Access to a remote file may also fail due to different mappings of user IDs (UID) or group IDs (GID) on the local and remote systems.

[EAGAIN]  
[EBADFID]  
[EBADNAME]  
[EBUSY]  
[ECONVERT]  
[EDAMAGE]  
[EEXIST]

The named file, directory, or path already exists. Or, the last component of path is a symbolic link.

[EFAULT]  
[EFILECVT]  
[EINTR]  
[EINVAL]  
[EIO]  
[EJRNDAMAGE]  
[EJRNENTTOOLONG]  
[EJRNINACTIVE]  
[EJRNRCVSPC]  
[ELOOP]  
[EMLINK]  
[ENAMETOOLONG]  
[ENEWJRN]  
[ENEWJRNRCV]  
[ENOENT]  
[ENOMEM]  
[ENOSPC]  
[ENOSYS]  
[ENOTAVAIL]  
[ENOTDIR]  
[ENOTSAFE]  
[ENOTSUP]  
[EPERM]  
[EROOBJ]  
[ESTALE]

If you are accessing a remote file through the Network File System, the file may have been deleted at the server.

[EUNKNOWN]  

If interaction with a file server is required to access the object, errno could indicate one of the following errors:

Error condition Additional information
[EADDRNOTAVAIL]  
[ECONNABORTED]  
[ECONNREFUSED]  
[ECONNRESET]  
[EHOSTDOWN]  
[EHOSTUNREACH]  
[ENETDOWN]  
[ENETRESET]  
[ENETUNREACH]  
[ESTALE]

If you are accessing a remote file through the Network File System, the file may have been deleted at the server.

[ETIMEDOUT]  
[EUNATCH]  


Error Messages

The following messages may be sent from this function:

Message ID Error Message Text
CPE3418 E Possible APAR condition or hardware failure.
CPFA0D4 E File system error occurred. Error number &1.
CPF3CF2 E Error(s) occurred during running of &1 API.
CPF9872 E Program or service program &1 in library &2 ended. Reason code &3.

Usage Notes

  1. This function will fail with error code [ENOTSAFE] when all the following conditions are true:


  2. "Root" (/), QOpenSys, and User-Defined File System Differences

    The user who creates the directory becomes its owner.

    The S_ISGID bit of the directory affects what the group ID (GID) is for objects that are created in the directory. If the S_ISGID bit of the parent directory is off, the group ID (GID) is set to the effective GID of the thread creating the object. If the S_ISGID bit of the parent directory is on, the group ID (GID) is copied from the parent directory in which the new directory is being created.

    The owner, primary group, and public object authorities (*OBJEXIST, *OBJMGT, *OBJALTER, and *OBJREF) are copied from the parent directory's owner, primary group, and public object authorities. This occurs even when the new directory has a different owner than the parent directory. The owner, primary group, and public data authorities (*R, *W, and *X) are derived from the permissions specified in the mode (except for those permissions that are also set in the file mode creation mask). The new directory does not have any private authorities or authorization list. It only has authorities for the owner, primary group, and public.

    The create object scanning attribute value for this directory is copied from the create object scanning attribute value of the parent directory. For more information on this attribute, see Qp0lSetAttr()--Set Attributes.

    Start of change The create object auditing attribute value for this directory will be set to *SYSVAL. For more information on this attribute, see Qp0lSetAttr()--Set Attributes. End of change


  3. QSYS.LIB and Independent ASP QSYS.LIB File System Differences

    The user who creates the directory becomes its owner. The group ID is copied from the primary user ID, if one exists.

    The owner is given *ALL object authority to the new directory. The group object authorities are copied from the user profile of the owner. The public receives no object authority to the directory.

    The primary group authorities specified in mode are not saved if no primary group exists.

    The change and modification times for the directory that contains the new directory are only set when the new directory is a database file.

    Start of change The create object auditing attribute value for this directory will be set to *SYSVAL. For more information on this attribute, see Qp0lSetAttr()--Set Attributes. End of change


  4. QDLS File System Differences

    The user who creates the directory becomes its owner. The group ID is copied from the parent folder in which the new directory is being created.

    The object authority of the owner is set to *OBJMGT + *OBJEXIST + *OBJALTER + *OBJREF.

    The primary group and public object authority and all other authorities are copied from the parent folder.

    The owner, primary group, and public data authority (including *OBJOPR) are derived from the permissions specified in mode (except those permissions that are also set in the file mode creation mask).

    The primary group authorities specified in mode are not saved if no primary group exists.


  5. QOPT File System Differences

    When the volume on which the directory is being created is formatted in Universal Disk Format (UDF):

    When the volume on which the directory is being created is not formatted in Universal Disk Format (UDF):

    A directory cannot be created as a direct child of /QOPT.

    The change and modification times of the parent directory are not updated.


  6. Network File System Differences

    Local access to remote directories through the Network File System may produce unexpected results due to conditions at the server. The creation of a directory may fail if permissions and other attributes that are stored locally by the Network File System are more restrictive than those at the server. A later attempt to create a file can succeed when the locally stored data has been refreshed. (Several options on the Add Mounted File System (ADDMFS) command determine the time between refresh operations of local data.) The creation can also succeed after the file system has been remounted.

    If you try to re-create a directory that was recently deleted, the request may fail because data that was stored locally by the Network File System still has a record of the directory's existence. The creation succeeds when the locally stored data has been updated.


  7. QNetWare File System Differences

    The QNetWare file system does not fully support mode bits. See NetWare on iSeries for more information.


  8. QNTC File System Differences

    Directory authorities are inherited from the access control list (if any exists) of the parent directory. The mode bits are ignored.

    In addition to the normal mkdir() function, in the QNTC file system, mkdir() can be used to add a server directory under the /QNTC directory level. Directories for all functional Windows NT servers in the local subnet are automatically created. However, Windows NT servers outside the local subnet must be added by using mkdir() or the MKDIR command. For example:

         char new_dir[]="/QNTC/NTSRV1";
         mkdir(new_dir,NULL)
    

    would add the NTSRV1 server into the QNTC directory structure for future access of files and directories on that server.

    It is also possible to add the server by using the TCP/IP address. For example:

         char new_dir[]="/QNTC/9.130.67.24";
         mkdir(new_dir,NULL)
    

    The directories added using mkdir() in the QNTC file system will not persist across IPLs. Thus, mkdir() or the Make Directory (MKDIR) command must be reissued after every system IPL.


Related Information


Example

See Code disclaimer information for information pertaining to code 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!");
}


API introduced: V3R1
Top | UNIX-Type APIs | APIs by category