简体   繁体   中英

execvp won't work with cut

I'm making work to school and I have a problem with "cut" command when I try launch this command in c++. So this is my exercise-> I want launch this command in C++ -> "cut -d':' -f5 < file" I wrote text from the file to variable input in main function. expected as result of command -> "five" but I got only an error message "cut: the delimiter must be a single character Try 'cut --help' for more information."

do you know why ? Thanks for help :)

this is my test code:

#include <iostream>
#include <sys/wait.h>
#include <unistd.h>
#include <string>
#include <cstdio>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cstdlib>

const int BUFFER_SIZE = 4096;

using namespace std;

char *argCat[] = { "cat", (char *)0 };
char *argEcho[] = { "echo", "Hello", (char *)0 };
char *argCut[] = { "cut", "-d':'", "-f5", (char *)0};

char *stringCharConvertor(string paString)
{
    char * temp = new char[paString.size() + 1];
    std::copy(paString.begin(), paString.end(), temp);
    temp[paString.size()] = '\0';
    return temp;
}

void executeCommand(char** paCommand, string &paOutput, string &paInput)
{
    char** arg = paCommand;
    bool validInput = paInput == "" ? false : true;

    int PARRENT_TO_CHILD[2];
    int CHILD_TO_PARRENT[2];
    if(pipe(CHILD_TO_PARRENT) < 0)
        perror("pipe error");



    if(validInput)
    {

        if(pipe(PARRENT_TO_CHILD) < 0)
            perror("pipe error");
        char* temp = stringCharConvertor(paInput);
        write(PARRENT_TO_CHILD[1], temp, strlen(temp));
        close(PARRENT_TO_CHILD[1]);
    }


    pid_t PID = fork();
    if(PID > 0)
    {
        int s;
        char buffer[BUFFER_SIZE+1];
        memset(buffer, '\0', sizeof(buffer));
        close[CHILD_TO_PARRENT[1]];
        wait(&s);
        if(read(CHILD_TO_PARRENT[0], buffer, BUFFER_SIZE) < 0)
            printf("error ");
        close(CHILD_TO_PARRENT[0]);
        paOutput = buffer;
        if(validInput)
            close(PARRENT_TO_CHILD[0]);
            cout << "\n"+paInput;
    }
    else
        if(PID == 0)
        {
            dup2(CHILD_TO_PARRENT[1], STDOUT_FILENO);
            close(CHILD_TO_PARRENT[1]);
            close(CHILD_TO_PARRENT[0]);
            if(validInput)
            {
                dup2(PARRENT_TO_CHILD[0], STDIN_FILENO);
                close(PARRENT_TO_CHILD[0]);
                close(PARRENT_TO_CHILD[1]);
            }
            if(execvp(arg[0], arg) < 0)
                close(CHILD_TO_PARRENT[1]);
        }
}


int main()
{
    string input = "one:two:three:four:five:six:seven:eight:nine:ten";
    string output = "";
    executeCommand(argCut, output, input);
    cout << "\n INPUT: "+input <<endl;
    cout << "\n OUTPUT: "+output <<endl;
    return 0;
}

you should try to replace

char *argCut[] = { "cut", "-d':'", "-f5", (char *)0};

by

char *argCut[] = { "cut", "-d:", "-f5", (char *)0};

no need for quotes

here's some rationale: running strace on your code shows:

[pid  7641] execve("/usr/bin/cut", ["cut", "-d':'", "-f5"], [/* 85 vars */]) = 0

which is more or less equivalent to the call

/bin/cut "-d':'" "-f5"

which gives the same error

In fact the shell does remove the extra quotes as you may see:

$ echo one:two:three:four:five | strace -f /bin/cut -d':' -f5  
execve("/bin/cut", ["/bin/cut", "-d:", "-f5"], [/* 85 vars */]) = 0
=> success

whereas:

$ echo one:two:three:four:five | strace -f /bin/cut "-d':'" -f5 
execve("/bin/cut", ["/bin/cut", "-d':'", "-f5"], [/* 85 vars */]) = 0
=> failure

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM