Exemplo n.º 1
0
int main(int argc, char * argv[]) {
	
	char result[25];
	get_command_path("casperjs", result);
	printf("\"%s\"", result);
	
	
}
Exemplo n.º 2
0
void run_command_str(char** arr_words, int num_words, int out_fd, int in_fd) {
	
	int saved_stdout = dup(STDOUT_FILENO);
	int saved_stdin  = dup(STDIN_FILENO);
	
	if (in_fd != -1)
	{
		if (dup2(in_fd, STDIN_FILENO) == -1) 
		{
			perror("dup2 on pipe/redirection into stdin: ");	 // now close pipe
			//exit(EXIT_FAILURE);
		}
	}
	
	if (dup2(out_fd, STDOUT_FILENO) == -1) 
	{
		perror("dup2 on pipe/redirection into stdout: ");
		//exit(EXIT_FAILURE);
	}
	
	char* command_file = get_command_path(arr_words[0]);
	
	if (command_file == NULL) {
		return;
	}
	
	char* params[num_words - 1 + 2];		// num_words minus command name plus command file and \0
	params[0] = command_file;
	params[num_words] = NULL;
	
	int c;
	for(c = 1; c < num_words; c++) {
		params[c] = arr_words[c];
	}
	
	int f_val = fork();
	switch(f_val) {
		case 0:
		{
			
			if (execve(command_file, params, NULL) == -1) {
				perror("execvp: ");
				//exit(EXIT_FAILURE);
			}
			exit(EXIT_SUCCESS);
			break;
		}
		case -1:
		{
			perror("fork: ");
			exit(EXIT_FAILURE);
			break;
		}
		default:
		{
			wait(NULL);
			
			if (dup2(saved_stdout, STDOUT_FILENO) == -1) 
			{
				perror("dup2 on pipe/redirection into stdout: ");
				//exit(EXIT_FAILURE);
			}
			
			if (in_fd != -1)
			{
				if (dup2(saved_stdin, STDIN_FILENO)  == -1) 
				{
					perror("dup2 on pipe/redirection into stdin: ");	 // now close pipe
					//exit(EXIT_FAILURE);
				}
			}
			
			break;
		}
	}
}