1 #include "threadfork.h"
14 ThreadFork::ThreadFork()
18 pthread_mutexattr_t attr;
19 pthread_mutexattr_init(&attr);
20 pthread_mutex_init(&start_lock, &attr);
24 arguments = new char*[MAX_ARGS];
31 ThreadFork::~ThreadFork()
36 // if(stdin_fd) fclose(stdin_fd);
41 // pthread_join(tid, 0);
43 for(int i = 0; i < total_arguments; i++)
44 delete [] arguments[i];
46 pthread_mutex_destroy(&start_lock);
50 void* ThreadFork::entrypoint(void *ptr)
52 ThreadFork *threadfork = (ThreadFork*)ptr;
56 void ThreadFork::start_command(char *command_line, int pipe_stdin)
58 this->command_line = command_line;
59 this->pipe_stdin = pipe_stdin;
62 pthread_attr_init(&attr);
65 stdin_fd = popen(command_line, "w");
67 // pthread_mutex_lock(&start_lock);
70 // pthread_create(&tid, &attr, entrypoint, this);
71 // pthread_mutex_lock(&start_lock);
72 // pthread_mutex_unlock(&start_lock);
76 void ThreadFork::run()
81 char argument[BCTEXTLEN];
82 char command_line[BCTEXTLEN];
85 strcpy(command_line, this->command_line);
89 // Set up arguments for exec
92 while(*ptr != ' ' && *ptr != 0)
98 arguments[total_arguments] = new char[strlen(path) + 1];
99 strcpy(arguments[total_arguments], path);
102 arguments[total_arguments] = 0;
107 argument_ptr = argument;
108 while(*ptr != ' ' && *ptr != 0)
110 *argument_ptr++ = *ptr++;
114 arguments[total_arguments] = new char[strlen(argument) + 1];
115 strcpy(arguments[total_arguments], argument);
117 arguments[total_arguments] = 0;
132 stdin_fd = fdopen(filedes[1], "w");
135 pthread_mutex_unlock(&start_lock);
144 if(pipe_stdin) dup2(filedes[0], fileno(stdin));
148 execvp(path, arguments);
158 pthread_mutex_unlock(&start_lock);
161 if(waitpid(pid, &return_value, WUNTRACED) == -1)
163 perror("ThreadFork::run: waitpid");
171 FILE* ThreadFork::get_stdin()
178 // c-file-style: "linux"