-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipex-utils.c
105 lines (90 loc) · 2.3 KB
/
pipex-utils.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* pipex-utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jleslee <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/12/05 21:19:17 by jleslee #+# #+# */
/* Updated: 2021/12/09 18:35:59 by jleslee ### ########.fr */
/* */
/* ************************************************************************** */
#include "pipex.h"
// Подсчитываем длинну строки до ch
int len_ch(char *str, char ch)
{
int i;
i = 0;
while (str[i] && str[i] != ch)
i++;
if (str[i] == ch)
return (i);
return (-1);
}
// Если строки до n одинаковы, возвращаем 0
int ncompare(char *str1, char *str2, int n)
{
while (*str1 && *str2 && *str1 == *str2 && --n > 0)
{
str1++;
str2++;
}
return (*str2 - *str1);
}
// соединяем путь и команду
char *make_command(char *path, char *bin)
{
int i;
int j;
char *buff;
buff = malloc(sizeof(char) * (len_ch(path, 0) + len_ch(bin, 0) + 2));
i = 0;
j = 0;
while (path[j])
buff[i++] = path[j++];
buff[i++] = '/';
j = 0;
while (bin[j])
buff[i++] = bin[j++];
buff[i] = 0;
return (buff);
}
// Копируем строку до n
char *str_ndup(char *str, unsigned int n)
{
unsigned int i;
char *buff;
i = 0;
buff = malloc(sizeof(char) * (n + 1));
while (i < n)
buff[i++] = *str++;
buff[n] = 0;
return (buff);
}
// Разбиваем строку
char **str_split(char *str, char sep)
{
char **tab;
int count;
int i;
int j;
count = 0;
j = 0;
while (str[j])
{
if (str[j++] == sep)
count++;
}
tab = malloc(sizeof(char *) * (count + 2));
tab[count + 1] = NULL;
i = 0;
while (i < count + 1)
{
j = 0;
while (str[j] && str[j] != sep)
j++;
tab[i++] = str_ndup(str, j);
str = str + j + 1;
}
return (tab);
}