You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
1.8 KiB

3 years ago
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* built-in.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/06 09:02:57 by narnaud #+# #+# */
3 years ago
/* Updated: 2022/05/03 18:18:23 by narnaud ### ########.fr */
3 years ago
/* */
/* ************************************************************************** */
#include "minishell.h"
3 years ago
int ft_echo(char *argv[])
3 years ago
{
int no_nl;
no_nl = 0;
if (argv[1])
{
argv++;
if (ft_strncmp(*argv, "-n", 3) == 0 && argv++)
no_nl = 1;
while (*argv)
{
3 years ago
ft_putstr_fd(*argv, 1);
3 years ago
argv++;
if (*argv)
3 years ago
ft_putstr_fd(" ", 1);
3 years ago
}
}
if (!no_nl)
3 years ago
ft_putstr_fd("\n", 1);
3 years ago
return (0);
}
3 years ago
int ft_pwd(char **argv)
3 years ago
{
char *dir;
(void)argv;
dir = ft_calloc(PATHS_MAX_SIZE, sizeof(char));
getcwd(dir, PATHS_MAX_SIZE);
3 years ago
ft_putstr_fd(dir, 1);
ft_putstr_fd("\n", 1);
3 years ago
free(dir);
return (0);
}
3 years ago
int ft_cd(char **argv)
3 years ago
{
if (argv[1])
{
if (access(argv[1], X_OK))
{
printf("Minishell: %s: %s: Permission denied\n", argv[0], argv[1]);
return (0);
}
else if (access(argv[1], F_OK))
{
printf("Minishell: %s: %s: Not a directory\n", argv[0], argv[1]);
return (0);
}
chdir(argv[1]);
return (1);
}
else
{
chdir(getenv("HOME"));
}
return (0);
}
int close_minishell(int exit_code)
{
exit(exit_code);
}