100 lines
2.2 KiB
C
Raw Normal View History

2024-04-25 11:46:41 +02:00
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/24 11:18:04 by adjoly #+# #+# */
2024-04-30 12:36:16 +02:00
/* Updated: 2024/04/29 13:35:58 by adjoly ### ########.fr */
2024-04-25 11:46:41 +02:00
/* */
/* ************************************************************************** */
#include <readline/readline.h>
2024-04-30 12:36:16 +02:00
#include <readline/history.h>
2024-04-25 11:46:41 +02:00
#include <stdio.h>
2024-04-27 17:39:26 +02:00
#include <stdlib.h>
2024-04-30 12:36:16 +02:00
#include <stdbool.h>
#include <fcntl.h>
#include "minishell.h"
2024-04-25 11:46:41 +02:00
#include "libft.h"
2024-04-30 12:36:16 +02:00
bool is_str(char *src, char *dst)
2024-04-25 11:46:41 +02:00
{
2024-04-27 17:39:26 +02:00
while (*src && *dst && *src == *dst)
{
src++;
dst++;
}
2024-04-30 12:36:16 +02:00
if (*dst)
return (false);
return (true);
2024-04-25 11:46:41 +02:00
}
2024-04-30 12:36:16 +02:00
char *get_hostname(void)
2024-04-27 17:39:26 +02:00
{
2024-04-30 12:36:16 +02:00
char *hostname;
char *tmp;
int host_file;
host_file = open();
read();
tmp = hostname;
while (*tmp)
tmp++;
return
}
char *get_prompt(void)
{
char **prompt;
char *ret;
2024-04-27 17:39:26 +02:00
char *home;
2024-04-30 12:36:16 +02:00
char **tmp;
2024-04-27 17:39:26 +02:00
2024-04-30 12:36:16 +02:00
prompt = malloc(1000);
prompt[0] = getenv("USER");
prompt[1] = "@";
prompt[2] = get_hostname();
2024-04-27 17:39:26 +02:00
home = getenv("HOME");
2024-04-30 12:36:16 +02:00
prompt[3] = getenv("PWD");
prompt[4] = ">";
ret = ft_calloc(1000, sizeof(char));
2024-04-27 17:39:26 +02:00
2024-04-30 12:36:16 +02:00
if (!ft_strncmp(prompt[3], home, ft_strlen(home)))
prompt[3] += ft_strlen(home);
tmp = prompt;
while (*tmp)
2024-04-27 17:39:26 +02:00
{
2024-04-30 12:36:16 +02:00
ft_strlcat(ret, *tmp, ft_strlen(ret) + ft_strlen(*tmp) + 1);
tmp++;
2024-04-27 17:39:26 +02:00
}
2024-04-30 12:36:16 +02:00
free(prompt);
return (ret);
2024-04-27 17:39:26 +02:00
}
2024-04-25 11:46:41 +02:00
int main(int ac, char **av, char **env)
{
char *test;
char **lll;
2024-04-30 12:36:16 +02:00
char *ret;
2024-04-25 11:46:41 +02:00
(void)ac;
(void)av;
(void)env;
while (1)
{
2024-04-30 12:36:16 +02:00
ret = get_prompt();
test = readline(ret);
free(ret);
add_history(test);
2024-04-25 11:46:41 +02:00
lll = ft_split(test, ' ');
2024-04-27 17:39:26 +02:00
if (!*lll)
continue;
if (is_str(test, "exit"))
break;
2024-04-25 11:46:41 +02:00
}
2024-04-27 17:39:26 +02:00
ft_freearr((void **)lll);
2024-04-25 11:46:41 +02:00
return (0);
}