[PATCH] Remove arguments, patch vixen, add hunter AI
[progcomp2012.git] / judge / manager / program.cpp
index 02c9cb8..c936de3 100644 (file)
 #include <string.h>
 #include <stdio.h>
 
+#include <stdio.h>
+#include <sys/time.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+
 using namespace std;
 
 
@@ -26,7 +32,7 @@ Program::Program(const char * executablePath) : input(NULL), output(NULL), pid(0
 {
        
                
-       
+       /*
        vector<char*> args;
        if (executablePath[0] != '"')
                args.push_back((char*)executablePath);
@@ -60,10 +66,11 @@ Program::Program(const char * executablePath) : input(NULL), output(NULL), pid(0
        char **  arguments = NULL;
         if (args.size() > 0)
        {
-               arguments = new char*[args.size()+2];
+               arguments = new char*[args.size()];
                for (unsigned int i=0; i < args.size(); ++i)
                        arguments[i] = args[i];
        }
+       */
        //See if file exists and is executable...
        if (access(executablePath, X_OK) != 0)
        {
@@ -94,7 +101,8 @@ Program::Program(const char * executablePath) : input(NULL), output(NULL), pid(0
 
                if (access(executablePath, X_OK) == 0) //Check we STILL have permissions to start the file
                {
-                       execv(executablePath,arguments); ///Replace process with desired executable
+                       execl(executablePath, executablePath, (char*)(NULL)); ///Replace process with desired executable
+                       //execv(executablePath,arguments); ///Replace process with desired executable
                }
                perror("execv error:\n");
                fprintf(stderr, "Program::Program - Could not run program \"%s\"!\n", executablePath);
@@ -221,6 +229,31 @@ bool Program::GetMessage(string & buffer, double timeout)
        if (!Running() || timeout == 0)
                return false;
 
+       struct timeval tv;
+       fd_set readfds;
+       
+       tv.tv_sec = (int)(timeout);
+       tv.tv_usec = (timeout - (double)((int)timeout)) * 1000000;
+       
+       int fd = fileno(input);
+
+       FD_ZERO(&readfds);
+       FD_SET(fd, &readfds);
+
+       select(fd+1, &readfds, NULL, NULL, &tv);
+
+       if (!FD_ISSET(fd, &readfds))
+               return false; //Timed out
+       //fprintf(stderr, "Got message!\n");
+       for (char c = fgetc(input); c != '\n' && (int)(c) != EOF; c = fgetc(input))
+       {       
+               //fprintf(stderr, "%c", c);
+               buffer += c;
+       }
+       //fprintf(stderr, "%s\n", buffer.c_str());
+       return true;
+
+       /* Old way, using threads, which apparently is terrible
        assert(&buffer != NULL);
        GetterThread getterThread(input, buffer);
        assert(&(getterThread.buffer) != NULL);
@@ -251,7 +284,7 @@ bool Program::GetMessage(string & buffer, double timeout)
        if (buffer.size() == 1 && buffer[0] == EOF)
                return false;
        return true;
-
+       */
 
 }
 

UCC git Repository :: git.ucc.asn.au