add new functions notcurses_{host,account}name()
parent
32ed5a57b0
commit
a346a5bf64
@ -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)**
|
@ -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…
Reference in New Issue