pipex/mandatory/pipex_utils.c
Etienne Rey-bethbeder 6f5907bf3b 666
2023-03-21 16:13:08 +01:00

68 lines
1.7 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* pipex_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/19 17:02:51 by erey-bet #+# #+# */
/* Updated: 2023/03/21 15:43:42 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
#include "pipex.h"
int get_next(char *cmd, char c)
{
int i;
i = 0;
while (cmd[i] != c)
{
if (!cmd[i])
return (-1);
i++;
}
return (i);
}
int print_error(int type, char *error)
{
if (type == 0)
{
write(2, "pipex: no such file or directory: ", 35);
write(2, error, ft_strlen(error));
}
else
{
write(2, "pipex: command not found: ", 26);
ft_putstr_fd(error, 2);
}
write(2, "\n", 1);
return (1);
}
char *get_command(char *cmd)
{
int i;
char *new_str;
i = get_next(cmd, ' ');
if (i != -1)
new_str = ft_calloc(get_next(cmd, ' ') + 1, 1);
else
new_str = ft_calloc(ft_strlen(cmd) + 1, 1);
if (new_str == NULL)
return (NULL);
i = -1;
while (cmd[++i] != ' ' && cmd[i])
new_str[i] = cmd[i];
new_str[i] = '\0';
if (ft_strlen(new_str) == 0)
{
free(new_str);
return (NULL);
}
return (new_str);
}