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.

86 lines
2.1 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/07 23:17:15 by narnaud@stude ### ########.fr */
3 years ago
/* */
/* ************************************************************************** */
#include "minishell.h"
int ft_echo(t_command *cmd)
3 years ago
{
int no_nl;
int fd_out;
3 years ago
int i;
if (cmd->fd[1])
fd_out = cmd->fd[1];
3 years ago
else
fd_out = 1;
3 years ago
no_nl = 0;
3 years ago
i = 1;
if (cmd->argv[1])
3 years ago
{
3 years ago
if (ft_strncmp(cmd->argv[1], "-n", 3) == 0 && i++)
3 years ago
no_nl = 1;
3 years ago
while (cmd->argv[i])
3 years ago
{
3 years ago
ft_putstr_fd(cmd->argv[i], fd_out);
if (cmd->argv[++i])
ft_putstr_fd(" ", fd_out);
3 years ago
}
}
if (!no_nl)
ft_putstr_fd("\n", fd_out);
3 years ago
return (0);
}
int ft_pwd(t_command *cmd)
3 years ago
{
char *dir;
3 years ago
int fd_out;
3 years ago
if (cmd->fd[1])
fd_out = cmd->fd[1];
else
fd_out = 1;
3 years ago
dir = ft_calloc(PATHS_MAX_SIZE, sizeof(char));
getcwd(dir, PATHS_MAX_SIZE);
ft_putstr_fd(dir, fd_out);
ft_putstr_fd("\n", fd_out);
3 years ago
free(dir);
return (0);
}
int ft_cd(t_command *cmd)
3 years ago
{
if (cmd->argv[1])
3 years ago
{
if (access(cmd->argv[1], X_OK))
3 years ago
return (file_error(cmd->argv[0], cmd->argv[1], \
"Permission denied\n"));
else if (access(cmd->argv[1], F_OK))
3 years ago
return (file_error(cmd->argv[0], cmd->argv[1], \
"Not a directory\n"));
chdir(cmd->argv[1]);
3 years ago
return (1);
}
else
chdir(getenv("HOME"));
return (0);
}
3 years ago
int ft_exit(t_datas *datas, t_command *cmd)
3 years ago
{
3 years ago
if (cmd->argc > 1)
halt(datas, EXIT_SUCCESS);
else
halt(datas, ft_atoi(cmd->argv[1]));
return (1);
3 years ago
}