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 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.

The _EDC_UMASK_DFLT environment variable controls how the C runtime library sets the default umask. If z/OS® UNIX are available, the runtime library establishes a default umask value of 022 octal, and queries the value of the _EDC_UMASK_DFLT environment variable. _EDC_UMASK_DFLT can have the following values:
NO (case insensitive)
The library should not change the umask.
A valid octal value
The library should use this as the default value for the umask.

Any other value for the environment variable causes the runtime library to use 022 octal as the umask value.

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

Related information