dvitomp fix from Akira
[mplib] / src / texk / kpathsea / kpsestat.c
1 /* kpsestat -- show file permissions of a file in octal form.
2
3    Copyright 2008 Karl Berry.
4    Copyright 1997, 2000, 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-stat.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23
24 /*
25  *      kpsestat mode x
26  *      Print stat bits of file x on stdout, as modified by mode.
27  */
28
29 int
30 main P2C(int, argc, char **, argv)
31 {
32     char * mode_string;
33     int to_set, to_keep, to_clear;
34     int result;
35     struct stat f;
36
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\
42 \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);
46         exit(0);
47     } else if (argc > 1 && strcmp (argv[1], "--version") == 0) {
48         printf ("%s (%s)\n\
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]);
55         exit (0);
56     }
57
58     /* insist on exactly two args */
59     if (argc != 3) {
60         fprintf (stderr, "%s: Need exactly two arguments.\n\
61 Try `%s --help' for more information.\n", argv[0], argv[0]);
62         exit(1);
63     }
64
65     mode_string = argv[1];
66     to_set = to_keep = to_clear = 0;
67     for (;;++mode_string) {
68         int affected = 0;
69         int action = 0;
70         int value = 0;
71
72         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;
79             }
80     no_more_affected:
81         if (affected == 0)
82             affected = 07777;
83         action = *mode_string;
84         ++mode_string;
85         for (;;++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;
93             }
94     no_more_values:
95         switch (action) {
96         case '-': to_clear |= value; break;
97         case '=': to_keep  |= value; break;
98         case '+': to_set   |= value; break;
99         default:
100             fprintf(stderr, "%s: Invalid mode\n", argv[0]);
101             exit(1);
102         }
103         if (*mode_string != ',')
104             break;
105     }
106     if (*mode_string != 0) {
107         fprintf(stderr, "%s: Invalid mode.\n", argv[0]);
108         exit(1);
109     }
110
111     /* does the file exist? */
112     if (stat (argv[2], &f) < 0) {
113         fprintf(stderr, "%s: ", argv[0]);
114         perror(argv[2]);
115         return 1;
116     }
117    
118     result = f.st_mode & 07777;
119     result |= to_set;
120     result |= to_keep & result;
121     result &= ~to_clear;
122
123     printf("%o\n", result);
124
125     return 0;
126 }