blob: b8e327d11863fcd5025b0dc26d06f91d9cedae32 [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 */
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +01009//config:config READLINK
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020010//config: bool "readlink (3.6 kb)"
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010011//config: default y
12//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020013//config: This program reads a symbolic link and returns the name
14//config: of the file it points to
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010015//config:
16//config:config FEATURE_READLINK_FOLLOW
17//config: bool "Enable canonicalization by following all symlinks (-f)"
18//config: default y
19//config: depends on READLINK
20//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020021//config: Enable the readlink option (-f).
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010022
Denys Vlasenko39194f02017-08-03 19:00:01 +020023//applet:IF_READLINK(APPLET_NOFORK(readlink, readlink, BB_DIR_USR_BIN, BB_SUID_DROP, readlink))
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010024
25//kbuild:lib-$(CONFIG_READLINK) += readlink.o
Pere Orga34425382011-03-31 14:43:25 +020026
27//usage:#define readlink_trivial_usage
28//usage: IF_FEATURE_READLINK_FOLLOW("[-fnv] ") "FILE"
29//usage:#define readlink_full_usage "\n\n"
30//usage: "Display the value of a symlink"
31//usage: IF_FEATURE_READLINK_FOLLOW( "\n"
Pere Orga34425382011-03-31 14:43:25 +020032//usage: "\n -f Canonicalize by following all symlinks"
33//usage: "\n -n Don't add newline"
34//usage: "\n -v Verbose"
35//usage: )
36
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000037#include "libbb.h"
38
Denys Vlasenkoa99aa6e2009-06-12 13:16:21 +020039/*
40 * # readlink --version
41 * readlink (GNU coreutils) 6.10
42 * # readlink --help
43 * -f, --canonicalize
44 * canonicalize by following every symlink in
45 * every component of the given name recursively;
46 * all but the last component must exist
47 * -e, --canonicalize-existing
48 * canonicalize by following every symlink in
49 * every component of the given name recursively,
50 * all components must exist
51 * -m, --canonicalize-missing
52 * canonicalize by following every symlink in
53 * every component of the given name recursively,
54 * without requirements on components existence
55 * -n, --no-newline do not output the trailing newline
56 * -q, --quiet, -s, --silent suppress most error messages
57 * -v, --verbose report error messages
58 *
Mike Frysinger1b49c252013-03-12 11:38:03 -040059 * bbox supports: -f (partially) -n -v (fully), -q -s (accepts but ignores)
60 * Note: we export the -f flag, but our -f behaves like coreutils' -e.
61 * Unfortunately, there isn't a C lib function we can leverage to get this
62 * behavior which means we'd have to implement the full stack ourselves :(.
Denys Vlasenkoa99aa6e2009-06-12 13:16:21 +020063 */
64
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000065int readlink_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000066int readlink_main(int argc UNUSED_PARAM, char **argv)
Mark Whitley872138d2000-10-09 18:56:47 +000067{
Rob Landleyba502172005-09-11 23:45:28 +000068 char *buf;
Denis Vlasenko456fa6c2006-10-20 18:36:55 +000069 char *fname;
Mark Whitley8a633262001-04-30 18:17:00 +000070
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000071 IF_FEATURE_READLINK_FOLLOW(
Denis Vlasenko456fa6c2006-10-20 18:36:55 +000072 unsigned opt;
73 /* We need exactly one non-option argument. */
Denys Vlasenko22542ec2017-08-08 21:55:02 +020074 opt = getopt32(argv, "^" "fnvsq" "\0" "=1");
Denis Vlasenko456fa6c2006-10-20 18:36:55 +000075 fname = argv[optind];
76 )
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000077 IF_NOT_FEATURE_READLINK_FOLLOW(
Denis Vlasenko456fa6c2006-10-20 18:36:55 +000078 const unsigned opt = 0;
79 if (argc != 2) bb_show_usage();
80 fname = argv[1];
81 )
Mark Whitley872138d2000-10-09 18:56:47 +000082
Denis Vlasenko456fa6c2006-10-20 18:36:55 +000083 /* compat: coreutils readlink reports errors silently via exit code */
Denys Vlasenkoa99aa6e2009-06-12 13:16:21 +020084 if (!(opt & 4)) /* not -v */
85 logmode = LOGMODE_NONE;
Denis Vlasenko456fa6c2006-10-20 18:36:55 +000086
Denys Vlasenko39194f02017-08-03 19:00:01 +020087 /* NOFORK: only one alloc is allowed; must free */
Denys Vlasenkoa99aa6e2009-06-12 13:16:21 +020088 if (opt & 1) { /* -f */
Jeremie Koenigb1754622010-05-27 15:32:19 +020089 buf = xmalloc_realpath(fname);
Denis Vlasenko456fa6c2006-10-20 18:36:55 +000090 } else {
Denis Vlasenko6ca04442007-02-11 16:19:28 +000091 buf = xmalloc_readlink_or_warn(fname);
Denis Vlasenko456fa6c2006-10-20 18:36:55 +000092 }
Ned Luddc6fbed52004-12-08 16:47:28 +000093
Eric Andersen28355a32001-05-07 17:48:28 +000094 if (!buf)
95 return EXIT_FAILURE;
Denys Vlasenkoa99aa6e2009-06-12 13:16:21 +020096 printf((opt & 2) ? "%s" : "%s\n", buf);
Denys Vlasenko39194f02017-08-03 19:00:01 +020097 free(buf);
Mark Whitley872138d2000-10-09 18:56:47 +000098
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000099 fflush_stdout_and_exit(EXIT_SUCCESS);
Mark Whitley872138d2000-10-09 18:56:47 +0000100}