Mark Whitley | 872138d | 2000-10-09 18:56:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Mini readlink implementation for busybox |
| 4 | * |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 5 | * Copyright (C) 2000,2001 Matt Kraai <kraai@alumni.carnegiemellon.edu> |
Mark Whitley | 872138d | 2000-10-09 18:56:47 +0000 | [diff] [blame] | 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Mark Whitley | 872138d | 2000-10-09 18:56:47 +0000 | [diff] [blame] | 8 | */ |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 9 | |
| 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 Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 15 | //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 Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 20 | #include "libbb.h" |
| 21 | |
Denys Vlasenko | a99aa6e | 2009-06-12 13:16:21 +0200 | [diff] [blame] | 22 | /* |
| 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 | * |
| 42 | * bbox supports: -f -n -v (fully), -q -s (accepts but ignores) |
| 43 | */ |
| 44 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 45 | int readlink_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 46 | int readlink_main(int argc UNUSED_PARAM, char **argv) |
Mark Whitley | 872138d | 2000-10-09 18:56:47 +0000 | [diff] [blame] | 47 | { |
Rob Landley | ba50217 | 2005-09-11 23:45:28 +0000 | [diff] [blame] | 48 | char *buf; |
Denis Vlasenko | 456fa6c | 2006-10-20 18:36:55 +0000 | [diff] [blame] | 49 | char *fname; |
Mark Whitley | 8a63326 | 2001-04-30 18:17:00 +0000 | [diff] [blame] | 50 | |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 51 | IF_FEATURE_READLINK_FOLLOW( |
Denis Vlasenko | 456fa6c | 2006-10-20 18:36:55 +0000 | [diff] [blame] | 52 | unsigned opt; |
| 53 | /* We need exactly one non-option argument. */ |
| 54 | opt_complementary = "=1"; |
Denys Vlasenko | a99aa6e | 2009-06-12 13:16:21 +0200 | [diff] [blame] | 55 | opt = getopt32(argv, "fnvsq"); |
Denis Vlasenko | 456fa6c | 2006-10-20 18:36:55 +0000 | [diff] [blame] | 56 | fname = argv[optind]; |
| 57 | ) |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 58 | IF_NOT_FEATURE_READLINK_FOLLOW( |
Denis Vlasenko | 456fa6c | 2006-10-20 18:36:55 +0000 | [diff] [blame] | 59 | const unsigned opt = 0; |
| 60 | if (argc != 2) bb_show_usage(); |
| 61 | fname = argv[1]; |
| 62 | ) |
Mark Whitley | 872138d | 2000-10-09 18:56:47 +0000 | [diff] [blame] | 63 | |
Denis Vlasenko | 456fa6c | 2006-10-20 18:36:55 +0000 | [diff] [blame] | 64 | /* compat: coreutils readlink reports errors silently via exit code */ |
Denys Vlasenko | a99aa6e | 2009-06-12 13:16:21 +0200 | [diff] [blame] | 65 | if (!(opt & 4)) /* not -v */ |
| 66 | logmode = LOGMODE_NONE; |
Denis Vlasenko | 456fa6c | 2006-10-20 18:36:55 +0000 | [diff] [blame] | 67 | |
Denys Vlasenko | a99aa6e | 2009-06-12 13:16:21 +0200 | [diff] [blame] | 68 | if (opt & 1) { /* -f */ |
Jeremie Koenig | b175462 | 2010-05-27 15:32:19 +0200 | [diff] [blame] | 69 | buf = xmalloc_realpath(fname); |
Denis Vlasenko | 456fa6c | 2006-10-20 18:36:55 +0000 | [diff] [blame] | 70 | } else { |
Denis Vlasenko | 6ca0444 | 2007-02-11 16:19:28 +0000 | [diff] [blame] | 71 | buf = xmalloc_readlink_or_warn(fname); |
Denis Vlasenko | 456fa6c | 2006-10-20 18:36:55 +0000 | [diff] [blame] | 72 | } |
Ned Ludd | c6fbed5 | 2004-12-08 16:47:28 +0000 | [diff] [blame] | 73 | |
Eric Andersen | 28355a3 | 2001-05-07 17:48:28 +0000 | [diff] [blame] | 74 | if (!buf) |
| 75 | return EXIT_FAILURE; |
Denys Vlasenko | a99aa6e | 2009-06-12 13:16:21 +0200 | [diff] [blame] | 76 | printf((opt & 2) ? "%s" : "%s\n", buf); |
Rob Landley | b7128c6 | 2005-09-11 01:05:30 +0000 | [diff] [blame] | 77 | |
Jeremie Koenig | b175462 | 2010-05-27 15:32:19 +0200 | [diff] [blame] | 78 | if (ENABLE_FEATURE_CLEAN_UP) |
Bernhard Reutner-Fischer | e15d757 | 2006-06-02 20:56:16 +0000 | [diff] [blame] | 79 | free(buf); |
Mark Whitley | 872138d | 2000-10-09 18:56:47 +0000 | [diff] [blame] | 80 | |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 81 | fflush_stdout_and_exit(EXIT_SUCCESS); |
Mark Whitley | 872138d | 2000-10-09 18:56:47 +0000 | [diff] [blame] | 82 | } |