logo

utils-std

Collection of commonly available Unix tools git clone https://anongit.hacktivis.me/git/utils-std.git/
commit: 6d8806f6749d1e3a70cb2306c9e84cc58b2dc932
parent caac31f9777c48d7b2b0cbad767b4b548eb266b4
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Sat, 14 Jun 2025 13:05:53 +0200

cmd/realpath: add support for -q

Diffstat:

Mcmd/realpath.113+++++++++----
Mcmd/realpath.c29++++++++++++++++++++---------
Mtest-cmd/realpath.sh5++++-
3 files changed, 33 insertions(+), 14 deletions(-)

diff --git a/cmd/realpath.1 b/cmd/realpath.1 @@ -11,7 +11,7 @@ .Nm .Op Fl E Ns | Ns Fl e .Op Fl n Ns | Ns Fl z -.Op Fl s +.Op Fl qs .Ar path... .Sh DESCRIPTION The @@ -35,6 +35,11 @@ Fail when does not exists. .It Fl n Do not print a trailing separator. +.It Fl q +Do not print errors/warnings when +.Xr realpath 3 +fails. +Messages about other errors, such as bad usage, are still printed. .It Fl s Do not resolve symlinks. .It Fl z @@ -52,9 +57,9 @@ specification. .Pp The options .Fl n , -.Fl s -and -.Fl z +.Fl q , +.Fl s , +.Fl z , and support for multiple .Ar path arguments are extensions present for compatibility with existing software. diff --git a/cmd/realpath.c b/cmd/realpath.c @@ -18,6 +18,7 @@ static bool must_exists = false; static bool offline = false; +static bool quiet = false; const char *argv0 = NULL; static char sep = '\n'; @@ -47,7 +48,9 @@ print_realpath(char *path) if(must_exists || errno != ENOENT) { - fprintf(stderr, "%s: error: Failed canonilizing \"%s\": %s\n", argv0, path, strerror(errno)); + if(!quiet) + fprintf(stderr, "%s: error: Failed canonilizing \"%s\": %s\n", argv0, path, strerror(errno)); + return 1; } @@ -56,7 +59,9 @@ print_realpath(char *path) if(child == NULL) { // Return as if realpath just failed - fprintf(stderr, "%s: error: Failed canonilizing \"%s\": %s\n", argv0, path, strerror(errno)); + if(!quiet) + fprintf(stderr, "%s: error: Failed canonilizing \"%s\": %s\n", argv0, path, strerror(errno)); + return 1; } @@ -65,12 +70,14 @@ print_realpath(char *path) char *parent = realpath(path, NULL); if(!parent) { - fprintf(stderr, - "%s: error: Failed canonilizing parent of full path \"%s/%s\": %s\n", - argv0, - path, - child, - strerror(errno)); + if(!quiet) + fprintf(stderr, + "%s: error: Failed canonilizing parent of full path \"%s/%s\": %s\n", + argv0, + path, + child, + strerror(errno)); + return 1; } @@ -95,10 +102,11 @@ static int main_realpath(int argc, char *argv[]) { must_exists = false; + quiet = false; int offset_sep = 0; - for(int c = -1; (c = getopt_nolong(argc, argv, ":Eemnsz")) != -1;) + for(int c = -1; (c = getopt_nolong(argc, argv, ":Eemnszq")) != -1;) { switch(c) { @@ -117,6 +125,9 @@ main_realpath(int argc, char *argv[]) case 'z': sep = '\0'; break; + case 'q': + quiet = true; + break; case ':': fprintf(stderr, "%s: error: Missing operand for option: '-%c'\n", argv0, optopt); usage_realpath(); diff --git a/test-cmd/realpath.sh b/test-cmd/realpath.sh @@ -3,7 +3,7 @@ # SPDX-License-Identifier: MPL-2.0 target="$(dirname "$0")/../cmd/realpath" -plans=31 +plans=34 . "$(dirname "$0")/tap.sh" t . '.' "${PWD} @@ -21,6 +21,7 @@ t --exit=1 /var/empty/foo/bar/ /var/empty/foo/bar/ 'realpath: error: Failed cano ' t --exit=1 /var/empty/foo/bar// /var/empty/foo/bar// 'realpath: error: Failed canonilizing parent of full path "/var/empty/foo/bar": No such file or directory ' +t --exit=1 q:/var/empty/foo/bar// '-q /var/empty/foo/bar//' t e:/ '-e /' '/ ' @@ -38,6 +39,7 @@ t --exit=1 e:/var/empty/foo/bar/ '-e /var/empty/foo/bar/' 'realpath: error: Fail ' t --exit=1 e:/var/empty/foo/bar// '-e /var/empty/foo/bar//' 'realpath: error: Failed canonilizing "/var/empty/foo/bar//": No such file or directory ' +t --exit=1 qe:/var/empty/foo/bar// '-qe /var/empty/foo/bar//' # Non-directory @@ -54,6 +56,7 @@ t --exit=1 e:/dev/null/ '-e /dev/null/' 'realpath: error: Failed canonilizing "/ ' t --exit=1 E:/dev/null/ '-E /dev/null/' 'realpath: error: Failed canonilizing "/dev/null/": Not a directory ' +t --exit=1 qE:/dev/null/ '-qE /dev/null/' t --exit=1 /dev/null/.. /dev/null/.. 'realpath: error: Failed canonilizing "/dev/null/..": Not a directory '