welcome - welcome - a terminal program which always greets you

git clone git://git.bcharge.de/welcome.git

About | Log | Files | Refs | License

commit 899d7115ca91dd98ba24a92d2e8764dbb0be1408
parent 2c7eec83c6d91083326d161be2f054ba3140f10e
Author: Bakar Chargeishvili <bakar@bcharge.de>
Date:   Wed, 18 Dec 2024 17:44:55 +0100

Extract the first name from passwd entry

Diffstat:
Mwelcome.c | 34+++++++++++++++++++++++++++++++++-
1 file changed, 33 insertions(+), 1 deletion(-)

diff --git a/welcome.c b/welcome.c @@ -6,6 +6,8 @@ #include <stdio.h> #include <curl/curl.h> #include <locale.h> +#include <pwd.h> +#include <unistd.h> #define MAXPIPE 1024 #define EVBOX_W 50 @@ -116,10 +118,40 @@ int syspipe(char* input, char* output, size_t* max_width) return 0; } +char* get_first_name() +{ + // Try to get a first name from passwd entry + struct passwd* pw = getpwuid(getuid()); + if (pw != NULL && pw->pw_gecos != NULL && strlen(pw->pw_gecos) > 0) { + char* name_copy = strdup(pw->pw_gecos); + char* first_part = strtok(name_copy, ","); + + if (first_part != NULL) { + char* first_name = strtok(first_part, " "); + if (first_name != NULL) { + return strdup(first_name); + } + free(name_copy); + } + free(name_copy); + } + + // If it fails, return username + if (pw != NULL) { + return strdup(pw->pw_name); + } + + // Last resort + return strdup("user"); +} + int print_greet(WINDOW* frame) { char buf[500] = {0}; - const char *cmd = "figlet Welcome Bakar!"; + char* name = get_first_name(); + char command[100]; + sprintf(command, "figlet Welcome %s!", name); + const char *cmd = command; FILE *fp;