#include <sys/types.h> #include <sys/stat.h> mode_t umask(mode_t creationMask);
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
exec
function, the user ID of the process is also set for
execution.
S_ISGID
exec
function, the group ID of the process is also
set for execution.
S_IRUSR
S_IWUSR
S_IXUSR
S_IRWXU
S_IRGRP
S_IWGRP
S_IXGRP
S_IRWXG
S_IROTH
S_IWOTH
S_IXOTH
S_IRWXO
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.
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"); }
ccreat
, mkdir
, mkfifo
, mknod
,
open
Copyright (c) 1998 SAS Institute Inc. Cary, NC, USA. All rights reserved.