Fix hostname printing.

ssh doesn't set HOSTNAME in env, that has to be determined separately.
What indicates a ssh session are some SSH_* env vars.
This commit is contained in:
Érico Rolim 2021-02-22 12:34:34 -03:00
parent 1f1adc1663
commit 815fdf1294
4 changed files with 26 additions and 6 deletions

View File

@ -13,7 +13,7 @@ bindir = $(PREFIX)/bin
all: ep
ep: ep.c out.c path.c git.c lang.c $(LANGUAGE)
ep: ep.c out.c path.c git.c lang.c ssh.c $(LANGUAGE)
install: ep
install -m755 $< $(bindir)/ep

6
ep.c
View File

@ -71,13 +71,9 @@ int main(int argc, char **argv)
p("[chroot] ");
const char *home = getenv("HOME");
const char *hostname = getenv("HOSTNAME");
/* show we are on a different machine */
if (hostname) {
p(hostname);
p(" ");
}
print_ssh();
print_pwd(home);

3
ep.h
View File

@ -35,4 +35,7 @@ void print_git(void);
void *lang_thread(void *);
void print_lang(uint64_t);
/* from ssh.c */
void print_ssh(void);
#endif

21
ssh.c Normal file
View File

@ -0,0 +1,21 @@
#include <stdlib.h>
#include <sys/utsname.h>
#include "ep.h"
void print_ssh(void)
{
/* expect ssh to have set these variables - we don't need to prompt if not using a tty */
const char *sshcon = getenv("SSH_CONNECTION"), *sshtty = getenv("SSH_TTY");
if (sshcon && *sshcon && sshtty && *sshtty) {
struct utsname u;
/* don't print anything if uname fails or nodename is empty */
if (uname(&u) || !u.nodename[0])
return;
p("(");
p(u.nodename);
p(") ");
}
}