summaryrefslogtreecommitdiff
path: root/Build/source/texk/kpathsea/kpsestat.c
blob: 1c457dc06c7ae25b9d25ab17a631ab7555305d1c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/* kpsestat -- show file permissions of a file in octal form.

   Copyright 2008, 2016 Karl Berry.
   Copyright 1997, 2000, 2001, 2005 Olaf Weber.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this software; if not, see <http://www.gnu.org/licenses/>.  */

#include <kpathsea/config.h>
#include <kpathsea/c-stat.h>
#include <stdio.h>
#include <stdlib.h>

/*
 *      kpsestat mode x
 *      Print stat bits of file x on stdout, as modified by mode.
 */

int
main (int argc, char **argv)
{
    char * mode_string;
    int to_set, to_keep, to_clear;
    int result;
    struct stat f;

    if (argc > 1 && strcmp (argv[1], "--help") == 0) {
        printf ("Usage: %s MODE FILE\n\
  Print octal permissions of FILE as modified by MODE on standard output.\n\
  MODE is a subset of the symbolic permissions accepted by chmod.\n\
  Use MODE = to obtain the unchanged permissions.\n\
\n\
--help      display this help and exit\n\
--version   output version information and exit\n\n", argv[0]);
        fputs ("Email bug reports to tex-k@tug.org.\n", stdout);
        exit(0);
    } else if (argc > 1 && strcmp (argv[1], "--version") == 0) {
        printf ("%s (%s)\n\
Copyright (C) 1997 Olaf Weber.\n\
There is NO warranty.  You may redistribute this software\n\
under the terms of the GNU General Public License.\n\
For more information about these matters, see the file named GPL.\n\
Primary author of %s: Olaf Weber.\n",
argv[0], KPSEVERSION, argv[0]);
        exit (0);
    }

    /* insist on exactly two args */
    if (argc != 3) {
        fprintf (stderr, "%s: Need exactly two arguments.\n\
Try `%s --help' for more information.\n", argv[0], argv[0]);
        exit(1);
    }

    mode_string = argv[1];
    to_set = to_keep = to_clear = 0;
    for (;;++mode_string) {
        int affected = 0;
        int action = 0;
        int value = 0;

        for (;;++mode_string)
            switch (*mode_string) {
            case 'u': affected |= 04700; break;
            case 'g': affected |= 02070; break;
            case 'o': affected |= 01007; break;
            case 'a': affected |= 07777; break;
            default: goto no_more_affected;
            }
    no_more_affected:
        if (affected == 0)
            affected = 07777;
        action = *mode_string;
        ++mode_string;
        for (;;++mode_string)
            switch (*mode_string) {
            case 'r': value |= 00444 & affected; break;
            case 'w': value |= 00222 & affected; break;
            case 'x': value |= 00111 & affected; break;
            case 's': value |= 06000 & affected; break;
            case 't': value |= 01000 & affected; break;
            default: goto no_more_values;
            }
    no_more_values:
        switch (action) {
        case '-': to_clear |= value; break;
        case '=': to_keep  |= value; break;
        case '+': to_set   |= value; break;
        default:
            fprintf(stderr, "%s: Invalid mode\n", argv[0]);
            exit(1);
        }
        if (*mode_string != ',')
            break;
    }
    if (*mode_string != 0) {
        fprintf(stderr, "%s: Invalid mode.\n", argv[0]);
        exit(1);
    }

    /* does the file exist? */
    if (stat (argv[2], &f) < 0) {
        fprintf(stderr, "%s: ", argv[0]);
        perror(argv[2]);
        return 1;
    }

    result = f.st_mode & 07777;
    result |= to_set;
    result |= to_keep & result;
    result &= ~to_clear;

    printf("%o\n", result);

    return 0;
}