Hello, Watching the slcon talks[0], I heard that a realpath utility was low-hanging fruit which was wanted. I wrote a very simple utility, which is mostly just a wrapper around realpath(). I did not implement any of the non-trivial gnu flags. I don't use them, but I'm not sure how useful they are to others.
Please find a patch attached. Feedback is welcome, I hope this is useful. -Brad [0] http://suckless.org/conference/
>From 8c72624eb0508f01a860389bba841cf5b9d2c3de Mon Sep 17 00:00:00 2001 From: Brad Barden <[email protected]> Date: Sat, 14 Nov 2015 08:46:29 -0600 Subject: [PATCH] Add 'realpath' utility --- Makefile | 1 + realpath.1 | 34 ++++++++++++++++++++++++++++++++++ realpath.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+) create mode 100644 realpath.1 create mode 100644 realpath.c diff --git a/Makefile b/Makefile index c2d8266..4fae077 100644 --- a/Makefile +++ b/Makefile @@ -124,6 +124,7 @@ BIN =\ printf\ pwd\ readlink\ + realpath\ renice\ rm\ rmdir\ diff --git a/realpath.1 b/realpath.1 new file mode 100644 index 0000000..ce9710d --- /dev/null +++ b/realpath.1 @@ -0,0 +1,34 @@ +.Dd 2015-11-14 +.Dt REALPATH 1 +.Os sbase +.Sh NAME +.Nm realpath +.Nd Print absolute file or directory name +.Sh SYNOPSIS +.Nm +.Op Fl m +.Op Fl q +.Ar file +.Op file ... +.Sh DESCRIPTION +.Nm +prints the absolute canonical path for each given file or directory. All path +components must exist, unless the +.Fl m +flag is given. +.Sh OPTIONS +.Bl -tag -width Ds +.It Fl m +Do not treat a missing file, directory, or directories as an error. +.It Fl q +Suppress error messages. Exit status will indicate if any errors were +encountered. +.El +.Sh EXIT STATUS +.Bl -tag -width Ds +.It 0 +All paths were successfully resolved. +.It 1 +An error was encountered resolving at least one of the given paths. +.Sh SEE ALSO +.Xr realpath 3 diff --git a/realpath.c b/realpath.c new file mode 100644 index 0000000..5f9a892 --- /dev/null +++ b/realpath.c @@ -0,0 +1,50 @@ +/* See LICENSE file for copyright and license details. */ +#include <errno.h> +#include <limits.h> +#include <stdlib.h> +#include <stdio.h> +#include <string.h> + +#include "util.h" + +static int haderrors = 0; +static int mflag = 0; +static int qflag = 0; + +static void +usage(void) +{ + eprintf("usage: %s [-mq] file [file ...]\n", argv0); +} + +int +main(int argc, char *argv[]) +{ + int i; + char rp[PATH_MAX]; + + ARGBEGIN { + case 'm': + mflag = 1; + break; + case 'q': + qflag = 1; + break; + default: + usage(); + } ARGEND + + if (!argc) + usage(); + + for (i=0; i<argc; i++) { + if (realpath(argv[i], rp) == NULL && !(errno == ENOENT && mflag)) { + if (!qflag) + weprintf("'%s':", argv[i]); + ++haderrors; + } else { + printf("%s\n", rp); + } + } + return(haderrors ? 1 : 0); +} -- 2.3.6
