blob: 2ed5e2cac05fd39df8e5954babd2d4b80ada8898 [file] [log] [blame]
Mark Whitley872138d2000-10-09 18:56:47 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Mini readlink implementation for busybox
4 *
Eric Andersenbdfd0d72001-10-24 05:00:29 +00005 * Copyright (C) 2000,2001 Matt Kraai <kraai@alumni.carnegiemellon.edu>
Mark Whitley872138d2000-10-09 18:56:47 +00006 *
Bernhard Reutner-Fischer7fee0c42006-09-13 16:39:19 +00007 * Licensed under GPL v2 or later, see file LICENSE in this tarball for details.
Mark Whitley872138d2000-10-09 18:56:47 +00008 */
Denis Vlasenkob6adbf12007-05-26 19:00:18 +00009#include "libbb.h"
10
Denys Vlasenkoa99aa6e2009-06-12 13:16:21 +020011/*
12 * # readlink --version
13 * readlink (GNU coreutils) 6.10
14 * # readlink --help
15 * -f, --canonicalize
16 * canonicalize by following every symlink in
17 * every component of the given name recursively;
18 * all but the last component must exist
19 * -e, --canonicalize-existing
20 * canonicalize by following every symlink in
21 * every component of the given name recursively,
22 * all components must exist
23 * -m, --canonicalize-missing
24 * canonicalize by following every symlink in
25 * every component of the given name recursively,
26 * without requirements on components existence
27 * -n, --no-newline do not output the trailing newline
28 * -q, --quiet, -s, --silent suppress most error messages
29 * -v, --verbose report error messages
30 *
31 * bbox supports: -f -n -v (fully), -q -s (accepts but ignores)
32 */
33
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000034int readlink_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000035int readlink_main(int argc UNUSED_PARAM, char **argv)
Mark Whitley872138d2000-10-09 18:56:47 +000036{
Rob Landleyba502172005-09-11 23:45:28 +000037 char *buf;
Denis Vlasenko456fa6c2006-10-20 18:36:55 +000038 char *fname;
Mark Whitley8a633262001-04-30 18:17:00 +000039
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000040 IF_FEATURE_READLINK_FOLLOW(
Denis Vlasenko456fa6c2006-10-20 18:36:55 +000041 unsigned opt;
42 /* We need exactly one non-option argument. */
43 opt_complementary = "=1";
Denys Vlasenkoa99aa6e2009-06-12 13:16:21 +020044 opt = getopt32(argv, "fnvsq");
Denis Vlasenko456fa6c2006-10-20 18:36:55 +000045 fname = argv[optind];
46 )
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000047 IF_NOT_FEATURE_READLINK_FOLLOW(
Denis Vlasenko456fa6c2006-10-20 18:36:55 +000048 const unsigned opt = 0;
49 if (argc != 2) bb_show_usage();
50 fname = argv[1];
51 )
Mark Whitley872138d2000-10-09 18:56:47 +000052
Denis Vlasenko456fa6c2006-10-20 18:36:55 +000053 /* compat: coreutils readlink reports errors silently via exit code */
Denys Vlasenkoa99aa6e2009-06-12 13:16:21 +020054 if (!(opt & 4)) /* not -v */
55 logmode = LOGMODE_NONE;
Denis Vlasenko456fa6c2006-10-20 18:36:55 +000056
Denys Vlasenkoa99aa6e2009-06-12 13:16:21 +020057 if (opt & 1) { /* -f */
Jeremie Koenigb1754622010-05-27 15:32:19 +020058 buf = xmalloc_realpath(fname);
Denis Vlasenko456fa6c2006-10-20 18:36:55 +000059 } else {
Denis Vlasenko6ca04442007-02-11 16:19:28 +000060 buf = xmalloc_readlink_or_warn(fname);
Denis Vlasenko456fa6c2006-10-20 18:36:55 +000061 }
Ned Luddc6fbed52004-12-08 16:47:28 +000062
Eric Andersen28355a32001-05-07 17:48:28 +000063 if (!buf)
64 return EXIT_FAILURE;
Denys Vlasenkoa99aa6e2009-06-12 13:16:21 +020065 printf((opt & 2) ? "%s" : "%s\n", buf);
Rob Landleyb7128c62005-09-11 01:05:30 +000066
Jeremie Koenigb1754622010-05-27 15:32:19 +020067 if (ENABLE_FEATURE_CLEAN_UP)
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +000068 free(buf);
Mark Whitley872138d2000-10-09 18:56:47 +000069
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000070 fflush_stdout_and_exit(EXIT_SUCCESS);
Mark Whitley872138d2000-10-09 18:56:47 +000071}