LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   access() function call does not work (https://www.linuxquestions.org/questions/linux-newbie-8/access-function-call-does-not-work-282484/)

rafiqul 01-26-2005 10:45 AM

access() function call does not work
 
I am supposed to verify a file accesibilty, whether it is readable, or writable..so i used access() system call. It is not working...

Here is my check...

#define FILE "./file.txt"

//I am changing the permission as r only
chmod (FILE, S_IRUSR | S_IRGRP | S_IRGRP)

//I am checking here if it is read, or write able
if(access(FILE, W_OK) == 0)
{
//file is wrioteable
} else if(access(FILE, R_OK) == 0)
{
//file is readable
}

I was expecting file is readable only...how come it is writeable ?

Any idea ??

Disillusionist 01-26-2005 02:28 PM

A couple of things.

FILE is a data type used to declare file pointers, as such you should not use it as a macro

Next, ensure you include the correct library in your quote. I believe access is in the unistd.h library which was not included in the code supplied.

However I think your problem is that you are most likely running this code as root which supercedes the permissions on the file, so it always assumes a successful call to access, therefore it doesn't fall into the else if block of code.

BTW, this doesn't really belong in the Newbie forum.


All times are GMT -5. The time now is 11:44 AM.