-
Notifications
You must be signed in to change notification settings - Fork 0
/
printing.c
132 lines (122 loc) · 3.4 KB
/
printing.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <pwd.h>
#include <grp.h>
#include <sys/types.h>
#include <dirent.h>
#include "my_ls.h"
char *print_year(time_t *time_arg)
{
char *times;
int i;
i = 0;
times = ctime(time_arg);
while (times[i] != ' ')
i = i + 1;
i = i + 1;
times = times + i;
i = 0;
while (times[i++] != ' ');
i = i + 1;
while (times[i++] != ' ');
times[i] = ' ';
i = i + 1;
while (times[i + 8] >= '0' && times[i + 8] <= '9')
{
times[i] = times[i + 8];
i = i + 1;
}
times[i] = 0;
return (times);
}
int print_device(t_max_size *max, struct stat *res_stat,
char *file_name, int g)
{
struct passwd *info_uid;
struct group *info_grp;
info_uid = getpwuid(res_stat->st_uid);
info_grp = getgrgid(res_stat->st_gid);
if (g == 0)
{
my_printf(" %*d %-*s %-*s %4d, %3d %s %s",
max->nb_links, res_stat->st_nlink, max->user, info_uid->pw_name,
max->grp, info_grp->gr_name, major(res_stat->st_rdev),
minor(res_stat->st_rdev), cut_time(&res_stat->st_mtime),
cut_name(file_name));
}
else
{
my_printf(" %*d %-*s %4d, %3d %s %s",
max->nb_links, res_stat->st_nlink, max->grp, info_grp->gr_name,
major(res_stat->st_rdev), minor(res_stat->st_rdev),
cut_time(&res_stat->st_mtime), cut_name(file_name));
}
return (0);
}
int print_file(t_max_size *max, struct stat *res_stat,
char *file_name, int g)
{
struct passwd *info_uid;
struct group *info_grp;
info_uid = getpwuid(res_stat->st_uid);
info_grp = getgrgid(res_stat->st_gid);
if (g == 0)
{
my_printf(" %*d %-*s %-*s %*llu %s %s",
max->nb_links, res_stat->st_nlink, max->user, info_uid->pw_name,
max->grp, info_grp->gr_name, max->size_file, res_stat->st_size,
cut_time(&res_stat->st_mtime), cut_name(file_name));
}
else
{
my_printf(" %*d %-*s %*llu %s %s",
max->nb_links, res_stat->st_nlink,
max->grp, info_grp->gr_name, max->size_file, res_stat->st_size,
cut_time(&res_stat->st_mtime), cut_name(file_name));
}
return (0);
}
int print_directories(t_max_size *max, struct stat *res_stat,
char *file_name, int g)
{
struct passwd *info_uid;
struct group *info_grp;
info_uid = getpwuid(res_stat->st_uid);
info_grp = getgrgid(res_stat->st_gid);
print_permissions(res_stat->st_mode);
if (g == 0)
{
my_printf(" %*d %-*s %-*s %*llu %s %s",
max->nb_links, res_stat->st_nlink, max->user, info_uid->pw_name,
max->grp, info_grp->gr_name, max->size_file, res_stat->st_size,
cut_time(&res_stat->st_mtime), file_name);
}
else
{
my_printf(" %*d %-*s %*llu %s %s",
max->nb_links, res_stat->st_nlink,
max->grp, info_grp->gr_name, max->size_file, res_stat->st_size,
cut_time(&res_stat->st_mtime), file_name);
}
return (0);
}
unsigned int print_block_allocated(char *dir_name)
{
unsigned int block_allocated;
struct stat res_stat;
struct dirent *read_dir;
char *name_with_path;
DIR *dir;
if ((dir = opendir(dir_name)) == NULL)
return (-1);
block_allocated = 0;
while ((read_dir = readdir(dir)))
{
if ((name_with_path = str_cat(dir_name, read_dir->d_name)) == NULL)
return (-1);
if ((lstat(name_with_path, &res_stat)) == 0)
if (read_dir->d_name[0] != '.')
block_allocated += res_stat.st_blocks;
}
closedir(dir);
my_printf("total %lld\n", block_allocated / 2);
return (0);
}