🔨」 fix(Prompt): Added protection to alloc -> to-do = change return (NULL) to exit function and free all

This commit is contained in:
2024-05-17 14:30:23 +02:00
parent 12dd4c3faa
commit 984f602e82
3 changed files with 31 additions and 5 deletions

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/02 10:36:31 by adjoly #+# #+# */
/* Updated: 2024/05/15 16:29:19 by adjoly ### ########.fr */
/* Updated: 2024/05/17 13:53:43 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
@ -21,7 +21,11 @@ char *get_hostname(void)
char *delimiter;
buf = ft_calloc(254, sizeof(char));
if (!buf)
return (ft_strdup("nixos"));
host_file = open("/etc/hostname", O_RDONLY);
if (host_file <= -1)
return (ft_strdup("nixos"));
read(host_file, buf, 254);
delimiter = ft_strchr(buf, '.');
if (!delimiter)
@ -34,6 +38,8 @@ char *get_hostname(void)
return (hostname);
}
hostname = ft_calloc(delimiter - buf + 1, sizeof(char));
if (!hostname)
return (ft_strdup("nixos"));
ft_strlcpy(hostname, buf, delimiter - buf + 1);
free(buf);
return (hostname);

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/30 13:25:42 by adjoly #+# #+# */
/* Updated: 2024/05/15 16:21:25 by adjoly ### ########.fr */
/* Updated: 2024/05/17 14:25:57 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
@ -18,10 +18,22 @@ char *get_prompt(void)
char *prompt;
prompt = getenv("USER");
if (!prompt)
prompt = ft_strdup("nixos");
prompt = ft_strjoin(prompt, "@");
if (!prompt)
return (NULL);
prompt = ft_strjoin_free(prompt, get_hostname());
if (!prompt)
return (NULL);
prompt = ft_strjoin_free_s1(prompt, ":");
if (!prompt)
return (NULL);
prompt = ft_strjoin_free(prompt, get_pwd());
if (!prompt)
return (NULL);
prompt = ft_strjoin_free_s1(prompt, "$ ");
if (!prompt)
return (NULL);
return (prompt);
}

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/02 14:42:00 by adjoly #+# #+# */
/* Updated: 2024/05/02 15:49:42 by adjoly ### ########.fr */
/* Updated: 2024/05/17 14:25:28 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
@ -19,9 +19,17 @@ char *get_pwd(void)
char *home;
pwd = getenv("PWD");
if (!pwd)
return (NULL);
home = getenv("HOME");
if (!pwd)
return (NULL);
if (!ft_strncmp(pwd, home, ft_strlen(home)))
{
pwd += ft_strlen(home);
pwd = ft_strjoin("~", pwd);
return (pwd);
pwd = ft_strjoin("~", pwd);
if (!pwd)
return (NULL);
}
return (ft_strdup(pwd));
}