(subshell_name_quote): use GString for string forming.

(do_subshell_chdir): changed related to modified subshell_name_quote().

Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
This commit is contained in:
Andrew Borodin 2013-04-06 18:54:19 +04:00 committed by Slava Zanko
parent ce2f642093
commit 6d172837f0

View File

@ -1092,13 +1092,12 @@ exit_subshell (void)
* *
*/ */
static char * static GString *
subshell_name_quote (const char *s) subshell_name_quote (const char *s)
{ {
char *ret, *d; GString *ret;
const char *su, *n; const char *su, *n;
const char *quote_cmd_start, *quote_cmd_end; const char *quote_cmd_start, *quote_cmd_end;
int c;
if (subshell_type == FISH) if (subshell_type == FISH)
{ {
@ -1111,49 +1110,36 @@ subshell_name_quote (const char *s)
quote_cmd_end = "'`\""; quote_cmd_end = "'`\"";
} }
/* Factor 5 because we need \, 0 and 3 other digits per character. */ ret = g_string_sized_new (64);
d = ret = g_try_malloc (1 + (5 * strlen (s)) + (strlen (quote_cmd_start))
+ (strlen (quote_cmd_end)));
if (d == NULL)
return NULL;
/* Prevent interpreting leading `-' as a switch for `cd' */ /* Prevent interpreting leading `-' as a switch for `cd' */
if (*s == '-') if (s[0] == '-')
{ g_string_append (ret, "./");
*d++ = '.';
*d++ = '/';
}
/* Copy the beginning of the command to the buffer */ /* Copy the beginning of the command to the buffer */
strcpy (d, quote_cmd_start); g_string_append (ret, quote_cmd_start);
d += strlen (quote_cmd_start);
/* /*
* Print every character except digits and letters as a backslash-escape * Print every character except digits and letters as a backslash-escape
* sequence of the form \0nnn, where "nnn" is the numeric value of the * sequence of the form \0nnn, where "nnn" is the numeric value of the
* character converted to octal number. * character converted to octal number.
*/ */
su = s; for (su = s; su[0] != '\0'; su = n)
for (; su[0] != '\0';)
{ {
n = str_cget_next_char_safe (su); n = str_cget_next_char_safe (su);
if (str_isalnum (su)) if (str_isalnum (su))
{ g_string_append_len (ret, su, n - su);
memcpy (d, su, n - su);
d += n - su;
}
else else
{ {
int c;
for (c = 0; c < n - su; c++) for (c = 0; c < n - su; c++)
{ g_string_append_printf (ret, "\\0%03o", (unsigned char) su[c]);
sprintf (d, "\\0%03o", (unsigned char) su[c]);
d += 5;
}
} }
su = n;
} }
strcpy (d, quote_cmd_end); g_string_append (ret, quote_cmd_end);
return ret; return ret;
} }
@ -1166,7 +1152,6 @@ void
do_subshell_chdir (const vfs_path_t * vpath, gboolean update_prompt, gboolean reset_prompt) do_subshell_chdir (const vfs_path_t * vpath, gboolean update_prompt, gboolean reset_prompt)
{ {
char *pcwd; char *pcwd;
char *temp;
char *directory; char *directory;
pcwd = vfs_path_to_str_flags (current_panel->cwd_vpath, 0, VPF_RECODE); pcwd = vfs_path_to_str_flags (current_panel->cwd_vpath, 0, VPF_RECODE);
@ -1188,25 +1173,19 @@ do_subshell_chdir (const vfs_path_t * vpath, gboolean update_prompt, gboolean re
write_all (mc_global.tty.subshell_pty, " cd ", 4); write_all (mc_global.tty.subshell_pty, " cd ", 4);
directory = vfs_path_to_str (vpath); directory = vfs_path_to_str (vpath);
if (directory != '\0') if (directory != NULL)
{ {
char *translate; char *translate;
translate = vfs_translate_path_n (directory); translate = vfs_translate_path_n (directory);
if (translate != NULL) if (translate != NULL)
{ {
GString *temp;
temp = subshell_name_quote (translate); temp = subshell_name_quote (translate);
if (temp) write_all (mc_global.tty.subshell_pty, temp->str, temp->len);
{ g_string_free (temp, TRUE);
write_all (mc_global.tty.subshell_pty, temp, strlen (temp));
g_free (temp);
}
else
{
/* Should not happen unless the directory name is so long
that we don't have memory to quote it. */
write_all (mc_global.tty.subshell_pty, ".", 1);
}
g_free (translate); g_free (translate);
} }
else else