From 4126cfc55c61088318c8e09824b4a0a3d567e1a5 Mon Sep 17 00:00:00 2001 From: "Kyle J. McKay" Date: Sun, 22 Aug 2021 13:12:10 -0700 Subject: [PATCH] readlink.c: support the -n option If there are two arguments instead of one and the first argument is exactly '-n' then omit the trailing '\n' from a successful result. The '-n' option seems to be pretty much the one and only universally supported option. Signed-off-by: Kyle J. McKay --- src/readlink.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/readlink.c b/src/readlink.c index 7b9be46..4cd7d63 100644 --- a/src/readlink.c +++ b/src/readlink.c @@ -1,7 +1,7 @@ /* readlink.c - readlink(2) access from command line -Copyright (C) 2017 Kyle J. McKay. +Copyright (C) 2017,2021 Kyle J. McKay. All rights reserved. Permission is hereby granted, free of charge, to any person obtaining a copy @@ -32,16 +32,27 @@ SOFTWARE. static char buf[MIN_PATH_MAX]; -int main(int argc, char *argv[]) +/* readlink [-n] filename */ +int main(int argc, char **argv) { ssize_t len; + int opt_n = 0; - if (argc != 2) + if (argc < 2 || argc > 3) return 1; + if (argc == 3) { + if (argv[1][0] != '-' || argv[1][1] != 'n' || argv[1][2] != '\0') + return 1; + opt_n = 1; + ++argv; + } len = readlink(argv[1], buf, sizeof(buf) - 1); if (len <= 0 || len >= (ssize_t)sizeof(buf)) return 1; buf[len] = 0; - puts(buf); + if (opt_n) + fputs(buf, stdout); + else + puts(buf); return 0; } -- 2.11.4.GIT