如何使用execl()函数通过cut命令运行stdin?

如何使用execl()函数通过cut命令运行stdin?,c,fifo,dup2,execl,C,Fifo,Dup2,Execl,我的目标是通过FIFO在孩子和父母之间建立IPC。孩子应该跑 execl ("/bin/cat", "cat", "/etc/passwd", (char *)0); 将其输出重定向到父输入,父输入应运行以下命令: cut -l : -f 1 并将其输出到命令行 现在,我已经成功地链接了我的FIFO,并将子进程的输出重定向到父进程的输入。我已经做了多次测试,连接正常。问题在于切割的EXCL,它应该是这样的: execlp("/bin/cut", "cut", "-l:", "-f", "1"

我的目标是通过FIFO在孩子和父母之间建立IPC。孩子应该跑

execl ("/bin/cat", "cat", "/etc/passwd", (char *)0);
将其输出重定向到父输入,父输入应运行以下命令:

cut -l : -f 1
并将其输出到命令行

现在,我已经成功地链接了我的FIFO,并将子进程的输出重定向到父进程的输入。我已经做了多次测试,连接正常。问题在于切割的EXCL,它应该是这样的:

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,fork并调用函数。 我的问题可能与父级运行cut的stdin有关,但我不这么认为,或者我假设execl直接从stdin读取,而它没有。 我想这真的是因为我没有正确地编写直通EXCEL

对代码的任何更正,甚至我表达一些想法的方式都可能表明我没有正确理解某些内容,我们将不胜感激。
感谢您的帮助

如评论中所述,GNU、BSD和POSIX下的cut命令不支持a-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
$
它使用名为rmk的自定义make变量和自定义makefile,但cut命令的位置是通过UFLAGS user flags宏在命令行上指定的。C代码中的STRING和EXPAND宏是一个麻烦,但没有试图通过调用make或rmk的shell获取双引号,然后通过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;
}

代码与您的代码没有太大区别。我清除了一些杂物。我确实确保closemyfifo由cat执行;它不是原版的。这可能很重要。

为什么要使用FIFO或命名管道而不是正常、普通、匿名的管道?哪个版本的cut支持-l选项?GNU cut和BSD Mac OS X cut都没有。您似乎将其用作与-d选项字段分隔符等效的字段分隔符,这对于读取密码文件很有意义。此外,您的机器上是否有插接/存储箱?它在我的/usr/bin中,两个目录是不同的。是的,当我复制代码时,我使用了-l标志来检查我从控制台得到的错误。我的错误。/usr/bin/cut工作得很好。
…
nobody
root
daemon
_uucp
_taskgated
_networkd
…