-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_utils.c
98 lines (88 loc) · 2.21 KB
/
ft_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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaraval <tmaraval@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/12/13 13:18:27 by tmaraval #+# #+# */
/* Updated: 2018/01/05 10:18:22 by tmaraval ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_ls.h"
void ft_print_usage(int i, int j, char **arv)
{
ft_putstr_fd(FT_LS, 2);
ft_putstr_fd("illegal option ", 2);
ft_putstr_fd("-- ", 2);
ft_putchar_fd(arv[i][j], 2);
ft_putendl_fd("", 2);
ft_putendl_fd("usage: ft_ls [-artlR1g] [file ...]", 2);
}
void ft_ls_perr(char *path)
{
char *str;
if ((str = (char *)malloc(sizeof(char) *
(ft_strlen(FT_LS) + ft_strlen(path) + 5))) == NULL)
{
ft_putendl("some malloc failed");
exit(EXIT_FAILURE);
}
ft_bzero(str, ft_strlen(FT_LS) + ft_strlen(path) + 5);
ft_strcat(str, FT_LS);
if (ft_strlen(path) == 0)
ft_strcat(str, "open");
else
ft_strcat(str, path);
perror(str);
free(str);
if (ft_strlen(path) == 0)
exit(EXIT_FAILURE);
}
void ft_sort_optdir(char **dir, int dirnbr)
{
int swap;
int i;
char *temp;
swap = TRUE;
while (swap == TRUE)
{
swap = FALSE;
i = 0;
while (i < dirnbr - 1)
{
if (ft_strcmp(dir[i], dir[i + 1]) > 0)
{
temp = dir[i];
dir[i] = dir[i + 1];
dir[i + 1] = temp;
swap = TRUE;
}
i++;
}
}
}
int ft_get_dirnbr(char **dir)
{
int i;
i = 0;
while (dir[i])
{
i++;
}
return (i);
}
void ft_delete_in_tab(char **dir, int index)
{
int dirnbr;
char *temp;
dirnbr = ft_get_dirnbr(dir);
temp = dir[index];
while (index + 1 < dirnbr)
{
dir[index] = dir[index + 1];
index++;
}
dir[index] = 0;
free(temp);
}