For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > July 2007 > gile









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author gile
ashu

2007-07-20, 7:10 pm

i want to make a program that accept a file and chechs if it is
readable and writable and give appropriate messeage ?
i can make program to accpet filename from command line and open it
in any omde and write data in it, but to know its permission is
seems tricky, is there apecific function available ?

Bit Twister

2007-07-20, 7:10 pm

On Fri, 20 Jul 2007 06:24:12 -0700, ashu wrote:
> i want to make a program that accept a file and chechs if it is
> readable and writable and give appropriate messeage ?
> i can make program to accpet filename from command line and open it
> in any omde and write data in it, but to know its permission is
> seems tricky, is there apecific function available ?


Just for fun, do a
man -k permission | grep file

for more information on returned values, enter
man first_word_here
Jens Thoms Toerring

2007-07-20, 7:10 pm

ashu <ashishmourya21@gmail.com> wrote:
> i want to make a program that accept a file and chechs if it is
> readable and writable and give appropriate messeage ?
> i can make program to accpet filename from command line and open it
> in any omde and write data in it, but to know its permission is
> seems tricky, is there apecific function available ?


Since you have been posting this before in comp.lang.c I am
going to assume that you want to do that in C (otherwise it
would be good if you would tell what else you want to use).
There are at leat two function that you can use to check the
permissions of a file. On the one hand there's stat() that
returns a structure that also contains the permission bits
of the file. On the other hand there's the access() function
that you pass the mode you want to open the file in and it
returns 0 if you have the necessary permissions to open the
file in this mode, otherwise 1.

But please note: if you are going to open the file if you have
the necessary permissions then it is normally better to simply
try to open it. If you first check the permissions and only
then open it there's a chance that another process changes the
permissions in between and it would had been better if you had
opened the file instead directly and checked if you succeeded.

Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com