readlink: Remove broken support for -m and -e

There is no need to bloat readlink with these options.  No one has
ever used them anyway.
This commit is contained in:
sin 2015-11-16 16:28:36 +00:00
parent d24584466c
commit 11fee4c8ba
2 changed files with 17 additions and 56 deletions

View File

@ -1,4 +1,4 @@
.Dd 2015-10-08 .Dd 2015-11-16
.Dt READLINK 1 .Dt READLINK 1
.Os sbase .Os sbase
.Sh NAME .Sh NAME
@ -6,7 +6,7 @@
.Nd print symbolic link target or canonical file name .Nd print symbolic link target or canonical file name
.Sh SYNOPSIS .Sh SYNOPSIS
.Nm .Nm
.Op Fl e | Fl f | Fl m .Op Fl f
.Op Fl n .Op Fl n
.Ar path .Ar path
.Sh DESCRIPTION .Sh DESCRIPTION
@ -19,12 +19,11 @@ If not,
exits with a non-zero return value. exits with a non-zero return value.
.Sh OPTIONS .Sh OPTIONS
.Bl -tag -width Ds .Bl -tag -width Ds
.It Fl e | Fl f | Fl m .It Fl f
Canonicalize Canonicalize
.Ar name , .Ar path ,
which needn't be a symlink, which needn't be a symlink,
by recursively following every symlink in its path components. by recursively following every symlink in its path components.
All | All but the last | No path components must exist.
.It Fl n .It Fl n
Do not print the terminating newline. Do not print the terminating newline.
.El .El

View File

@ -1,34 +1,28 @@
/* See LICENSE file for copyright and license details. */ /* See LICENSE file for copyright and license details. */
#include <sys/stat.h> #include <limits.h>
#include <libgen.h>
#include <unistd.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h>
#include "util.h" #include "util.h"
static void static void
usage(void) usage(void)
{ {
eprintf("usage: %s [-e | -f | -m] [-n] path\n", argv0); eprintf("usage: %s [-fn] path\n", argv0);
} }
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
struct stat st; char buf[PATH_MAX];
ssize_t n; ssize_t n;
int nflag = 0, mefflag = 0; int nflag = 0, fflag = 0;
char buf1[PATH_MAX], buf2[PATH_MAX], arg[PATH_MAX],
*p, *slash, *prefix, *lp, *b = buf1;
ARGBEGIN { ARGBEGIN {
case 'm':
case 'e':
case 'f': case 'f':
mefflag = ARGC(); fflag = ARGC();
break; break;
case 'n': case 'n':
nflag = 1; nflag = 1;
@ -43,48 +37,16 @@ main(int argc, char *argv[])
if (strlen(argv[0]) >= PATH_MAX) if (strlen(argv[0]) >= PATH_MAX)
eprintf("path too long\n"); eprintf("path too long\n");
switch (mefflag) { if (fflag) {
case 'm': if (!realpath(argv[0], buf))
slash = strchr(argv[0], '/');
prefix = (slash == argv[0]) ? "/" : (!slash) ? "./" : "";
estrlcpy(arg, prefix, sizeof(arg));
estrlcat(arg, argv[0], sizeof(arg));
for (lp = "", p = arg + (argv[0][0] == '/'); *p; p++) {
if (*p != '/')
continue;
*p = '\0';
if (!realpath(arg, b)) {
*p = '/';
goto mdone;
}
b = (b == buf1) ? buf2 : buf1;
lp = p;
*p = '/';
}
if (!realpath(arg, b)) {
mdone:
b = (b == buf1) ? buf2 : buf1;
estrlcat(b, lp, sizeof(arg));
}
break;
case 'e':
if (stat(argv[0], &st) < 0)
eprintf("stat %s:", argv[0]);
if (!realpath(argv[0], b))
eprintf("realpath %s:", argv[0]); eprintf("realpath %s:", argv[0]);
break; } else {
case 'f': if ((n = readlink(argv[0], buf, PATH_MAX - 1)) < 0)
if (!realpath(argv[0], b))
eprintf("realpath %s:", argv[0]);
break;
default:
if ((n = readlink(argv[0], b, PATH_MAX - 1)) < 0)
eprintf("readlink %s:", argv[0]); eprintf("readlink %s:", argv[0]);
b[n] = '\0'; buf[n] = '\0';
} }
fputs(b, stdout);
fputs(buf, stdout);
if (!nflag) if (!nflag)
putchar('\n'); putchar('\n');