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