Notcurses 3.0.16
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;
43 struct passwd* pret;
44 long blen = sysconf(_SC_GETPW_R_SIZE_MAX);
45 if(blen < 0){
46 logwarn("couldn't get getpwuid sysconf");
47 blen = 4096;
48 }
49 char* buf = malloc(blen);
50 if(buf == NULL){
51 return NULL;
52 }
53 if(getpwuid_r(uid, &p, buf, blen, &pret) || !pret){
54 free(buf);
55 return NULL;
56 }
57 char* ret = strdup(p.pw_name);
58 free(buf);
59 return ret;
60#else
61 DWORD unlen = UNLEN + 1;
62 char* un = malloc(unlen);
63 if(un == NULL){
64 return NULL;
65 }
66 if(!GetUserNameExA(NameSamCompatible, un, &unlen)){
67 logerror("couldn't get user name");
68 free(un);
69 return NULL;
70 }
71 return un;
72#endif
73}
#define logerror(fmt,...)
Definition logging.h:32
#define logwarn(fmt,...)
Definition logging.h:37
return NULL
Definition termdesc.h:229

◆ notcurses_hostname()

char * notcurses_hostname ( void  )

Definition at line 75 of file util.c.

75 {
76#ifndef __MINGW32__
77 char hostname[_POSIX_HOST_NAME_MAX + 1];
78 if(gethostname(hostname, sizeof(hostname)) == 0){
79 char* fqdn = strchr(hostname, '.');
80 if(fqdn){
81 *fqdn = '\0';
82 }
83 return strdup(hostname);
84 }
85#else // windows
86 char lp[MAX_COMPUTERNAME_LENGTH + 1];
87 DWORD s = sizeof(lp);
88 if(GetComputerNameA(lp, &s)){
89 return strdup(lp);
90 }
91#endif
92 return NULL;
93}

◆ notcurses_osversion()

char * notcurses_osversion ( void  )

Definition at line 95 of file util.c.

95 {
96#ifdef __MINGW32__
97 // FIXME get version
98 return strdup("Microsoft Windows");
99#else
100#ifdef __APPLE__
101#define PREFIX "macOS "
102 char osver[30] = PREFIX; // shrug
103 size_t oldlenp = sizeof(osver) - strlen(PREFIX);
104 if(sysctlbyname("kern.osproductversion", osver + strlen(PREFIX),
105 &oldlenp, NULL, 0) == 0){
106 return strdup(osver);
107 }
108 return strdup("macOS");
109#else
110 struct utsname uts;
111 if(uname(&uts)){
112 logerror("failure invoking uname (%s)", strerror(errno));
113 return NULL;
114 }
115 const size_t nlen = strlen(uts.sysname);
116 const size_t rlen = strlen(uts.release);
117 size_t tlen = nlen + rlen + 2;
118 char* ret = malloc(tlen);
119 memcpy(ret, uts.sysname, nlen);
120 ret[nlen] = ' ';
121 strcpy(ret + nlen + 1, uts.release);
122 return ret;
123#endif
124#undef PREFIX
125#endif
126}
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: