0

EDIT:: I do not refer my question to utsname.h. Instead, I'm looking to get the distribution version e.g.: Ubuntu 20.04.1. (Just the numbers).

I'm creating a function in C which return the build version on each OS. I already have it working on MS-Windows and macOS; However, I couldn't find anything to make it work on Linux(Ubuntu). Here is the code of my function with already works fine on MS-W and macOS.

struct OS_VERSION_LAT {
    double osMayor, osMenor, osBuild;
};

typedef struct OS_VERSION_LAT Struct;

Struct buscar_os_version() { // Search the build version on each OS
    Struct os_v;

    #ifdef WIN32
        OSVERSIONINFO osvi;

        ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
        osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);

        GetVersionEx(&osvi);

        os_v.osMayor = osvi.dwMajorVersion;
        os_v.osMenor = osvi.dwMinorVersion;
        os_v.osBuild = osvi.dwBuildNumber;
    #elif __APPLE__
        char cmd[64];
        for (int i=0; i<=3; i++){
            sprintf(cmd, "sw_vers -productVersion | awk -F '.' '{print $%d}'", i);

            FILE* stdoutFile = popen(cmd, "r");

            int resp = 0;
            if (stdoutFile) {
                char buff[16];
                char *stdout = fgets(buff, sizeof(buff), stdoutFile);
                pclose(stdoutFile);
                sscanf(stdout, "%d", &resp);
            }
            switch(i) {
                case 1:
                    os_v.osMayor = resp;
                case 2:
                    os_v.osMenor = resp;
                case 3:
                    os_v.osBuild = resp;
                    break;
            }
        }
    #else
    #endif

    return os_v;
}
Melvin Guerrero
  • 342
  • 3
  • 10
  • If you are calling external tools from C anyway, look for the `lsb_release` command. – tripleee Jan 05 '21 at 08:30
  • @tripleee thank you, I will check on that command; However, Is there any function or header file in C that can be used instead of the any external terminal tools? Thank you very much. – Melvin Guerrero Jan 06 '21 at 04:45
  • If you can still [edit] your question to clarify what exactly you want, it can certainly be reopened. "Release version of Linux" sounds vaguely like kernel version, which is what you get from `uname`; Linux distro and version is well-defined for LSB but there are obviously Linux distros and non-distro builds which don't adhere to the LSB spec (and still then some distros are rolling releases, so "release" as such is not meaningful). For LSB (which includes Ubuntu), you can simply parse the text file `/etc/lsb_release`. – tripleee Jan 06 '21 at 08:45
  • Notice that I updated the duplicate target. Probably no point in editing, though the target is nominally for C++, not C. Several answers there are portable to C, still. – tripleee Jan 06 '21 at 08:55

0 Answers0