简体   繁体   中英

execve error when calling execve( man, args, env )

I'm having trouble with this execve command. I can use it to run most other commands just fine in my program, but if I try to do like man ls or something I get this error.

man: can't execute pager: No such file or directory
man: command exited with status 255: LESS=-ix8RmPm Manual page ls(1) ?ltline     %lt?L/%L.:byte %bB?s/%s..?e (END):?pB %pB\%.. (press h for help or q to quit)$PM Manual page   ls(1) ?ltline %lt?L/%L.:byte %bB?s/%s..?e (END):?pB %pB\%.. (press h for help or q to quit)$ MAN_PN=ls(1) pager -s

Here is how I'm calling it:

execve( cmdPath, args, env );

where cmdPath is the path(in this case /usr/bin/man ) args is a char* where args[0] = man , args[1] = ls env is my env*[] passed from main.

Any help would be much appreciated. I'm dying here.

Null terminate the arguments you pass to execve. Something like

char *args[3];
// other args..
args[2] = (char*) 0;

It is undefined behavior otherwise which is probably why it has worked sometimes in the past and this time you got unlucky.

This works:

int main(int argc, char *argv[], char *env[])
{
    char *args[3];

    args[0] = "man";
    args[1] = "ls";
    args[2] = (char*) 0;

    execve("/usr/bin/man", args, env);
}

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