2013-06-15 14:17:57 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2015-02-14 20:02:41 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2013-06-15 14:17:57 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2014-11-13 17:29:30 +00:00
|
|
|
|
2013-06-15 14:17:57 +00:00
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2015-01-31 23:43:43 +00:00
|
|
|
eprintf("usage: %s [-efmn] name\n", argv0);
|
2013-06-15 14:17:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2015-02-11 08:55:17 +00:00
|
|
|
char buf1[PATH_MAX], buf2[PATH_MAX], arg[PATH_MAX];
|
2014-11-13 20:24:47 +00:00
|
|
|
int nflag = 0;
|
2015-02-11 08:55:17 +00:00
|
|
|
int mefflag = 0;
|
2013-07-09 23:40:28 +00:00
|
|
|
ssize_t n;
|
2015-02-11 08:55:17 +00:00
|
|
|
struct stat st;
|
|
|
|
char *p = arg, *lp = NULL, *b = buf1;
|
2013-06-15 14:17:57 +00:00
|
|
|
|
|
|
|
ARGBEGIN {
|
2013-08-15 09:55:21 +00:00
|
|
|
case 'm':
|
2015-02-11 08:55:17 +00:00
|
|
|
case 'e':
|
2013-06-15 14:17:57 +00:00
|
|
|
case 'f':
|
2015-02-11 08:55:17 +00:00
|
|
|
mefflag = ARGC();
|
2013-06-15 14:17:57 +00:00
|
|
|
break;
|
|
|
|
case 'n':
|
2014-11-13 20:24:47 +00:00
|
|
|
nflag = 1;
|
2013-06-15 14:17:57 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
|
|
|
|
|
|
|
if (argc != 1)
|
|
|
|
usage();
|
|
|
|
|
|
|
|
if (strlen(argv[0]) > PATH_MAX - 1)
|
2014-12-04 12:14:20 +00:00
|
|
|
eprintf("path too long\n");
|
2013-06-15 14:17:57 +00:00
|
|
|
|
2015-02-11 08:55:17 +00:00
|
|
|
#define SWAP_BUF() (b = (b == buf1 ? buf2 : buf1));
|
|
|
|
switch (mefflag) {
|
|
|
|
case 'm':
|
|
|
|
if (argv[0][0] == '/') { /* case when path is on '/' */
|
|
|
|
arg[0] = '/';
|
|
|
|
arg[1] = '\0';
|
|
|
|
p++;
|
|
|
|
} else if (!strchr(argv[0], '/')) { /* relative path */
|
|
|
|
arg[0] = '.';
|
|
|
|
arg[1] = '/';
|
|
|
|
arg[2] = '\0';
|
|
|
|
} else
|
|
|
|
arg[0] = '\0';
|
2015-02-11 11:51:57 +00:00
|
|
|
if (strlcat(arg, argv[0], PATH_MAX) >= PATH_MAX)
|
|
|
|
eprintf("path too long\n");
|
2015-02-11 08:55:17 +00:00
|
|
|
while ((p = strchr(p, '/'))) {
|
|
|
|
*p = '\0';
|
|
|
|
if (!realpath(arg, b)) {
|
|
|
|
*p = '/';
|
|
|
|
goto mdone;
|
|
|
|
}
|
|
|
|
SWAP_BUF();
|
|
|
|
lp = p;
|
|
|
|
*p++ = '/';
|
|
|
|
}
|
|
|
|
if (!realpath(arg, b)) {
|
|
|
|
mdone:
|
|
|
|
SWAP_BUF();
|
|
|
|
if (lp) {
|
|
|
|
/* drop the extra '/' on root */
|
|
|
|
lp += (argv[0][0] == '/' &&
|
|
|
|
lp - arg == 1);
|
2015-02-11 11:51:57 +00:00
|
|
|
if (strlcat(b, lp, PATH_MAX) >= PATH_MAX)
|
|
|
|
eprintf("path too long\n");
|
2015-02-11 08:55:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'e':
|
|
|
|
if (stat(argv[0], &st) < 0)
|
2015-02-11 11:58:02 +00:00
|
|
|
eprintf("stat %s:", argv[0]);
|
2015-02-11 08:55:17 +00:00
|
|
|
case 'f':
|
|
|
|
if (!realpath(argv[0], b))
|
2015-02-11 11:58:02 +00:00
|
|
|
eprintf("realpath %s:", argv[0]);
|
2015-02-11 08:55:17 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if ((n = readlink(argv[0], b, PATH_MAX - 1)) < 0)
|
2015-02-11 11:58:02 +00:00
|
|
|
eprintf("readlink %s:", argv[0]);
|
2015-02-11 08:55:17 +00:00
|
|
|
b[n] = '\0';
|
2013-06-15 14:17:57 +00:00
|
|
|
}
|
2015-02-11 08:55:17 +00:00
|
|
|
printf("%s", b);
|
2013-06-15 14:17:57 +00:00
|
|
|
if (!nflag)
|
|
|
|
putchar('\n');
|
|
|
|
|
2014-10-02 22:46:04 +00:00
|
|
|
return 0;
|
2013-06-15 14:17:57 +00:00
|
|
|
}
|