[utils] implement notcurses_osversion #2293

pull/2294/head
nick black 3 years ago
parent 6e2ab83a08
commit 85b6d55577
No known key found for this signature in database
GPG Key ID: 5F43400C21CBFACC

@ -350,24 +350,13 @@ dragonfly_ncneofetch(fetched_info* fi){
static const distro_info*
xnu_ncneofetch(fetched_info* fi){
static distro_info fbsd = {
static distro_info xnu = {
.name = "OS X",
.logofile = "/System/Library/PrivateFrameworks/LoginUIKit.framework/Versions/A/Frameworks/LoginUICore.framework/Versions/A/Resources/apple@2x.png",
};
fi->neologo = get_neofetch_art("Darwin");
#ifdef __APPLE__
#define PREFIX "macOS "
char osver[30] = PREFIX; // shrug
size_t oldlenp = sizeof(osver) - strlen(PREFIX);
if(sysctlbyname("kern.osproductversion", osver + strlen(PREFIX),
&oldlenp, NULL, 0) == 0){
fi->distro_pretty = strdup(osver);
}
#undef PREFIX
#else
fi->distro_pretty = strdup("macOS");
#endif
return &fbsd;
fi->distro_pretty = notcurses_osversion();
return &xnu;
}
static int

@ -17,21 +17,6 @@ init_banner_warnings(const notcurses* nc, fbuf* f, const char* clreol){
return 0;
}
static inline const char*
osname(void){
#ifdef __MINGW64__
return "Windows";
#elif defined(__APPLE__)
return "macOS";
#elif defined(__gnu_hurd__)
return "GNU Hurd";
#elif defined(BSD)
return "BSD";
#else
return "Linux";
#endif
}
// unless the suppress_banner flag was set, print some version information and
// (if applicable) warnings to ttyfp. we are not yet on the alternate screen.
int init_banner(const notcurses* nc, fbuf* f){
@ -41,12 +26,13 @@ int init_banner(const notcurses* nc, fbuf* f){
}
if(!nc->suppress_banner){
term_fg_palindex(nc, f, 50 % nc->tcache.caps.colors);
char* osver = notcurses_osversion();
fbuf_printf(f, "%snotcurses %s on %s %s%s(%s)" NL,
clreol, notcurses_version(),
nc->tcache.termname ? nc->tcache.termname : "?",
nc->tcache.termversion ? nc->tcache.termversion : "",
nc->tcache.termversion ? " " : "",
osname());
nc->tcache.termversion ? " " : "", osver ? osver : "unknown");
free(osver);
term_fg_palindex(nc, f, nc->tcache.caps.colors <= 256 ?
14 % nc->tcache.caps.colors : 0x2080e0);
if(nc->tcache.cellpixy && nc->tcache.cellpixx){

@ -1,10 +1,18 @@
#ifndef __MINGW64__
#include <pwd.h>
#include <unistd.h>
#if defined(__linux__) || defined(__gnu_hurd__)
#include <sys/utsname.h>
#include <sys/sysinfo.h>
#elif !defined(__MINGW64__)
#include <sys/sysctl.h>
#include <sys/utsname.h>
#endif
#else
#include <winsock2.h>
#define SECURITY_WIN32
#include <secext.h>
#include <sysinfoapi.h>
#endif
#include "internal.h"
@ -70,3 +78,36 @@ char* notcurses_hostname(void){
#endif
return NULL;
}
char* notcurses_osversion(void){
#ifdef __MINGW64__
// FIXME get version
return strdup("Microsoft Windows");
#else
#ifdef __APPLE__
#define PREFIX "macOS "
char osver[30] = PREFIX; // shrug
size_t oldlenp = sizeof(osver) - strlen(PREFIX);
if(sysctlbyname("kern.osproductversion", osver + strlen(PREFIX),
&oldlenp, NULL, 0) == 0){
return strdup(osver);
}
return strdup("macOS");
#else
struct utsname uts;
if(uname(&uts)){
logerror("failure invoking uname (%s)\n", strerror(errno));
return NULL;
}
const size_t nlen = strlen(uts.sysname);
const size_t rlen = strlen(uts.release);
size_t tlen = nlen + rlen + 2;
char* ret = malloc(tlen);
memcpy(ret, uts.sysname, nlen);
ret[nlen] = ' ';
strcpy(ret + nlen + 1, uts.release);
return ret;
#endif
#undef PREFIX
#endif
}

Loading…
Cancel
Save