add new functions notcurses_{host,account}name()

pull/1978/head
nick black 3 years ago
parent 32ed5a57b0
commit a346a5bf64
No known key found for this signature in database
GPG Key ID: 5F43400C21CBFACC

@ -1,7 +1,9 @@
This document attempts to list user-visible changes and any major internal
rearrangements of Notcurses.
* 2.3.13 (not yet released)
* 2.3.13 (2021-08-04)
* Added the portable utility functions `notcurses_accountname()` and
`notcurses_hostname()`.
* 2.3.12 (2021-07-29)
* `notcurses_getc()` and `ncdirect_getc()` no longer accept a `sigset_t*`

@ -76,6 +76,7 @@
<a href="notcurses_stop.3.html">notcurses_stop</a>—collapse the context<br/>
<a href="notcurses_tabbed.3.html">notcurses_tabbed</a>—tabbed interface widget<br/>
<a href="notcurses_tree.3.html">notcurses_tree</a>—line-based widget for hierarchical data<br/>
<a href="notcurses_util.3.html">notcurses_util</a>—portable utility functions<br/>
<a href="notcurses_visual.3.html">notcurses_visual</a>—operations on <tt>ncvisual</tt> objects<br/>
</div>
<div style="float:right;clear:right">

@ -0,0 +1,35 @@
% notcurses_util(3)
% nick black <nickblack@linux.com>
% v2.3.12
# NAME
notcurses_util - portable utility functions
# SYNOPSIS
**#include <notcurses/notcurses.h>**
**char* notcurses_accountname(void);**
**char* notcurses_hostname(void);**
# DESCRIPTION
Notcurses provides some utility functions, usually to abstract away
platform-dependent differences.
**notcurses_accountname** returns a heap-allocated copy of the account
name under which the program is running. **notcurses_hostname** returns
a heap-allocated copy of the local host name.
# NOTES
# RETURN VALUES
All functions return ***NULL*** on error.
# SEE ALSO
**hostname(1)**,
**notcurses(3)**

@ -94,6 +94,12 @@ typedef enum {
// -1 if a non-printable/illegal character is encountered.
API int ncstrwidth(const char* mbs);
// Returns a heap-allocated copy of the user name under which we are running.
API ALLOC char* notcurses_accountname(void);
// Returns a heap-allocated copy of the local host name.
API ALLOC char* notcurses_hostname(void);
// input functions like notcurses_get() return ucs32-encoded uint32_t. convert
// a series of uint32_t to utf8. result must be at least 4 bytes per input
// uint32_t (6 bytes per uint32_t will future-proof against Unicode expansion).

@ -0,0 +1,51 @@
#ifndef __MINGW64__
#include <pwd.h>
#include <unistd.h>
#endif
#include "internal.h"
char* notcurses_accountname(void){
#ifndef __MINGW64__
const char* un;
if( (un = getenv("LOGNAME")) ){
return strdup(un);
}
uid_t uid = getuid();
struct passwd* p = getpwuid(uid);
if(p == NULL){
return NULL;
}
return strdup(p->pw_name);
#else
DWORD unlen = UNLEN + 1;
char* un = malloc(unlen);
if(un == NULL){
return NULL;
}
if(GetUserName(un, &unlen)){ // FIXME probably want GetUserNameEx
free(un);
return NULL;
}
return un;
#endif
}
char* notcurses_hostname(void){
#ifndef __MINGW64__
char hostname[_POSIX_HOST_NAME_MAX + 1];
if(gethostname(hostname, sizeof(hostname)) == 0){
char* fqdn = strchr(hostname, '.');
if(fqdn){
*fqdn = '\0';
}
return strdup(hostname);
}
#else // windows
char lp[MAX_COMPUTERNAME_LENGTH + 1];
size_t s = sizeof(lp);
if(GetComputerNameA(lp, &s)){
return strdup(lp);
}
#endif
return NULL;
}
Loading…
Cancel
Save