umask() — Set and retrieve file creation mask

Standards

Standards / Extensions C or C++ Dependencies

POSIX.1
XPG4
XPG4.2
Single UNIX Specification, Version 3

both  

Format

#define _POSIX_SOURCE
#include <sys/stat.h>

mode_t umask(mode_t newmask);

General description

Changes the file creation mask of the process. newmask specifies new file-permission bits for the file creation mask of the process.

This mask restricts the setting of (or turns off) file-permission bits specified in the mode argument that is used with all open(), creat(), mkdir(), and mkfifo() functions issued by the current process. File-permission bits set to 1 in the file creation mask are set to 0 in the file-permission bits of files that are created by the process.

For example, if a call to open() specifies a mode argument with file-permission bits, the file creation mask of the process affects the mode argument; bits that are 1 in the mask are set to 0 in the mode argument, and therefore in the mode of the created file.

Only the file-permission bits of the new mask are used. The meaning of other bits is implementation-defined. For more information on these symbols, refer to chmod() — Change the mode of a file or directory.

Start of changeThe _EDC_UMASK_DFLT environment variable controls how the C runtime library sets the default umask if the calling program was not started by one of the exec or spawn functions.
  • When _EDC_UMASK_DFLT is not set, if z/OS® UNIX BPXPRMxx statement UMASK is set to a valid value, the default umask value is the same as that value; otherwise, the C runtime library establishes a default umask value of 022.
  • When _EDC_UMASK_DFLT is set to one of the following values:
    NO (case insensitive)
    The C runtime library does not change the default umask value, which depends on how BPXPRMxx statement UMASK is set.
    A valid octal value
    The library uses this octal value as the default umask value. The BPXPRMxx statement UMASK is overridden by this value.
    Any other value
    If the BPXPRMxx statement UMASK is set to a valid value, the default umask value is the same as that value; otherwise, the C runtime library establishes a default umask value of 022.
End of change

Returned value

umask() is always successful and returns the previous value of the file creation mask.

There are no documented errno values.

Example

CELEBU01
/* CELEBU01

   This example will work only from C/MVS, not C++/MVS.

 */
#define _POSIX_SOURCE
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#undef _POSIX_SOURCE
#include <stdio.h>

main() {
  int fd;
  mode_t oldmask;

  printf("Your old umask is %i\n",oldmask=umask(S_IRWXG));
  if ((fd = creat("umask.file", S_IRWXU|S_IRWXG)) < 0)
    perror("creat() error");
  else {
    system("ls -l umask.file");
    close(fd);
    unlink("umask.file");
  }
  umask(oldmask);
}
Output
-rwx------   1 WELLIE   SYS1           0 Apr 19 14:50 umask.file