sbase/readlink.c

96 lines
1.7 KiB
C
Raw Normal View History

/* See LICENSE file for copyright and license details. */
2015-02-14 20:02:41 +00:00
#include <sys/stat.h>
#include <libgen.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "util.h"
static void
usage(void)
{
eprintf("usage: %s [-e | -f | -m] [-n] path\n", argv0);
}
int
main(int argc, char *argv[])
{
2015-02-11 08:55:17 +00:00
struct stat st;
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 {
case 'm':
2015-02-11 08:55:17 +00:00
case 'e':
case 'f':
2015-02-11 08:55:17 +00:00
mefflag = ARGC();
break;
case 'n':
nflag = 1;
break;
default:
usage();
} ARGEND;
if (argc != 1)
usage();
if (strlen(argv[0]) >= PATH_MAX)
eprintf("path too long\n");
2015-02-11 08:55:17 +00:00
switch (mefflag) {
case 'm':
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';
2015-02-11 08:55:17 +00:00
if (!realpath(arg, b)) {
*p = '/';
goto mdone;
}
b = (b == buf1) ? buf2 : buf1;
lp = p;
*p = '/';
}
if (!realpath(arg, b)) {
2015-02-11 08:55:17 +00:00
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]);
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:
if ((n = readlink(argv[0], b, PATH_MAX - 1)) < 0)
eprintf("readlink %s:", argv[0]);
b[n] = '\0';
}
fputs(b, stdout);
if (!nflag)
putchar('\n');
2014-10-02 22:46:04 +00:00
return 0;
}