1

I'm wondering if there is similar commands to getprop/setprop that are exist in Android.

I want to enable to do changes for certain properties for a compiled app without compiling it again.

I know that on Android I can do so with adb shell setprop but I didn't find something similar in Linux/Mac.

There is a way to do something like that with environment variable on Linux (maybe the same thing on Mac) but is there something else?

I'll give an example for what I want to do: Let's say that I have a program that uses a default number of threads for parallelization and I wont to change the number of threads after compilation. I can do so by adding an environment variable, set it in my session then run my app.

The following code demonstrates this:

#include <sstream>
#include <cstdlib>

template <class T>
bool getEnv(const char* envVariable, T& value) {
    auto envValue = getenv(envVariable);
    if (!envValue) {
        return false;
    }
    if constexpr (std::is_same_v<T, int>) {
        auto tempValue = strtol(envValue, nullptr, 10);
        if (tempValue < std::numeric_limits<int>::min() ||
             tempValue > std::numeric_limits<int>::max()) {
            return false;
        }
        value = tempValue;
    } else if constexpr  (std::is_same_v<T, double>) {
        auto tempValue = strtod(envValue, nullptr);
        if (tempValue < std::numeric_limits<double>::min() || 
             tempValue > std::numeric_limits<double>::max()) {
            return false;
        }
        value = static_cast<double>(tempValue);
    } else {
        // make sure this won't compile
        static_assert(!std::is_same_v<T, T>);
    }
    return true;
}

int main() {
    int number_of_threads;
    int status = getEnv("NUMBER_OF_THREADS", number_of_threads);
    if (!status) {
        std::cerr << "Failed to get NUMBER_OF_THREADS\n";
        return 1;
    }
    std::cout << "NUMBER_OF_THREADS=" << number_of_threads << "\n";

    return 0;
}

On the terminal:

idancw:~/envTest/cmake-build-debug> export NUMBER_OF_THREADS=4
idancw:~/envTest/cmake-build-debug> ./test 
NUMBER_OF_THREADS=4
idancw:~/envTest/cmake-build-debug> export NUMBER_OF_THREADS=10
idancw:~/envTest/cmake-build-debug> ./test                     
NUMBER_OF_THREADS=10

Is there other way to do so without using env variable, similar to Android?

I gauss that I can use the env approach on Mac too, but there is something else?

Thanks :)

Idan Cohen
  • 125
  • 1
  • 6

1 Answers1

0

To answer the initial question, let's see, what exeactly getprop / setprop do on Android.

For example, from here we know, that getprop / setprop are just API to read and write some database, stored in files on filesystem. This API manages by system process called "property_service".

So the qualities we may want are: system-wide, accessible via some native API, persistent database. As the native API is in list of desires, we could predict there will be no cross-platform solution out of the box :)

On Windows I would say it sounds like Registry. It definetely stored on filesystem, there is WinAPI to access or change it, and it persists reboot.

On macOS the closest thing I could think of is NSUserDefaults.

For the linux afaik there is no uniform solution to all distributions, but there are a lot of places to store and read parameters from filesystem, widely discussed.

Arthur Bulakaiev
  • 1,207
  • 8
  • 17