unable to display OS name in C using utsname

Viewed 30
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#define _POSIX_C_SOURCE
#include <sys/utsname.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/times.h>
#include <sched.h>

int main(void) {
    puts("!!!Hello World!!!"); /* prints !!!Hello World!!! */

    struct utsname uts;
    struct tms time;

    //obtaining cpu type and model
    execlp("start","starting", NULL);
    /*
    int outfd = open("starting.txt", O_CREAT|O_WRONLY|O_TRUNC, 0644);
    if(!outfd) {
            perror("open");
            return EXIT_FAILURE;
    }
    dup2(outfd, 1);
    close(outfd);*/
    if (uname(&uts) < 0)
        perror("uname() error");
    else {
        printf("Sysname:  %s\n", uts.sysname);
        printf("Nodename: %s\n", uts.nodename);
        printf("Release:  %s\n", uts.release);
        printf("Version:  %s\n", uts.version);
        printf("Machine:  %s\n", uts.machine);
    }
    puts("done");
    return EXIT_SUCCESS;
}

I have been trying to get the OS version using uts but for some reason I can't seem to obtain it, instead I am getting the output:

!!!Hello World!!!
Sysname:  CYGWIN_NT-10.0-18363
Nodename: LAPTOP-K1QSQFS4
Release:  3.3.6-341.x86_64
Version:  2022-09-05 11:15 UTC
Machine:  x86_64
done

When I want it to output my Host OS Windows 10

1 Answers

You're using Cygwin. If you look at the relevant part of the source of its uname() function, you see:

/* Cygwin "version" aka build date */
strcpy (name->version, cygwin_version.dll_build_date);

So getting a date for the version is perfectly normal, intended behavior in the cygwin environment.

Related