|
| 1 | +From 007521ac3c95bc76e3d59c6dbfe75d06c8075c33 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Mark Nudelman <markn@greenwoodsoftware.com> |
| 3 | +Date: Thu, 11 Apr 2024 17:49:48 -0700 |
| 4 | +Subject: [PATCH] Fix bug when viewing a file whose name contains a newline. |
| 5 | + |
| 6 | +--- |
| 7 | + filename.c | 31 +++++++++++++++++++++++++------ |
| 8 | + 1 file changed, 25 insertions(+), 6 deletions(-) |
| 9 | + |
| 10 | +diff --git a/filename.c b/filename.c |
| 11 | +index f90e0e82..a52c6354 100644 |
| 12 | +--- a/filename.c |
| 13 | ++++ b/filename.c |
| 14 | +@@ -133,6 +133,15 @@ static constant char * metachars(void) |
| 15 | + return (strchr(metachars(), c) != NULL); |
| 16 | + } |
| 17 | + |
| 18 | ++/* |
| 19 | ++ * Must use quotes rather than escape char for this metachar? |
| 20 | ++ */ |
| 21 | ++static int must_quote(char c) |
| 22 | ++{ |
| 23 | ++ /* {{ Maybe the set of must_quote chars should be configurable? }} */ |
| 24 | ++ return (c == '\n'); |
| 25 | ++} |
| 26 | ++ |
| 27 | + /* |
| 28 | + * Insert a backslash before each metacharacter in a string. |
| 29 | + */ |
| 30 | +@@ -164,6 +173,9 @@ public char * shell_quoten(constant char *s, size_t slen) |
| 31 | + * doesn't support escape chars. Use quotes. |
| 32 | + */ |
| 33 | + use_quotes = 1; |
| 34 | ++ } else if (must_quote(*p)) |
| 35 | ++ { |
| 36 | ++ len += 3; /* open quote + char + close quote */ |
| 37 | + } else |
| 38 | + { |
| 39 | + /* |
| 40 | +@@ -193,15 +205,22 @@ public char * shell_quoten(constant char *s, size_t slen) |
| 41 | + { |
| 42 | + while (*s != '\0') |
| 43 | + { |
| 44 | +- if (metachar(*s)) |
| 45 | ++ if (!metachar(*s)) |
| 46 | + { |
| 47 | +- /* |
| 48 | +- * Add the escape char. |
| 49 | +- */ |
| 50 | ++ *p++ = *s++; |
| 51 | ++ } else if (must_quote(*s)) |
| 52 | ++ { |
| 53 | ++ /* Surround the char with quotes. */ |
| 54 | ++ *p++ = openquote; |
| 55 | ++ *p++ = *s++; |
| 56 | ++ *p++ = closequote; |
| 57 | ++ } else |
| 58 | ++ { |
| 59 | ++ /* Insert an escape char before the char. */ |
| 60 | + strcpy(p, esc); |
| 61 | + p += esclen; |
| 62 | ++ *p++ = *s++; |
| 63 | + } |
| 64 | +- *p++ = *s++; |
| 65 | + } |
| 66 | + *p = '\0'; |
| 67 | + } |
0 commit comments