🔨」 fix(Issue : prompt/historique #3): Fixed and normed

This commit is contained in:
2024-05-21 20:58:25 +02:00
parent 0020764f51
commit 390485dab3
2 changed files with 35 additions and 14 deletions

View File

@ -6,40 +6,61 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/02 10:36:31 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 "fcntl.h"
#include "libft.h" #include "libft.h"
char *get_hostname(void) char *get_fullhostname(char *buf)
{ {
char *hostname; char *hostname;
char *buf;
int host_file;
char *delimiter; 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)); buf = ft_calloc(254, sizeof(char));
if (!buf) if (!buf)
return (ft_strdup("nixos")); return (ft_strdup("nixos"));
host_file = open("/etc/hostname", O_RDONLY); host_file = open("/etc/hostname", O_RDONLY);
if (host_file <= -1) if (host_file <= -1)
{
free(buf);
return (ft_strdup("nixos")); return (ft_strdup("nixos"));
}
read(host_file, buf, 254); 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, '.'); delimiter = ft_strchr(buf, '.');
if (!delimiter) if (!delimiter)
{ return (get_fullhostname(buf));
hostname = ft_strdup(buf);
free(buf);
delimiter = ft_strchr(hostname, '\n');
if (delimiter)
hostname[delimiter - hostname] = '\0';
return (hostname);
}
hostname = ft_calloc(delimiter - buf + 1, sizeof(char)); hostname = ft_calloc(delimiter - buf + 1, sizeof(char));
if (!hostname) if (!hostname)
{
free(buf);
return (ft_strdup("nixos")); return (ft_strdup("nixos"));
}
ft_strlcpy(hostname, buf, delimiter - buf + 1); ft_strlcpy(hostname, buf, delimiter - buf + 1);
free(buf); free(buf);
return (hostname); return (hostname);

View File

@ -6,12 +6,12 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/30 13:25:42 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 "libft.h"
#include "minishell.h" #include "prompt.h"
char *get_prompt(void) char *get_prompt(void)
{ {