[PATCH] Add 'realpath' utility

From: Brad Barden <brad_AT_13os.net>
Date: Sat, 14 Nov 2015 08:46:29 -0600

---
 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
_AT_@ -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
_AT_@ -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
_AT_@ -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
--BOKacYhQ+x31HxR3--
Received on Mon Sep 17 2001 - 00:00:00 CEST

This archive was generated by hypermail 2.3.0 : Mon Nov 16 2015 - 17:00:12 CET