[1765] | 1 | /* vi: set sw=4 ts=4: */
|
---|
| 2 | /*
|
---|
| 3 | * Mini readlink implementation for busybox
|
---|
| 4 | *
|
---|
| 5 | * Copyright (C) 2000,2001 Matt Kraai <kraai@alumni.carnegiemellon.edu>
|
---|
| 6 | *
|
---|
[2725] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
---|
[1765] | 8 | */
|
---|
[3232] | 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"
|
---|
| 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 |
|
---|
[2725] | 20 | #include "libbb.h"
|
---|
[1765] | 21 |
|
---|
[2725] | 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 | */
|
---|
[1765] | 44 |
|
---|
[2725] | 45 | int readlink_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
---|
| 46 | int readlink_main(int argc UNUSED_PARAM, char **argv)
|
---|
[1765] | 47 | {
|
---|
| 48 | char *buf;
|
---|
| 49 | char *fname;
|
---|
| 50 |
|
---|
[2725] | 51 | IF_FEATURE_READLINK_FOLLOW(
|
---|
[1765] | 52 | unsigned opt;
|
---|
| 53 | /* We need exactly one non-option argument. */
|
---|
| 54 | opt_complementary = "=1";
|
---|
[2725] | 55 | opt = getopt32(argv, "fnvsq");
|
---|
[1765] | 56 | fname = argv[optind];
|
---|
| 57 | )
|
---|
[2725] | 58 | IF_NOT_FEATURE_READLINK_FOLLOW(
|
---|
[1765] | 59 | const unsigned opt = 0;
|
---|
| 60 | if (argc != 2) bb_show_usage();
|
---|
| 61 | fname = argv[1];
|
---|
| 62 | )
|
---|
| 63 |
|
---|
| 64 | /* compat: coreutils readlink reports errors silently via exit code */
|
---|
[2725] | 65 | if (!(opt & 4)) /* not -v */
|
---|
| 66 | logmode = LOGMODE_NONE;
|
---|
[1765] | 67 |
|
---|
[2725] | 68 | if (opt & 1) { /* -f */
|
---|
| 69 | buf = xmalloc_realpath(fname);
|
---|
[1765] | 70 | } else {
|
---|
| 71 | buf = xmalloc_readlink_or_warn(fname);
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | if (!buf)
|
---|
| 75 | return EXIT_FAILURE;
|
---|
[2725] | 76 | printf((opt & 2) ? "%s" : "%s\n", buf);
|
---|
[1765] | 77 |
|
---|
[2725] | 78 | if (ENABLE_FEATURE_CLEAN_UP)
|
---|
[1765] | 79 | free(buf);
|
---|
| 80 |
|
---|
| 81 | fflush_stdout_and_exit(EXIT_SUCCESS);
|
---|
| 82 | }
|
---|