summaryrefslogtreecommitdiff
path: root/command/chmod.c
blob: 8bf6aab8abed2d93e447659bfebd8937856a82b2 (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/**
 *  file: chmod.c
 *  author: Tyler Murphy
 */

#include "command.h"
#include "lslib.h"

#include <stdlib.h>
#include <sys/types.h>
#include <pwd.h>
#include <unistd.h>
#include <dirent.h>
#include <string.h>

/**
 * If to add remove or directly set bits to a file
 */
enum method {
    ADD,
    SUB,
    SET
};

/**
 * Flags to set in chmod
 */
static struct {
    bool recurse;           /* if to recurse directorys */
    bool list_changed;      /* if to list what was updated */
    bool verbose;           /* if to list all output */
    bool quiet;             /* if to silence errors */
    enum method method;     /* the method to apply to the mode */
    mode_t mode;            /* the mode to apply with the method */
} flags;

/**
 * Help function for chmod
 */
static void help (void) {
    printf("Usage: chmod [-Rcvf] MODE[,MODE]... FILE...\n\n");
    printf("MODE is octal number (bit pattern sstrwxrwxrwx) or {+|-|=}[rwxXst]\n\n");
    printf("\t-R\tRecurse\n");
	printf("\t-c\tList changed files\n");
	printf("\t-v\tVerbose\n");
	printf("\t-f\tHide errors\n");
}

/** 
 * Takes in each argument that has a single - and parses it 
 * @param c the character after the -
 * @param next the next argument in argv that hasnt been parsed
 * @reutrn if the next arg was used or if the arg was invalid
 */
static int short_arg(char c, char* next) {
    UNUSED(next);
    switch (c) {
        case 'R':
            flags.recurse = true;
            break;
        case 'c':
            flags.list_changed = true;
            break;
        case 'v':
            flags.verbose = true;
            break;
        case 'f':
            flags.quiet = true;
            break;
        case 'r':
        case 'w':
        case 'x':
        case 'X':
        case 't':
        case 's':
            return ARG_IGNORE;
        default:
            return ARG_INVALID;
    }
    return ARG_UNUSED;
}

/** 
 * Given a file path, modify its mode, and recurse if set to
 * @param path the file path to modify
 */
static void chmod_file(char* path) {
    /* allocate arguments on stack */
    int save; /* path buffer save */
    struct stat s; /* file info */
    DIR* d; /* if recursing, open dir */
    struct dirent* file; /* if recursing, file being read */
    mode_t mode = 0; /* silence the uninitalized var error */

    /* add path to path buffer */
    save = push_path_buffer(path);
    
    /* get file statics, if failed return */
    if (lstat(get_path_buffer(), &s) < 0) {
        if (!flags.quiet) {
            error_s("cannot stat '%s'", get_path_buffer()); /* error if failed */
        }
        pop_path_buffer(save);
        return;
    }

    /* get the new mode given the method and proved mode */
    if (flags.method == SET) {
        mode = flags.mode;;
    } else if (flags.method == ADD) {
        mode = s.st_mode | flags.mode;
    } else if (flags.method == SUB) {
        mode = s.st_mode & ~flags.mode;
    }
    
    /* attempt to modify the file mode, error and return if failed */
    if (chmod(get_path_buffer(), mode) < 0) {
        if (!flags.quiet) {
            error_s("cannot chmod '%s'", get_path_buffer()); /* error if failed */
        }
        pop_path_buffer(save); /* clean up */
        return;
    } else if (flags.list_changed) {
        output("changed '%s' to %o", get_path_buffer(), mode); /* print if set to be verbose */
    }

    /* if not set to recurse, dont bother to do future checks */
    if (!flags.recurse) {
        pop_path_buffer(save);
        return;
    }

    /* if not a dir, cant recurse and then return */
    if (!S_ISDIR(s.st_mode)) {
        pop_path_buffer(save);
        return;
    }

    /* get the directory, if failed, error and return */
    d = opendir(get_path_buffer());
    if (d == NULL) {
        if (!flags.quiet) {
            error_s("cannot open dir '%s'", get_path_buffer()); /* error if failed */
        }
        pop_path_buffer(save); /* clean up */
        return;
    }

    /* chmod each file read */
    while ((file = readdir(d)) != NULL) {
        if (is_dot_dir(file->d_name)) continue; /* ignore dot dirs */
        chmod_file(file->d_name);
    }
    
    /* clean up */
    closedir(d);
    pop_path_buffer(save);
}

/**
 * Parse a given mode and its method
 * @param input a pointer to the string to check
 * @return the mode parsed, mode parsing could not be done if reached , if so call again
 */
static mode_t parse_mode(char** input) {
    mode_t mode = 00000; /* default mode */
    char* str = *input; /* get the input string */

    /* check method for mode */
    switch (*str) {
        case '=':
            flags.method = SET;
            break;
        case '+':
            flags.method = ADD;
            break;
        case '-':
            flags.method = SUB;
            break;
        default:
            flags.method = SET;
            mode = get_mode(str); /* get mode and skip next checks */
            goto end;
    }

next: /* check what bits to update */
    str++;
    switch (*str) {
        case 'r':   /* read */
            mode |= 00444;
            goto next;
        case 'w':   /* read */
            mode |= 00200;
            goto next;
        case 'x':   /* execute */
            mode |= 00111;
            goto next;
        case 'X':   /* empty */
            mode |= 00000;
            goto next;
        case 's':   /* setuid and setgid */
            mode |= 06000;
            goto next;
        case 't':   /* sticky */
            mode |= 01000;
            goto next;
        case '\0':
        case ',':    /* stop reading args */
            goto end;
        default:
            error("invalid option: %c", *str); /* error when invalid */
    }

end: /* read till next argument and update input string pointer */

    while (true) {
        
        if (*str == '\0') {
            *input = NULL; /* no mroe args */
            break;
        }

        if (*str == ',') {
            *input = ++str; /* another argument found */
            break;
        }

        str++;
    }

    return mode; /* return mode */
}

/**
 * Modify a files permission mode
 */
COMMAND(chmod_main) {
    
    int start, i; 
    
    /* get default flags */
    flags.recurse = false;
    flags.list_changed = false;
    flags.verbose = false;
    flags.quiet = false;
    flags.mode = 0;

    /* parse arguments */
    start = parse_args(argc, argv, help, short_arg, NULL);

    /* if not arguments error and return */
    if (argc - start < 1) {
        if (!flags.quiet) {
            error("no mode provided");
        } else {
            return EXIT_FAILURE;
        }
    }

    /* parse mode continusally until no more mode arguments found */
    do {
        flags.mode |= parse_mode(&argv[start]);
    } while (argv[start] != NULL);

    /* if no files found error and return */
    if (argc - start < 2) {
        if (!flags.quiet) {
            error("no files passed");
        } else {
            return EXIT_FAILURE;
        }
    }

    /* for each file passed, chmod */
    for (i = start + 1; i < argc; i++) {
        chmod_file(argv[i]);
    }

    return EXIT_SUCCESS; /* return */
}