-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_export.c
100 lines (91 loc) · 2.18 KB
/
utils_export.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils_export.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: brenaudo <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/04 16:26:02 by brenaudo #+# #+# */
/* Updated: 2023/01/05 12:27:11 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int str_cmp_sort(char *s1, char *s2)
{
while (*s1 == *s2 && *s1 && *s2)
{
s1++;
s2++;
}
if (*s1 == *s2)
return (0);
else if (*s1 < *s2)
return (-1);
else
return (1);
}
void sort_ascii(char **envp)
{
int i;
int j;
char *tmp;
i = 0;
while (envp[i])
{
j = 0;
while (envp[j + 1])
{
if (str_cmp_sort(envp[j], envp[j + 1]) == 1)
{
tmp = envp[j];
envp[j] = envp[j + 1];
envp[j + 1] = tmp;
}
j++;
}
i++;
}
}
char **cut_on_first(char *str, char sep)
{
int i;
char **ret;
ret = NULL;
while (ret == NULL)
ret = ft_calloc(3, sizeof(char *));
ret[2] = NULL;
i = 0;
while (str && str[i] && str[i] != sep)
i++;
ret[0] = ft_substr(str, 0, i);
ret[1] = ft_substr(str, i, ft_strlen(str) - i);
return (ret);
}
char *longest_str(char *s1, char *s2)
{
int i;
int j;
i = 0;
j = 0;
while (s1 && s1[i])
i++;
while (s2 && s2[j])
j++;
if (i > j)
return (s1);
else
return (s2);
}
void print_export_line(char **envp_entry_split, int fd_out)
{
ft_putstr_fd("declare -x ", fd_out);
ft_putstr_fd(envp_entry_split[0], fd_out);
if (envp_entry_split[1][0])
{
ft_putchar_fd(envp_entry_split[1][0], fd_out);
ft_putchar_fd('\"', fd_out);
ft_putstr_fd(envp_entry_split[1] + 1, fd_out);
ft_putchar_fd('\"', fd_out);
}
ft_putchar_fd('\n', fd_out);
}