aboutsummaryrefslogtreecommitdiffstats
path: root/src/firejail/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/firejail/main.c')
-rw-r--r--src/firejail/main.c104
1 files changed, 1 insertions, 103 deletions
diff --git a/src/firejail/main.c b/src/firejail/main.c
index cbc3d57cf..e86d78ff1 100644
--- a/src/firejail/main.c
+++ b/src/firejail/main.c
@@ -2004,109 +2004,7 @@ int main(int argc, char **argv) {
2004 cfg.window_title = "appimage"; 2004 cfg.window_title = "appimage";
2005 } 2005 }
2006 else { 2006 else {
2007 // calculate the length of the command 2007 build_cmdline(&cfg.command_line, &cfg.window_title, argc, argv, prog_index);
2008 int i;
2009 int len = 0;
2010 int argcnt = argc - prog_index;
2011 int j;
2012 bool in_quotes = false;
2013
2014 for (i = 0; i < argcnt; i++) {
2015 in_quotes = false;
2016 for (j = 0; j < strlen(argv[i + prog_index]); j++) {
2017 if (argv[i + prog_index][j] == '\'') {
2018 if (in_quotes)
2019 len++;
2020 if (j > 0 && argv[i + prog_index][j-1] == '\'')
2021 len++;
2022 else
2023 len += 3;
2024 in_quotes = false;
2025 } else {
2026 if (!in_quotes)
2027 len++;
2028 len++;
2029 in_quotes = true;
2030 }
2031 }
2032 if (in_quotes) {
2033 len++;
2034 }
2035 if (strlen(argv[i + prog_index]) == 0) {
2036 len += 2;
2037 }
2038 len++;
2039 }
2040
2041 // build the string
2042 cfg.command_line = malloc(len + 1); // + '\0'
2043 if (!cfg.command_line)
2044 errExit("malloc");
2045 cfg.window_title = malloc(len + 1); // + '\0'
2046 if (!cfg.window_title)
2047 errExit("malloc");
2048
2049 char *ptr1 = cfg.command_line;
2050 char *ptr2 = cfg.window_title;
2051 for (i = 0; i < argcnt; i++) {
2052
2053 // enclose args by single quotes,
2054 // and since single quote can't be represented in single quoted text
2055 // each occurence of it should be enclosed by double quotes
2056 in_quotes = false;
2057 for (j = 0; j < strlen(argv[i + prog_index]); j++) {
2058 // single quote
2059 if (argv[i + prog_index][j] == '\'') {
2060 if (in_quotes) {
2061 // close quotes
2062 ptr1[0] = '\'';
2063 ptr1++;
2064 }
2065 // previous char was single quote too
2066 if (j > 0 && argv[i + prog_index][j-1] == '\'') {
2067 ptr1--;
2068 sprintf(ptr1, "\'\"");
2069 }
2070 // this first in series
2071 else
2072 {
2073 sprintf(ptr1, "\"\'\"");
2074 }
2075 ptr1 += strlen(ptr1);
2076 in_quotes = false;
2077 }
2078 // anything other
2079 else
2080 {
2081 if (!in_quotes) {
2082 // open quotes
2083 ptr1[0] = '\'';
2084 ptr1++;
2085 }
2086 ptr1[0] = argv[i + prog_index][j];
2087 ptr1++;
2088 in_quotes = true;
2089 }
2090 }
2091 // close quotes
2092 if (in_quotes) {
2093 ptr1[0] = '\'';
2094 ptr1++;
2095 }
2096 // handle empty argument case
2097 if (strlen(argv[i + prog_index]) == 0) {
2098 sprintf(ptr1, "\'\'");
2099 ptr1 += strlen(ptr1);
2100 }
2101 // add space
2102 sprintf(ptr1, " ");
2103 ptr1 += strlen(ptr1);
2104
2105 sprintf(ptr2, "%s ", argv[i + prog_index]);
2106 ptr2 += strlen(ptr2);
2107 }
2108
2109 assert(len == strlen(cfg.command_line));
2110 } 2008 }
2111 2009
2112 assert(cfg.command_name); 2010 assert(cfg.command_name);