aboutsummaryrefslogtreecommitdiff
path: root/dir_monitor.c
diff options
context:
space:
mode:
Diffstat (limited to 'dir_monitor.c')
-rw-r--r--dir_monitor.c47
1 files 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 <stdlib.h>
#include <string.h>
#include <unistd.h>
+#include <ctype.h>
#include <getopt.h>
#include <stdint.h>
#include <errno.h>
@@ -18,6 +19,7 @@
#include <dirent.h>
#include <sys/stat.h>
#include <time.h>
+#include <sys/ioctl.h>
#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<x; i++) {
+ if (fputc(c, fdout) == EOF) {
+ LOGERR("ERROR: Failed to write char 0x%02X to stream %d\n", c, fdout->_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;
}