From 09f39518f23f0bd59ace2f72c3f3717c5e64f9a6 Mon Sep 17 00:00:00 2001 From: Thorsten Töpper Date: Sat, 14 Jun 2025 15:33:09 +0200 Subject: dir_monitor.c: print Total size --- dir_monitor.c | 47 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 41 insertions(+), 6 deletions(-) diff --git a/dir_monitor.c b/dir_monitor.c index 2d86fb0..8ff6781 100644 --- a/dir_monitor.c +++ b/dir_monitor.c @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -18,6 +19,7 @@ #include #include #include +#include #define LOGERR(...) {fprintf(stderr, "[%s:%d] %s: ", __FILE__, __LINE__, __func__); fprintf(stderr, __VA_ARGS__);} @@ -46,6 +48,8 @@ struct list_head { struct list_node *create_node(char *fname, size_t fsize, time_t ftime); void destroy_list(struct list_head *list); +int fputc_all_cols(char c, FILE *fdout); +int fputc_width_x(char c, size_t x, FILE *fdout); struct list_head *get_data_from_directory(char *path); int insert_sorted_by_size(struct list_head *list, struct list_node *node); int parse_arguments(int argc, char **argv); @@ -53,7 +57,6 @@ void print_list(struct list_head *list); void set_option(const char *option_name, char *option_argument); - /* === GLOBAL VARIABLES === */ struct option long_options[] = { { "show-hidden-entries", no_argument, 0, 0 }, @@ -103,6 +106,35 @@ inline void destroy_list(struct list_head *list) { } +inline int fputc_all_cols(char c, FILE *fdout) { + struct winsize terminal; + ioctl(STDOUT_FILENO, TIOCGWINSZ, &terminal); + return fputc_width_x(c, ((terminal.ws_col == 0) ? 72 : terminal.ws_col), fdout); +} + + +/* Not the most performant way, but during output the time critical tasks are already done. */ +inline int fputc_width_x(char c, size_t x, FILE *fdout) { + size_t i=0; + + if (fdout == NULL) { + LOGERR("ERROR: No output stream given.\n"); + return EOF; + } + + if ( ! isprint(c) ) { c = '?'; } + + for (i=0; i_fileno); + return EOF; + } + } + + return 1; +} + + /* TODO: later when, other parameters are actively used move the action to a template */ inline int insert_sorted_by_size(struct list_head *list, struct list_node *node) { struct list_node *ptr = NULL; @@ -144,9 +176,9 @@ void print_list(struct list_head *list) { struct list_node *ptr; struct tm *tm = NULL; char timestamp[128]; + size_t total_size = 0; if (list == NULL) return; - ptr = list->first; while (ptr != NULL) { printf(" %8lu %2s ", ((ptr->fsize>=1024) ? (ptr->fsize/1024) : ptr->fsize), @@ -160,10 +192,13 @@ void print_list(struct list_head *list) { strftime(timestamp, sizeof(timestamp), "%H:%M:%S", tm); printf(" %8s ", timestamp); } - printf(" %s\n", ptr->fname); + total_size += ptr->fsize; ptr = ptr->next; } + fputc_all_cols('=', stdout); + printf("\nTotal size: %llu %s\n", ((total_size>1024) ? total_size/1024 : total_size), + ((total_size >= 1024) ? "kB" : "")); } @@ -282,11 +317,11 @@ int main(int argc, char **argv) { /* TODO: Handle options */ if (argc < 2) { fprintf(stderr, "Call: %s OPTIONS path_to_open\n", argv[0]); - fprintf(stderr, "\n OPTIONS are\n"); + fprintf(stderr, "\nOPTIONS are\n"); /* long name, short name, optional argument, explanation */ - fprintf(stderr, " %20s %2s %10s %s\n", "--long-timestamp", "-t", "", + fprintf(stderr, " %-25s %2s %10s %s\n", "--long-timestamp", "-t", "", "Print timestamp in long form yyyymmdd HH:MM:SS ZONE"); - fprintf(stderr, " %20s %2s %10s %s\n", "--show-hidden-entries", "-v", "", + fprintf(stderr, " %-25s %2s %10s %s\n", "--show-hidden-entries", "-v", "", "Show hidden entries in the directory"); return EXIT_FAILURE; } -- cgit v1.2.3-70-g09d2