From c5f0d28481815036b4fecb6aa963cc3777002c0a Mon Sep 17 00:00:00 2001 From: cylgom Date: Sat, 30 Jun 2018 11:43:05 +0200 Subject: [PATCH] changed the default greeting to hostname --- src/lang.h | 1 - src/ncui.c | 5 ++++- src/utils.c | 38 +++++++++++++++++++++++++++++++++++++- src/utils.h | 1 + 4 files changed, 42 insertions(+), 3 deletions(-) diff --git a/src/lang.h b/src/lang.h index c686237..88849cf 100644 --- a/src/lang.h +++ b/src/lang.h @@ -2,7 +2,6 @@ #define _LANG_H_ /* UI strings */ -#define LY_LANG_GREETING "Welcome to ly !" #define LY_LANG_VALID_CREDS "Logged In" #define LY_LANG_LOGOUT "Logged out" #define LY_LANG_SHELL "shell" diff --git a/src/ncui.c b/src/ncui.c index 39c735f..e29db3e 100644 --- a/src/ncui.c +++ b/src/ncui.c @@ -160,10 +160,12 @@ void init_scene(struct ncwin* win, struct ncform* form) void init_draw(struct ncwin* win, struct ncform* form) { char line[LY_LIM_LINE_CONSOLE]; + char *greeting; /* frame */ box(win->win, 0, 0); /* initializes error output and prints greeting message */ - error_init(win->win, win->width, LY_LANG_GREETING); + hostname(&greeting); + error_init(win->win, win->width, greeting); /* prints shutdown & reboot hints */ snprintf(line, sizeof(line), "F1 %s F2 %s", LY_LANG_SHUTDOWN, LY_LANG_REBOOT); @@ -174,6 +176,7 @@ void init_draw(struct ncwin* win, struct ncform* form) post_form(form->form); /* dumps window buffer */ wrefresh(win->win); + free(greeting); } void end_form(struct ncform* form) diff --git a/src/utils.c b/src/utils.c index 1f6393f..6fea250 100644 --- a/src/utils.c +++ b/src/utils.c @@ -1,5 +1,6 @@ #define _XOPEN_SOURCE 500 - +#define _DEFAULT_SOURCE +#define _POSIX_C_SOURCE 200809L /* std lib */ #include #include @@ -14,6 +15,11 @@ #include #include #include +/* sockets */ +#include +#include +#include +#include void kernel_log(int mode) { @@ -58,6 +64,36 @@ char* trim(char* s) return s; } + + +void hostname(char** out) { + struct addrinfo hints; + struct addrinfo *info; + char hostname[1024]; + char* dot; + int result; + + hostname[1023] = '\0'; + gethostname(hostname, 1023); + memset(&hints, 0, sizeof hints); + hints.ai_family = AF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + hints.ai_flags = AI_CANONNAME; + result = getaddrinfo(hostname, "http", &hints, &info); + + if(result == 0 && info != NULL) + { + dot = strchr(info->ai_canonname, '.'); + *out = strndup(info->ai_canonname, dot - info->ai_canonname); + } + else + { + *out = strdup(""); + } + + freeaddrinfo(info); +} + void error_init(WINDOW* win, int width, const char* s) { static WINDOW* win_stack = NULL; diff --git a/src/utils.h b/src/utils.h index 857f4b3..c3d7e5e 100644 --- a/src/utils.h +++ b/src/utils.h @@ -7,6 +7,7 @@ void kernel_log(int mode); char* trim(char* s); char* strdup(const char* src); +void hostname(char** out); void error_init(WINDOW* win, int width, const char* s); void error_print(const char* s); chtype get_curses_char(int y, int x);