| 1 |
/* access -- test for access permissions of a file. |
| 2 |
|
| 3 |
Copyright 2008, 2009-2019 Karl Berry. |
| 4 |
Copyright 1997-2001, 2005 Olaf Weber. |
| 5 |
|
| 6 |
This program is free software; you can redistribute it and/or modify |
| 7 |
it under the terms of the GNU General Public License as published by |
| 8 |
the Free Software Foundation; either version 2 of the License, or |
| 9 |
(at your option) any later version. |
| 10 |
|
| 11 |
This program is distributed in the hope that it will be useful, |
| 12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
GNU General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License |
| 17 |
along with this software; if not, see <http://www.gnu.org/licenses/>. */ |
| 18 |
|
| 19 |
#include <kpathsea/config.h> |
| 20 |
#include <kpathsea/c-unistd.h> |
| 21 |
#include <kpathsea/c-memstr.h> |
| 22 |
|
| 23 |
/* |
| 24 |
* access mode x |
| 25 |
* Returns 0 is x exists and can be accessed in accordance with mode. |
| 26 |
* We use this rather than test because test looks at the permissions |
| 27 |
* only, which doesn't take read-only file systems into account. |
| 28 |
*/ |
| 29 |
|
| 30 |
int |
| 31 |
main (int argc, char **argv) |
| 32 |
{ |
| 33 |
int mode; |
| 34 |
int status; |
| 35 |
char * i; |
| 36 |
|
| 37 |
if (argc > 1 && strcmp (argv[1], "--help") == 0) { |
| 38 |
printf("Usage: %s -MODE FILE\n\ |
| 39 |
MODE is one or more of rwx. Exit successfully if FILE exists and is\n\ |
| 40 |
readable (r), writable (w), or executable (x).\n\ |
| 41 |
\n\ |
| 42 |
--help display this help and exit\n\ |
| 43 |
--version output version information and exit\n\n", argv[0]); |
| 44 |
fputs ("Email bug reports to [email protected].\n", stdout); |
| 45 |
exit(0); |
| 46 |
} else if (argc > 1 && strcmp (argv[1], "--version") == 0) { |
| 47 |
printf ("%s (%s)\n\ |
| 48 |
Copyright (C) 2009 Olaf Weber & Karl Berry.\n\ |
| 49 |
There is NO warranty. You may redistribute this software\n\ |
| 50 |
under the terms of the GNU General Public License\n\ |
| 51 |
For more information about these matters, see the file named COPYING.\n\ |
| 52 |
Primary author of %s: Olaf Weber.\n", |
| 53 |
argv[0], KPSEVERSION, argv[0]); |
| 54 |
exit (0); |
| 55 |
} |
| 56 |
|
| 57 |
/* insist on exactly two args */ |
| 58 |
if (argc != 3) { |
| 59 |
fprintf(stderr, "%s: Need exactly two arguments.\n\ |
| 60 |
Try `%s --help' for more information.\n", argv[0], argv[0]); |
| 61 |
exit(1); |
| 62 |
} |
| 63 |
|
| 64 |
/* The option parsing is somewhat primitive. The mode must be |
| 65 |
* specified as a single parameter: an optional '-' followed by |
| 66 |
* zero or more letters 'r', 'w', or 'x'. This may change. |
| 67 |
*/ |
| 68 |
mode = 0; |
| 69 |
i = argv[1]; |
| 70 |
for (i = argv[1]; *i; ++i) |
| 71 |
switch (*i) { |
| 72 |
case 'r': mode |= R_OK; break; |
| 73 |
case 'w': mode |= W_OK; break; |
| 74 |
#if defined(_WIN32) |
| 75 |
/* access() crashes for unsupported test of x bit */ |
| 76 |
case 'x': break; |
| 77 |
#else |
| 78 |
case 'x': mode |= X_OK; break; |
| 79 |
#endif |
| 80 |
case '-': if (i == argv[1]) break; |
| 81 |
default: |
| 82 |
fprintf(stderr, "%s: Invalid MODE.\n", argv[0]); |
| 83 |
exit(1); |
| 84 |
} |
| 85 |
|
| 86 |
status = access(argv[2], mode); |
| 87 |
|
| 88 |
/* fail if the access call failed */ |
| 89 |
if (status != 0) { |
| 90 |
return 1; |
| 91 |
} |
| 92 |
|
| 93 |
/* otherwise, succeed */ |
| 94 |
return 0; |
| 95 |
} |