C 为什么我的缓冲区没有连接?
如果main没有参数,那么我的程序应该执行C 为什么我的缓冲区没有连接?,c,debugging,io,environment-variables,C,Debugging,Io,Environment Variables,如果main没有参数,那么我的程序应该执行printenv | sort | less,我已经实现了这个功能。如果main有参数,那么程序应该执行printenv | grep | sort | less,我的问题是调试不起作用。我可以在我的代码中尝试语句printf,但它不起任何作用。为什么?为什么我的要求的后一部分不起作用?这个程序有什么问题 预期输出为printenv | grep | sort | less。例如,我想查询环境变量,以便执行a.out JOBS COMPIZ UPSTAR
printenv | sort | less
,我已经实现了这个功能。如果main有参数,那么程序应该执行printenv | grep | sort | less
,我的问题是调试不起作用。我可以在我的代码中尝试语句printf
,但它不起任何作用。为什么?为什么我的要求的后一部分不起作用?这个程序有什么问题
预期输出为printenv | grep | sort | less
。例如,我想查询环境变量,以便执行a.out JOBS COMPIZ UPSTART
应该执行与printenv | grep-e'JOBS\| COMPIZ\| UPSTART'| sort | less
相同的操作
相反,我在尝试分叉一系列命令时得到了意外的输出
#include <sys/types.h> /* definierar bland annat typen pid_t */
#include <errno.h> /* definierar felkontrollvariabeln errno */
#include <stdio.h> /* definierar stderr, dit felmeddelanden skrivs */
#include <stdlib.h> /* definierar bland annat exit() */
#include <unistd.h> /* definierar bland annat fork() */
struct command
{
const char **argv;
};
int
spawn_proc (int in, int out, struct command *cmd)
{
pid_t pid;
if ((pid = fork ()) == 0)
{
if (in != 0)
{
dup2 (in, 0);
close (in);
}
if (out != 1)
{
dup2 (out, 1);
close (out);
}
return execvp (cmd->argv [0], (char * const *)cmd->argv);
}
return pid;
}
int
fork_pipes (int n, struct command *cmd)
{
int i;
pid_t pid;
int in, fd [2];
/* The first process should get its input from the original file descriptor 0. */
in = 0;
/* Note the loop bound, we spawn here all, but the last stage of the pipeline. */
for (i = 0; i < n - 1; ++i)
{
pipe (fd);
/* f [1] is the write end of the pipe, we carry `in` from the prev iteration. */
spawn_proc (in, fd [1], cmd + i);
/* No need for the write and of the pipe, the child will write here. */
close (fd [1]);
/* Keep the read end of the pipe, the next child will read from there. */
in = fd [0];
}
/* Last stage of the pipeline - set stdin be the read end of the previous pipe
and output to the original file descriptor 1. */
if (in != 0)
dup2 (in, 0);
/* Execute the last stage with the current process. */
return execvp (cmd [i].argv [0], (char * const *)cmd [i].argv);
}
int
main (int argc, char ** argv)
{
printf("in main...");
int i;
if (argc == 1) {
const char *printenv[] = { "printenv", 0};
const char *sort[] = { "sort", 0 };
const char *less[] = { "less", 0 };
struct command cmd [] = { {printenv}, {sort}, {less} };
return fork_pipes (3, cmd);
}
if (argc > 1) {
char *tmp = argv[1];
for( i=1; i<argc-1; i++)
{
sprintf(tmp, "%s%s%s", tmp, "|", argv[i]);
}
const char *printenv[] = { "printenv", 0};
const char *grep[] = { "grep", "-E", tmp, NULL};
const char *sort[] = { "sort", 0 };
const char *less[] = { "less", 0 };
struct command cmd [] = { {printenv}, {grep}, {sort}, {less} };
return fork_pipes (4, cmd);
}
}
#包括/*definierar bland annat typen pid*/
#包括/*定义者Felkontrolln errno*/
#包括/*定义标准,包括*/
#include/*definierar bland annat exit()*/
#包括/*定义者bland annat fork()*/
结构命令
{
常量字符**argv;
};
int
生成程序(整数输入、整数输出、结构命令*cmd)
{
pid_t pid;
如果((pid=fork())==0)
{
如果(in!=0)
{
dup2(in,0);
关闭(in);
}
如果(输出!=1)
{
dup2(out,1);
收尾;
}
返回execvp(cmd->argv[0],(char*const*)cmd->argv);
}
返回pid;
}
int
fork_管道(int n,结构命令*cmd)
{
int i;
pid_t pid;
int-in,fd[2];
/*第一个进程应从原始文件描述符0获取其输入*/
in=0;
/*请注意循环绑定,我们在这里生成所有,但管道的最后一个阶段除外*/
对于(i=0;i1){
char*tmp=argv[1];
对于(i=1;i问题的一部分是,您通过写入argv[1]
(由于tmp=argv[1]
语句)来写入只读内存段。更严重的是,您很可能超出了argv[1]的大小
。相反,您应该将字符串连接到新的具有足够大小的可写缓冲区
要将字符串连接到tmp
变量中,可以使用类似以下代码:
// Compute required buffer length
int len = 1; // adds 1 to the length to account for the \0 terminating char
for( i=1; i<argc; i++)
{
len += strlen(argv[i]) + 2; // +2 accounts for length of "\\|"
}
// Allocate buffer
tmp = (char*) malloc(len);
tmp[0] = '\0';
// Concatenate argument into buffer
int pos = 0;
for( i=1; i<argc; i++)
{
pos += sprintf(tmp+pos, "%s%s", (i==1?"":"|"), argv[i]);
}
printf("tmp:%s", tmp);
fflush(stdout); // force string to be printed
...
free(tmp);
//计算所需的缓冲区长度
int len=1;//将1添加到长度中以说明\0终止字符
对于(i=1;我把你的问题标题放在更有意义的地方。“为什么我的程序不起作用?”完全不知道您在问什么问题或遇到什么问题。问题标题应包含对本网站未来读者在搜索结果中看到它时有用的信息。谢谢。@NiklasRosencrantz我已调整生成的字符串,使其更符合您尝试的内容要做。谢谢。现在我只需要知道如何将to arguments逐字发送到grep,因为我的旧调用不再工作。我想我应该使用一些其他指令,因为我们不再保留|
。您打算将什么样的参数传递到grep?顺便说一句,如果您想要参数,您需要生成一个字符串数组当您使用execvp
调用grep时,s将作为分隔参数传递(就像您对grep[]={“grep”,“-E”,tmp,NULL};
)啊!“\\\\\\”与“\\\”分隔符完全不同。@sleuthye第一个参数前面不应该有”\\\\\\\\\\\”
否则您将创建一个类似于grep-E'\\\\\\\\\\\\\\\\\作业[…]的命令行“