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 | * |
Bernhard Reutner-Fischer | 7fee0c4 | 2006-09-13 16:39:19 +0000 | [diff] [blame] | 7 | * Licensed under GPL v2 or later, see file LICENSE in this tarball for details. |
Mark Whitley | 872138d | 2000-10-09 18:56:47 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Ned Ludd | c6fbed5 | 2004-12-08 16:47:28 +0000 | [diff] [blame] | 10 | #include <getopt.h> |
Mark Whitley | 872138d | 2000-10-09 18:56:47 +0000 | [diff] [blame] | 11 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 12 | #include "libbb.h" |
| 13 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 14 | int readlink_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Mark Whitley | 872138d | 2000-10-09 18:56:47 +0000 | [diff] [blame] | 15 | int readlink_main(int argc, char **argv) |
| 16 | { |
Rob Landley | ba50217 | 2005-09-11 23:45:28 +0000 | [diff] [blame] | 17 | char *buf; |
Denis Vlasenko | 456fa6c | 2006-10-20 18:36:55 +0000 | [diff] [blame] | 18 | char *fname; |
Mark Whitley | 8a63326 | 2001-04-30 18:17:00 +0000 | [diff] [blame] | 19 | |
Denis Vlasenko | 456fa6c | 2006-10-20 18:36:55 +0000 | [diff] [blame] | 20 | USE_FEATURE_READLINK_FOLLOW( |
| 21 | unsigned opt; |
| 22 | /* We need exactly one non-option argument. */ |
| 23 | opt_complementary = "=1"; |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 24 | opt = getopt32(argv, "f"); |
Denis Vlasenko | 456fa6c | 2006-10-20 18:36:55 +0000 | [diff] [blame] | 25 | fname = argv[optind]; |
| 26 | ) |
| 27 | SKIP_FEATURE_READLINK_FOLLOW( |
| 28 | const unsigned opt = 0; |
| 29 | if (argc != 2) bb_show_usage(); |
| 30 | fname = argv[1]; |
| 31 | ) |
Mark Whitley | 872138d | 2000-10-09 18:56:47 +0000 | [diff] [blame] | 32 | |
Denis Vlasenko | 456fa6c | 2006-10-20 18:36:55 +0000 | [diff] [blame] | 33 | /* compat: coreutils readlink reports errors silently via exit code */ |
| 34 | logmode = LOGMODE_NONE; |
| 35 | |
| 36 | if (opt) { |
| 37 | buf = realpath(fname, bb_common_bufsiz1); |
| 38 | } else { |
Denis Vlasenko | 6ca0444 | 2007-02-11 16:19:28 +0000 | [diff] [blame] | 39 | buf = xmalloc_readlink_or_warn(fname); |
Denis Vlasenko | 456fa6c | 2006-10-20 18:36:55 +0000 | [diff] [blame] | 40 | } |
Ned Ludd | c6fbed5 | 2004-12-08 16:47:28 +0000 | [diff] [blame] | 41 | |
Eric Andersen | 28355a3 | 2001-05-07 17:48:28 +0000 | [diff] [blame] | 42 | if (!buf) |
| 43 | return EXIT_FAILURE; |
Mark Whitley | 872138d | 2000-10-09 18:56:47 +0000 | [diff] [blame] | 44 | puts(buf); |
Rob Landley | b7128c6 | 2005-09-11 01:05:30 +0000 | [diff] [blame] | 45 | |
Denis Vlasenko | 74324c8 | 2007-06-04 10:16:52 +0000 | [diff] [blame] | 46 | if (ENABLE_FEATURE_CLEAN_UP && !opt) |
Bernhard Reutner-Fischer | e15d757 | 2006-06-02 20:56:16 +0000 | [diff] [blame] | 47 | free(buf); |
Mark Whitley | 872138d | 2000-10-09 18:56:47 +0000 | [diff] [blame] | 48 | |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 49 | fflush_stdout_and_exit(EXIT_SUCCESS); |
Mark Whitley | 872138d | 2000-10-09 18:56:47 +0000 | [diff] [blame] | 50 | } |