diff options
| author | Thorsten Töpper <atsutane@freethoughts.de> | 2025-07-05 15:53:12 +0200 |
|---|---|---|
| committer | Thorsten Töpper <atsutane@freethoughts.de> | 2025-07-05 15:53:12 +0200 |
| commit | 1ac227b896de661fe4e77e4229a4ab7d17331559 (patch) | |
| tree | d88c5e9bd6d3ff35398e089d02c53543fb00f545 /src | |
| parent | b8d37052d18723ff2a8572f5092069e80785f93a (diff) | |
| download | dir_monitor-1ac227b896de661fe4e77e4229a4ab7d17331559.tar.gz dir_monitor-1ac227b896de661fe4e77e4229a4ab7d17331559.tar.bz2 | |
output: add stat item type to format string
Diffstat (limited to 'src')
| -rw-r--r-- | src/options.c | 1 | ||||
| -rw-r--r-- | src/output.c | 43 |
2 files changed, 44 insertions, 0 deletions
diff --git a/src/options.c b/src/options.c index 293ca3d..93f0ceb 100644 --- a/src/options.c +++ b/src/options.c @@ -75,6 +75,7 @@ void usage(char *executable) { fprintf(stderr, " A - access time\n"); fprintf(stderr, " C - change time\n"); fprintf(stderr, " M - modification time\n"); + fprintf(stderr, " T - type\n"); } diff --git a/src/output.c b/src/output.c index ec566d6..102645f 100644 --- a/src/output.c +++ b/src/output.c @@ -10,6 +10,7 @@ #include <ctype.h> #include <time.h> #include <sys/ioctl.h> +#include <sys/stat.h> #include <errno.h> #include "output.h" @@ -28,6 +29,7 @@ void out_print_size(struct list_node *ptr); void out_print_time(time_t tv); void out_print_time_by_option(struct list_node *ptr); +void out_print_type(struct list_node *ptr); /* === IMPLEMENTATION === */ @@ -115,6 +117,43 @@ inline void out_print_time_by_option(struct list_node *ptr) { }; out_print_time(time_value); } + +inline void out_print_type(struct list_node *ptr) { + char entry[16] = ""; + + /* regular file, directory and symlinks are most common */ + if (S_ISREG(ptr->ln_stat.st_mode)) { + sprintf(entry, "regular file"); + } else if (S_ISDIR(ptr->ln_stat.st_mode)) { + sprintf(entry, "directory"); + } else if (S_ISLNK(ptr->ln_stat.st_mode)) { + sprintf(entry, "symlink"); + } else if (S_ISBLK(ptr->ln_stat.st_mode)) { + sprintf(entry, "block"); + } else if (S_ISCHR(ptr->ln_stat.st_mode)) { + sprintf(entry, "char"); + } else if (S_ISFIFO(ptr->ln_stat.st_mode)) { + sprintf(entry, "fifo"); + } else if (S_ISSOCK(ptr->ln_stat.st_mode)) { + sprintf(entry, "socket"); + /* The following macros operate on the complete struct stat */ + } else if (S_TYPEISMQ(&(ptr->ln_stat))) { + sprintf(entry, "message queue"); + } else if (S_TYPEISSEM(&(ptr->ln_stat))) { + sprintf(entry, "semaphore"); + } else if (S_TYPEISSHM(&(ptr->ln_stat))) { + sprintf(entry, "shared memory"); +#if 0 + /* Although mentioned in the POSIX man page, not part of glibc */ + } else if (S_TYPEISTMO(&(ptr->ln_stat))) { + sprintf(entry, "memory object"); +#endif + } else { + sprintf(entry, "unidentified"); + } + /* The ones longer than 12 characters are rare. */ + printf(" %-12s ", entry); +} /* === END OF FORMATTING FUNCTIONS === */ @@ -161,6 +200,7 @@ void print_list(struct list_head *list) { /* For now with a large walk around common format strings and prevention of attack vectors. * n - name * s - file size + * T - type * * Time related * t - timestamp set by option_time_field @@ -217,6 +257,9 @@ void print_list_formatted(const char *format, struct list_head *list) { case 'M': out_print_time(ptr->ln_stat.st_mtim.tv_sec); break; + case 'T': + out_print_type(ptr); + break; case ' ': /* just ignore this without warning */ break; default: |
