118 lines
2.5 KiB
C
118 lines
2.5 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* pipex_utils.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2023/01/19 17:02:51 by erey-bet #+# #+# */
|
|
/* Updated: 2023/01/20 17:48:55 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, "zsh: no such file or directory: ", 32);
|
|
write(2, error, ft_strlen(error));
|
|
}
|
|
if (type == 1)
|
|
{
|
|
write(2, "zsh: command not found: ", 24);
|
|
if (get_next(error, ' ') == -1)
|
|
write(2, error, ft_strlen(error));
|
|
else
|
|
write(2, error, ft_strlen(error)
|
|
- (ft_strlen(error) - get_next(error, ' ')));
|
|
}
|
|
write(2, "\n", 1);
|
|
return (1);
|
|
}
|
|
|
|
char **get_flags(char *cmd)
|
|
{
|
|
int i;
|
|
int y;
|
|
char *new_str;
|
|
char **flags;
|
|
|
|
i = get_next(cmd, ' ') + 1;
|
|
if (i == 0)
|
|
{
|
|
flags = ft_calloc(2, sizeof(char *));
|
|
return (flags);
|
|
}
|
|
new_str = ft_calloc(ft_strlen(cmd) - i + 1, 1);
|
|
if (new_str == NULL)
|
|
return (NULL);
|
|
y = 0;
|
|
while (cmd[i])
|
|
new_str[y++] = cmd[i++];
|
|
new_str[y] = '\0';
|
|
flags = ft_split(new_str, ' ');
|
|
i = 0;
|
|
free(new_str);
|
|
return (flags);
|
|
}
|
|
|
|
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';
|
|
return (new_str);
|
|
}
|
|
|
|
char **add_cmd(char **flg, char *cmd)
|
|
{
|
|
char *tmp;
|
|
int i;
|
|
int check;
|
|
|
|
tmp = NULL;
|
|
i = 0;
|
|
check = 0;
|
|
while (check == 0)
|
|
{
|
|
if (flg[i] == NULL)
|
|
check = 1;
|
|
if (tmp != NULL)
|
|
flg[i] = tmp;
|
|
tmp = flg[i];
|
|
i++;
|
|
}
|
|
flg[0] = ft_strdup(cmd);
|
|
if (flg[0] == NULL)
|
|
return (NULL);
|
|
flg[i] = NULL;
|
|
return (flg);
|
|
}
|