3172
|
1 /* kpsestat -- show file permissions of a file in octal form. |
|
2 Copyright (C) 1997 Olaf Weber. |
|
3 |
|
4 This program is free software; you can redistribute it and/or modify |
|
5 it under the terms of the GNU General Public License as published by |
|
6 the Free Software Foundation; either version 2, or (at your option) |
|
7 any later version. |
|
8 |
|
9 This program is distributed in the hope that it will be useful, |
|
10 but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 GNU General Public License for more details. |
|
13 |
|
14 You should have received a copy of the GNU General Public License |
|
15 along with this program; if not, write to the Free Software Foundation, |
|
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
|
17 |
|
18 #include <kpathsea/config.h> |
|
19 #include <kpathsea/c-stat.h> |
|
20 #include <stdio.h> |
|
21 #include <stdlib.h> |
|
22 |
|
23 #ifdef WIN32 |
|
24 #include <string.h> |
|
25 #endif |
|
26 |
|
27 /* |
|
28 * kpsestat mode x |
|
29 * Print stat bits of file x on stdout, as modified by mode. |
|
30 */ |
|
31 |
|
32 int main (argc, argv) |
|
33 int argc; |
|
34 char *argv[]; |
|
35 { |
|
36 char * mode_string; |
|
37 int to_set, to_keep, to_clear; |
|
38 int result; |
|
39 struct stat f; |
|
40 |
|
41 if (argc > 1 && strcmp (argv[1], "--help") == 0) { |
|
42 printf ("Usage: %s MODE FILE\n\ |
|
43 Print octal permissions of FILE as modified by MODE on standard output.\n\ |
|
44 MODE is a subset of the symbolic permissions accepted by chmod.\n\ |
|
45 Use MODE = to obtain the unchanged permissions.\n\ |
|
46 \n\ |
|
47 --help display this help and exit\n\ |
|
48 --version output version information and exit\n\n", argv[0]); |
|
49 fputs ("Email bug reports to tex-k@mail.tug.org.\n", stdout); |
|
50 exit(0); |
|
51 } else if (argc > 1 && strcmp (argv[1], "--version") == 0) { |
|
52 printf ("%s (%s)\n\ |
|
53 Copyright (C) 1997 Olaf Weber.\n\ |
|
54 There is NO warranty. You may redistribute this software\n\ |
|
55 under the terms of the GNU General Public License.\n\ |
|
56 For more information about these matters, see the file named COPYING.\n\ |
|
57 Primary author of %s: Olaf Weber.\n", |
|
58 argv[0], KPSEVERSION, argv[0]); |
|
59 exit (0); |
|
60 } |
|
61 |
|
62 /* insist on exactly two args */ |
|
63 if (argc != 3) { |
|
64 fprintf (stderr, "%s: Need exactly two arguments.\n\ |
|
65 Try `%s --help' for more information.\n", argv[0], argv[0]); |
|
66 exit(1); |
|
67 } |
|
68 |
|
69 mode_string = argv[1]; |
|
70 to_set = to_keep = to_clear = 0; |
|
71 for (;;++mode_string) { |
|
72 int affected = 0; |
|
73 int action = 0; |
|
74 int value = 0; |
|
75 |
|
76 for (;;++mode_string) |
|
77 switch (*mode_string) { |
|
78 case 'u': affected |= 04700; break; |
|
79 case 'g': affected |= 02070; break; |
|
80 case 'o': affected |= 01007; break; |
|
81 case 'a': affected |= 07777; break; |
|
82 default: goto no_more_affected; |
|
83 } |
|
84 no_more_affected: |
|
85 if (affected == 0) |
|
86 affected = 07777; |
|
87 action = *mode_string; |
|
88 ++mode_string; |
|
89 for (;;++mode_string) |
|
90 switch (*mode_string) { |
|
91 case 'r': value |= 00444 & affected; break; |
|
92 case 'w': value |= 00222 & affected; break; |
|
93 case 'x': value |= 00111 & affected; break; |
|
94 case 's': value |= 06000 & affected; break; |
|
95 case 't': value |= 01000 & affected; break; |
|
96 default: goto no_more_values; |
|
97 } |
|
98 no_more_values: |
|
99 switch (action) { |
|
100 case '-': to_clear |= value; break; |
|
101 case '=': to_keep |= value; break; |
|
102 case '+': to_set |= value; break; |
|
103 default: |
|
104 fprintf(stderr, "%s: Invalid mode\n", argv[0]); |
|
105 exit(1); |
|
106 } |
|
107 if (*mode_string != ',') |
|
108 break; |
|
109 } |
|
110 if (*mode_string != 0) { |
|
111 fprintf(stderr, "%s: Invalid mode.\n", argv[0]); |
|
112 exit(1); |
|
113 } |
|
114 |
|
115 /* does the file exist? */ |
|
116 if (stat (argv[2], &f) < 0) { |
|
117 perror(argv[0]); |
|
118 return 1; |
|
119 } |
|
120 |
|
121 result = f.st_mode & ~S_IFMT; |
|
122 result |= to_set; |
|
123 result |= to_keep & result; |
|
124 result &= ~to_clear; |
|
125 |
|
126 printf("%o\n", result); |
|
127 |
|
128 return 0; |
|
129 } |