1
0
mirror of https://github.com/KeyZox71/ft_minipowershell.git synced 2025-05-14 08:18:45 +02:00

」 feat: Prompt finished

This commit is contained in:
2024-05-02 15:50:58 +02:00
parent 272061eafc
commit bbadc3e663
71 changed files with 83 additions and 86 deletions

36
src/prompt/get_hostname.c Normal file
View File

@ -0,0 +1,36 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_hostname.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/02 10:36:31 by adjoly #+# #+# */
/* Updated: 2024/05/02 13:42:24 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "fcntl.h"
#include "libft.h"
char *get_hostname(void)
{
char *hostname;
char *buf;
int host_file;
char *delimiter;
buf = ft_calloc(254, sizeof(char));
host_file = open("/etc/hostname", O_RDONLY);
read(host_file, buf, 254);
delimiter = ft_strchr(buf, '.');
if (!delimiter)
{
free(buf);
return (buf);
}
hostname = ft_calloc(delimiter - buf, sizeof(char));
ft_strlcpy(hostname, buf, delimiter - buf + 1);
free(buf);
return (hostname);
}

27
src/prompt/get_prompt.c Normal file
View File

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_prompt.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/30 13:25:42 by adjoly #+# #+# */
/* Updated: 2024/05/02 15:50:14 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include "minishell.h"
char *get_prompt(void)
{
char *prompt;
prompt = getenv("USER");
prompt = ft_strjoin(prompt, "@");
prompt = ft_strjoin_free(prompt, get_hostname());
prompt = ft_strjoin_free_s1(prompt, ":");
prompt = ft_strjoin_free(prompt, get_pwd());
prompt = ft_strjoin_free_s1(prompt, "$ ");
return (prompt);
}

27
src/prompt/get_pwd.c Normal file
View File

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_pwd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* 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 */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "libft.h"
char *get_pwd(void)
{
char *pwd;
char *home;
pwd = getenv("PWD");
home = getenv("HOME");
if (!ft_strncmp(pwd, home, ft_strlen(home)))
pwd += ft_strlen(home);
pwd = ft_strjoin("~", pwd);
return (pwd);
}