execvp

C++, linux, fork, execvp, waitpid and SIGTSP

元气小坏坏 提交于 2019-12-22 14:00:54
问题 I'm implementing a Terminal for a Home Work. I almost finished, I just need to implement a bg ( Background ) and a fg ( Foreground ) commands. my code looks like this: void run(){ string command[] = parseMyInput( getInput() ); int fork_result = fork(); if( -1 == fork_result ) //handle error else if( 0 == fork_result ){ // child setpgrp(); // I don't want the children to get the signals if( -1 == execvp( command[0], makeArgs(command) ) ) //handle error } else { // parent if( command[ length -

program stops after execvp( command.argv[0], command.argv)

僤鯓⒐⒋嵵緔 提交于 2019-12-20 05:15:34
问题 I am writing a small shell program that takes a command and executes it. If the user enters a not valid command the if statement returns a -1. If the command is correct it executes the command, however once it executes the command the program ends. What am I doing wrong that is does not execute the lines of code after it? I have tested execvp( command.argv[0], command.argv) with ls and cat commands so I am pretty sure it works. Here is my code. int shell(char *cmd_str ){ int commandLength=0;

Executing a command with execvp

帅比萌擦擦* 提交于 2019-12-14 02:39:02
问题 I have an array of command strings I want to execute by calling execvp() : char* commands[] = ["ls -l", "ps -a", "ps"]; char* command = commands[0]; ... How do I execute the command with execvp ? 回答1: Here's a possible usage example for you. This takes the command to execute from its arguments or you can uncomment the hardcoded example. I recommend you look up the used commands in their respective man pages. For execvp , the declaration is int execvp(const char *file, char *const argv[]);

Wrong types involving converting a vector to feed to execvp

心已入冬 提交于 2019-12-13 06:27:25
问题 I have a vector of strings vector<string> args that I need to convert to a c string to in turn run as arguments through an execvp call. I keep getting the error "invalid conversion from const char* to char* for the line in my loop. I don't understand how to fix this while still resulting in something I can feed to execvp. While I have seen similar posts, none of the solutions I have come across seem to fix my issue. int argsLen = c->args.size(); char **argv = new char* [c->args.size() + 1];

C - error when attempting to pass /bin/ls to execvp

半腔热情 提交于 2019-12-13 04:44:24
问题 I am working on a C program that needs to be able to execute certain commands using execvp, and I have implemented this with: execvp(arguments[0], arguments); where arguments[] is an array of stings. For the most part, my implementation works fine - e.g. if arguments is {"touch", "somefile.txt"} then the touch command is called as expected. However, when I attempt to pass ls to execvp with arguments being something like {"/bin/ls", "-a", "."} , the ls function prints the directory listing as

C++ const char* To const char* const

不想你离开。 提交于 2019-12-12 11:26:20
问题 I am currently writing an assignment for my class that is supposed to act as a very basic shell. I am nearly finished, but I am running into an issue with execvp and my character array of parameters. Here is a light snippet of my code. //Split the left content args istringstream iss(left); while(getline(iss, s, ' ')){ v.push_back(s); } //Get the split string and put it into array const char* cmd_left[v.size()+1]; for(unsigned int i = 0; i < v.size(); i++){ cmd_left[i] = v.at(i).c_str(); } cmd

C - Using execvp with user input

时间秒杀一切 提交于 2019-12-12 02:48:25
问题 I'm currently trying to have my C program read Unix arguments from the user. I've so far searched this site but I haven't been able to figure out exactly what I'm doing wrong - though admittedly my pointer implementation skills are rather limited. The following is how I have the code now; I've been messing around with the pointers with no luck. The errors are also saying that I need to use const *char, but I've seen in other examples the *char can be input by the user. #include <stdio.h>

execvp not working when converting from vector<string> to vector<char*> to char**

一世执手 提交于 2019-12-12 01:43:31
问题 Going from a vector of strings to a vector of char* to a char**, was working when the argument came in as char**, but the conversion seems to have a problem and I'm not able to find the difference. Is there a better way to do this? vector<string> args; /* code that correctly parses args from user input */ pid_t kidpid = fork(); if (kidpid < 0) { perror("Internal error: cannot fork."); return -1; } else if (kidpid == 0) { // I am the child. vector<char*>argcs; for(int i=1;i<args.size();i++) {

why calling cd shell command through system() or execvp() from a child process won't work?

微笑、不失礼 提交于 2019-12-11 02:58:43
问题 I understand that i am supposed to use chdir() but I just need an explanation as to why calling cd shell command through system or execvp() from a child process would not work? Thanks!! 回答1: Because chdir only modifies the environment of the current process. It can't touch the environment of the parent. See also the link posted by tripleee. 来源: https://stackoverflow.com/questions/7120493/why-calling-cd-shell-command-through-system-or-execvp-from-a-child-process-w

After execvp returns, why doesn't my program pick up where it left off?

喜你入骨 提交于 2019-12-10 17:28:40
问题 I have a block of code like this that runs as a child thread: if(someVar == 1){ doSomeStuff; _exit(0) } else execvp(*(temp->_arguments), temp->_arguments); printf("I'm done\n"); When I run the program with someVar == 1, I understand that the _exit(0) call kills my thread. However, when it's set to 0, why doesn't the program continue after the execvp() call and do the printf statement? 回答1: If you exec* (call any exec function from the exec family), then the code of a new program is loaded