norm fix, leaks next

This commit is contained in:
Jack Halford 2016-11-07 17:54:30 +01:00
parent f132551431
commit f108baab24
13 changed files with 180 additions and 44 deletions

View file

@ -1,5 +1,17 @@
#ifndef FTLS_H /* ************************************************************************** */
# define FTLS_H /* */
/* ::: :::::::: */
/* ftls.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/07 15:10:03 by jhalford #+# #+# */
/* Updated: 2016/11/07 17:38:28 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FT_LS_H
# define FT_LS_H
# define ALL_OPTS "lRart" # define ALL_OPTS "lRart"
# include "libft.h" # include "libft.h"
# include <errno.h> # include <errno.h>
@ -12,20 +24,23 @@
# include <grp.h> # include <grp.h>
# include <sys/xattr.h> # include <sys/xattr.h>
typedef struct s_lsdata struct s_lsdata
{ {
struct stat stat; struct stat stat;
char *path; char *path;
struct dirent *dirent; struct dirent *dirent;
} t_lsdata; };
typedef struct s_pads struct s_pads
{ {
int nlink; int nlink;
int name; int name;
int gr_name; int gr_name;
int size; int size;
} t_pads; };
typedef struct s_lsdata t_lsdata;
typedef struct s_pads t_pads;
void ft_ls_dirs(t_list *dir, char *opts); void ft_ls_dirs(t_list *dir, char *opts);
void ft_ls_files(t_list *ent, t_list *dir, char *opts); void ft_ls_files(t_list *ent, t_list *dir, char *opts);
@ -41,7 +56,8 @@ int ft_lsdata_cmp0(t_lsdata *dat1, char *dataref);
void ft_ent_filter(t_list **ent, char *opts); void ft_ent_filter(t_list **ent, char *opts);
void ft_ent_sort(t_list **ent, char *opts); void ft_ent_sort(t_list **ent, char *opts);
void ft_ent_print(t_list *ent, char *opts, t_lsdata *topdir, t_list *nextdir); void ft_ent_print(
t_list *ent, char *opts, t_lsdata *topdir, t_list *nextdir);
t_list *ft_ent_get_dirs(t_list *ent); t_list *ft_ent_get_dirs(t_list *ent);
t_list *ft_dir_get_ents(t_lsdata *dir); t_list *ft_dir_get_ents(t_lsdata *dir);
@ -60,7 +76,6 @@ int ft_ls_long_middle(struct stat *stat, t_pads *pads);
void ft_ls_long_date(struct stat *stat); void ft_ls_long_date(struct stat *stat);
int ft_ls_long_lnk(t_lsdata *data); int ft_ls_long_lnk(t_lsdata *data);
void ft_error_option(char c); void ft_error_option(char c);
void ft_error_dir(char *s); void ft_error_dir(char *s);

@ -1 +1 @@
Subproject commit 05fe22bdf1552453d437f04fcc6b67a791cf749d Subproject commit 34d66b7d370ecf8b2db35e73f8544d93e45ed4c9

View file

@ -1,4 +1,16 @@
#include "ftls.h" /* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_ls.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/07 14:57:21 by jhalford #+# #+# */
/* Updated: 2016/11/07 17:41:26 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_ls.h"
void ft_ls_dirs(t_list *dir, char *opts) void ft_ls_dirs(t_list *dir, char *opts)
{ {
@ -11,20 +23,15 @@ void ft_ls_dirs(t_list *dir, char *opts)
{ {
dirdata = dir->content; dirdata = dir->content;
dir = dir->next; dir = dir->next;
/* ft_lstfree(ent); */
ent = ft_dir_get_ents(dirdata); ent = ft_dir_get_ents(dirdata);
ft_ent_filter(&ent, opts); ft_ent_filter(&ent, opts);
ft_ent_sort(&ent, opts); ft_ent_sort(&ent, opts);
ft_ent_print(ent, opts, dirdata, dir); ft_ent_print(ent, opts, dirdata, dir);
if (ft_strchr(opts, 'R')) if (ft_strchr(opts, 'R'))
{ {
ft_debug();
dir_r = ft_ent_get_dirs(ent); dir_r = ft_ent_get_dirs(ent);
ft_debug();
ft_lst_merge(&dir_r, dir); ft_lst_merge(&dir_r, dir);
ft_debug();
dir = dir_r; dir = dir_r;
ft_debug();
} }
} }
} }

View file

@ -1,8 +1,20 @@
#include "ftls.h" /* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_ls_long.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/07 14:58:05 by jhalford #+# #+# */
/* Updated: 2016/11/07 16:43:01 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_ls.h"
int ft_ls_long(t_list *ent, t_lsdata *topdir) int ft_ls_long(t_list *ent, t_lsdata *topdir)
{ {
t_pads pads = {0, 0, 0, 0}; t_pads pads;
if (topdir) if (topdir)
ft_ls_long_total(ent); ft_ls_long_total(ent);

View file

@ -1,4 +1,16 @@
#include "ftls.h" /* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_ls_short.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/07 14:56:28 by jhalford #+# #+# */
/* Updated: 2016/11/07 15:29:56 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_ls.h"
void ft_ls_short(t_list *ent) void ft_ls_short(t_list *ent)
{ {

View file

@ -1,4 +1,16 @@
#include "ftls.h" /* ************************************************************************** */
/* */
/* ::: :::::::: */
/* lib_dirents.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/07 15:00:05 by jhalford #+# #+# */
/* Updated: 2016/11/07 17:42:16 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_ls.h"
void ft_ent_filter(t_list **ent, char *opts) void ft_ent_filter(t_list **ent, char *opts)
{ {
@ -59,6 +71,7 @@ t_list *ft_ent_get_dirs(t_list *ent)
&& ft_strcmp(dirent->d_name, ".") && ft_strcmp(dirent->d_name, ".")
&& ft_strcmp(dirent->d_name, "..")) && ft_strcmp(dirent->d_name, ".."))
{ {
tmp->next = NULL;
ft_lsteadd(&dir, tmp); ft_lsteadd(&dir, tmp);
} }
} }

View file

@ -1,4 +1,16 @@
#include "ftls.h" /* ************************************************************************** */
/* */
/* ::: :::::::: */
/* lib_dirs.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/07 14:58:49 by jhalford #+# #+# */
/* Updated: 2016/11/07 15:30:07 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_ls.h"
t_list *ft_dir_get_ents(t_lsdata *topdir) t_list *ft_dir_get_ents(t_lsdata *topdir)
{ {
@ -13,13 +25,8 @@ t_list *ft_dir_get_ents(t_lsdata *topdir)
{ {
data.path = ft_strjoin(topdir->path, "/"); data.path = ft_strjoin(topdir->path, "/");
data.path = ft_strjoin(data.path, dirent->d_name); data.path = ft_strjoin(data.path, dirent->d_name);
/* ft_printf("looking at file: %s\n", data.path); */
/* ft_printf("stat ret: %i\n", stat(data.path, &data.stat)); */
lstat(data.path, &data.stat); lstat(data.path, &data.stat);
data.dirent = dirent; data.dirent = dirent;
/* char *date = ctime(&data.stat.st_mtime); */
/* date[ft_strlen(date) - 1] = '\0'; */
/* ft_printf("%s %i %s\n", date, data.stat.st_ino, data.dirent->d_name); */
ft_lstadd(&ent, ft_lstnew(&data, sizeof(data))); ft_lstadd(&ent, ft_lstnew(&data, sizeof(data)));
} }
closedir(stream); closedir(stream);

View file

@ -1,5 +1,16 @@
#include "ftls.h" /* ************************************************************************** */
#include <stdio.h> /* */
/* ::: :::::::: */
/* lib_error.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/07 14:57:43 by jhalford #+# #+# */
/* Updated: 2016/11/07 15:30:13 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_ls.h"
void ft_error_option(char c) void ft_error_option(char c)
{ {

View file

@ -1,4 +1,16 @@
#include "ftls.h" /* ************************************************************************** */
/* */
/* ::: :::::::: */
/* lib_ls_long.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/07 14:59:51 by jhalford #+# #+# */
/* Updated: 2016/11/07 17:29:49 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_ls.h"
int ft_ls_long_middle(struct stat *stat, t_pads *pads) int ft_ls_long_middle(struct stat *stat, t_pads *pads)
{ {
@ -30,7 +42,6 @@ void ft_ls_long_date(struct stat *stat)
time = ft_time_isrecent(stat->st_mtime) time = ft_time_isrecent(stat->st_mtime)
? ft_strsub(date, 11, 5) ? ft_strsub(date, 11, 5)
: ft_strsub(date, 20, 4); : ft_strsub(date, 20, 4);
/* ft_putstr(day); */
ft_printf(" %s %s %5s", day, month, time); ft_printf(" %s %s %5s", day, month, time);
} }
@ -48,4 +59,3 @@ void ft_ls_long_type(mode_t m)
type = S_ISLNK(m) ? 'l' : type; type = S_ISLNK(m) ? 'l' : type;
ft_printf("%c", type); ft_printf("%c", type);
} }

View file

@ -1,4 +1,16 @@
#include "ftls.h" /* ************************************************************************** */
/* */
/* ::: :::::::: */
/* lib_ls_long2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/07 15:01:01 by jhalford #+# #+# */
/* Updated: 2016/11/07 17:38:27 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_ls.h"
#include <errno.h> #include <errno.h>
void ft_ls_long_rights(int st_mode) void ft_ls_long_rights(int st_mode)
@ -29,16 +41,15 @@ void ft_ls_long_rights(int st_mode)
int ft_ls_long_lnk(t_lsdata *data) int ft_ls_long_lnk(t_lsdata *data)
{ {
struct stat statbuf;
int ret; int ret;
char buf[1024];
ft_bzero(&statbuf, sizeof(statbuf));
if (S_ISLNK(data->stat.st_mode)) if (S_ISLNK(data->stat.st_mode))
{ {
ft_printf(" -> %lld\n", statbuf.st_size); if ((ret = readlink(data->path, buf, 1024)) < 0)
ft_printf("path: %s\n", data->path); return (-1);
if ((ret = stat(data->path, &statbuf)) == -1) buf[ret] = '\0';
ft_printf("stat=%i, errno=%i\n", ret, errno); ft_printf(" -> %s\n", buf);
} }
else else
ft_putendl(""); ft_putendl("");
@ -74,7 +85,6 @@ void ft_ls_long_total(t_list *ent)
data = ent->content; data = ent->content;
stat = data->stat; stat = data->stat;
ent = ent->next; ent = ent->next;
total += stat.st_blocks; total += stat.st_blocks;
} }
ft_printf("total %i\n", total); ft_printf("total %i\n", total);
@ -87,12 +97,15 @@ int ft_ls_long_pads(t_list *ent, t_pads *pads)
struct stat stat; struct stat stat;
t_lsdata *data; t_lsdata *data;
pads->nlink = 0;
pads->name = 0;
pads->gr_name = 0;
pads->size = 0;
while (ent) while (ent)
{ {
data = ent->content; data = ent->content;
stat = data->stat; stat = data->stat;
ent = ent->next; ent = ent->next;
if ((pwd = getpwuid(stat.st_uid)) == NULL) if ((pwd = getpwuid(stat.st_uid)) == NULL)
return (1); return (1);
if ((grp = getgrgid(stat.st_gid)) == NULL) if ((grp = getgrgid(stat.st_gid)) == NULL)

View file

@ -1,4 +1,16 @@
#include "ftls.h" /* ************************************************************************** */
/* */
/* ::: :::::::: */
/* lib_lsdata.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/07 14:59:09 by jhalford #+# #+# */
/* Updated: 2016/11/07 15:30:33 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_ls.h"
void ft_lsdata_filename(t_lsdata *data) void ft_lsdata_filename(t_lsdata *data)
{ {

View file

@ -1,4 +1,16 @@
#include "ftls.h" /* ************************************************************************** */
/* */
/* ::: :::::::: */
/* lib_parse.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/07 15:02:46 by jhalford #+# #+# */
/* Updated: 2016/11/07 17:53:03 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_ls.h"
char *ft_parse_ls(int ac, char **av, t_list **dir, t_list **ent) char *ft_parse_ls(int ac, char **av, t_list **dir, t_list **ent)
{ {
@ -55,7 +67,7 @@ int ft_parse_ls_options(int ac, char **av, char *opts)
if (av[i][0] == '-' && av[i][1] != '\0') if (av[i][0] == '-' && av[i][1] != '\0')
{ {
j = 0; j = 0;
while(av[i][++j]) while (av[i][++j])
{ {
if (ft_strchr(ALL_OPTS, av[i][j])) if (ft_strchr(ALL_OPTS, av[i][j]))
{ {

View file

@ -1,10 +1,22 @@
#include "ftls.h" /* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/07 15:00:15 by jhalford #+# #+# */
/* Updated: 2016/11/07 17:42:33 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_ls.h"
int main(int ac, char **av) int main(int ac, char **av)
{ {
t_list *dir; t_list *dir;
t_list *ent; t_list *ent;
char *opts; char *opts;
dir = NULL; dir = NULL;
ent = NULL; ent = NULL;