theB3RV
theB3RV

Reputation: 924

C++ system calls "SED"

I am trying to run the sep command using a c++ program. When I compile the below code I get a warning for argv[0]="sep"; stating "deprecated conversion from string constant to âchar*â [-Wwrite-strings]." when I run the below program I get Exec Failed! ever time from the line below execvp().

#include<iostream>
#include<string>
#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <fcntl.h>

using namespace std;

int main(int argc, char **argv){
    pid_t pid;
    int fail;

    argv[0] = "sep";

    int i=0;
    while(i < argc){
        cout<< i << ": " << argv[i] <<endl;
        i++;
    }

    if(argc < 5){
        cout<< "./upload -i -r <key> <source> <destination>" <<endl;
    }else{
        pid = fork();
        if(pid < 0){
            cout<<"Fork Failed!\n";
            exit(1);
        }else if(pid == 0){                 //if you are in the child process
            fail = execvp("scp", argv); //execute command, return -1 on fail
            cout<< "Exec Failed!\n";
            exit(1);
        }else{
            int status;
            waitpid(pid, &status, 0);   //wait for each pid
        }
    }
    return 0;
}

Upvotes: 0

Views: 794

Answers (1)

theB3RV
theB3RV

Reputation: 924

Wow...typo.

Where argv[0] = "sep";

should have been "scp"

    char** args;

    args = new char*[argc+2]; //forcing 2 flags
    args[0] = "scp";
    args[1] = "-i";
    args[2] = "-r";
    args[3] = argv[1]; //key
    args[4] = argv[2]; //source
    args[5] = argv[3]; //destination

    execvp(args[0], args);

Upvotes: 1

Related Questions