diff options
author | Jakob Kaivo <jkk@ung.org> | 2019-08-02 21:39:28 -0400 |
---|---|---|
committer | Jakob Kaivo <jkk@ung.org> | 2019-08-02 21:39:28 -0400 |
commit | 25965a249435a3fda795422cc61caf161580d441 (patch) | |
tree | 13e57f3c5df0fd1ce777a1b7943edb7d4914dd8b /rm.c | |
parent | b33cf8ef880fd3fc68492ecf07c029c9494d75e7 (diff) |
check against yesexpr instead of hardcoding "yes" and "y"
Diffstat (limited to 'rm.c')
-rw-r--r-- | rm.c | 19 |
1 files changed, 15 insertions, 4 deletions
@@ -27,9 +27,11 @@ #include <ftw.h> #include <libgen.h> #include <limits.h> +#include <langinfo.h> +#include <locale.h> +#include <regex.h> #include <stdio.h> #include <string.h> -#include <strings.h> #include <sys/stat.h> #include <unistd.h> @@ -43,6 +45,9 @@ static int retval = 0; static int rm_prompt(const char *p) { + static regex_t yesre = { 0 }; + static int compiled = 0; + if (mode == FORCE) { return 1; } @@ -51,13 +56,17 @@ static int rm_prompt(const char *p) return 1; } - fprintf(stderr, "remove %s? ", p); + fprintf(stderr, "rm: %s? ", p); char buf[MAX_CANON]; fgets(buf, sizeof(buf), stdin); - /* TODO: use yesexpr */ - if (!strcasecmp("yes\n", buf) || !(strcasecmp("y\n", buf))) { + if (!compiled) { + char *yesexpr = nl_langinfo(YESEXPR); + regcomp(&yesre, yesexpr, REG_EXTENDED | REG_ICASE | REG_NOSUB); + } + + if (regexec(&yesre, buf, 0, NULL, 0) == 0) { return 1; } @@ -93,6 +102,8 @@ int rm(const char *p, const struct stat *st, int typeflag, struct FTW *f) int main(int argc, char **argv) { + setlocale(LC_ALL, ""); + int c; while ((c = getopt(argc, argv, "fiRr")) != -1) { switch (c) { |