Notcurses 3.0.13
a blingful library for TUIs and character graphics
Loading...
Searching...
No Matches
util.c File Reference
#include <pwd.h>
#include <unistd.h>
#include <sys/sysctl.h>
#include <sys/utsname.h>
#include "internal.h"
Include dependency graph for util.c:

Go to the source code of this file.

Functions

int set_loglevel_from_env (ncloglevel_e *llptr)
 
char * notcurses_accountname (void)
 
char * notcurses_hostname (void)
 
char * notcurses_osversion (void)
 

Function Documentation

◆ notcurses_accountname()

char * notcurses_accountname ( void  )

Definition at line 35 of file util.c.

35 {
36#ifndef __MINGW32__
37 const char* un;
38 if( (un = getenv("LOGNAME")) ){
39 return strdup(un);
40 }
41 uid_t uid = getuid();
42 struct passwd* p = getpwuid(uid);
43 if(p == NULL){
44 return NULL;
45 }
46 return strdup(p->pw_name);
47#else
48 DWORD unlen = UNLEN + 1;
49 char* un = malloc(unlen);
50 if(un == NULL){
51 return NULL;
52 }
53 if(!GetUserNameExA(NameSamCompatible, un, &unlen)){
54 logerror("couldn't get user name");
55 free(un);
56 return NULL;
57 }
58 return un;
59#endif
60}
free(duplicated)
#define logerror(fmt,...)
Definition logging.h:32
return NULL
Definition termdesc.h:229
Here is the call graph for this function:

◆ notcurses_hostname()

char * notcurses_hostname ( void  )

Definition at line 62 of file util.c.

62 {
63#ifndef __MINGW32__
64 char hostname[_POSIX_HOST_NAME_MAX + 1];
65 if(gethostname(hostname, sizeof(hostname)) == 0){
66 char* fqdn = strchr(hostname, '.');
67 if(fqdn){
68 *fqdn = '\0';
69 }
70 return strdup(hostname);
71 }
72#else // windows
73 char lp[MAX_COMPUTERNAME_LENGTH + 1];
74 DWORD s = sizeof(lp);
75 if(GetComputerNameA(lp, &s)){
76 return strdup(lp);
77 }
78#endif
79 return NULL;
80}

◆ notcurses_osversion()

char * notcurses_osversion ( void  )

Definition at line 82 of file util.c.

82 {
83#ifdef __MINGW32__
84 // FIXME get version
85 return strdup("Microsoft Windows");
86#else
87#ifdef __APPLE__
88#define PREFIX "macOS "
89 char osver[30] = PREFIX; // shrug
90 size_t oldlenp = sizeof(osver) - strlen(PREFIX);
91 if(sysctlbyname("kern.osproductversion", osver + strlen(PREFIX),
92 &oldlenp, NULL, 0) == 0){
93 return strdup(osver);
94 }
95 return strdup("macOS");
96#else
97 struct utsname uts;
98 if(uname(&uts)){
99 logerror("failure invoking uname (%s)", strerror(errno));
100 return NULL;
101 }
102 const size_t nlen = strlen(uts.sysname);
103 const size_t rlen = strlen(uts.release);
104 size_t tlen = nlen + rlen + 2;
105 char* ret = malloc(tlen);
106 memcpy(ret, uts.sysname, nlen);
107 ret[nlen] = ' ';
108 strcpy(ret + nlen + 1, uts.release);
109 return ret;
110#endif
111#undef PREFIX
112#endif
113}
Here is the caller graph for this function:

◆ set_loglevel_from_env()

int set_loglevel_from_env ( ncloglevel_e llptr)

Definition at line 19 of file util.c.

19 {
20 const char* ll = getenv("NOTCURSES_LOGLEVEL");
21 if(ll == NULL){
22 return 0;
23 }
24 char* endl;
25 long l = strtol(ll, &endl, 10);
26 if(l < NCLOGLEVEL_PANIC || l > NCLOGLEVEL_TRACE){
27 logpanic("illegal NOTCURSES_LOGLEVEL: %s", ll);
28 return -1;
29 }
30 *llptr = l;
31 loginfo("got loglevel from environment: %ld", l);
32 return 0;
33}
API int API int API int uint64_t uint64_t uint64_t ll
Definition direct.h:216
#define loginfo(fmt,...)
Definition logging.h:42
#define logpanic(fmt,...)
Definition logging.h:22
@ NCLOGLEVEL_TRACE
Definition notcurses.h:977
Here is the caller graph for this function: