diff options
author | Rory Bradford <roryrjb@gmail.com> | 2021-07-17 08:12:24 +0100 |
---|---|---|
committer | Rory Bradford <roryrjb@gmail.com> | 2021-07-17 08:12:24 +0100 |
commit | cf8487217b4362b968d40fa7e7e1bd882a3ab5b9 (patch) | |
tree | 0ea193b958e887d8938a374cbbbb4ac213a8a3ab | |
parent | 85d1e100ffad438d5ef27c080c4d74b0b282fb43 (diff) | |
download | rf-cf8487217b4362b968d40fa7e7e1bd882a3ab5b9.tar.gz |
Escape strings in output to avoid problems in other tools
Signed-off-by: Rory Bradford <roryrjb@gmail.com>
-rw-r--r-- | rf.c | 43 |
1 files changed, 42 insertions, 1 deletions
diff --git a/rf.c b/rf.c index b862f57..389020f 100644 --- a/rf.c +++ b/rf.c @@ -3,6 +3,7 @@ #define _XOPEN_SOURCE 700 +#include <ctype.h> #include <dirent.h> #include <errno.h> #include <fnmatch.h> @@ -95,6 +96,44 @@ static int excluded(const char *name) { return 0; } +char *escape_string(char *input) { + char ch; + int i = 0; + int j = 0; + char *output = calloc(sizeof(char), strlen(input) * 2); + + while ((ch = input[i]) != '\0') { + switch (ch) { + case ' ': + case '\t': + case '\'': + case '(': + case ')': + output[j++] = '\\'; + output[j++] = ch; + i++; + break; + default: + output[j++] = input[i++]; + } + } + + return output; +} + +int has_spaces(char *input) { + char ch; + int i = 0; + + while ((ch = input[i++]) != '\0') { + if (isspace(ch)) { + return 1; + } + } + + return 0; +} + /* return 1 if breaking early (e.g. reaching limit) otherwise return 0 */ static int recurse_find(char **patterns, int *pattern_count, const char *dirname, struct switches *switches) { @@ -170,7 +209,9 @@ static int recurse_find(char **patterns, int *pattern_count, if (matched) { if (is_child(entry->d_name) != 0) { - printf("%s\n", full_path); + char *escaped = escape_string(full_path); + printf("%s\n", escaped); + free(escaped); } if (switches->limit > 0 && |