Skip to content

Commit

Permalink
Fix bug when viewing a file whose name contains a newline.
Browse files Browse the repository at this point in the history
  • Loading branch information
gwsw committed Apr 12, 2024
1 parent 8e68ac4 commit 007521a
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions filename.c
Expand Up @@ -127,11 +127,20 @@ static constant char * metachars(void)
/*
* Is this a shell metacharacter?
*/
static int metachar(char c)
static lbool metachar(char c)
{
return (strchr(metachars(), c) != NULL);
}

/*
* Must use quotes rather than escape char for this metachar?
*/
static lbool must_quote(char c)
{
/* {{ Maybe the set of must_quote chars should be configurable? }} */
return (c == '\n');
}

/*
* Insert a backslash before each metacharacter in a string.
*/
Expand Down Expand Up @@ -164,6 +173,9 @@ public char * shell_quoten(constant char *s, size_t slen)
* doesn't support escape chars. Use quotes.
*/
use_quotes = TRUE;
} else if (must_quote(*p))
{
len += 3; /* open quote + char + close quote */
} else
{
/*
Expand Down Expand Up @@ -194,15 +206,22 @@ public char * shell_quoten(constant char *s, size_t slen)
constant char *es = s + slen;
while (s < es)
{
if (metachar(*s))
if (!metachar(*s))
{
/*
* Add the escape char.
*/
*np++ = *s++;
} else if (must_quote(*s))
{
/* Surround the char with quotes. */
*np++ = openquote;
*np++ = *s++;
*np++ = closequote;
} else
{
/* Insert an escape char before the char. */
strcpy(np, esc);
np += esclen;
*np++ = *s++;
}
*np++ = *s++;
}
*np = '\0';
}
Expand Down

0 comments on commit 007521a

Please sign in to comment.