Audit readlink(1)
1) Properly document e, f and m-flags in the manpage. 2) Clear up the code for the m-flag-handling. Add idiomatic '/'-path-traversal as already seen in mkdir(1). 3) Unwrap the SWAP_BUF()-macro. 4) BUGFIX: Actually handle the f-flag properly. Only resolve the dirname and append the basename later. 5) Use fputs() instead of printf("%s", ...).
This commit is contained in:
parent
e6c20fe367
commit
a531865fe5
2
README
2
README
|
@ -55,7 +55,7 @@ The following tools are implemented ('*' == finished, '#' == UTF-8 support,
|
||||||
=*| printenv non-posix none
|
=*| printenv non-posix none
|
||||||
#*| printf yes none
|
#*| printf yes none
|
||||||
=*| pwd yes none
|
=*| pwd yes none
|
||||||
= readlink non-posix none
|
=*| readlink non-posix none
|
||||||
=*| renice yes none
|
=*| renice yes none
|
||||||
=*| rm yes none (-i)
|
=*| rm yes none (-i)
|
||||||
=*| rmdir yes none
|
=*| rmdir yes none
|
||||||
|
|
31
readlink.1
31
readlink.1
|
@ -1,4 +1,4 @@
|
||||||
.Dd January 31, 2015
|
.Dd March 20, 2015
|
||||||
.Dt READLINK 1
|
.Dt READLINK 1
|
||||||
.Os sbase
|
.Os sbase
|
||||||
.Sh NAME
|
.Sh NAME
|
||||||
|
@ -6,30 +6,27 @@
|
||||||
.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 fn
|
.Op Fl e | Fl f | Fl m
|
||||||
.Ar name
|
.Op Fl n
|
||||||
|
.Ar path
|
||||||
.Sh DESCRIPTION
|
.Sh DESCRIPTION
|
||||||
If
|
|
||||||
.Ar name
|
|
||||||
is a symbolic link,
|
|
||||||
.Nm
|
.Nm
|
||||||
writes its target to stdout.
|
writes the target of
|
||||||
If
|
.Ar path ,
|
||||||
.Fl f
|
if it is a symbolic link, to stdout.
|
||||||
is not set and
|
If not,
|
||||||
.Ar name
|
|
||||||
is not a symbolic link,
|
|
||||||
.Nm
|
.Nm
|
||||||
exits with a nonzero exit code without printing anything.
|
exits with a non-zero return value.
|
||||||
.Sh OPTIONS
|
.Sh OPTIONS
|
||||||
.Bl -tag -width Ds
|
.Bl -tag -width Ds
|
||||||
.It Fl f
|
.It Fl e | Fl f | Fl m
|
||||||
Canonicalize
|
Canonicalize
|
||||||
.Ar name ,
|
.Ar name ,
|
||||||
which does not need to be a symlink,
|
which needn't be a symlink,
|
||||||
by recursively following every symlink in its 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 output the trailing newline.
|
Do not print the terminating newline.
|
||||||
.El
|
.El
|
||||||
.Sh SEE ALSO
|
.Sh SEE ALSO
|
||||||
.Xr readlink 2 ,
|
.Xr readlink 2 ,
|
||||||
|
|
55
readlink.c
55
readlink.c
|
@ -1,6 +1,7 @@
|
||||||
/* See LICENSE file for copyright and license details. */
|
/* See LICENSE file for copyright and license details. */
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
|
||||||
|
#include <libgen.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
@ -11,18 +12,17 @@
|
||||||
static void
|
static void
|
||||||
usage(void)
|
usage(void)
|
||||||
{
|
{
|
||||||
eprintf("usage: %s [-efmn] name\n", argv0);
|
eprintf("usage: %s [-e | -f | -m] [-n] path\n", argv0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
main(int argc, char *argv[])
|
main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
char buf1[PATH_MAX], buf2[PATH_MAX], arg[PATH_MAX];
|
|
||||||
int nflag = 0;
|
|
||||||
int mefflag = 0;
|
|
||||||
ssize_t n;
|
|
||||||
struct stat st;
|
struct stat st;
|
||||||
char *p = arg, *lp = NULL, *b = buf1;
|
ssize_t n;
|
||||||
|
int nflag = 0, mefflag = 0;
|
||||||
|
char buf1[PATH_MAX], buf2[PATH_MAX], arg[PATH_MAX],
|
||||||
|
*p, *slash, *prefix, *lp, *b = buf1;
|
||||||
|
|
||||||
ARGBEGIN {
|
ARGBEGIN {
|
||||||
case 'm':
|
case 'm':
|
||||||
|
@ -40,57 +40,54 @@ main(int argc, char *argv[])
|
||||||
if (argc != 1)
|
if (argc != 1)
|
||||||
usage();
|
usage();
|
||||||
|
|
||||||
if (strlen(argv[0]) > PATH_MAX - 1)
|
if (strlen(argv[0]) >= PATH_MAX)
|
||||||
eprintf("path too long\n");
|
eprintf("path too long\n");
|
||||||
|
|
||||||
#define SWAP_BUF() (b = (b == buf1 ? buf2 : buf1));
|
|
||||||
switch (mefflag) {
|
switch (mefflag) {
|
||||||
case 'm':
|
case 'm':
|
||||||
if (argv[0][0] == '/') { /* case when path is on '/' */
|
slash = strchr(argv[0], '/');
|
||||||
arg[0] = '/';
|
prefix = (slash == argv[0]) ? "/" : (!slash) ? "./" : "";
|
||||||
arg[1] = '\0';
|
|
||||||
p++;
|
estrlcpy(arg, prefix, sizeof(arg));
|
||||||
} else if (!strchr(argv[0], '/')) { /* relative path */
|
|
||||||
arg[0] = '.';
|
|
||||||
arg[1] = '/';
|
|
||||||
arg[2] = '\0';
|
|
||||||
} else
|
|
||||||
arg[0] = '\0';
|
|
||||||
estrlcat(arg, argv[0], sizeof(arg));
|
estrlcat(arg, argv[0], sizeof(arg));
|
||||||
while ((p = strchr(p, '/'))) {
|
|
||||||
|
for (lp = "", p = arg + (argv[0][0] == '/'); *p; p++) {
|
||||||
|
if (*p != '/')
|
||||||
|
continue;
|
||||||
*p = '\0';
|
*p = '\0';
|
||||||
if (!realpath(arg, b)) {
|
if (!realpath(arg, b)) {
|
||||||
*p = '/';
|
*p = '/';
|
||||||
goto mdone;
|
goto mdone;
|
||||||
}
|
}
|
||||||
SWAP_BUF();
|
b = (b == buf1) ? buf2 : buf1;
|
||||||
lp = p;
|
lp = p;
|
||||||
*p++ = '/';
|
*p = '/';
|
||||||
}
|
}
|
||||||
if (!realpath(arg, b)) {
|
if (!realpath(arg, b)) {
|
||||||
mdone:
|
mdone:
|
||||||
SWAP_BUF();
|
b = (b == buf1) ? buf2 : buf1;
|
||||||
if (lp) {
|
|
||||||
/* drop the extra '/' on root */
|
|
||||||
lp += (argv[0][0] == '/' &&
|
|
||||||
lp - arg == 1);
|
|
||||||
estrlcat(b, lp, sizeof(arg));
|
estrlcat(b, lp, sizeof(arg));
|
||||||
}
|
}
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case 'e':
|
case 'e':
|
||||||
if (stat(argv[0], &st) < 0)
|
if (stat(argv[0], &st) < 0)
|
||||||
eprintf("stat %s:", argv[0]);
|
eprintf("stat %s:", argv[0]);
|
||||||
case 'f': /* fallthrough */
|
|
||||||
if (!realpath(argv[0], b))
|
if (!realpath(argv[0], b))
|
||||||
eprintf("realpath %s:", argv[0]);
|
eprintf("realpath %s:", argv[0]);
|
||||||
break;
|
break;
|
||||||
|
case 'f':
|
||||||
|
p = dirname(estrdup(argv[0]));
|
||||||
|
if (!realpath(p, b))
|
||||||
|
eprintf("realpath %s:", p);
|
||||||
|
estrlcat(b, "/", sizeof(arg));
|
||||||
|
estrlcat(b, basename(estrdup(argv[0])), sizeof(arg));
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
if ((n = readlink(argv[0], b, PATH_MAX - 1)) < 0)
|
if ((n = readlink(argv[0], b, PATH_MAX - 1)) < 0)
|
||||||
eprintf("readlink %s:", argv[0]);
|
eprintf("readlink %s:", argv[0]);
|
||||||
b[n] = '\0';
|
b[n] = '\0';
|
||||||
}
|
}
|
||||||
printf("%s", b);
|
fputs(b, stdout);
|
||||||
if (!nflag)
|
if (!nflag)
|
||||||
putchar('\n');
|
putchar('\n');
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user