blob: d73ef4ddb568e218a2a3a1951e7ce34573f70351 [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 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Mark Whitley872138d2000-10-09 18:56:47 +00008 */
Pere Orga34425382011-03-31 14:43:25 +02009
10//usage:#define readlink_trivial_usage
11//usage: IF_FEATURE_READLINK_FOLLOW("[-fnv] ") "FILE"
12//usage:#define readlink_full_usage "\n\n"
13//usage: "Display the value of a symlink"
14//usage: IF_FEATURE_READLINK_FOLLOW( "\n"
Pere Orga34425382011-03-31 14:43:25 +020015//usage: "\n -f Canonicalize by following all symlinks"
16//usage: "\n -n Don't add newline"
17//usage: "\n -v Verbose"
18//usage: )
19
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000020#include "libbb.h"
21
Denys Vlasenkoa99aa6e2009-06-12 13:16:21 +020022/*
23 * # readlink --version
24 * readlink (GNU coreutils) 6.10
25 * # readlink --help
26 * -f, --canonicalize
27 * canonicalize by following every symlink in
28 * every component of the given name recursively;
29 * all but the last component must exist
30 * -e, --canonicalize-existing
31 * canonicalize by following every symlink in
32 * every component of the given name recursively,
33 * all components must exist
34 * -m, --canonicalize-missing
35 * canonicalize by following every symlink in
36 * every component of the given name recursively,
37 * without requirements on components existence
38 * -n, --no-newline do not output the trailing newline
39 * -q, --quiet, -s, --silent suppress most error messages
40 * -v, --verbose report error messages
41 *
maxwen27116ba2015-08-14 21:41:28 +020042 * bbox supports: -f (partially) -n -v (fully), -q -s (accepts but ignores)
43 * Note: we export the -f flag, but our -f behaves like coreutils' -e.
44 * Unfortunately, there isn't a C lib function we can leverage to get this
45 * behavior which means we'd have to implement the full stack ourselves :(.
Denys Vlasenkoa99aa6e2009-06-12 13:16:21 +020046 */
47
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000048int readlink_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000049int readlink_main(int argc UNUSED_PARAM, char **argv)
Mark Whitley872138d2000-10-09 18:56:47 +000050{
Rob Landleyba502172005-09-11 23:45:28 +000051 char *buf;
Denis Vlasenko456fa6c2006-10-20 18:36:55 +000052 char *fname;
Mark Whitley8a633262001-04-30 18:17:00 +000053
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000054 IF_FEATURE_READLINK_FOLLOW(
Denis Vlasenko456fa6c2006-10-20 18:36:55 +000055 unsigned opt;
56 /* We need exactly one non-option argument. */
57 opt_complementary = "=1";
Denys Vlasenkoa99aa6e2009-06-12 13:16:21 +020058 opt = getopt32(argv, "fnvsq");
Denis Vlasenko456fa6c2006-10-20 18:36:55 +000059 fname = argv[optind];
60 )
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000061 IF_NOT_FEATURE_READLINK_FOLLOW(
Denis Vlasenko456fa6c2006-10-20 18:36:55 +000062 const unsigned opt = 0;
63 if (argc != 2) bb_show_usage();
64 fname = argv[1];
65 )
Mark Whitley872138d2000-10-09 18:56:47 +000066
Denis Vlasenko456fa6c2006-10-20 18:36:55 +000067 /* compat: coreutils readlink reports errors silently via exit code */
Denys Vlasenkoa99aa6e2009-06-12 13:16:21 +020068 if (!(opt & 4)) /* not -v */
69 logmode = LOGMODE_NONE;
Denis Vlasenko456fa6c2006-10-20 18:36:55 +000070
Denys Vlasenkoa99aa6e2009-06-12 13:16:21 +020071 if (opt & 1) { /* -f */
Jeremie Koenigb1754622010-05-27 15:32:19 +020072 buf = xmalloc_realpath(fname);
Denis Vlasenko456fa6c2006-10-20 18:36:55 +000073 } else {
Denis Vlasenko6ca04442007-02-11 16:19:28 +000074 buf = xmalloc_readlink_or_warn(fname);
Denis Vlasenko456fa6c2006-10-20 18:36:55 +000075 }
Ned Luddc6fbed52004-12-08 16:47:28 +000076
Eric Andersen28355a32001-05-07 17:48:28 +000077 if (!buf)
78 return EXIT_FAILURE;
Denys Vlasenkoa99aa6e2009-06-12 13:16:21 +020079 printf((opt & 2) ? "%s" : "%s\n", buf);
Rob Landleyb7128c62005-09-11 01:05:30 +000080
Jeremie Koenigb1754622010-05-27 15:32:19 +020081 if (ENABLE_FEATURE_CLEAN_UP)
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +000082 free(buf);
Mark Whitley872138d2000-10-09 18:56:47 +000083
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000084 fflush_stdout_and_exit(EXIT_SUCCESS);
Mark Whitley872138d2000-10-09 18:56:47 +000085}