Hi
readlink is explicitly documented to silently exit 1 if run without -f,
and GNU readlink behaves the same way. I doubt that should change.
On Tue, Apr 04, 2017 at 04:40:19PM -0500, Scott Cheloha wrote:
> This patch replaces a custom error message in readlink.c with err(3).
> The custom message and call to strlen(3) aren't needed because
> readlink(2) checks the length of the argument string and sets errno
> to ENAMETOOLONG if it is too long.
>
> Dropping strlen(3) lets us drop string.h.
>
> Using err(3) also lets us trivially report the myriad ways readlink(2)
> can fail. At the moment we just exit 1, which can be misleading during
> interactive use.
>
> While here, do other miscellaneous style(9)-type changes.
>
> Any takers?
>
> --
> Scott Cheloha
>
> P.S. returning from main() is preferred over exit(3), right?
>
> Index: readlink.c
> ===================================================================
> RCS file: /cvs/src/usr.bin/readlink/readlink.c,v
> retrieving revision 1.27
> diff -u -p -r1.27 readlink.c
> --- readlink.c 9 Oct 2015 01:37:08 -0000 1.27
> +++ readlink.c 4 Apr 2017 21:10:41 -0000
> @@ -32,17 +32,17 @@
> #include <limits.h>
> #include <stdio.h>
> #include <stdlib.h>
> -#include <string.h>
> #include <unistd.h>
>
> -static void usage(void);
> +static __dead void usage(void);
>
> int
> main(int argc, char *argv[])
> {
> char buf[PATH_MAX];
> - int n, ch, nflag = 0, fflag = 0;
> - extern int optind;
> + int ch, fflag, n, nflag;
> +
> + fflag = nflag = 0;
>
> if (pledge("stdio rpath", NULL) == -1)
> err(1, "pledge");
> @@ -64,30 +64,26 @@ main(int argc, char *argv[])
> if (argc != 1)
> usage();
>
> - n = strlen(argv[0]);
> - if (n > PATH_MAX - 1) {
> - fprintf(stderr,
> - "readlink: filename longer than PATH_MAX-1 (%d)\n",
> - PATH_MAX - 1);
> - exit(1);
> - }
> -
> if (fflag) {
> if (realpath(argv[0], buf) == NULL)
> err(1, "%s", argv[0]);
> } else {
> - if ((n = readlink(argv[0], buf, sizeof buf-1)) < 0)
> - exit(1);
> + if ((n = readlink(argv[0], buf, sizeof(buf) - 1)) == -1) {
> + if (errno == EINVAL)
> + errx(1, "%s: Not a symbolic link", argv[0]);
> + else
> + err(1, "%s", argv[0]);
> + }
> buf[n] = '\0';
> }
>
> printf("%s", buf);
> if (!nflag)
> putchar('\n');
> - exit(0);
> + return 0;
> }
>
> -static void
> +static __dead void
> usage(void)
> {
> (void)fprintf(stderr, "usage: readlink [-fn] file\n");
>