logo

utils-std

Collection of commonly available Unix tools git clone https://anongit.hacktivis.me/git/utils-std.git/
commit: 8d6f89f25242702793a1e3f96de1b3867a6ca825
parent 5964e42966f6cdeb4ec84c303374b5fa6a1305d8
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Sat, 22 Mar 2025 21:29:27 +0100

cmd/*: Add basic got_long_opt handling

Diffstat:

Mcmd/base64.c2+-
Mcmd/basename.c2+-
Mcmd/cat.c2+-
Mcmd/chmod.c2+-
Mcmd/chown.c2+-
Mcmd/cmp.c2+-
Mcmd/cut.c2+-
Mcmd/date.c2+-
Mcmd/df.c2+-
Mcmd/env.c2+-
Mcmd/head.c2+-
Mcmd/id.c2+-
Mcmd/install.c2+-
Mcmd/join.c2+-
Mcmd/ln.c2+-
Mcmd/mkdir.c2+-
Mcmd/mkfifo.c2+-
Mcmd/mknod.c2+-
Mcmd/mktemp.c2+-
Mcmd/mv.c2+-
Mcmd/nice.c2+-
Mcmd/nproc.c2+-
Mcmd/paste.c2+-
Mcmd/pathchk.c2+-
Mcmd/pwd.c2+-
Mcmd/realpath.c4++--
Mcmd/renice.c2+-
Mcmd/rm.c2+-
Mcmd/rmdir.c2+-
Mcmd/seq.c15++++++++-------
Mcmd/sha1sum.c2+-
Mcmd/sha256sum.c2+-
Mcmd/sha512sum.c2+-
Mcmd/shuf.c2+-
Mcmd/strings.c2+-
Mcmd/sync.c7++++---
Mcmd/tee.c2+-
Mcmd/time.c2+-
Mcmd/timeout.c2+-
Mcmd/touch.c2+-
Mcmd/tr.c2+-
Mcmd/truncate.c2+-
Mcmd/uname.c2+-
Mcmd/uniq.c2+-
Mcmd/wc.c2+-
Mcmd/which.c2+-
Mtest-cmd/cat.sh1+
47 files changed, 58 insertions(+), 55 deletions(-)

diff --git a/cmd/base64.c b/cmd/base64.c @@ -286,7 +286,7 @@ main(int argc, char *argv[]) fprintf(stderr, "%s: error: Missing operand for option '-%c'\n", argv0, optopt); return 1; case '?': - fprintf(stderr, "%s: error: Unrecognised option '-%c'\n", argv0, optopt); + if(!got_long_opt) fprintf(stderr, "%s: error: Unrecognised option '-%c'\n", argv0, optopt); return 1; } } diff --git a/cmd/basename.c b/cmd/basename.c @@ -70,7 +70,7 @@ main(int argc, char *argv[]) usage(); return 1; case '?': - fprintf(stderr, "%s: error: Unrecognised option '-%c'\n", argv0, optopt); + if(!got_long_opt) fprintf(stderr, "%s: error: Unrecognised option '-%c'\n", argv0, optopt); usage(); return 1; } diff --git a/cmd/cat.c b/cmd/cat.c @@ -37,7 +37,7 @@ main(int argc, char *argv[]) usage(); return 1; case '?': - fprintf(stderr, "%s: error: Unrecognised option: '-%c'\n", argv0, optopt); + if(!got_long_opt) fprintf(stderr, "%s: error: Unrecognised option: '-%c'\n", argv0, optopt); usage(); return 1; default: diff --git a/cmd/chmod.c b/cmd/chmod.c @@ -380,7 +380,7 @@ main(int argc, char *argv[]) usage(); return 1; case '?': // GNU - fprintf(stderr, "%s: error: Unrecognised option: '-%c'\n", argv0, optopt); + if(!got_long_opt) fprintf(stderr, "%s: error: Unrecognised option: '-%c'\n", argv0, optopt); usage(); return 1; } diff --git a/cmd/chown.c b/cmd/chown.c @@ -249,7 +249,7 @@ main(int argc, char *argv[]) usage(); return 1; case '?': - fprintf(stderr, "%s: error: Unrecognised option: '-%c'\n", argv0, optopt); + if(!got_long_opt) fprintf(stderr, "%s: error: Unrecognised option: '-%c'\n", argv0, optopt); usage(); return 1; default: diff --git a/cmd/cmp.c b/cmd/cmp.c @@ -132,7 +132,7 @@ main(int argc, char *argv[]) usage(); return 1; case '?': - fprintf(stderr, "%s: error: Unrecognised option: '-%c'\n", argv0, optopt); + if(!got_long_opt) fprintf(stderr, "%s: error: Unrecognised option: '-%c'\n", argv0, optopt); usage(); return 1; default: diff --git a/cmd/cut.c b/cmd/cut.c @@ -429,7 +429,7 @@ main(int argc, char *argv[]) fprintf(stderr, "%s: error: Option '-%c' requires an operand\n", argv0, optopt); return 1; case '?': - fprintf(stderr, "%s: error: Unhandled option '-%c'\n", argv0, optopt); + if(!got_long_opt) fprintf(stderr, "%s: error: Unhandled option '-%c'\n", argv0, optopt); return 1; default: fprintf(stderr, "%s: error: Unhandled getopt case '%c'\n", argv0, c); diff --git a/cmd/date.c b/cmd/date.c @@ -267,7 +267,7 @@ main(int argc, char *argv[]) usage(); return 1; case '?': - fprintf(stderr, "%s: error: Unrecognised option: '-%c'\n", argv0, optopt); + if(!got_long_opt) fprintf(stderr, "%s: error: Unrecognised option: '-%c'\n", argv0, optopt); usage(); return 1; } diff --git a/cmd/df.c b/cmd/df.c @@ -89,7 +89,7 @@ main(int argc, char *argv[]) fprintf(stderr, "%s: error: Missing operand for option: '-%c'\n", argv0, optopt); return 1; case '?': - fprintf(stderr, "%s: error: Unrecognised option: '-%c'\n", argv0, optopt); + if(!got_long_opt) fprintf(stderr, "%s: error: Unrecognised option: '-%c'\n", argv0, optopt); return 1; } } diff --git a/cmd/env.c b/cmd/env.c @@ -123,7 +123,7 @@ main(int argc, char *argv[]) usage(); return 1; case '?': - fprintf(stderr, "env: error: Unrecognised option: '-%c'\n", optopt); + if(!got_long_opt) fprintf(stderr, "env: error: Unrecognised option: '-%c'\n", optopt); usage(); return 1; default: diff --git a/cmd/head.c b/cmd/head.c @@ -224,7 +224,7 @@ main(int argc, char *argv[]) usage(); return 1; case '?': - fprintf(stderr, "%s: error: Unrecognised option: '-%c'\n", argv0, optopt); + if(!got_long_opt) fprintf(stderr, "%s: error: Unrecognised option: '-%c'\n", argv0, optopt); usage(); return 1; default: diff --git a/cmd/id.c b/cmd/id.c @@ -210,7 +210,7 @@ main(int argc, char *argv[]) real_flag = true; break; case '?': - fprintf(stderr, "%s: error: Unhandled option '-%c'\n", argv0, optopt); + if(!got_long_opt) fprintf(stderr, "%s: error: Unhandled option '-%c'\n", argv0, optopt); usage(); free(groups); return 1; diff --git a/cmd/install.c b/cmd/install.c @@ -277,7 +277,7 @@ main(int argc, char *argv[]) usage(); return 1; case '?': - fprintf(stderr, "install: error: Unknown option '-%c'\n", optopt); + if(!got_long_opt) fprintf(stderr, "install: error: Unknown option '-%c'\n", optopt); usage(); return 1; } diff --git a/cmd/join.c b/cmd/join.c @@ -201,7 +201,7 @@ main(int argc, char *argv[]) usage(); return 1; case '?': - fprintf(stderr, "%s: error: Unrecognised option: '-%c'\n", argv0, optopt); + if(!got_long_opt) fprintf(stderr, "%s: error: Unrecognised option: '-%c'\n", argv0, optopt); usage(); return 1; default: diff --git a/cmd/ln.c b/cmd/ln.c @@ -162,7 +162,7 @@ main(int argc, char *argv[]) verbose = true; break; case '?': - fprintf(stderr, "ln: error: Unknown option '-%c'\n", optopt); + if(!got_long_opt) fprintf(stderr, "ln: error: Unknown option '-%c'\n", optopt); usage(); break; } diff --git a/cmd/mkdir.c b/cmd/mkdir.c @@ -77,7 +77,7 @@ main(int argc, char *argv[]) usage(); return 1; case '?': - fprintf(stderr, "%s: error: Unrecognised option: '-%c'\n", argv0, optopt); + if(!got_long_opt) fprintf(stderr, "%s: error: Unrecognised option: '-%c'\n", argv0, optopt); usage(); return 1; default: diff --git a/cmd/mkfifo.c b/cmd/mkfifo.c @@ -47,7 +47,7 @@ main(int argc, char *argv[]) usage(); return 1; case '?': - fprintf(stderr, "mkfifo: error: Unrecognised option: '-%c'\n", optopt); + if(!got_long_opt) fprintf(stderr, "mkfifo: error: Unrecognised option: '-%c'\n", optopt); usage(); return 1; default: diff --git a/cmd/mknod.c b/cmd/mknod.c @@ -71,7 +71,7 @@ main(int argc, char *argv[]) usage(); return 1; case '?': - fprintf(stderr, "mknod: error: Unrecognised option: '-%c'\n", optopt); + if(!got_long_opt) fprintf(stderr, "mknod: error: Unrecognised option: '-%c'\n", optopt); usage(); return 1; default: diff --git a/cmd/mktemp.c b/cmd/mktemp.c @@ -119,7 +119,7 @@ main(int argc, char *argv[]) o_unsafe = true; break; case '?': - fprintf(stderr, "%s: error: Unrecognised option: '-%c'\n", argv0, optopt); + if(!got_long_opt) fprintf(stderr, "%s: error: Unrecognised option: '-%c'\n", argv0, optopt); return 1; default: fprintf(stderr, "%s: error: Unhandled getopt case '%c'\n", argv0, c); diff --git a/cmd/mv.c b/cmd/mv.c @@ -434,7 +434,7 @@ main(int argc, char *argv[]) usage(); return 1; case '?': - fprintf(stderr, "mv: error: Unrecognised option: '-%c'\n", optopt); + if(!got_long_opt) fprintf(stderr, "mv: error: Unrecognised option: '-%c'\n", optopt); usage(); return 1; } diff --git a/cmd/nice.c b/cmd/nice.c @@ -52,7 +52,7 @@ main(int argc, char *argv[]) usage(); return 125; case '?': - fprintf(stderr, "%s: error: Unrecognised option: '-%c'\n", argv0, optopt); + if(!got_long_opt) fprintf(stderr, "%s: error: Unrecognised option: '-%c'\n", argv0, optopt); usage(); return 125; default: diff --git a/cmd/nproc.c b/cmd/nproc.c @@ -44,7 +44,7 @@ main(int argc, char *argv[]) usage(); return 1; case '?': - fprintf(stderr, "%s: error: Unrecognised option: '-%c'\n", argv0, optopt); + if(!got_long_opt) fprintf(stderr, "%s: error: Unrecognised option: '-%c'\n", argv0, optopt); usage(); return 1; } diff --git a/cmd/paste.c b/cmd/paste.c @@ -253,7 +253,7 @@ main(int argc, char *argv[]) usage(); return 1; case '?': - fprintf(stderr, "%s: error: Unrecognised option: '-%c'\n", argv0, optopt); + if(!got_long_opt) fprintf(stderr, "%s: error: Unrecognised option: '-%c'\n", argv0, optopt); usage(); return 1; default: diff --git a/cmd/pathchk.c b/cmd/pathchk.c @@ -62,7 +62,7 @@ main(int argc, char *argv[]) usage(); return 1; case '?': - fprintf(stderr, "pathchk: error: Unrecognised option: '-%c'\n", optopt); + if(!got_long_opt) fprintf(stderr, "pathchk: error: Unrecognised option: '-%c'\n", optopt); usage(); return 1; } diff --git a/cmd/pwd.c b/cmd/pwd.c @@ -91,7 +91,7 @@ main(int argc, char *argv[]) usage(); return 1; case '?': - fprintf(stderr, "%s: error: Unrecognised option '-%c'\n", argv0, optopt); + if(!got_long_opt) fprintf(stderr, "%s: error: Unrecognised option '-%c'\n", argv0, optopt); usage(); return 1; } diff --git a/cmd/realpath.c b/cmd/realpath.c @@ -122,7 +122,7 @@ main_realpath(int argc, char *argv[]) usage_realpath(); return 1; case '?': - fprintf(stderr, "%s: error: Unrecognised option: '-%c'\n", argv0, optopt); + if(!got_long_opt) fprintf(stderr, "%s: error: Unrecognised option: '-%c'\n", argv0, optopt); usage_realpath(); return 1; } @@ -184,7 +184,7 @@ main_readlink(int argc, char *argv[]) usage_readlink(); return 1; case '?': - fprintf(stderr, "%s: error: Unrecognised option: '-%c'\n", argv0, optopt); + if(!got_long_opt) fprintf(stderr, "%s: error: Unrecognised option: '-%c'\n", argv0, optopt); usage_readlink(); return 1; } diff --git a/cmd/renice.c b/cmd/renice.c @@ -124,7 +124,7 @@ main(int argc, char *argv[]) usage(); return 1; case '?': - fprintf(stderr, "renice: error: Unrecognised option: '-%c'\n", optopt); + if(!got_long_opt) fprintf(stderr, "renice: error: Unrecognised option: '-%c'\n", optopt); usage(); return 1; default: diff --git a/cmd/rm.c b/cmd/rm.c @@ -197,7 +197,7 @@ main(int argc, char *argv[]) usage(); return 1; case '?': - fprintf(stderr, "rm: error: Unrecognised option: '-%c'\n", optopt); + if(!got_long_opt) fprintf(stderr, "rm: error: Unrecognised option: '-%c'\n", optopt); usage(); return 1; default: diff --git a/cmd/rmdir.c b/cmd/rmdir.c @@ -70,7 +70,7 @@ main(int argc, char *argv[]) usage(); return 1; case '?': - fprintf(stderr, "rmdir: error: Unrecognised option: '-%c'\n", optopt); + if(!got_long_opt) fprintf(stderr, "rmdir: error: Unrecognised option: '-%c'\n", optopt); usage(); return 1; default: diff --git a/cmd/seq.c b/cmd/seq.c @@ -99,13 +99,14 @@ main(int argc, char *argv[]) usage(); return 1; case '?': - if(isdigit(optopt)) - { - fprintf(stderr, - "seq: warning: Pass -- if the first non-option argument starts with a dash(-)\n"); - optind--; - goto getopt_end; - } + if(!got_long_opt) + if(isdigit(optopt)) + { + fprintf(stderr, + "seq: warning: Pass -- if the first non-option argument starts with a dash(-)\n"); + optind--; + goto getopt_end; + } usage(); return 1; } diff --git a/cmd/sha1sum.c b/cmd/sha1sum.c @@ -179,7 +179,7 @@ main(int argc, char *argv[]) fprintf(stderr, "%s: error: Missing operand for option: '-%c'\n", argv0, optopt); return 1; case '?': - fprintf(stderr, "%s: error: Unrecognised option: '-%c'\n", argv0, optopt); + if(!got_long_opt) fprintf(stderr, "%s: error: Unrecognised option: '-%c'\n", argv0, optopt); return 1; default: abort(); diff --git a/cmd/sha256sum.c b/cmd/sha256sum.c @@ -179,7 +179,7 @@ main(int argc, char *argv[]) fprintf(stderr, "%s: error: Missing operand for option: '-%c'\n", argv0, optopt); return 1; case '?': - fprintf(stderr, "%s: error: Unrecognised option: '-%c'\n", argv0, optopt); + if(!got_long_opt) fprintf(stderr, "%s: error: Unrecognised option: '-%c'\n", argv0, optopt); return 1; default: abort(); diff --git a/cmd/sha512sum.c b/cmd/sha512sum.c @@ -179,7 +179,7 @@ main(int argc, char *argv[]) fprintf(stderr, "%s: error: Missing operand for option: '-%c'\n", argv0, optopt); return 1; case '?': - fprintf(stderr, "%s: error: Unrecognised option: '-%c'\n", argv0, optopt); + if(!got_long_opt) fprintf(stderr, "%s: error: Unrecognised option: '-%c'\n", argv0, optopt); return 1; default: abort(); diff --git a/cmd/shuf.c b/cmd/shuf.c @@ -132,7 +132,7 @@ main(int argc, char *argv[]) usage(); return 1; case '?': - fprintf(stderr, "%s: error: Unrecognised option: '-%c'\n", argv0, optopt); + if(!got_long_opt) fprintf(stderr, "%s: error: Unrecognised option: '-%c'\n", argv0, optopt); usage(); return 1; } diff --git a/cmd/strings.c b/cmd/strings.c @@ -172,7 +172,7 @@ main(int argc, char *argv[]) usage(); return 1; case '?': - fprintf(stderr, "strings: error: Unrecognised option: '-%c'\n", optopt); + if(!got_long_opt) fprintf(stderr, "strings: error: Unrecognised option: '-%c'\n", optopt); usage(); return 1; default: diff --git a/cmd/sync.c b/cmd/sync.c @@ -43,9 +43,10 @@ main(int argc, char *argv[]) break; #endif case '?': - fprintf(stderr, - "sync: error: Unrecognized option '-%c', continuing with error status set\n", - optopt); + if(!got_long_opt) + fprintf(stderr, + "sync: error: Unrecognized option '-%c', continuing with error status set\n", + optopt); err = 1; break; default: diff --git a/cmd/tee.c b/cmd/tee.c @@ -45,7 +45,7 @@ main(int argc, char *argv[]) fprintf(stderr, "tee: error: Missing operand for option: '-%c'\n", optopt); return 1; case '?': - fprintf(stderr, "tee: error: Unrecognised option: '-%c'\n", optopt); + if(!got_long_opt) fprintf(stderr, "tee: error: Unrecognised option: '-%c'\n", optopt); return 1; default: abort(); diff --git a/cmd/time.c b/cmd/time.c @@ -56,7 +56,7 @@ main(int argc, char *argv[]) usage(); return 1; case '?': - fprintf(stderr, "time: error: Unrecognised option: '-%c'\n", optopt); + if(!got_long_opt) fprintf(stderr, "time: error: Unrecognised option: '-%c'\n", optopt); usage(); return 1; } diff --git a/cmd/timeout.c b/cmd/timeout.c @@ -142,7 +142,7 @@ main(int argc, char *argv[]) usage(); return 1; case '?': - fprintf(stderr, "timeout: error: Unrecognised option: '-%c'\n", optopt); + if(!got_long_opt) fprintf(stderr, "timeout: error: Unrecognised option: '-%c'\n", optopt); usage(); return 1; } diff --git a/cmd/touch.c b/cmd/touch.c @@ -209,7 +209,7 @@ main(int argc, char *argv[]) fprintf(stderr, "touch: error: Missing operand for option: '-%c'\n", optopt); return 1; case '?': - fprintf(stderr, "touch: error: Unrecognised option: '-%c'\n", optopt); + if(!got_long_opt) fprintf(stderr, "touch: error: Unrecognised option: '-%c'\n", optopt); return 1; } } diff --git a/cmd/tr.c b/cmd/tr.c @@ -97,7 +97,7 @@ main(int argc, char *argv[]) usage(); return 1; case '?': - fprintf(stderr, "%s: error: Unhandled option '-%c'\n", argv0, optopt); + if(!got_long_opt) fprintf(stderr, "%s: error: Unhandled option '-%c'\n", argv0, optopt); usage(); return 1; default: diff --git a/cmd/truncate.c b/cmd/truncate.c @@ -73,7 +73,7 @@ main(int argc, char *argv[]) usage(); return 1; case '?': - fprintf(stderr, "truncate: error: Unrecognised option: '-%c'\n", optopt); + if(!got_long_opt) fprintf(stderr, "truncate: error: Unrecognised option: '-%c'\n", optopt); usage(); return 1; default: diff --git a/cmd/uname.c b/cmd/uname.c @@ -80,7 +80,7 @@ main(int argc, char *argv[]) usage(); return 1; case '?': - fprintf(stderr, "uname: error: Unrecognised option: '-%c'\n", optopt); + if(!got_long_opt) fprintf(stderr, "uname: error: Unrecognised option: '-%c'\n", optopt); usage(); return 1; default: diff --git a/cmd/uniq.c b/cmd/uniq.c @@ -99,7 +99,7 @@ main(int argc, char *argv[]) fprintf(stderr, "%s: error: Option '-%c' requires an operand\n", argv0, optopt); return 1; case '?': - fprintf(stderr, "%s: error: Unhandled option '-%c'\n", argv0, optopt); + if(!got_long_opt) fprintf(stderr, "%s: error: Unhandled option '-%c'\n", argv0, optopt); return 1; default: fprintf(stderr, "%s: error: Unhandled getopt case '%c'\n", argv0, c); diff --git a/cmd/wc.c b/cmd/wc.c @@ -262,7 +262,7 @@ main(int argc, char *argv[]) usage(); return 1; case '?': - fprintf(stderr, "%s: error: Unrecognised option: '-%c'\n", argv0, optopt); + if(!got_long_opt) fprintf(stderr, "%s: error: Unrecognised option: '-%c'\n", argv0, optopt); usage(); return 1; default: diff --git a/cmd/which.c b/cmd/which.c @@ -38,7 +38,7 @@ main(int argc, char *argv[]) opt_s = true; break; case '?': - fprintf(stderr, "which: error: Unrecognised option: '-%c'\n", optopt); + if(!got_long_opt) fprintf(stderr, "which: error: Unrecognised option: '-%c'\n", optopt); return 1; default: abort(); diff --git a/test-cmd/cat.sh b/test-cmd/cat.sh @@ -48,4 +48,5 @@ t --exit=1 enoent /var/empty/e/no/ent "cat: error: Failed opening file '/var/emp t_file doubledash "$WD/inputs/all_bytes" -- "$WD/inputs/all_bytes" t --exit=1 tripledash "--- $WD/inputs/all_bytes" "cat: error: Long options unsupported: '---' +Usage: cat [-u] [files ...] "