From 390485dab3acbb5290651ba5c37624440089a993 Mon Sep 17 00:00:00 2001 From: adjoly Date: Tue, 21 May 2024 20:58:25 +0200 Subject: [PATCH] =?UTF-8?q?=E3=80=8C=F0=9F=94=A8=E3=80=8D=20fix(Issue=20:?= =?UTF-8?q?=20prompt/historique=20#3):=20Fixed=20and=20normed?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/prompt/get_hostname.c | 45 ++++++++++++++++++++++++++++----------- src/prompt/get_prompt.c | 4 ++-- 2 files changed, 35 insertions(+), 14 deletions(-) diff --git a/src/prompt/get_hostname.c b/src/prompt/get_hostname.c index 29ee2a3..e421691 100644 --- a/src/prompt/get_hostname.c +++ b/src/prompt/get_hostname.c @@ -6,40 +6,61 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/02 10:36:31 by adjoly #+# #+# */ -/* Updated: 2024/05/17 13:53:43 by adjoly ### ########.fr */ +/* Updated: 2024/05/21 20:55:19 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ #include "fcntl.h" #include "libft.h" -char *get_hostname(void) +char *get_fullhostname(char *buf) { char *hostname; - char *buf; - int host_file; char *delimiter; + hostname = ft_strdup(buf); + free(buf); + delimiter = ft_strchr(hostname, '\n'); + if (delimiter) + hostname[delimiter - hostname] = '\0'; + return (hostname); +} + +char *__get_host_str(void) +{ + int host_file; + char *buf; + buf = ft_calloc(254, sizeof(char)); if (!buf) return (ft_strdup("nixos")); host_file = open("/etc/hostname", O_RDONLY); if (host_file <= -1) + { + free(buf); return (ft_strdup("nixos")); + } read(host_file, buf, 254); + close(host_file); + return (buf); +} + +char *get_hostname(void) +{ + char *hostname; + char *buf; + char *delimiter; + + buf = __get_host_str(); delimiter = ft_strchr(buf, '.'); if (!delimiter) - { - hostname = ft_strdup(buf); - free(buf); - delimiter = ft_strchr(hostname, '\n'); - if (delimiter) - hostname[delimiter - hostname] = '\0'; - return (hostname); - } + return (get_fullhostname(buf)); hostname = ft_calloc(delimiter - buf + 1, sizeof(char)); if (!hostname) + { + free(buf); return (ft_strdup("nixos")); + } ft_strlcpy(hostname, buf, delimiter - buf + 1); free(buf); return (hostname); diff --git a/src/prompt/get_prompt.c b/src/prompt/get_prompt.c index 47ff350..8a07050 100644 --- a/src/prompt/get_prompt.c +++ b/src/prompt/get_prompt.c @@ -6,12 +6,12 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/30 13:25:42 by adjoly #+# #+# */ -/* Updated: 2024/05/17 14:25:57 by adjoly ### ########.fr */ +/* Updated: 2024/05/21 20:48:02 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" -#include "minishell.h" +#include "prompt.h" char *get_prompt(void) {