]> git.cworth.org Git - apitrace/blobdiff - common/os_posix.cpp
Move mutex abstraction to os_thread.hpp.
[apitrace] / common / os_posix.cpp
index ef329d08082c74885c130d8d9449e201a4ecf2f5..65c5404e3c0e64dd92c2184143474e05fe815b10 100644 (file)
@@ -31,7 +31,7 @@
 
 #include <unistd.h>
 #include <sys/time.h>
-#include <pthread.h>
+#include <sys/wait.h>
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <signal.h>
@@ -41,6 +41,7 @@
 #endif
 
 #ifdef __APPLE__
+#include <sys/syslimits.h> // PATH_MAX
 #include <mach-o/dyld.h>
 #endif
 
 #endif
 
 #include "os.hpp"
-#include "os_path.hpp"
+#include "os_string.hpp"
 
 
 namespace os {
 
 
-static pthread_mutex_t 
-mutex = PTHREAD_MUTEX_INITIALIZER;
-
-
-void
-acquireMutex(void)
-{
-    pthread_mutex_lock(&mutex);
-}
-
-
-void
-releaseMutex(void)
-{
-    pthread_mutex_unlock(&mutex);
-}
-
-
-Path
+String
 getProcessName(void)
 {
-    Path path;
+    String path;
     size_t size = PATH_MAX;
     char *buf = path.buf(size);
 
@@ -88,6 +71,7 @@ getProcessName(void)
         *buf = 0;
         return path;
     }
+    len = strlen(buf);
 #else
     ssize_t len;
     len = readlink("/proc/self/exe", buf, size - 1);
@@ -110,10 +94,10 @@ getProcessName(void)
     return path;
 }
 
-Path
+String
 getCurrentDir(void)
 {
-    Path path;
+    String path;
     size_t size = PATH_MAX;
     char *buf = path.buf(size);
 
@@ -124,6 +108,43 @@ getCurrentDir(void)
     return path;
 }
 
+bool
+String::exists(void) const
+{
+    struct stat st;
+    int err;
+
+    err = stat(str(), &st);
+    if (err) {
+        return false;
+    }
+
+    if (!S_ISREG(st.st_mode))
+        return false;
+
+    return true;
+}
+
+int execute(char * const * args)
+{
+    pid_t pid = fork();
+    if (pid == 0) {
+        // child
+        execvp(args[0], args);
+        fprintf(stderr, "error: failed to execute %s\n", args[0]);
+        exit(-1);
+    } else {
+        // parent
+        if (pid == -1) {
+            fprintf(stderr, "error: failed to fork\n");
+            return -1;
+        }
+        int status = -1;
+        waitpid(pid, &status, 0);
+        return status;
+    }
+}
+
 void
 log(const char *format, ...)
 {