umask -- Change File Mode Creation Mask

SYNOPSIS

 #include <sys/types.h>
 #include <sys/stat.h>
 

 mode_t umask(mode_t creationMask);
 

DESCRIPTION

umask changes the file mode creation mask to the value specified by creationMask. The file mode creation mask controls which permission bits may be set when a file is created. Bits that are set to 1 in the file mode creation mask are used to mask the corresponding permission bits when a file is created. For example, setting the file group read permission bit, S_IRGRP, in the file mode creation mask will prevent this bit from being set when a new file is created.

The value of creationMask is formed by ORing any of the following symbols, which are defined in the <stat.h> include file:

S_ISUID
sets the user ID for execution. When the specified file is processed through an exec function, the user ID of the process is also set for execution.
S_ISGID
sets group ID for execution. When the specified file is processed through an exec function, the group ID of the process is also set for execution.
S_IRUSR
sets file owner permission to read.
S_IWUSR
sets file owner permission to write.
S_IXUSR
sets file owner permission to execute.
S_IRWXU
sets file owner permission to read, write, and execute.
S_IRGRP
sets group permission to read.
S_IWGRP
sets group permission to write.
S_IXGRP
sets group permission to execute.
S_IRWXG
sets group permission to read, write, and execute.
S_IROTH
sets general permission to read.
S_IWOTH
sets general permission to write.
S_IXOTH
sets general permission to execute.
S_IRWXO
sets general permission to read, write, and execute.

RETURN VALUE

umask always returns the previously defined file mode creation mask. There is no way to read the value of the creation mask without changing it. It requires two calls to umask to determine the value of the mask: first you read the original value, then you reset the mask to the original value.

EXAMPLE

The following code fragment illustrates the use of umask to change the file mode creation mask:
  #include <sys/types.h>
  #include <sys/stat.h>
  #include <stdio.h>

  main()
  {
     mode_t oldMask, newMask;

        /* Get old mask, temporarily setting the mask to 0.             */
     oldMask = umask((mode_t) 0);

        /* Print old mask. Octal values are used by mask.               */
     printf("Old mask = %on", (int) oldMask);

        /* Make sure group read permission is allowed.                  */
     if (oldMask & S_IRGRP) {
        printf("Changing group read permission from MASKED to UNMASKED.n");
        oldMask = (oldMask ^ S_IRGRP);
     }
        /* Make sure group write and execute permission is not allowed. */
     newMask = (oldMask|S_IWGRP|S_IXGRP);

     umask(newMask);                                /* Update mask.     */
     printf("New mask = %onn", (int) newMask);    /* Print new mask.  */

     printf("The file mode creation mask now specifies:nn");
     printf("     Group read permission      UNMASKEDn");
     printf("     Group write permission     MASKEDn");
     printf("     Group execute permission   MASKEDn");
  }

 

RELATED FUNCTIONS

ccreat, mkdir, mkfifo, mknod, open


Copyright (c) 1998 SAS Institute Inc. Cary, NC, USA. All rights reserved.