-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolumns.c
More file actions
117 lines (106 loc) · 2.86 KB
/
Copy pathcolumns.c
File metadata and controls
117 lines (106 loc) · 2.86 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* columns.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: asarandi <asarandi@student.42.us.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/12/04 05:22:23 by asarandi #+# #+# */
/* Updated: 2017/12/05 02:26:42 by asarandi ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_ls.h"
long list_length(t_file *list)
{
long i;
i = 0;
while (list)
{
i++;
list = list->next;
}
return (i);
}
void across_output(int num_col, t_file *list)
{
uintmax_t i;
intmax_t len;
i = 0;
while (list)
{
ft_printf(1, "%s", list->name);
print_entry_symbol(list->st.st_mode);
if (list->next == NULL)
break ;
if ((i + 1) % num_col == 0)
ft_printf(1, "\n");
else
{
len = ft_strlen(list->name);
print_tabs(len + has_symbol(list->st.st_mode));
}
list = list->next;
i++;
}
write(1, "\n", 1);
}
void column_output(int total, int nth, int num_col, t_file *list)
{
int j;
t_file *next;
t_file *last;
int row_max;
last = get_nth_element(list, nth);
while (list)
{
next = list;
row_max = get_row_end(list, num_col, nth);
j = 0;
while (next)
{
print_column_item(next);
if ((--total <= 0) || (++j >= row_max))
break ;
print_tabs(ft_strlen(next->name) + has_symbol(next->st.st_mode));
next = get_nth_element(next, nth);
}
ft_printf(1, "\n");
list = list->next;
if (list == last)
break ;
}
}
void print_columns(t_file *list)
{
struct winsize ws;
int num_col;
int tabs_longest_name;
int nth;
int len;
if ((isatty(1) == 0) || (ioctl(0, TIOCGWINSZ, &ws) == -1))
return (print_basic(list));
get_directory_widths(list);
tabs_longest_name = (g_width.name / TAB_SIZE) + 1;
num_col = ((ws.ws_col / TAB_SIZE)) / tabs_longest_name;
len = list_length(list);
if (len <= num_col)
return (print_across(list));
nth = len / num_col;
while (nth * num_col < len)
nth += 1;
column_output(len, nth, num_col, list);
}
void print_across(t_file *list)
{
struct winsize ws;
int num_col;
int tabs_longest_name;
if (isatty(1) == 0)
return (print_basic(list));
if ((num_col = ioctl(0, TIOCGWINSZ, &ws)) == -1)
return (print_basic(list));
get_directory_widths(list);
tabs_longest_name = (g_width.name / TAB_SIZE) + 1;
num_col = (ws.ws_col / TAB_SIZE) / tabs_longest_name;
across_output(num_col, list);
}