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