英文:
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 = " ";
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 .
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 <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char **argv) {
size_t total = 0;
for (int i = 1; i < argc; i++) {
total += 1 + strlen(argv[i]);
}
char *str = malloc(total + 1);
if (str == NULL) {
perror("cannot allocate string");
return 1;
}
size_t pos = 0;
*str = '#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char **argv) {
size_t total = 0;
for (int i = 1; i < argc; i++) {
total += 1 + strlen(argv[i]);
}
char *str = malloc(total + 1);
if (str == NULL) {
perror("cannot allocate string");
return 1;
}
size_t pos = 0;
*str = '\0';
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], ";") && *argv[i]) {
if (pos > 0)
str[pos++] = ' ';
pos += strlen(strcpy(str + pos, argv[i]));
}
}
// test results
printf("%s\n", str);
// go on with executing argv as a child process
//...
// free memory
free(str);
return 0;
}
';
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], ";") && *argv[i]) {
if (pos > 0)
str[pos++] = ' ';
pos += strlen(strcpy(str + pos, argv[i]));
}
}
// test results
printf("%s\n", 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论