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) {