diff options
Diffstat (limited to 'src/Fl_Native_File_Chooser_Kdialog.cxx')
| -rw-r--r-- | src/Fl_Native_File_Chooser_Kdialog.cxx | 62 |
1 files changed, 36 insertions, 26 deletions
diff --git a/src/Fl_Native_File_Chooser_Kdialog.cxx b/src/Fl_Native_File_Chooser_Kdialog.cxx index d0580162d..68cb4729e 100644 --- a/src/Fl_Native_File_Chooser_Kdialog.cxx +++ b/src/Fl_Native_File_Chooser_Kdialog.cxx @@ -73,7 +73,7 @@ static int fnfc_dispatch(int /*event*/, Fl_Window* /*win*/) { } -void Fl_Kdialog_Native_File_Chooser_Driver::build_command(std::string& command) { +void Fl_Kdialog_Native_File_Chooser_Driver::build_command(char *command, int maxlen) { const char *option; switch (_btype) { case Fl_Native_File_Chooser::BROWSE_DIRECTORY: @@ -103,23 +103,21 @@ void Fl_Kdialog_Native_File_Chooser_Driver::build_command(std::string& command) } // Build command - command = "kdialog"; + int pos = 0; + pos += snprintf(command + pos, maxlen - pos, "kdialog"); if (_title) { - std::string quoted_title = _title; shell_quote(quoted_title); - command += " --title "; - command += quoted_title; + char quoted_title[FL_PATH_MAX]; + shell_quote(_title, quoted_title, sizeof(quoted_title)); + pos += snprintf(command + pos, maxlen - pos, " --title %s", quoted_title); } - command += " "; - command += option; - command += " "; - command += preset; + pos += snprintf(command + pos, maxlen - pos, " %s %s", option, preset); if (_parsedfilt) { - std::string quoted_filt = _parsedfilt; shell_quote(quoted_filt); // NOTE: orig code used double quoting -erco 1/10/24 - command += " "; - command += quoted_filt; + char quoted_filt[FL_PATH_MAX * 2]; + shell_quote(_parsedfilt, quoted_filt, sizeof(quoted_filt)); + pos += snprintf(command + pos, maxlen - pos, " %s", quoted_filt); } - command += " 2> /dev/null"; // get rid of stderr - // printf("command = %s\n", command.c_str()); + snprintf(command + pos, maxlen - pos, " 2> /dev/null"); + // printf("command = %s\n", command); } @@ -146,10 +144,10 @@ int Fl_Kdialog_Native_File_Chooser_Driver::show() { return retval; } - std::string command; - build_command(command); - //fprintf(stderr, "DEBUG: POPEN: %s\n", command.c_str()); - FILE *pipe = popen(command.c_str(), "r"); + char command[FL_PATH_MAX * 4]; + build_command(command, sizeof(command)); + //fprintf(stderr, "DEBUG: POPEN: %s\n", command); + FILE *pipe = popen(command, "r"); fnfc_pipe_struct data; data.all_files = NULL; if (pipe) { @@ -307,17 +305,29 @@ const char *Fl_Kdialog_Native_File_Chooser_Driver::title() const { return _title; } -// Add shell quotes around string 's'. +// Add shell quotes around string 'in', store result in 'out'. // Handles quoting embedded quotes. // -void Fl_Kdialog_Native_File_Chooser_Driver::shell_quote(std::string& s) { - std::string out = "'"; // leading quote - for (int t=0; t<(int)s.size(); t++) { - if (s[t] == '\'') out += "'\"'\"'"; // quote any quotes - else out += s[t]; +void Fl_Kdialog_Native_File_Chooser_Driver::shell_quote(const char *in, char *out, int maxlen) { + int pos = 0; + out[pos++] = '\''; // leading quote + while (*in && pos < maxlen - 6) { // leave room for closing quote + null + if (*in == '\'') { + // quote any quotes: replace ' with '"'"' + if (pos + 5 < maxlen) { + out[pos++] = '\''; + out[pos++] = '"'; + out[pos++] = '\''; + out[pos++] = '"'; + out[pos++] = '\''; + } + } else { + out[pos++] = *in; + } + in++; } - out += "'"; // trailing quote - s = out; + out[pos++] = '\''; // trailing quote + out[pos] = '\0'; } /** |
