functions under 25 lines

This commit is contained in:
Jack Halford 2016-09-25 22:32:26 +02:00
parent c54a9928f4
commit 02b96bca41
6 changed files with 61 additions and 73 deletions

View file

@ -6,11 +6,14 @@ ft_ent_print src/lib_dirents.c /^void ft_ent_print(t_list *ent, char *opts, t_ls
ft_ent_sort src/lib_dirents.c /^void ft_ent_sort(t_list **ent, char *opts)$/
ft_error_dir src/lib_error.c /^void ft_error_dir(char *s)$/
ft_error_option src/lib_error.c /^void ft_error_option(char c)$/
ft_ls_dirs src/ft_ls.c /^void ft_ls_dirs(t_list *dir, char *opts)$/
ft_ls_files src/ft_ls.c /^void ft_ls_files(t_list *ent, t_list *dir, char *o/
ft_ls_long src/ft_ls_long.c /^int ft_ls_long(t_list *ent, t_lsdata *topdir)$/
ft_ls_long_date src/lib_ls_long.c /^void ft_ls_long_date(struct stat *stat)$/
ft_ls_long_lnk src/lib_ls_long2.c /^int ft_ls_long_lnk(t_lsdata *data)$/
ft_ls_long_middle src/lib_ls_long.c /^int ft_ls_long_middle(struct stat *stat, t_pads */
ft_ls_long_pads src/lib_ls_long2.c /^int ft_ls_long_pads(t_list *ent, t_pads *pads)$/
ft_ls_long_print src/ft_ls_long.c /^int ft_ls_long_print(t_list *ent, t_pads pads)$/
ft_ls_long_rights src/lib_ls_long2.c /^void ft_ls_long_rights(int st_mode)$/
ft_ls_long_total src/lib_ls_long2.c /^void ft_ls_long_total(t_list *ent)$/
ft_ls_long_type src/lib_ls_long.c /^void ft_ls_long_type(mode_t m)$/
@ -20,5 +23,6 @@ ft_lsdata_cmp0 src/lib_lsdata.c /^int ft_lsdata_cmp0(t_lsdata *dat, char *datar
ft_lsdata_cmp_name src/lib_lsdata.c /^int ft_lsdata_cmp_name(t_lsdata *dat1, t_lsdata */
ft_lsdata_cmp_time src/lib_lsdata.c /^int ft_lsdata_cmp_time(t_lsdata *dat1, t_lsdata */
ft_lsdata_filename src/lib_lsdata.c /^void ft_lsdata_filename(t_lsdata *data)$/
ft_parse_ls src/lib_parse.c /^void ft_parse_ls(int ac, char **av, t_list **dir, /
ft_parse_ls src/lib_parse.c /^char *ft_parse_ls(int ac, char **av, t_list **dir,/
ft_parse_ls_files src/lib_parse.c /^void ft_parse_ls_files(int ac, char **av, t_list */
ft_parse_ls_options src/lib_parse.c /^int ft_parse_ls_options(int ac, char **av, char */

View file

@ -27,8 +27,12 @@ typedef struct s_pads
int size;
} t_pads;
void ft_parse_ls(int ac, char **av, t_list **dir, t_list **ent, char *opts);
void ft_ls_dirs(t_list *dir, char *opts);
void ft_ls_files(t_list *ent, t_list *dir, char *opts);
char *ft_parse_ls(int ac, char **av, t_list **dir, t_list **ent);
int ft_parse_ls_options(int ac, char **av, char *opts);
void ft_parse_ls_files(int ac, char **av, t_list **dir, t_list **ent);
void ft_lsdata_filename(t_lsdata *data);
int ft_lsdata_cmp_name(t_lsdata *dat1, t_lsdata *dat2);
@ -43,7 +47,9 @@ t_list *ft_ent_get_dirs(t_list *ent);
t_list *ft_dir_get_ents(t_lsdata *dir);
void ft_ls_short(t_list *ent);
int ft_ls_long(t_list *ent, t_lsdata *topdir);
int ft_ls_long_print(t_list *ent, t_pads pads);
void ft_ls_long_total(t_list *ent);
int ft_ls_long_pads(t_list *ent, t_pads *pads);

View file

@ -2,9 +2,6 @@
int ft_ls_long(t_list *ent, t_lsdata *topdir)
{
t_lsdata *data;
struct stat stat;
struct dirent *dirent;
t_pads pads = {0, 0, 0, 0};
if (topdir)
@ -13,22 +10,33 @@ int ft_ls_long(t_list *ent, t_lsdata *topdir)
return (1);
while (ent)
{
data = ent->content;
stat = data->stat;
dirent = data->dirent;
if (ft_ls_long_print(ent, pads))
return (1);
ent = ent->next;
ft_ls_long_type(stat.st_mode);
ft_ls_long_rights(stat.st_mode);
if (ft_ls_long_xattr(data->path))
return (1);
if (ft_ls_long_middle(&stat, &pads))
return (1);
ft_ls_long_date(&stat);
ft_printf(" ");
ft_lsdata_filename(data);
if (ft_ls_long_lnk(data))
return (1);
}
return (0);
}
int ft_ls_long_print(t_list *ent, t_pads pads)
{
t_lsdata *data;
struct stat stat;
struct dirent *dirent;
data = ent->content;
stat = data->stat;
dirent = data->dirent;
ent = ent->next;
ft_ls_long_type(stat.st_mode);
ft_ls_long_rights(stat.st_mode);
if (ft_ls_long_xattr(data->path))
return (1);
if (ft_ls_long_middle(&stat, &pads))
return (1);
ft_ls_long_date(&stat);
ft_printf(" ");
ft_lsdata_filename(data);
if (ft_ls_long_lnk(data))
return (1);
return (0);
}

View file

@ -48,7 +48,6 @@ t_list *ft_ent_get_dirs(t_list *ent)
t_list *dir;
t_list *tmp;
/* ft_printf("checking dir: %s\n", topdir->path); */
dir = NULL;
while (ent)
{
@ -56,19 +55,12 @@ t_list *ft_ent_get_dirs(t_list *ent)
dirent = data->dirent;
tmp = ent;
ent = ent->next;
/* ft_printf("checking: %s\n", data->path); */
if (dirent->d_type == DT_DIR
&& ft_strcmp(dirent->d_name, ".")
&& ft_strcmp(dirent->d_name, ".."))
{
/* data->path = ft_strjoin(topdir->path, "/"); */
/* data->path = ft_strjoin(data->path, dirent->d_name); */
/* stat(data->path, &data->stat); */
/* ft_printf("found dir: %s\n", data->path); */
ft_lsteadd(&dir, tmp);
}
/* else */
/* ft_printf("%s is not a dir\n", data->path); */
}
return (dir);
}

View file

@ -1,38 +1,42 @@
#include "ftls.h"
void ft_parse_ls(int ac, char **av, t_list **dir, t_list **ent, char *opts)
char *ft_parse_ls(int ac, char **av, t_list **dir, t_list **ent)
{
int i;
char *opts;
t_lsdata data;
DIR *stream;
ft_bzero(opts, 7);
data.dirent = NULL;
if (!(opts = (char *)ft_strnew(sizeof(char) * 7)))
return (NULL);
i = ft_parse_ls_options(ac, av, opts);
/* ft_strlsort(av + i, ac - i, &ft_strcmp); */
if (ac - i <= 1)
ft_strcat(opts, "0");
/* ft_printf("options: %s\n", opts); */
/* ft_printf("%i, %i\n", i, ac); */
ft_strcat(opts, (ac - i <= 1) ? "0" : "");
if (i == ac)
{
data.path = ft_strdup(".");
/* ft_printf("stat ret: %i\n", stat(data.path, &data.stat)); */
data.dirent = NULL;
ft_lstadd(dir, ft_lstnew(&data, sizeof(data)));
}
ft_parse_ls_files(ac - i, av + i, dir, ent);
return (opts);
}
void ft_parse_ls_files(int ac, char **av, t_list **dir, t_list **ent)
{
DIR *stream;
t_lsdata data;
int i;
data.dirent = NULL;
i = 0;
while (i < ac)
{
data.path = ft_strdup(av[i]);
if (lstat(data.path, &data.stat) < 0)
ft_error_dir(data.path);
else if (!(stream = opendir(data.path)))
{
/* ft_printf("found file: %s\n", data.path); */
ft_lstadd(ent, ft_lstnew(&data, sizeof(data)));
}
else
{
/* ft_printf("found dir: %s\n", data.path); */
ft_lstadd(dir, ft_lstnew(&data, sizeof(data)));
closedir(stream);
}

View file

@ -1,42 +1,16 @@
#include "ftls.h"
#include "ftls.h"
int main(int ac, char **av)
{
t_list *dir;
t_list *dir_r;
t_list *ent;
t_lsdata *dirdata;
char *opts;
dir = NULL;
ent = NULL;
opts = (char *)malloc(sizeof(char) * 7);
ft_parse_ls(ac, av, &dir, &ent, opts);
if (ent)
{
ft_ent_filter(&ent, opts);
ft_ent_sort(&ent, opts);
ft_ent_print(ent, opts, NULL, dir);
}
ft_ent_sort(&dir, opts);
while (dir)
{
dirdata = dir->content;
dir = dir->next;
/* ft_lstfree(ent); */
ent = ft_dir_get_ents(dirdata);
ft_ent_filter(&ent, opts);
ft_ent_sort(&ent, opts);
ft_ent_print(ent, opts, dirdata, dir);
if (ft_strchr(opts, 'R'))
{
dir_r = ft_ent_get_dirs(ent);
ft_lst_merge(&dir_r, dir);
dir = dir_r;
}
/* if (dir) */
/* ft_printf("\n"); */
/* ft_lsdata_print(dir); */
}
opts = ft_parse_ls(ac, av, &dir, &ent);
ft_ls_files(ent, dir, opts);
ft_ls_dirs(dir, opts);
return (errno);
}