beta-0.89.2
[luatex.git] / source / texk / kpathsea / access.c
blobe6fb91174d2c90d09be9854a8da64fbd9d55feef
1 /* access -- test for access permissions of a file.
3 Copyright 2008, 2009 Karl Berry.
4 Copyright 1997-2001, 2005 Olaf Weber.
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.
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.
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/>. */
19 #include <kpathsea/config.h>
20 #include <kpathsea/c-unistd.h>
21 #include <kpathsea/c-memstr.h>
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.
30 int
31 main (int argc, char **argv)
33 int mode;
34 int status;
35 char * i;
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 tex-k@tug.org.\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);
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);
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.
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 case 'x': mode |= X_OK; break;
75 case '-': if (i == argv[1]) break;
76 default:
77 fprintf(stderr, "%s: Invalid MODE.\n", argv[0]);
78 exit(1);
81 status = access(argv[2], mode);
83 /* fail if the access call failed */
84 if (status != 0) {
85 return 1;
88 /* otherwise, succeed */
89 return 0;