From aa9d18e3ed53e9376133d8a815f03acacfa809a8 Mon Sep 17 00:00:00 2001 From: Andrew Borodin Date: Sat, 12 Jan 2019 14:28:35 +0300 Subject: [PATCH] (examine_cd): refactoring: use GString. Signed-off-by: Andrew Borodin --- src/filemanager/command.c | 55 +++++++++++++----------------- tests/src/filemanager/examine_cd.c | 10 ++++++ 2 files changed, 33 insertions(+), 32 deletions(-) diff --git a/src/filemanager/command.c b/src/filemanager/command.c index c3538faf8..7e71f4d37 100644 --- a/src/filemanager/command.c +++ b/src/filemanager/command.c @@ -94,55 +94,56 @@ static input_colors_t command_colors; static char * examine_cd (const char *_path) { + /* *INDENT-OFF* */ typedef enum - { copy_sym, subst_var } state_t; + { + copy_sym, + subst_var + } state_t; + /* *INDENT-ON* */ state_t state = copy_sym; - char *q; - size_t qlen; + GString *q; char *path_tilde, *path; - char *p, *r; + char *p; /* Tilde expansion */ path = strutils_shell_unescape (_path); path_tilde = tilde_expand (path); g_free (path); - /* Leave space for further expansion */ - qlen = strlen (path_tilde) + MC_MAXPATHLEN; - q = g_malloc (qlen); + q = g_string_sized_new (32); /* Variable expansion */ - for (p = path_tilde, r = q; *p != '\0' && r < q + MC_MAXPATHLEN;) + for (p = path_tilde; *p != '\0';) { switch (state) { case copy_sym: if (p[0] == '\\' && p[1] == '$') { - /* skip backslash */ - p++; - /* copy dollar */ - *(r++) = *(p++); + g_string_append_c (q, '$'); + p += 2; } else if (p[0] != '$' || p[1] == '[' || p[1] == '(') - *(r++) = *(p++); + { + g_string_append_c (q, *p); + p++; + } else state = subst_var; break; case subst_var: { - char *s; + char *s = NULL; char c; - const char *t; + const char *t = NULL; /* skip dollar */ p++; - if (p[0] != '{') - s = NULL; - else + if (p[0] == '{') { p++; s = strchr (p, '}'); @@ -157,21 +158,13 @@ examine_cd (const char *_path) *s = c; if (t == NULL) { - *(r++) = '$'; + g_string_append_c (q, '$'); if (p[-1] != '$') - *(r++) = '{'; + g_string_append_c (q, '{'); } else { - size_t tlen; - - tlen = strlen (t); - - if (r + tlen < q + MC_MAXPATHLEN) - { - strncpy (r, t, tlen + 1); - r += tlen; - } + g_string_append (q, t); p = s; if (*s == '}') p++; @@ -188,9 +181,7 @@ examine_cd (const char *_path) g_free (path_tilde); - *r = '\0'; - - return q; + return g_string_free (q, FALSE); } /* --------------------------------------------------------------------------------------------- */ diff --git a/tests/src/filemanager/examine_cd.c b/tests/src/filemanager/examine_cd.c index f8dda26fc..3152d9793 100644 --- a/tests/src/filemanager/examine_cd.c +++ b/tests/src/filemanager/examine_cd.c @@ -137,6 +137,16 @@ START_TEST (test_examine_cd) check_examine_cd ("/test/path", "/test/path"); + check_examine_cd ("$AAA", "aaa"); + check_examine_cd ("${AAA}", "aaa"); + check_examine_cd ("$AAA/test", "aaa/test"); + check_examine_cd ("${AAA}/test", "aaa/test"); + + check_examine_cd ("/$AAA", "/aaa"); + check_examine_cd ("/${AAA}", "/aaa"); + check_examine_cd ("/$AAA/test", "/aaa/test"); + check_examine_cd ("/${AAA}/test", "/aaa/test"); + check_examine_cd ("/test/path/$AAA", "/test/path/aaa"); check_examine_cd ("/test/path/$AAA/test2", "/test/path/aaa/test2"); check_examine_cd ("/test/path/test1$AAA/test2", "/test/path/test1aaa/test2");