From 9cd4eeb8a10aa0390e87b3be4d652499c2c0ef73 Mon Sep 17 00:00:00 2001 From: Gregory Date: Mon, 27 Mar 2017 02:30:24 +0300 Subject: [PATCH] determine how to get basic info anot file/dir --- CMakeLists.txt | 2 +- inc/ft_ls.h | 8 ++++++++ src/main.c | 39 +++++++++++++++++++++++++++++++++++++-- 3 files changed, 46 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d1c8a76..386ef46 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 3.6) project(ft_ls) -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Werror -Wextra") +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Werror -Wextra -O0 -g") include_directories(inc libft/includes) # headers link_directories(libft) # libraries diff --git a/inc/ft_ls.h b/inc/ft_ls.h index b9097f9..a4204b2 100644 --- a/inc/ft_ls.h +++ b/inc/ft_ls.h @@ -13,7 +13,15 @@ #ifndef FT_LS_H # define FT_LS_H +# include "libft.h" # include +# include # include +# include +# include +# include +# include +# include +# include #endif diff --git a/src/main.c b/src/main.c index 933c21c..bdde2f7 100644 --- a/src/main.c +++ b/src/main.c @@ -12,7 +12,42 @@ #include "ft_ls.h" -int main(void) +int main(int argc, char **argv) { - return (write(1, "aa\n", 3)); + DIR *dp; + struct dirent *ep; + struct stat *stat_buf; + char *str_tmp; + int blocks; + + argc = argc; + dp = opendir (argv[1]); + blocks = 0; + if (dp != NULL) + { + while ((ep = readdir (dp))) + { + str_tmp = ft_strjoin("../", ep->d_name); + stat_buf = malloc(sizeof(struct stat)); + stat(str_tmp, stat_buf); + printf("%s:\n", str_tmp); + printf(" nlinks: %d\n", (int)stat_buf->st_nlink); + printf(" st_mode: %d\n", S_ISCHR(stat_buf->st_mode)); + printf(" MAJOR: %d\n", (int)major(stat_buf->st_rdev)); + printf(" MINOR: %d\n", (int)minor(stat_buf->st_rdev)); + printf(" st_uid: %s\n", getpwuid(stat_buf->st_uid)->pw_name); + printf(" st_gid: %s\n", getgrgid(stat_buf->st_gid)->gr_name); + printf(" st_size: %d\n", (int)stat_buf->st_size); + printf(" st_atime: %s", ctime(&stat_buf->st_atim.tv_sec)); + printf(" st_blocks: %d\n\n", (int)stat_buf->st_blocks); + blocks += stat_buf->st_blocks * S_BLKSIZE / 1024; + free(stat_buf); + free(str_tmp); + } + (void) closedir (dp); + printf(" total blocks: %d\n", blocks); + } + else + printf("%s\n", strerror(errno)); + return 0; }