我正在使用ubuntu 12.04我正试图"制作"一个项目.我收到此错误:
g++: error trying to exec 'cc1plus': execvp: No such file or directory
Run Code Online (Sandbox Code Playgroud)
我安装了g ++.
因此定义了execvp:
int execvp(const char *file, char *const argv[]);
Run Code Online (Sandbox Code Playgroud)
这使得这样的代码无法使用:
const char* argv[] = {"/bin/my", "command", "here", NULL};
execvp(argv[0], argv);
Run Code Online (Sandbox Code Playgroud)
这是意外遗漏吗?围绕这个const_cast是否安全?或者做一些execvp实现实际上是在那个内存上乱写?
用户将读取一行,我将保留第一个单词作为execvp的命令.
让我们说他会输入"cat file.txt" ...命令将是cat.但我不知道如何使用它execvp(),我读了一些教程,但仍然没有得到它.
#include <stdio.h>
#include <stdlib.h>
int main()
{
char *buf;
char command[32];
char name[32];
char *pointer;
char line[80];
printf(">");
while((buf = readline(""))!=NULL){
if (strcmp(buf,"exit")==0)
break;
if(buf[0]!=NULL)
add_history(buf);
pointer = strtok(buf, " ");
if(pointer != NULL){
strcpy(command, pointer);
}
pid_t pid;
int status;
if ((pid = fork()) < 0) {
printf("*** ERROR: forking child process failed\n");
exit(1);
}
else if (pid == 0) {
if (execvp(command, buf) < 0) {
printf("*** ERROR: exec failed\n");
exit(1); …Run Code Online (Sandbox Code Playgroud) 你头脑中的问题答案:是的,这是针对学校的.不,我不能使用线程.是的,我寻找答案,有些人说"是",其他人则说"不".我也正在检查我的教授,因为如果其他人要对其进行评分并且他们要求"修复",我不想不公平地失去分数.
有了这样说......在Linux系统上考虑这个简单的c程序.我malloc的东西,然后叉.我把我的项目归结为确切的问题:
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
int main( void )
{
char * args [] = { "someinvalidcommand", NULL };
// malloc before the fork (happens in parent process)
char * something = (char *)malloc(sizeof(char));
pid_t child_pid = fork();
// are there now two things that need to be freed:
// one for each process?
if(child_pid == 0) // child process
{
//free(something); // is this needed?
// execvp (it won't return if succeeded) …Run Code Online (Sandbox Code Playgroud) 我必须使用系统调用fork()/ execvp()在C中开发一个简单的shell.到目前为止,我的代码接受一个命令,使用strtok将其拆分为数组argv然后我调用fork来创建子进程并执行命令.我在ubuntu上工作,其中大多数命令都在/ bin /目录中,所以我附加程序名称(例如/ bin/ls)并将其用于execvp的第一个arg然后我给它argv数组.如果我输入命令"ls",我的程序可以工作,但是当尝试其他命令甚至"ls -l"时,我会得到一个"ls:invalid选项".我不知道我在这里做错了什么.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFER_LEN 1024
int main(){
char line[BUFFER_LEN]; //get command line
char* argv[100]; //user command
char* path= "/bin/"; //set path at bin
char progpath[20]; //full file path
int argc; //arg count
while(1){
printf("My shell>> "); //print shell prompt
if(!fgets(line, BUFFER_LEN, stdin)){ //get command and put it in line
break; //if user hits CTRL+D break
}
if(strcmp(line, "exit\n")==0){ //check if command is exit
break;
}
char *token; //split command into …Run Code Online (Sandbox Code Playgroud) 在C中说,我想调用execvp()任何字符串命令.命令可以是:
char command[] = "ls -l";
char command[] = "rm *.txt";
char command[] = "cat makefile";
Run Code Online (Sandbox Code Playgroud)
我想把这个命令变量放在里面execvp().因此,exec()调味函数可以使用任何类型的任意命令运行.
我怎样才能做到这一点?谢谢.
注意:system()不允许.
我想使用管道和execvp函数在我的Linux C程序中模拟bash.例如
ls -l | wc -l
Run Code Online (Sandbox Code Playgroud)
有我的计划:
if(pipe(des_p) == -1) {perror("Failed to create pipe");}
if(fork() == 0) { //first fork
close(1); //closing stdout
dup(des_p[1]); //replacing stdout with pipe write
close(des_p[0]); //closing pipe read
close(des_p[1]); //closing pipe write
if(execvp(bash_args[0], bash_args)) // contains ls -l
/* error checking */
}
else {
if(fork() == 0) { //creating 2nd child
close(0); //closing stdin
dup(des_p[0]); //replacing stdin with pipe read
close(des_p[1]); //closing pipe write
close(des_p[0]); //closing pipe read
if(execvp(bash_args[another_place], bash_args)) //contains wc -l …Run Code Online (Sandbox Code Playgroud) 我想编写一个程序Shellcode.c,它在输入中接受一个文本文件,其中包含由换行符分隔的bash命令,并执行文本文件中的每个命令:例如,文本文件将包含:
echo Hello World
mkdir goofy
ls
Run Code Online (Sandbox Code Playgroud)
我试过这个(只是开始练习一个exec函数):
#include <stdio.h>
#include <unistd.h>
void main() {
char *name[3];
name[0] = "echo";
name[1] = "Hello World";
name[2] = NULL;
execvp("/bin/sh", name);
}
Run Code Online (Sandbox Code Playgroud)
作为回报,我得到了
echo: Can't open Hello World
Run Code Online (Sandbox Code Playgroud)
我坚持使用execvp函数,我哪里出错了?
我正在尝试将用户输入的参数传递给execvp().
到目前为止,我已经拆分了字符串.如果用户键入ls -a,temp则保存为"ls"和"-a",后跟NULL字符.我不太确定如何恰当地指出这一点execvp.在我看到它使用的例子中execvp(temp[position], temp).我知道我现在尝试这样做的方式是错误的,但我不确定如何正确地做到这一点!目前我遇到了分段错误.
int main(int argc, char *argv[])
{
char line[124];
int size = 124;
char *temp = NULL;
while(fgets(line, size, stdin) != NULL ) {
if (strcmp(line, "exit\n") == 0) {
exit(EXIT_SUCCESS);
}
temp = strtok(line, " ");
while (temp != NULL) {
printf("%s\n", temp);
temp = strtok(NULL, " ");
}
execvp(temp, &temp);
}
return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud) 当我调用时execvp,例如execvp(echo, b),其中b是命令a的参数数组,稍后更改此数组会影响先前执行的execvp调用吗?当我尝试调用execp(echo,b)时,它最终打印出来(null)而不是b中的内容.任何人都可以指出为什么以及我必须做什么才能正确传递参数?