123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- #include <err.h>
- #include <ctype.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdint.h>
- #include <string.h>
- #include <unistd.h>
- #include "util.h"
- static inline uint8_t
- white_listed(char c)
- {
- return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')
- || (c >= '0' && c <= '9') || (c >= '+' && c <= '/') || c == '='
- || c == '_';
- }
- void
- print_escaped(const char *str)
- {
- size_t size = strlen(str);
- for (size_t i = 0; i < size; i++)
- if (!white_listed(str[i])) goto escape;
- fputs(str, stdout);
- return;
- escape:
- putchar('\'');
- for (size_t i = 0; i < size; i++) {
- switch (str[i]) {
- case '\'' | '!':
- fputs("'\\", stdout);
- putchar(str[i]);
- putchar('\'');
- break;
- default:
- putchar(str[i]);
- }
- }
- putchar('\'');
- }
- uint8_t
- match_ext(const char *file, const char *ext)
- {
- const size_t str_size = strlen(file);
- const size_t ext_size = strlen(ext);
- if (str_size > ext_size) {
- for (size_t i = 0; i < ext_size; i++)
- if (ext[i]
- != tolower(
- (unsigned char)file[str_size - ext_size + i]))
- return 0;
- return !0;
- }
- return 0;
- }
- int
- raw_dryrun_or_exec(uint8_t nl, uint8_t dry_run, char *const argv[])
- {
- if (!!dry_run) {
- uint8_t first = !0;
- for (char **ptr = (char **)argv; *ptr; ++ptr, first = 0) {
- if (!first) fputs(" \\\n\t", stdout);
- print_escaped(*ptr);
- }
- if (!!nl) return putchar('\n') == EOF;
- return 0;
- }
- execvp(argv[0], argv);
- err(1, "unable to run: %s", argv[0]);
- }
- void
- fiter_null(char *argv[], size_t size)
- /*
- * filterr nulls from null terminated list
- *
- * example:
- * { "a", NULL, "b", NULL } => { "a", "b", NULL, ... }
- * { NULL, NULL, "x", NULL } => { "x", NULL, ... }
- */
- {
- if (size < 3) return;
- size_t i;
- for (i = 0, --size; i < size; i++)
- if (!argv[i]) {
- for (size_t j = 0; j + i < size; j++)
- argv[i + j] = argv[i + j + 1];
- --size;
- --i;
- }
- }
|