-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_ls.c
102 lines (94 loc) · 2.34 KB
/
my_ls.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
/*
** my_ls.c for my_ls in /home/gwitrand/Projects/Mini-project/PSU_2016_my_ls
**
** Made by Witrand Gaspard
** Login <[email protected]>
**
** Started on Sun Dec 4 22:41:52 2016 Witrand Gaspard
** Last update Sun Dec 4 22:41:52 2016 Witrand Gaspard
*/
#include <stdio.h>
#include <stdlib.h>
#include "my_ls.h"
void get_max(t_max_size *max, t_file_info *info)
{
(info->size_nb_links > max->nb_links) ? (max->nb_links = info->size_nb_links)
: (0);
(info->size_user > max->user) ? (max->user = info->size_user) : (0);
(info->size_group > max->grp) ? (max->grp = info->size_group) : (0);
(info->size_file > max->size_file) ? (max->size_file = info->size_file)
: (0);
}
void error_output(char *file_or_dir, int error)
{
if (error == 1)
my_putstr_error("my_ls: cannot access ");
else if (error == 2)
my_putstr_error("my_ls: cannot open directory ");
else if (error == 3)
my_putstr_error("my_ls: cannot read symbolic link ");
my_putstr_error(file_or_dir);
my_putstr_error(": ");
perror("");
}
t_max_size *init_max(t_max_size *max)
{
if ((max = (t_max_size*)my_malloc(sizeof(*max))) == NULL)
return (NULL);
max->nb_links = 0;
max->user = 0;
max->grp = 0;
max->size_file = 0;
return (max);
}
char *str_cat(char *s1, char *s2)
{
char *dest;
int i;
int j;
i = 0;
j = 0;
while (s1[i])
i = i + 1;
while (s2[j])
j = j + 1;
if ((dest = (char*)my_malloc(sizeof(char) * (i + j + 2))) == NULL)
return (NULL);
i = 0;
j = 0;
while (s1[i])
dest[i] = s1[i++];
if (dest[i - 1] != '/')
dest[i++] = '/';
while (s2[j])
dest[i + j] = s2[j++];
dest[i + j] = 0;
return (dest);
}
int my_ls(t_flags *options, t_list_file *list_file)
{
t_list_file *tmp;
int test;
int is_first;
int nb_args;
is_first = 1;
if (list_file == NULL)
if (push_file(options, &list_file, ".", 0) == -1)
return (-1);
if (options->d == 1)
return (disp_the_directory(options, list_file));
if ((test = ls_files_in_args(options, list_file)) == -1)
return (-1);
tmp = list_file;
if (test > 0 && is_good_dir_in_arg(list_file) > 0)
my_putchar('\n');
nb_args = nb_arg(list_file);
while (tmp)
{
if ((test = ls_directory(options, tmp->name, is_first, nb_args)) == -2)
return (-1);
(test != -1) ? (is_first = is_first + 1) : (0);
tmp = tmp->next;
}
return (0);
}