如何使用 execl() 函数通过 cut 命令 运行 我的标准输入?
How to run my stdin through the cut command using the execl() function?
我的objective是通过FIFO在child和parent之间做一个IPC。 child 应该 运行
execl ("/bin/cat", "cat", "/etc/passwd", (char *)0);
将其输出重定向到 parents 输入并且 parent 应该 运行 这个命令:
cut -l : -f 1
并将其输出到命令行。
现在,我已成功链接我的 FIFO 并将我的 child 进程的输出重定向到 parent 的输入。我已经完成了多项测试,并且该连接工作正常。问题出在剪切的 execl 上,它应该看起来像这样:
execlp("/bin/cut", "cut", "-l:", "-f", "1", NULL);
但我很确定它不是。
int cut(){
//
int myfifo;
char buf[MAX_BUF];
printf("\nCut opening FIFO");
if((myfifo = open("/tmp/myfifo", O_RDONLY | O_TRUNC))<0){
perror("open FIFO at cut");
quit(EXIT_FAILURE);}
else{printf("\nCut has FIFO opened and is reading\n");}
//read(myfifo, buf, MAX_BUF); outputting buf goes as supposed to
if( dup2(myfifo, 0) < 0 ){
perror("dup2 at cut");
quit(EXIT_FAILURE);}
//read(STDIN_FILENO, buf, MAX_BUF);
close(myfifo);
execlp("/bin/cut", "cut", "-l:", "-f", "1", NULL);
//this is for testing buf, but i guess the program shouldn't even get here
printf("\nCut has received: %s\nAnd is closing FIFO", buf);
return -1;
}
int cat(){
int myfifo;
//OPEN FIFO
printf("\nCat opening FIFO");
if( (myfifo = open("/tmp/myfifo", O_WRONLY | O_TRUNC) )<0){
perror("open FIFO at cat");
quit(EXIT_FAILURE);
}
else{
printf("\nCat has opened FIFO");
//WRITE OUTPUT OF "cat \etc\passwd" TO FIFO
dup2(myfifo, 1);
execl ("/bin/cat", "cat", "/etc/passwd", (char *)0);
}
close(myfifo);
return 0;
}
main目前只创建fifo(mkfifo)、forks()和调用函数。
我的问题可能与 parent 的标准输入有关(运行ning 剪切),但我不这么认为,或者我假设 execl() 直接从标准输入读取并且它没有.
我认为这真的是因为我没有正确地通过 execl() 编写 "cut"。
对代码的任何更正,甚至我表达某些想法的方式都可能表明我没有正确理解某些内容,将不胜感激。
感谢您的帮助
如评论中所述,GNU 和 BSD(以及 POSIX)下的 cut
命令不支持 -l
选项;您需要的选项是 -d
作为定界符。
这段代码对我有用。在我的机器上,/bin/cat
是正确的,但是 /usr/bin/cut
是正确的(不是 /bin/cut
),所以我不得不编译:
$ rmk cutcat UFLAGS=-DCUT_CMD=/usr/bin/cut && ./cutcat
gcc -O3 -g -std=c11 -Wall -Wextra -Werror -DCUT_CMD=/usr/bin/cut cutcat.c -o cutcat
$
它使用 make
的自定义变体(称为 rmk
)和自定义 makefile
,但 cut
命令的位置是在命令行中指定的通过 UFLAGS
(用户标志)宏。 C 代码中的 STRING
和 EXPAND
宏是一个麻烦,但没有尝试通过 shell 调用 make
(或rmk
) 然后 make
调用 运行 编译器的 shell。
代码:
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/stat.h>
#ifndef CUT_CMD
#define CUT_CMD /bin/cut
#endif
#ifndef CAT_CMD
#define CAT_CMD /bin/cat
#endif
#define EXPAND(x) #x
#define STRING(x) EXPAND(x)
static const char cut_cmd[] = STRING(CUT_CMD);
static const char cat_cmd[] = STRING(CAT_CMD);
static inline void quit(int status) { exit(status); }
enum { MAX_BUF = 4096 };
static void cut(void)
{
int myfifo;
printf("\nCut opening FIFO");
if ((myfifo = open("/tmp/myfifo", O_RDONLY | O_TRUNC)) < 0)
{
perror("open FIFO at cut");
quit(EXIT_FAILURE);
}
printf("\nCut has FIFO opened and is reading\n");
if (dup2(myfifo, 0) < 0)
{
perror("dup2 at cut");
quit(EXIT_FAILURE);
}
close(myfifo);
execlp(cut_cmd, "cut", "-d:", "-f", "1", NULL);
fprintf(stderr, "Failed to execute %s\n", cut_cmd);
exit(1);
}
static
void cat(void)
{
int myfifo;
printf("\nCat opening FIFO");
if ( (myfifo = open("/tmp/myfifo", O_WRONLY | O_TRUNC) ) < 0)
{
perror("open FIFO at cat");
quit(EXIT_FAILURE);
}
printf("\nCat has opened FIFO");
dup2(myfifo, 1);
close(myfifo);
execl(cat_cmd, "cat", "/etc/passwd", (char *)0);
fprintf(stderr, "Failed to execute %s\n", cat_cmd);
exit(1);
}
int main(void)
{
mkfifo("/tmp/myfifo", 0600);
if (fork() == 0)
cat();
else
cut();
/*NOTREACHED*/
fprintf(stderr, "You should not see this message\n");
return 0;
}
示例输出:
很多运行满足
…
nobody
root
daemon
_uucp
_taskgated
_networkd
…
该代码确实与您的代码没有太大区别。我删除了一些混乱。我确实确保 close(myfifo)
是由 cat()
执行的;它不是原来的。这可能很重要。
我的objective是通过FIFO在child和parent之间做一个IPC。 child 应该 运行
execl ("/bin/cat", "cat", "/etc/passwd", (char *)0);
将其输出重定向到 parents 输入并且 parent 应该 运行 这个命令:
cut -l : -f 1
并将其输出到命令行。
现在,我已成功链接我的 FIFO 并将我的 child 进程的输出重定向到 parent 的输入。我已经完成了多项测试,并且该连接工作正常。问题出在剪切的 execl 上,它应该看起来像这样:
execlp("/bin/cut", "cut", "-l:", "-f", "1", NULL);
但我很确定它不是。
int cut(){
//
int myfifo;
char buf[MAX_BUF];
printf("\nCut opening FIFO");
if((myfifo = open("/tmp/myfifo", O_RDONLY | O_TRUNC))<0){
perror("open FIFO at cut");
quit(EXIT_FAILURE);}
else{printf("\nCut has FIFO opened and is reading\n");}
//read(myfifo, buf, MAX_BUF); outputting buf goes as supposed to
if( dup2(myfifo, 0) < 0 ){
perror("dup2 at cut");
quit(EXIT_FAILURE);}
//read(STDIN_FILENO, buf, MAX_BUF);
close(myfifo);
execlp("/bin/cut", "cut", "-l:", "-f", "1", NULL);
//this is for testing buf, but i guess the program shouldn't even get here
printf("\nCut has received: %s\nAnd is closing FIFO", buf);
return -1;
}
int cat(){
int myfifo;
//OPEN FIFO
printf("\nCat opening FIFO");
if( (myfifo = open("/tmp/myfifo", O_WRONLY | O_TRUNC) )<0){
perror("open FIFO at cat");
quit(EXIT_FAILURE);
}
else{
printf("\nCat has opened FIFO");
//WRITE OUTPUT OF "cat \etc\passwd" TO FIFO
dup2(myfifo, 1);
execl ("/bin/cat", "cat", "/etc/passwd", (char *)0);
}
close(myfifo);
return 0;
}
main目前只创建fifo(mkfifo)、forks()和调用函数。 我的问题可能与 parent 的标准输入有关(运行ning 剪切),但我不这么认为,或者我假设 execl() 直接从标准输入读取并且它没有. 我认为这真的是因为我没有正确地通过 execl() 编写 "cut"。
对代码的任何更正,甚至我表达某些想法的方式都可能表明我没有正确理解某些内容,将不胜感激。 感谢您的帮助
如评论中所述,GNU 和 BSD(以及 POSIX)下的 cut
命令不支持 -l
选项;您需要的选项是 -d
作为定界符。
这段代码对我有用。在我的机器上,/bin/cat
是正确的,但是 /usr/bin/cut
是正确的(不是 /bin/cut
),所以我不得不编译:
$ rmk cutcat UFLAGS=-DCUT_CMD=/usr/bin/cut && ./cutcat
gcc -O3 -g -std=c11 -Wall -Wextra -Werror -DCUT_CMD=/usr/bin/cut cutcat.c -o cutcat
$
它使用 make
的自定义变体(称为 rmk
)和自定义 makefile
,但 cut
命令的位置是在命令行中指定的通过 UFLAGS
(用户标志)宏。 C 代码中的 STRING
和 EXPAND
宏是一个麻烦,但没有尝试通过 shell 调用 make
(或rmk
) 然后 make
调用 运行 编译器的 shell。
代码:
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/stat.h>
#ifndef CUT_CMD
#define CUT_CMD /bin/cut
#endif
#ifndef CAT_CMD
#define CAT_CMD /bin/cat
#endif
#define EXPAND(x) #x
#define STRING(x) EXPAND(x)
static const char cut_cmd[] = STRING(CUT_CMD);
static const char cat_cmd[] = STRING(CAT_CMD);
static inline void quit(int status) { exit(status); }
enum { MAX_BUF = 4096 };
static void cut(void)
{
int myfifo;
printf("\nCut opening FIFO");
if ((myfifo = open("/tmp/myfifo", O_RDONLY | O_TRUNC)) < 0)
{
perror("open FIFO at cut");
quit(EXIT_FAILURE);
}
printf("\nCut has FIFO opened and is reading\n");
if (dup2(myfifo, 0) < 0)
{
perror("dup2 at cut");
quit(EXIT_FAILURE);
}
close(myfifo);
execlp(cut_cmd, "cut", "-d:", "-f", "1", NULL);
fprintf(stderr, "Failed to execute %s\n", cut_cmd);
exit(1);
}
static
void cat(void)
{
int myfifo;
printf("\nCat opening FIFO");
if ( (myfifo = open("/tmp/myfifo", O_WRONLY | O_TRUNC) ) < 0)
{
perror("open FIFO at cat");
quit(EXIT_FAILURE);
}
printf("\nCat has opened FIFO");
dup2(myfifo, 1);
close(myfifo);
execl(cat_cmd, "cat", "/etc/passwd", (char *)0);
fprintf(stderr, "Failed to execute %s\n", cat_cmd);
exit(1);
}
int main(void)
{
mkfifo("/tmp/myfifo", 0600);
if (fork() == 0)
cat();
else
cut();
/*NOTREACHED*/
fprintf(stderr, "You should not see this message\n");
return 0;
}
示例输出:
很多运行满足
…
nobody
root
daemon
_uucp
_taskgated
_networkd
…
该代码确实与您的代码没有太大区别。我删除了一些混乱。我确实确保 close(myfifo)
是由 cat()
执行的;它不是原来的。这可能很重要。