2013-06-15 10:17:57 -04:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2015-02-14 15:02:41 -05:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2015-03-20 17:09:47 -04:00
|
|
|
#include <libgen.h>
|
2013-06-15 10:17:57 -04:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2014-11-13 12:29:30 -05:00
|
|
|
|
2013-06-15 10:17:57 -04:00
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2015-03-20 17:09:47 -04:00
|
|
|
eprintf("usage: %s [-e | -f | -m] [-n] path\n", argv0);
|
2013-06-15 10:17:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2015-02-11 03:55:17 -05:00
|
|
|
struct stat st;
|
2015-03-20 17:09:47 -04:00
|
|
|
ssize_t n;
|
|
|
|
int nflag = 0, mefflag = 0;
|
|
|
|
char buf1[PATH_MAX], buf2[PATH_MAX], arg[PATH_MAX],
|
|
|
|
*p, *slash, *prefix, *lp, *b = buf1;
|
2013-06-15 10:17:57 -04:00
|
|
|
|
|
|
|
ARGBEGIN {
|
2013-08-15 05:55:21 -04:00
|
|
|
case 'm':
|
2015-02-11 03:55:17 -05:00
|
|
|
case 'e':
|
2013-06-15 10:17:57 -04:00
|
|
|
case 'f':
|
2015-02-11 03:55:17 -05:00
|
|
|
mefflag = ARGC();
|
2013-06-15 10:17:57 -04:00
|
|
|
break;
|
|
|
|
case 'n':
|
2014-11-13 15:24:47 -05:00
|
|
|
nflag = 1;
|
2013-06-15 10:17:57 -04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
|
|
|
|
|
|
|
if (argc != 1)
|
|
|
|
usage();
|
|
|
|
|
2015-03-20 17:09:47 -04:00
|
|
|
if (strlen(argv[0]) >= PATH_MAX)
|
2014-12-04 07:14:20 -05:00
|
|
|
eprintf("path too long\n");
|
2013-06-15 10:17:57 -04:00
|
|
|
|
2015-02-11 03:55:17 -05:00
|
|
|
switch (mefflag) {
|
2015-02-20 07:27:57 -05:00
|
|
|
case 'm':
|
2015-03-20 17:09:47 -04:00
|
|
|
slash = strchr(argv[0], '/');
|
|
|
|
prefix = (slash == argv[0]) ? "/" : (!slash) ? "./" : "";
|
|
|
|
|
|
|
|
estrlcpy(arg, prefix, sizeof(arg));
|
2015-03-17 06:24:49 -04:00
|
|
|
estrlcat(arg, argv[0], sizeof(arg));
|
2015-03-20 17:09:47 -04:00
|
|
|
|
|
|
|
for (lp = "", p = arg + (argv[0][0] == '/'); *p; p++) {
|
|
|
|
if (*p != '/')
|
|
|
|
continue;
|
2015-02-20 07:27:57 -05:00
|
|
|
*p = '\0';
|
2015-02-11 03:55:17 -05:00
|
|
|
if (!realpath(arg, b)) {
|
2015-02-20 07:27:57 -05:00
|
|
|
*p = '/';
|
|
|
|
goto mdone;
|
|
|
|
}
|
2015-03-20 17:09:47 -04:00
|
|
|
b = (b == buf1) ? buf2 : buf1;
|
2015-02-20 07:27:57 -05:00
|
|
|
lp = p;
|
2015-03-20 17:09:47 -04:00
|
|
|
*p = '/';
|
2015-02-20 07:27:57 -05:00
|
|
|
}
|
|
|
|
if (!realpath(arg, b)) {
|
2015-02-11 03:55:17 -05:00
|
|
|
mdone:
|
2015-03-20 17:09:47 -04:00
|
|
|
b = (b == buf1) ? buf2 : buf1;
|
|
|
|
estrlcat(b, lp, sizeof(arg));
|
2015-02-20 07:27:57 -05:00
|
|
|
}
|
|
|
|
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;
|
2015-03-20 17:09:47 -04:00
|
|
|
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;
|
2015-02-20 07:27:57 -05:00
|
|
|
default:
|
|
|
|
if ((n = readlink(argv[0], b, PATH_MAX - 1)) < 0)
|
|
|
|
eprintf("readlink %s:", argv[0]);
|
|
|
|
b[n] = '\0';
|
2013-06-15 10:17:57 -04:00
|
|
|
}
|
2015-03-20 17:09:47 -04:00
|
|
|
fputs(b, stdout);
|
2013-06-15 10:17:57 -04:00
|
|
|
if (!nflag)
|
|
|
|
putchar('\n');
|
|
|
|
|
2014-10-02 18:46:04 -04:00
|
|
|
return 0;
|
2013-06-15 10:17:57 -04:00
|
|
|
}
|