On 6/13/2018 3:11 AM, Stuart Henderson wrote:
> On 2018/06/12 22:49, Brian Callahan wrote:
>> The realpath utility resolves all symbolic links, extra `/' characters
>> and references to /./ and /../ in the path. If path is absent, the
>> current working directory (`.') is assumed.
>>
>> It is a port of the realpath utility from DragonFly BSD.
>> ---
>>
>> I occasionally run into shell scripts that expect the utility.
> ..
>> OK?
> Port reads ok but realpath is just "readlink -f" (*some* implementations
> use a different exit code for a nonexistent file in a directory that does
> exist, but that can't be relied on).
>
> How widely is it actually used? It's barely any extra code on top of
> readlink.c so if it's useful enough to warrant adding to ports, maybe
> it's also useful enough for base as this diff does (manpage left out for
> now).

I have no opinion on whether this is a port or added directly to
readlink; it's fine by me either way. I don't think it's all that common
but it is a thing I see from time to time.

I'll note though that our implementation of readlink appears to be
different than the others in one meaningful way--they iterate over argv
whereas ours prints argv[0] (after getopt processing). The other
realpath's also iterate over argv. I'm not sure I've ever seen realpath
called with more than one argument in the wild though.

There would also be no -q flag, which again I've never seen in the wild.

~Brian

> Index: Makefile
> ===================================================================
> RCS file: /cvs/src/usr.bin/readlink/Makefile,v
> retrieving revision 1.2
> diff -u -p -r1.2 Makefile
> --- Makefile  18 Aug 1997 20:30:59 -0000      1.2
> +++ Makefile  13 Jun 2018 06:51:08 -0000
> @@ -2,4 +2,6 @@
>  
>  PROG=        readlink
>  
> +LINKS=       ${BINDIR}/readlink ${BINDIR}/realpath
> +
>  .include <bsd.prog.mk>
> 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        13 Jun 2018 06:51:08 -0000
> @@ -40,14 +40,21 @@ static void       usage(void);
>  int
>  main(int argc, char *argv[])
>  {
> -     char buf[PATH_MAX];
> +     extern char *__progname;
> +     char buf[PATH_MAX], *optstr;
>       int n, ch, nflag = 0, fflag = 0;
>       extern int optind;
>  
>       if (pledge("stdio rpath", NULL) == -1)
>               err(1, "pledge");
>  
> -     while ((ch = getopt(argc, argv, "fn")) != -1)
> +     if (strcmp(__progname, "realpath") == 0) {
> +             optstr = "";
> +             fflag = 1;
> +     } else
> +             optstr = "fn";
> +
> +     while ((ch = getopt(argc, argv, optstr)) != -1)
>               switch (ch) {
>               case 'f':
>                       fflag = 1;
> @@ -90,6 +97,9 @@ main(int argc, char *argv[])
>  static void
>  usage(void)
>  {
> -     (void)fprintf(stderr, "usage: readlink [-fn] file\n");
> +     if (strcmp(__progname, "realpath") == 0)
> +             (void)fprintf(stderr, "usage: realpath file\n");
> +     else
> +             (void)fprintf(stderr, "usage: readlink [-fn] file\n");
>       exit(1);
>  }
>


Reply via email to