- Get link
- X
- Other Apps
Featured Post
Posted by
Unknown
on
- Get link
- X
- Other Apps
This is a simple program to check the access of a file (whether it is readable , writable or executable or not).
It uses the standard linux system call access for this purpose.
It uses the standard linux system call access for this purpose.
//Filename : access.c //To compile : gcc -o acc access.c //To run : ./acc#include <errno.h> #include <stdio.h> #include <unistd.h> #include <stdlib.h> int main (int argc, char* argv[]) { char* path = argv[1]; int rval; if(argc != 2 ) { fprintf(stderr,"Usage : %s <file-path>\n",argv[0]); exit(0); } rval = access (path, F_OK); if (rval == 0) printf ("%s exists\n", path); else { /* Check file existence. */ if (errno == ENOENT) printf ("%s does not exist\n", path); /* Check file access. */ else if (errno == EACCES) printf ("%s is not accessible\n", path); return 0; } /* Check read access. */ rval = access (path, R_OK); if (rval == 0) printf ("%s is readable\n", path); else printf ("%s is not readable (access denied)\n", path); /* Check write access. */ rval = access (path, W_OK); if (rval == 0) printf ("%s is writable\n", path); else if (errno == EACCES) printf ("%s is not writable (access denied)\n", path); else if (errno == EROFS) printf ("%s is not writable (read-only filesystem)\n", path); return 0; }
Comments
Thank you Varun,
ReplyDeleteThe code is very helpful.