2023-12-24 17:13:17 -07:00
|
|
|
|
/*
|
2024-04-26 19:33:45 -06:00
|
|
|
|
* Copyright (c) 2023–2024 DTB <trinity@trinity.moe>
|
|
|
|
|
* Copyright (c) 2024 Emma Tebibyte <emma@tebibyte.media>
|
2023-12-24 17:13:17 -07:00
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify it under
|
|
|
|
|
* the terms of the GNU Affero General Public License as published by the Free
|
|
|
|
|
* Software Foundation, either version 3 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 Affero General Public License for more
|
|
|
|
|
* details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
|
* along with this program. If not, see https://www.gnu.org/licenses/.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <stdio.h> /* fprintf(3), stderr, NULL */
|
2024-04-26 19:30:33 -06:00
|
|
|
|
#include <stdlib.h> /* EXIT_FAILURE, EXIT_SUCCESS */
|
2024-02-16 01:13:37 -07:00
|
|
|
|
#include <string.h> /* memset(3), strchr(3) */
|
2024-07-15 13:17:00 -06:00
|
|
|
|
#include <sysexits.h> /* EX_USAGE */
|
2023-12-24 17:13:17 -07:00
|
|
|
|
#include <unistd.h> /* access(3), getopt(3), F_OK, R_OK, W_OK, X_OK */
|
|
|
|
|
#include <sys/stat.h> /* lstat(3), stat struct, S_ISBLK, S_ISCHR, S_ISDIR,
|
|
|
|
|
* S_ISFIFO, S_ISGID, S_ISREG, S_ISLNK, S_ISSOCK,
|
|
|
|
|
* S_ISUID, S_ISVTX */
|
|
|
|
|
|
2024-07-24 19:37:07 -06:00
|
|
|
|
char *program_name = "scrut";
|
2024-07-27 18:32:04 -06:00
|
|
|
|
#define OPTS "bcdefgkprsuwxLS"
|
|
|
|
|
static char *opts = OPTS;
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
|
usage(char *argv0) {
|
|
|
|
|
(void)fprintf(stderr, "Usage: %s [-" OPTS "] file...\n", argv0);
|
2023-12-24 17:13:17 -07:00
|
|
|
|
|
2024-07-12 16:04:07 -06:00
|
|
|
|
return EX_USAGE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
2024-07-27 18:32:04 -06:00
|
|
|
|
char sel[(sizeof opts) / (sizeof *opts)];
|
2024-07-24 19:37:07 -06:00
|
|
|
|
program_name = (argv[0] == NULL ? program_name : argv[0]);
|
2024-07-19 19:31:34 -06:00
|
|
|
|
|
2024-07-24 19:37:07 -06:00
|
|
|
|
if (argc < 2) { return usage(program_name); }
|
2023-12-24 17:13:17 -07:00
|
|
|
|
|
2024-07-19 19:31:34 -06:00
|
|
|
|
{ /* option parsing */
|
|
|
|
|
char *p;
|
2023-12-24 17:13:17 -07:00
|
|
|
|
|
2024-07-19 19:31:34 -06:00
|
|
|
|
memset(sel, '\0', sizeof sel);
|
2024-07-27 18:32:04 -06:00
|
|
|
|
for (int c; (c = getopt(argc, argv, opts)) != -1;) {
|
|
|
|
|
if ((p = strchr(opts, c)) == NULL) { return usage(program_name); }
|
|
|
|
|
else { sel[p - opts] = c; }
|
2024-07-12 16:04:07 -06:00
|
|
|
|
}
|
|
|
|
|
|
2024-07-19 19:34:37 -06:00
|
|
|
|
/* straighten out selections; permute out nulls */
|
|
|
|
|
p = sel;
|
|
|
|
|
for (size_t i = 0; i < (sizeof sel) / (sizeof *sel); ++i) {
|
2024-07-19 19:31:34 -06:00
|
|
|
|
if (sel[i] != '\0') {
|
|
|
|
|
*p = sel[i];
|
|
|
|
|
if (&sel[i] != p++) { sel[i] = '\0'; }
|
|
|
|
|
}
|
2024-02-16 01:13:37 -07:00
|
|
|
|
}
|
2024-07-12 16:04:07 -06:00
|
|
|
|
}
|
2023-12-24 17:13:17 -07:00
|
|
|
|
|
2024-07-24 19:37:07 -06:00
|
|
|
|
if (optind == argc) { return usage(program_name); }
|
2023-12-24 17:13:17 -07:00
|
|
|
|
|
2024-07-19 19:31:34 -06:00
|
|
|
|
for (argv += optind ; *argv != NULL; ++argv) {
|
|
|
|
|
struct stat buf;
|
|
|
|
|
|
2024-07-12 16:04:07 -06:00
|
|
|
|
if(access(*argv, F_OK) != 0 || lstat(*argv, &buf) == -1) {
|
2024-04-26 19:30:33 -06:00
|
|
|
|
return EXIT_FAILURE; /* doesn't exist or isn't stattable */
|
2024-07-12 16:04:07 -06:00
|
|
|
|
}
|
2023-12-24 17:13:17 -07:00
|
|
|
|
|
2024-07-19 19:31:34 -06:00
|
|
|
|
for (size_t i = 0; sel[i] != '\0'; ++i) {
|
|
|
|
|
if (
|
|
|
|
|
(sel[i] == 'b' && !S_ISBLK(buf.st_mode))
|
|
|
|
|
|| (sel[i] == 'c' && !S_ISCHR(buf.st_mode))
|
|
|
|
|
|| (sel[i] == 'd' && !S_ISDIR(buf.st_mode))
|
|
|
|
|
|| (sel[i] == 'e' && 0)
|
|
|
|
|
|| (sel[i] == 'f' && !S_ISREG(buf.st_mode))
|
|
|
|
|
|| (sel[i] == 'g' && !(buf.st_mode & S_ISGID))
|
|
|
|
|
|| (sel[i] == 'k' && !(buf.st_mode & S_ISVTX))
|
|
|
|
|
|| (sel[i] == 'p' && !S_ISFIFO(buf.st_mode))
|
|
|
|
|
|| (sel[i] == 'r' && access(*argv, R_OK) != 0)
|
|
|
|
|
|| (sel[i] == 'u' && !(buf.st_mode & S_ISUID))
|
|
|
|
|
|| (sel[i] == 'w' && access(*argv, W_OK) != 0)
|
|
|
|
|
|| (sel[i] == 'x' && access(*argv, X_OK) != 0)
|
|
|
|
|
|| (sel[i] == 'L' && !S_ISLNK(buf.st_mode))
|
|
|
|
|
|| (sel[i] == 'S' && !S_ISSOCK(buf.st_mode))
|
2024-07-12 16:04:07 -06:00
|
|
|
|
) { return EXIT_FAILURE; }
|
2024-07-19 19:31:34 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-12-24 17:13:17 -07:00
|
|
|
|
|
2024-04-25 16:46:05 -06:00
|
|
|
|
return EXIT_SUCCESS;
|
2023-12-24 17:13:17 -07:00
|
|
|
|
}
|