trouble using strcat() to concatenate command line arguments

huangapple go评论69阅读模式
英文:

trouble using strcat() to concatenate command line arguments

问题

I am trying to write a program for executing multiple programs from command line arguments. I start with concatenating the argv[x] strings I pass through calling the program into bigger strings that are separated by a semicolon.

我正在尝试编写一个程序,以执行来自命令行参数的多个程序。我开始通过将通过调用程序传递的argv[x]字符串连接成由分号分隔的较大字符串。

I later want to execute this strings as separate child processes within a parent process.

稍后,我希望将这些字符串作为单独的子进程在父进程中执行。

But I am having trouble concatenating the arguments correctly.

但是我在正确连接参数方面遇到了问题。

my code:

int main(int argc, char **argv) {

    char *wp = " ";
    for(int i=1; i < argc; i++){
        // if next argument is not a semicolon and is not null
        if((strcmp(argv[i], ";") != 0) && (argv[i+1] != NULL) ){ 
            // concat this argument with whitespace
            strcat(argv[i], wp);
            // concat this argument with the next                
            strcat(argv[i], argv[i+1]);       
        }
        // go on with concatenating next arguments after semicolon if any, into new string ...
    }
    // test results
    printf("\n%s", argv[1]);

    // go on with executing argv as a child process..
}

I call the above program with ./program ls -l -a . \; date
and the output is: ls -a .

我使用./program ls -l -a . \; date调用上述程序,输出结果是:ls -a .

Could someone explain why the complete series of arguments up until the semicolon is not shown? (ls -l -a .) Thank you

有人能解释为什么直到分号之前的完整参数系列没有显示吗?(ls -l -a .) 谢谢

英文:

I am trying to write a program for executing multiple programs from command line arguments. I start with concatenating the argv[x] strings I pass through calling the program into bigger strings that are separated by a semicolon.

I later want to execute this strings as separate child processes within a parent process.

But I am having trouble concatenating the arguments correctly.

my code:

int main(int argc, char **argv) {

    char *wp = &quot; &quot;;
    for(int i=1; i &lt; argc; i++){
            // if next argument is not a semicolon and is not null
            if((strcmp(argv[i],&quot;;&quot;) != 0) &amp;&amp; (argv[i+1] != NULL) ){ 
                // concat this argument with whitespace
                strcat(argv[i],wp);
                // concat this argument with the next                
                strcat(argv[i],argv[i+1]);       
            }
            // go on with concatenating next arguments after semicolon if any, into new string ...
            }
       }
    // test results
    printf(&quot;\n%s&quot;,argv[1]);

    // go on with executing argv as a child process..
}

I call the above program with ./program ls -l -a . \; date
and the output is: ls -a .

Could someone explain why the complete series of arguments up until the semicolon is not shown? (ls -l -a .) Thank you

答案1

得分: 3

You should not write beyond the end of the strings pointed to by argv: there is no guarantee that memory can be written. You should allocate an array with local storage or from the heap and construct the string there using strcpy, strcat, or even snprintf.

英文:

You should not write beyond the end of the strings pointed to by argv: there is no guarantee that memory can be written. You should allocate an array with local storage or from the heap and construct the string there using strcpy, strcat or even snprintf.

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;

int main(int argc, char **argv) {
    size_t total = 0;
    for (int i = 1; i &lt; argc; i++) {
        total += 1 + strlen(argv[i]);
    }
    char *str = malloc(total + 1);
    if (str == NULL) {
        perror(&quot;cannot allocate string&quot;);
        return 1;
    }
    size_t pos = 0;
    *str = &#39;
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;
int main(int argc, char **argv) {
size_t total = 0;
for (int i = 1; i &lt; argc; i++) {
total += 1 + strlen(argv[i]);
}
char *str = malloc(total + 1);
if (str == NULL) {
perror(&quot;cannot allocate string&quot;);
return 1;
}
size_t pos = 0;
*str = &#39;\0&#39;;
for (int i = 1; i &lt; argc; i++) {
if (strcmp(argv[i], &quot;;&quot;) &amp;&amp; *argv[i]) {
if (pos &gt; 0)
str[pos++] = &#39; &#39;;
pos += strlen(strcpy(str + pos, argv[i]));
}
}
// test results
printf(&quot;%s\n&quot;, str);
// go on with executing argv as a child process
//...
// free memory
free(str);
return 0;
}
&#39;; for (int i = 1; i &lt; argc; i++) { if (strcmp(argv[i], &quot;;&quot;) &amp;&amp; *argv[i]) { if (pos &gt; 0) str[pos++] = &#39; &#39;; pos += strlen(strcpy(str + pos, argv[i])); } } // test results printf(&quot;%s\n&quot;, str); // go on with executing argv as a child process //... // free memory free(str); return 0; }

答案2

得分: 0

我从调用程序传递的argv[x]字符串开始连接成一个更大的字符串。

问题包括

缓冲区大小不足

代码无法正确连接,因为目标缓冲区大小未指定为足够大。

// strcat(argv[i], wp); // 不好

OP的代码实际上会出现_未定义行为_(UB)。


应该根据OP未明确说明的更大目标来确定应该采取什么行动。

英文:

> I start with concatenating the argv[x] strings I pass through calling the program into bigger string

Problems include

Insufficient buffer size

Code fail to properly concatenate as the destination buffer is not specified to be large enough.

// strcat(argv[i],wp);  // Bad

OP's code instead experiences undefined behavior (UB).


What should be done instead depends on OP's larger unstated goal.

huangapple
  • 本文由 发表于 2023年5月11日 00:13:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76220615.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定