From 2793ae430e77042ab5effb5a80b8b27552cd313c Mon Sep 17 00:00:00 2001 From: Tom Bereknyei Date: Sat, 20 Jul 2024 16:37:01 -0400 Subject: [PATCH] feat: minimal flag to print less information --- Makefile | 2 +- pkgs/tracelinks/default.nix | 2 +- tracelinks.c | 34 ++++++++++++++++++++++++---------- 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/Makefile b/Makefile index be4de34..3256cea 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ .DEFAULT_GOAL = all NAME = tracelinks -VERSION = 1.0.0 +VERSION = 1.1.0 PREFIX ?= /usr/local CFLAGS = -Wall -g -DVERSION='"$(VERSION)"' diff --git a/pkgs/tracelinks/default.nix b/pkgs/tracelinks/default.nix index 623c883..e79e948 100644 --- a/pkgs/tracelinks/default.nix +++ b/pkgs/tracelinks/default.nix @@ -1,7 +1,7 @@ { self, stdenv, help2man }: stdenv.mkDerivation { pname = "tracelinks"; - version = "1.0.0"; + version = "1.1.0"; src = self; # Prevent the source from becoming a runtime dependency. diff --git a/tracelinks.c b/tracelinks.c index 0df6310..35090af 100644 --- a/tracelinks.c +++ b/tracelinks.c @@ -10,6 +10,7 @@ #include #include +static int minimal_flag = 0; static int debug_flag = 0; static int absolute_flag = 0; static int keep_going_flag = 0; @@ -26,6 +27,7 @@ usage(const int rc) { printf(" -a, --absolute report paths as absolute paths\n"); printf(" -k, --keep-going keep reporting on other paths after an error\n"); printf(" -h, --help print this help message\n"); + printf(" -m, --minimal print minimal information\n"); printf(" -d, --debug print extra debugging to STDERR\n"); printf(" -v, --version print version string\n"); exit(rc); @@ -133,7 +135,10 @@ tracelinks(int indent, const char *root, const char *path) { } print_indent(indent++); - printf("%s -> %.*s\n", pathbuf, (int) nbytes, linkbuf); + if (minimal_flag) + printf("%s ->\n", pathbuf); + else + printf("%s -> %.*s\n", pathbuf, (int) nbytes, linkbuf); if (d) { *d = '/'; @@ -156,14 +161,18 @@ tracelinks(int indent, const char *root, const char *path) { return(tracelinks(indent, pathbuf, d+1)); } print_indent(indent++); - switch (sb.st_mode & S_IFMT) { - case S_IFBLK: printf("%s: block device\n", pathbuf); break; - case S_IFCHR: printf("%s: character device\n", pathbuf); break; - case S_IFDIR: printf("%s: directory\n", pathbuf); break; - case S_IFIFO: printf("%s: FIFO/pipe\n", pathbuf); break; - case S_IFREG: printf("%s: regular file\n", pathbuf); break; - case S_IFSOCK: printf("%s: socket\n", pathbuf); break; - default: printf("%s: unknown?\n", pathbuf); + if (minimal_flag) + printf("%s\n", pathbuf); + else { + switch (sb.st_mode & S_IFMT) { + case S_IFBLK: printf("%s: block device\n", pathbuf); break; + case S_IFCHR: printf("%s: character device\n", pathbuf); break; + case S_IFDIR: printf("%s: directory\n", pathbuf); break; + case S_IFIFO: printf("%s: FIFO/pipe\n", pathbuf); break; + case S_IFREG: printf("%s: regular file\n", pathbuf); break; + case S_IFSOCK: printf("%s: socket\n", pathbuf); break; + default: printf("%s: unknown?\n", pathbuf); + } } if (d) { warnx("extra trailing characters: %s", d+1); @@ -183,6 +192,7 @@ main(int argc, char **argv) { {"absolute", no_argument, 0, 'a'}, {"keep-going", no_argument, 0, 'k'}, {"version", no_argument, 0, 'v'}, + {"minimal", no_argument, 0, 'm'}, {"debug", no_argument, 0, 'd'}, {"help", no_argument, 0, 'h'}, {0, 0, 0, 0} @@ -191,7 +201,7 @@ main(int argc, char **argv) { /* getopt_long stores the option index here. */ int option_index = 0; - c = getopt_long(argc, argv, "akvdh", long_options, &option_index); + c = getopt_long(argc, argv, "akvmdh", long_options, &option_index); /* Detect the end of the options. */ if (c == -1) @@ -210,6 +220,10 @@ main(int argc, char **argv) { puts(VERSION); exit(EXIT_SUCCESS); + case 'm': + minimal_flag = 1; + break; + case 'd': debug_flag = 1; break;