I am trying to change the default create mode for a file created
by fopen() function. I only want to allow the owner of the new
file (created by fopen(name,"w") call) to have read-write access
to it. Now, I figured out that I somehow have to use umask()
to change the default file-creation mode, but I don't know how
to use properly it. (I checked the man pages, although they
list the usage and stuff but they never gave a workable example or
something). I'ld really appreciate if any of you could help.
It will save me a lot of time. Thanks in advance for all the help.
Salman Mughal.
ms...@Ra.MsState.Edu
P.S.: I am still digging up the man pages trying to figure this thing out.
But, please, mail me any advices/suggestions to the above mentioned
address. Thanks.
>Hi all,
>Salman Mughal.
>ms...@Ra.MsState.Edu
It is simply the bit positions you want to be set to zero i.e. no access you
actually set to 1 in umask.
so to allow only ownership priviledges for a file it would be umask(077)
that will set the file to 700 -> -rwx------, now if you only want read write
for the file, since you probably dont want the user executing the file it
should be umask(477)
which will result in a file with a 300 file permission -> -rw-------
Dan |~\ /||\|/~_|_~|~> dan...@cts.com
Gervais |_//~|| |\_/|__|~\ "in search of that elusive ..."
Dan Gervais (dan...@cts.com) wrote:
: In article <msm6.78...@Ra.MsState.Edu> ms...@Ra.MsState.Edu (Muhammad Salman Mughal) writes:
: > I am trying to change the default create mode for a file created
: > by fopen() function. I only want to allow the owner of the new
: > file (created by fopen(name,"w") call) to have read-write access
: so to allow only ownership priviledges for a file it would be umask(077)
: that will set the file to 700 -> -rwx------, now if you only want read write
Setting umask does not set permissions on a file. It affects the mode
bits on any subsequent opens. In general you shouldn't touch umask
in user programs except in special cases. If you want to control
the permissions on a file then use the mode argument to open(2) when
creating it or use chmod(2) or fchmod(2) to change it later. What
Muhammad wants is open(filename, O_WRONLY | O_CREAT, 0400) which, assuming
the file didn't already exist, will create the file with -rw-------.
This also depends on the umask already being set to something reasonable
such as 022 or 002.
: for the file, since you probably dont want the user executing the file it
: should be umask(477)
: which will result in a file with a 300 file permission -> -rw-------
Now you're just entering the Twilight Zone. Even if umask did what
you think it does, 300 is *not* -rw------. It's --wx------. Probably
not very useful.
--
D'Arcy J.M. Cain (da...@druid.com) |
Planix, Inc. | Democracy is three wolves and a
Toronto, Ontario, Canada | sheep voting on what's for dinner.
+1 416 424 2871 (DoD#0082) (eNTP) |
In article <danger.19...@cts.com> dan...@cts.com (Dan Gervais) writes:
>so to allow only ownership priviledges for a file it would be umask(077)
>that will set the file to 700 -> -rwx------, now if you only want read write
>for the file, since you probably dont want the user executing the file it
>should be umask(477)
> which will result in a file with a 300 file permission -> -rw-------
What are you on?
read is 444, write is 222, and execute is 111 (by convention; I've been
unable to find a standards guarantee of it.). 300 is --wx------.
Obviously, this emphasizes the fact that you *REALLY* want to use the
symbolic constants for these values. Otherwise you'll be wrong.
Also, by default, files tend to open 666 &~umask, not 777.
>Dan |~\ /||\|/~_|_~|~> dan...@cts.com
>Gervais |_//~|| |\_/|__|~\ "in search of that elusive ..."
quite.
-s
--
Peter Seebach - se...@solutions.solon.com -- se...@intran.xerox.com
C/Unix proto-wizard -- C/Unix questions? Send mail for help.