1 /* kpsestat -- show file permissions of a file in octal form.
3 Copyright 2008 Karl Berry.
4 Copyright 1997, 2000, 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-stat.h>
26 * Print stat bits of file x on stdout, as modified by mode.
30 main (int argc
, char **argv
)
33 int to_set
, to_keep
, to_clear
;
37 if (argc
> 1 && strcmp (argv
[1], "--help") == 0) {
38 printf ("Usage: %s MODE FILE\n\
39 Print octal permissions of FILE as modified by MODE on standard output.\n\
40 MODE is a subset of the symbolic permissions accepted by chmod.\n\
41 Use MODE = to obtain the unchanged permissions.\n\
43 --help display this help and exit\n\
44 --version output version information and exit\n\n", argv
[0]);
45 fputs ("Email bug reports to tex-k@mail.tug.org.\n", stdout
);
47 } else if (argc
> 1 && strcmp (argv
[1], "--version") == 0) {
49 Copyright (C) 1997 Olaf Weber.\n\
50 There is NO warranty. You may redistribute this software\n\
51 under the terms of the GNU General Public License.\n\
52 For more information about these matters, see the file named GPL.\n\
53 Primary author of %s: Olaf Weber.\n",
54 argv
[0], KPSEVERSION
, argv
[0]);
58 /* insist on exactly two args */
60 fprintf (stderr
, "%s: Need exactly two arguments.\n\
61 Try `%s --help' for more information.\n", argv
[0], argv
[0]);
65 mode_string
= argv
[1];
66 to_set
= to_keep
= to_clear
= 0;
67 for (;;++mode_string
) {
73 switch (*mode_string
) {
74 case 'u': affected
|= 04700; break;
75 case 'g': affected
|= 02070; break;
76 case 'o': affected
|= 01007; break;
77 case 'a': affected
|= 07777; break;
78 default: goto no_more_affected
;
83 action
= *mode_string
;
86 switch (*mode_string
) {
87 case 'r': value
|= 00444 & affected
; break;
88 case 'w': value
|= 00222 & affected
; break;
89 case 'x': value
|= 00111 & affected
; break;
90 case 's': value
|= 06000 & affected
; break;
91 case 't': value
|= 01000 & affected
; break;
92 default: goto no_more_values
;
96 case '-': to_clear
|= value
; break;
97 case '=': to_keep
|= value
; break;
98 case '+': to_set
|= value
; break;
100 fprintf(stderr
, "%s: Invalid mode\n", argv
[0]);
103 if (*mode_string
!= ',')
106 if (*mode_string
!= 0) {
107 fprintf(stderr
, "%s: Invalid mode.\n", argv
[0]);
111 /* does the file exist? */
112 if (stat (argv
[2], &f
) < 0) {
113 fprintf(stderr
, "%s: ", argv
[0]);
118 result
= f
.st_mode
& 07777;
120 result
|= to_keep
& result
;
123 printf("%o\n", result
);