aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThorsten Töpper <atsutane@freethoughts.de>2025-06-12 22:11:56 +0200
committerThorsten Töpper <atsutane@freethoughts.de>2025-06-12 22:11:56 +0200
commit71ad5b238bfe951b61031d91e8e550e3e36316a2 (patch)
tree3c7daba93c984d9ef77f1a8b871b3162c2fa5053
parent45fc2872aee9af15a61d8dbae8bd9d2c8eecb437 (diff)
downloaddir_monitor-71ad5b238bfe951b61031d91e8e550e3e36316a2.tar.gz
dir_monitor-71ad5b238bfe951b61031d91e8e550e3e36316a2.tar.bz2
Add options for hidden files and long timestamps
-rw-r--r--dir_monitor.c86
1 files changed, 78 insertions, 8 deletions
diff --git a/dir_monitor.c b/dir_monitor.c
index 9903b12..2d86fb0 100644
--- a/dir_monitor.c
+++ b/dir_monitor.c
@@ -11,6 +11,7 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
+#include <getopt.h>
#include <stdint.h>
#include <errno.h>
#include <sys/types.h>
@@ -45,14 +46,22 @@ struct list_head {
struct list_node *create_node(char *fname, size_t fsize, time_t ftime);
void destroy_list(struct list_head *list);
+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);
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 },
+ { "long-timestamp", no_argument, 0, 0 },
+ { 0, 0, 0, 0 }
+};
bool option_show_hidden_entries = false;
-bool option_timestamp_short = true;
+bool option_timestamp_long = false;
/* === IMPLEMENTATION === */
@@ -144,12 +153,12 @@ void print_list(struct list_head *list) {
((ptr->fsize >= 1024) ? "kB" : ""));
tm = localtime(&ptr->ftime);
- if (option_timestamp_short) {
- strftime(timestamp, sizeof(timestamp), "%H:%M:%S", tm);
- printf(" %8s ", timestamp);
- } else {
+ if (option_timestamp_long) {
strftime(timestamp, sizeof(timestamp), "%Y%m%d %H:%M:%S %Z", tm);
printf(" %20s ", timestamp);
+ } else {
+ strftime(timestamp, sizeof(timestamp), "%H:%M:%S", tm);
+ printf(" %8s ", timestamp);
}
printf(" %s\n", ptr->fname);
@@ -174,8 +183,8 @@ struct list_head *get_data_from_directory(char *path) {
}
l = strlen(path);
- /* +2 due to possibly required / and of course string termination */
- if ((fullpath = calloc(l+256+2, sizeof(char))) == NULL) {
+ /* 258 filename and possibly required PATH_SEP and of course string termination */
+ if ((fullpath = calloc(l+258, sizeof(char))) == NULL) {
LOGERR("ERROR: Failed to allocate memory for list head.\n");
return NULL;
}
@@ -219,9 +228,70 @@ struct list_head *get_data_from_directory(char *path) {
}
+void set_option(const char *option_name, char *option_argument) {
+ if (option_name == NULL)
+ return;
+
+ if (strcmp(option_name, "show-hidden-entries") == 0) {
+ option_show_hidden_entries = true;
+ return;
+ }
+
+ if (strcmp(option_name, "long-timestamp") == 0) {
+ option_timestamp_long = true;
+ return;
+ }
+
+ LOGERR("ERROR: Option '%s' not recognized\n.", option_name);
+}
+
+int parse_arguments(int argc, char **argv) {
+ int c = 0, index;
+
+ while(1) {
+ index = 0;
+ c = getopt_long(argc, argv, "tv", long_options, &index);
+
+ if (c == -1) {
+ break;
+ }
+
+ switch (c) {
+ case 0:
+ set_option(long_options[index].name, optarg);
+ break;
+ case 't':
+ option_timestamp_long = true;
+ break;
+ case 'v':
+ option_show_hidden_entries = true;
+ break;
+ default:
+ LOGERR("ERROR: unrecognized option '0x%02X'\n", c);
+ break;
+ }
+ }
+
+ return optind;
+}
+
+
int main(int argc, char **argv) {
struct list_head *list = NULL;
- list = get_data_from_directory(argv[1]);
+ int path_index = 1;
+ /* TODO: Handle options */
+ if (argc < 2) {
+ fprintf(stderr, "Call: %s OPTIONS path_to_open\n", argv[0]);
+ fprintf(stderr, "\n OPTIONS are\n");
+ /* long name, short name, optional argument, explanation */
+ fprintf(stderr, " %20s %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", "",
+ "Show hidden entries in the directory");
+ return EXIT_FAILURE;
+ }
+ path_index = parse_arguments(argc, argv);
+ list = get_data_from_directory(argv[path_index]);
print_list(list);
destroy_list(list);
return EXIT_SUCCESS;