| 1 |
/*********************************************************************** |
| 2 |
* Code listing from "Advanced Linux Programming," by CodeSourcery LLC * |
| 3 |
* Copyright (C) 2001 by New Riders Publishing * |
| 4 |
* See COPYRIGHT for license information. * |
| 5 |
***********************************************************************/ |
| 6 |
|
| 7 |
#include <errno.h> |
| 8 |
#include <stdio.h> |
| 9 |
#include <unistd.h> |
| 10 |
|
| 11 |
int main (int argc, char* argv[]) |
| 12 |
{ |
| 13 |
char* path = argv[1]; |
| 14 |
int rval; |
| 15 |
|
| 16 |
/* Check file existence. */ |
| 17 |
rval = access (path, F_OK); |
| 18 |
if (rval == 0) |
| 19 |
printf ("%s exists\n", path); |
| 20 |
else { |
| 21 |
if (errno == ENOENT) |
| 22 |
printf ("%s does not exist\n", path); |
| 23 |
else if (errno == EACCES) |
| 24 |
printf ("%s is not accessible\n", path); |
| 25 |
return 0; |
| 26 |
} |
| 27 |
|
| 28 |
/* Check read access. */ |
| 29 |
rval = access (path, R_OK); |
| 30 |
if (rval == 0) |
| 31 |
printf ("%s is readable\n", path); |
| 32 |
else |
| 33 |
printf ("%s is not readable (access denied)\n", path); |
| 34 |
|
| 35 |
/* Check write access. */ |
| 36 |
rval = access (path, W_OK); |
| 37 |
if (rval == 0) |
| 38 |
printf ("%s is writable\n", path); |
| 39 |
else if (errno == EACCES) |
| 40 |
printf ("%s is not writable (access denied)\n", path); |
| 41 |
else if (errno == EROFS) |
| 42 |
printf ("%s is not writable (read-only filesystem)\n", path); |
| 43 |
|
| 44 |
return 0; |
| 45 |
} |