tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

system_utils_apple.cpp (1380B)


      1 //
      2 // Copyright 2020 The ANGLE Project Authors. All rights reserved.
      3 // Use of this source code is governed by a BSD-style license that can be
      4 // found in the LICENSE file.
      5 //
      6 
      7 // system_utils_apple.cpp: Implementation of OS-specific functions for Apple platforms
      8 
      9 #include "system_utils.h"
     10 
     11 #include <unistd.h>
     12 
     13 #include <CoreServices/CoreServices.h>
     14 #include <mach-o/dyld.h>
     15 #include <mach/mach.h>
     16 #include <mach/mach_time.h>
     17 #include <cstdlib>
     18 #include <vector>
     19 
     20 #include <array>
     21 
     22 namespace angle
     23 {
     24 std::string GetExecutablePath()
     25 {
     26    std::string result;
     27 
     28    uint32_t size = 0;
     29    _NSGetExecutablePath(nullptr, &size);
     30 
     31    std::vector<char> buffer;
     32    buffer.resize(size + 1);
     33 
     34    _NSGetExecutablePath(buffer.data(), &size);
     35    buffer[size] = '\0';
     36 
     37    if (!strrchr(buffer.data(), '/'))
     38    {
     39        return "";
     40    }
     41    return buffer.data();
     42 }
     43 
     44 std::string GetExecutableDirectory()
     45 {
     46    std::string executablePath = GetExecutablePath();
     47    size_t lastPathSepLoc      = executablePath.find_last_of("/");
     48    return (lastPathSepLoc != std::string::npos) ? executablePath.substr(0, lastPathSepLoc) : "";
     49 }
     50 
     51 double GetCurrentSystemTime()
     52 {
     53    mach_timebase_info_data_t timebaseInfo;
     54    mach_timebase_info(&timebaseInfo);
     55 
     56    double secondCoeff = timebaseInfo.numer * 1e-9 / timebaseInfo.denom;
     57    return secondCoeff * mach_absolute_time();
     58 }
     59 }  // namespace angle