[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 | */
|
---|
[2725] | 9 | #include "libbb.h"
|
---|
[1765] | 10 |
|
---|
[2725] | 11 | /*
|
---|
| 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 | */
|
---|
[1765] | 33 |
|
---|
[2725] | 34 | int readlink_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
---|
| 35 | int readlink_main(int argc UNUSED_PARAM, char **argv)
|
---|
[1765] | 36 | {
|
---|
| 37 | char *buf;
|
---|
| 38 | char *fname;
|
---|
| 39 |
|
---|
[2725] | 40 | IF_FEATURE_READLINK_FOLLOW(
|
---|
[1765] | 41 | unsigned opt;
|
---|
| 42 | /* We need exactly one non-option argument. */
|
---|
| 43 | opt_complementary = "=1";
|
---|
[2725] | 44 | opt = getopt32(argv, "fnvsq");
|
---|
[1765] | 45 | fname = argv[optind];
|
---|
| 46 | )
|
---|
[2725] | 47 | IF_NOT_FEATURE_READLINK_FOLLOW(
|
---|
[1765] | 48 | const unsigned opt = 0;
|
---|
| 49 | if (argc != 2) bb_show_usage();
|
---|
| 50 | fname = argv[1];
|
---|
| 51 | )
|
---|
| 52 |
|
---|
| 53 | /* compat: coreutils readlink reports errors silently via exit code */
|
---|
[2725] | 54 | if (!(opt & 4)) /* not -v */
|
---|
| 55 | logmode = LOGMODE_NONE;
|
---|
[1765] | 56 |
|
---|
[2725] | 57 | if (opt & 1) { /* -f */
|
---|
| 58 | buf = xmalloc_realpath(fname);
|
---|
[1765] | 59 | } else {
|
---|
| 60 | buf = xmalloc_readlink_or_warn(fname);
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | if (!buf)
|
---|
| 64 | return EXIT_FAILURE;
|
---|
[2725] | 65 | printf((opt & 2) ? "%s" : "%s\n", buf);
|
---|
[1765] | 66 |
|
---|
[2725] | 67 | if (ENABLE_FEATURE_CLEAN_UP)
|
---|
[1765] | 68 | free(buf);
|
---|
| 69 |
|
---|
| 70 | fflush_stdout_and_exit(EXIT_SUCCESS);
|
---|
| 71 | }
|
---|