MS <
ms@no_spam_thanks.com> writes:
>I need to create a directory from my C program with the user's default
>file mask.
>Initially this was used, it works fine:
>// Set the directory creation file mask to 755 (i.e. drwxr-xr-x).
>mode_t dirMask = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
>mkdir(filepath, dirMask)
>It occurred to me that while 755 is the most common mask, it would be
>better to use whatever the user's default mask is. So I tried this:
>mode_t defMask = umask(S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
>umask(defMask);
>mkdir(filepath, defMask)
The umask is a *mask* of the permission given to creat/open/mkdir so
the resulting mask is
defmask & ~umask
Since umask == defmask, the result is 0 as you have discovered:
>The above code does something I did not anticipate!! It successfully
>creates a dir (mkdir() returns 0) but with only the dir bit set.
>i.e. 'd---------'. Not what I want at all.
>How should I create a directory with the user's default file mask?
mkdir(dir, 0777);
Create a directory with all bits set in the mode and masked by the
umask. With a tyical umask of 022 you get a a resulting mode of 755.
Casper