1 #ifndef __MINGW64__
2 #include <pwd.h>
3 #include <unistd.h>
4 #if defined(__linux__) || defined(__gnu_hurd__)
5 #include <sys/utsname.h>
6 #include <sys/sysinfo.h>
7 #elif !defined(__MINGW64__)
8 #include <sys/sysctl.h>
9 #include <sys/utsname.h>
10 #endif
11 #else
12 #include <winsock2.h>
13 #define SECURITY_WIN32
14 #include <secext.h>
15 #include <sysinfoapi.h>
16 #endif
17 #include "internal.h"
18 
set_loglevel_from_env(ncloglevel_e * llptr)19 int set_loglevel_from_env(ncloglevel_e* llptr){
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\n", ll);
28     return -1;
29   }
30   *llptr = l;
31   loginfo("Got loglevel from environment: %ld\n", l);
32   return 0;
33 }
34 
notcurses_accountname(void)35 char* notcurses_accountname(void){
36 #ifndef __MINGW64__
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\n");
55     free(un);
56     return NULL;
57   }
58   return un;
59 #endif
60 }
61 
notcurses_hostname(void)62 char* notcurses_hostname(void){
63 #ifndef __MINGW64__
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 }
81 
notcurses_osversion(void)82 char* notcurses_osversion(void){
83 #ifdef __MINGW64__
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)\n", 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 }
114