user3109672 user3109672 - 15 days ago 4
C Question

Spawning a new terminal and opening vim

What I'm trying to achieve is to open a new terminal from a C/C++ program and run vim. I'm doing this by forking and execing "xterm -e vim [fname]". Try as I might, I can't seem to get xterm to understand what it is I want it to do.

Below is the relevant code segment:

int pid = fork();
if (pid){
int retstat;
waitpid (pid, &retstat, 0);

char* ifname_cchararr = (char*)malloc(ifname.length() + 1);
strcpy (ifname_cchararr, ifname.c_str());
char* const argv[4] = {"-e", "vim", ifname_cchararr, NULL};
// std::cout << ifname_cchararr<<std::endl;
execvp ("xterm", argv);

Running the program results in xterm complaining:

-e : Explicit shell already was /usr/bin/vim

-e : bad command line option "testfile"

I get the feeling I've messed up argc somehow, but I'm confused, because running the following in an xterm window:

xterm -e vim testfile

works perfectly fine.

Please enlighten me!


You forgot to add xterm as first argument in argv. It may seems a bit weird, that you have to add the program-name to argv, since you already tell execvp which program you're calling, but thats how it is. For more information to why, see this recently asked question on Unix & Linux: Why does argv include the program name