aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorThorsten Töpper <atsutane@freethoughts.de>2025-06-20 02:20:22 +0200
committerThorsten Töpper <atsutane@freethoughts.de>2025-06-20 02:20:22 +0200
commit548c43810fc07ea220b01c02baa4facff7a1d3ed (patch)
tree087cd6993fcc2019cc749218bfe2913462098415 /src
parent49997ac30b46c42a0366423cb2048f8257774805 (diff)
downloaddir_monitor-548c43810fc07ea220b01c02baa4facff7a1d3ed.tar.gz
dir_monitor-548c43810fc07ea220b01c02baa4facff7a1d3ed.tar.bz2
options: added -h --help
Diffstat (limited to 'src')
-rw-r--r--src/options.c47
1 files changed, 31 insertions, 16 deletions
diff --git a/src/options.c b/src/options.c
index e60d4a2..5d7aaaa 100644
--- a/src/options.c
+++ b/src/options.c
@@ -5,6 +5,7 @@
* vim:ts=4:sw=4:expandtab
*/
#include <stdio.h>
+#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <stdbool.h>
@@ -17,6 +18,7 @@
/* === GLOBAL VARIABLES === */
struct option long_options[] = {
+ { "help", no_argument, 0, 0 },
{ "long-timestamp", no_argument, 0, 0 },
{ "reverse-sort", no_argument, 0, 0 },
{ "show-hidden-entries", no_argument, 0, 0 },
@@ -37,7 +39,7 @@ bool option_timestamp_long = false;
*/
char option_time_field = 'm';
-
+char *exec_name;
/* === IMPLEMENTATION === */
@@ -45,15 +47,17 @@ void usage(char *executable) {
fprintf(stderr, "Call: %s OPTIONS path_to_open\n", executable);
fprintf(stderr, "\nOPTIONS are\n");
/* long name, short name, optional argument, explanation */
- fprintf(stderr, " %-25s %2s %10s %s\n", "--long-timestamp", "-t", "",
+ fprintf(stderr, " %-25s %2s %10s %s\n", "--help", "-h", "",
+ "Show this message and exit");
+ fprintf(stderr, " %-25s %2s %10s %s\n", "--long-timestamp", "-t", "",
"Print timestamp in long form yyyymmdd HH:MM:SS ZONE");
- fprintf(stderr, " %-25s %2s %10s %s\n", "--reverse-sort", "", "",
+ fprintf(stderr, " %-25s %2s %10s %s\n", "--reverse-sort", "", "",
"Sort reversed");
- fprintf(stderr, " %-25s %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");
- fprintf(stderr, " %-25s %2s %10s %s\n", "--sort-by", "", "size|time",
- "Sort either by time or size");
- fprintf(stderr, " %-25s %2s %10s %s\n", "--time-field", "", "a|c|m",
+ fprintf(stderr, " %-25s %2s %10s %s\n", "--sort-by", "", "size|time",
+ "Sort either by size or time");
+ fprintf(stderr, " %-25s %2s %10s %s\n", "--time-field", "", "a|c|m",
"Sort by (a)ccess, (c)hange or (m)odification time. Default: m");
}
@@ -67,12 +71,12 @@ void set_option(const char *option_name, char *option_argument) {
return;
/* options WITHOUT arguments */
- if (strcmp(option_name, "show-hidden-entries") == 0) {
- option_show_hidden_entries = true;
- return;
+ if (strcmp("help", option_name) == 0) {
+ usage(exec_name);
+ exit(EXIT_SUCCESS);
}
- if (strcmp(option_name, "long-timestamp") == 0) {
+ if (strcmp("long-timestamp", option_name) == 0) {
option_timestamp_long = true;
return;
}
@@ -82,14 +86,19 @@ void set_option(const char *option_name, char *option_argument) {
return;
}
+ if (strcmp("show-hidden-entries", option_name) == 0) {
+ option_show_hidden_entries = true;
+ return;
+ }
+
/* options WITH arguments */
if (option_argument == NULL || option_argument[0] == '\0') {
LOGERR("ERROR: option_name %s with missing option_argument\n",
option_name);
- return;
+ exit(EXIT_FAILURE);
}
- if (strcmp(option_name, "sort-by") == 0) {
+ if (strcmp("sort-by", option_name) == 0) {
if (strncmp("size", option_argument, 4) == 0) {
option_sort_type = SORT_BY_SIZE;
} else if (strncmp("time", option_argument, 4) == 0) {
@@ -101,7 +110,7 @@ void set_option(const char *option_name, char *option_argument) {
return;
}
- if (strcmp(option_name, "time-field") == 0) {
+ if (strcmp("time-field", option_name) == 0) {
switch (option_argument[0]) {
case 'a':
option_time_field = 'a';
@@ -128,10 +137,12 @@ void set_option(const char *option_name, char *option_argument) {
int parse_arguments(int argc, char **argv) {
int c = 0, index;
+ /* exec_name is a file internal global variable for --help in set_option() */
+ exec_name = argv[0];
while(1) {
index = 0;
- c = getopt_long(argc, argv, "tv", long_options, &index);
+ c = getopt_long(argc, argv, "htv", long_options, &index);
if (c == -1) {
break;
@@ -141,6 +152,9 @@ int parse_arguments(int argc, char **argv) {
case 0:
set_option(long_options[index].name, optarg);
break;
+ case 'h':
+ usage(exec_name);
+ exit(EXIT_SUCCESS);
case 't':
option_timestamp_long = true;
break;
@@ -151,7 +165,8 @@ int parse_arguments(int argc, char **argv) {
break;
default:
LOGERR("ERROR: unrecognized option 0x%02X '%c'\n", c, c);
- break;
+ usage(exec_name);
+ exit(EXIT_FAILURE);
}
}