execvp

C - Executing Bash Commands with Execvp

烈酒焚心 提交于 2019-11-27 06:04:40
问题 I want to write a program Shellcode.c that accepts in input a text file, which contains bash commands separeted by newline, and executes every commands in the text file: for example, the text file will contain: echo Hello World mkdir goofy ls I tried this one (just to begin practicing with one of the exec functions): #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); } I get, in return, echo:

How to use execvp()

感情迁移 提交于 2019-11-26 16:46:11
问题 The user will read a line and i will retain the first word as a command for execvp. Lets say he will type "cat file.txt" ... command will be cat . But i am not sure how to use this execvp() , i read some tutorials but still didn't get it. #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 =

Classic C. Using pipes in execvp function, stdin and stdout redirection

烈酒焚心 提交于 2019-11-26 11:25:17
问题 I want to simulate bash in my Linux C program using pipes and execvp function. e.g ls -l | wc -l There is my program: 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); /