-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprint.c
102 lines (93 loc) · 3.04 KB
/
print.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* print.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qdegraev <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/03/08 01:52:30 by qdegraev #+# #+# */
/* Updated: 2016/03/13 15:11:00 by qdegraev ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_ls.h"
void color(t_dircont *dc)
{
if (dc->type[0] == '-')
ft_printf("%-s", dc->name);
else if (dc->type[0] == 'd')
ft_printf("\033[36m%-s\033[0m", dc->name);
else if (dc->type[0] == 'l')
ft_printf("\033[35m%-s\033[0m", dc->name);
else if (dc->type[0] == 'p')
ft_printf("%-s", dc->name);
else if (dc->type[0] == 'b')
ft_printf("\033[7;34;36m%-s\033[0m", dc->name);
else if (dc->type[0] == 'l')
ft_printf("%-s", dc->name);
else if (dc->type[0] == 'c')
ft_printf("\033[27;34;43m%-s\033[0m", dc->name);
}
void print_size(t_dircont *dc, t_display *d)
{
if (dc->type[0] == 'c' || dc->type[0] == 'b')
ft_printf("%3d,%*d ", major(dc->stat.st_rdev), d->size_max > 4 ?
d->size_max : 4, minor(dc->stat.st_rdev));
else if (d->sys)
ft_printf("%*d ", d->size_max > 4 ? d->size_max + 4 : 8
, dc->stat.st_size);
else
ft_printf("%*d ", d->size_max, dc->stat.st_size);
}
void print_owner_group(t_dircont *dc, t_display *d)
{
struct group *gr;
struct passwd *pwd;
pwd = getpwuid(dc->stat.st_uid);
gr = getgrgid(dc->stat.st_gid);
if (!pwd || pwd->pw_name == NULL)
ft_printf("%*d ", d->owner_max, dc->stat.st_uid);
else
ft_printf("%-*s ", d->owner_max, pwd->pw_name);
if (!gr || pwd->pw_name == NULL)
ft_printf("%*d ", d->group_max, dc->stat.st_gid);
else
ft_printf("%-*s ", d->group_max, gr->gr_name);
}
void print_time(t_dircont *dc, t_display *d)
{
time_t t;
char *st;
t = !d->o->u ? (dc->stat.st_mtimespec).tv_sec :
(dc->stat.st_atimespec).tv_sec;
st = ctime(&t);
if (d->o->up_t)
ft_printf("%-.20s ", st + 4);
else if (time(NULL) - t >= 15778463 || t > time(NULL))
{
ft_printf("%-.7s ", st + 4);
ft_printf("%-.4s ", st + 20);
}
else
ft_printf("%-.12s ", st + 4);
}
void display_long(t_dircont *dc, t_display *d)
{
char *buff;
int i;
i = 0;
buff = (char*)malloc(255);
ft_printf("%-12s", dc->type);
ft_printf("%*d ", d->link_max, dc->stat.st_nlink);
print_owner_group(dc, d);
print_size(dc, d);
print_time(dc, d);
d->o->color ? color(dc) : ft_printf("%-s", dc->name);
if (dc->type[0] == 'l' && (i = readlink(dc->path, buff, 255)))
{
buff[i] = '\0';
ft_printf(" -> %s\n", buff);
}
else
ft_printf("\n");
ft_strdel(&buff);
}