harakit/src/scrut.c

113 lines
3.2 KiB
C
Raw Normal View History

/*
2024-04-26 19:33:45 -06:00
* Copyright (c) 20232024 DTB <trinity@trinity.moe>
* Copyright (c) 2024 Emma Tebibyte <emma@tebibyte.media>
* 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 */
#include <stdlib.h> /* EXIT_FAILURE, EXIT_SUCCESS */
#include <string.h> /* memset(3), strchr(3) */
#include <sysexits.h> /* EX_USAGE */
#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 */
static char args[] = "bcdefghkprsuwxLS";
static char ops[(sizeof args) / (sizeof *args)];
static char *program_name = "scrut";
2024-07-12 16:04:07 -06:00
int usage(char *s) {
fprintf(
stderr,
"Usage: %s [-%s] file...\n",
s == NULL ? program_name : s, args
);
return EX_USAGE;
}
int main(int argc, char *argv[]) {
struct stat buf;
int c;
size_t i;
char *p;
2024-07-12 16:04:07 -06:00
if (argc < 2) { return usage(argv[0]); }
memset(ops, '\0', sizeof ops);
2024-07-12 16:04:07 -06:00
while ((c = getopt(argc, argv, args)) != -1) {
if ((p = strchr(args, c)) == NULL) {
return usage(argv[0]);
} else {
ops[p - args] = c;
2024-07-12 16:04:07 -06:00
}
}
/* straighten out ops */
2024-07-12 16:04:07 -06:00
for (i = 0, p = ops; i < (sizeof ops) / (sizeof *ops); ++i) {
2024-07-12 16:18:48 -06:00
if (ops[i] != '\0') {
*p = ops[i];
2024-07-12 16:18:48 -06:00
if (&ops[i] != p++) { ops[i] = '\0'; }
}
2024-07-12 16:04:07 -06:00
}
2024-07-12 16:04:07 -06:00
if (optind == argc) { return usage(argv[0]); }
argv += optind;
2024-07-12 16:04:07 -06:00
do { /* while(*++argv != NULL); */
if(access(*argv, F_OK) != 0 || lstat(*argv, &buf) == -1) {
return EXIT_FAILURE; /* doesn't exist or isn't stattable */
2024-07-12 16:04:07 -06:00
}
2024-07-12 16:04:07 -06:00
for (i = 0; ops[i] != '\0'; ++i)
if (ops[i] == 'e') {
continue;
2024-07-12 16:04:07 -06:00
} else if (ops[i] == 'h') {
return usage(argv[0]);
} else if (
(ops[i] == 'b'
&& !S_ISBLK(buf.st_mode))
|| (ops[i] == 'c'
&& !S_ISCHR(buf.st_mode))
|| (ops[i] == 'd'
&& !S_ISDIR(buf.st_mode))
|| (ops[i] == 'f'
&& !S_ISREG(buf.st_mode))
|| (ops[i] == 'g'
&& !(buf.st_mode & S_ISGID))
|| (ops[i] == 'k'
&& !(buf.st_mode & S_ISVTX))
|| (ops[i] == 'p'
&& !S_ISFIFO(buf.st_mode))
|| (ops[i] == 'r'
&& access(*argv, R_OK) != 0)
|| (ops[i] == 'u'
&& !(buf.st_mode & S_ISUID))
|| (ops[i] == 'w'
&& access(*argv, W_OK) != 0)
|| (ops[i] == 'x'
&& access(*argv, X_OK) != 0)
|| (ops[i] == 'L'
&& !S_ISLNK(buf.st_mode))
|| (ops[i] == 'S'
2024-07-12 16:04:07 -06:00
&& !S_ISSOCK(buf.st_mode))
) { return EXIT_FAILURE; }
} while(*++argv != NULL);
return EXIT_SUCCESS;
}