blob: b6e389a21fa0418b2ea84c1610d3dc1525a9e2f1 [file] [log] [blame]
Mark Whitley872138d2000-10-09 18:56:47 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Mini readlink implementation for busybox
4 *
Eric Andersenbdfd0d72001-10-24 05:00:29 +00005 * Copyright (C) 2000,2001 Matt Kraai <kraai@alumni.carnegiemellon.edu>
Mark Whitley872138d2000-10-09 18:56:47 +00006 *
Bernhard Reutner-Fischer7fee0c42006-09-13 16:39:19 +00007 * Licensed under GPL v2 or later, see file LICENSE in this tarball for details.
Mark Whitley872138d2000-10-09 18:56:47 +00008 */
9
Ned Luddc6fbed52004-12-08 16:47:28 +000010#include <getopt.h>
Mark Whitley872138d2000-10-09 18:56:47 +000011
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000012#include "libbb.h"
13
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000014int readlink_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Mark Whitley872138d2000-10-09 18:56:47 +000015int readlink_main(int argc, char **argv)
16{
Rob Landleyba502172005-09-11 23:45:28 +000017 char *buf;
Denis Vlasenko456fa6c2006-10-20 18:36:55 +000018 char *fname;
Mark Whitley8a633262001-04-30 18:17:00 +000019
Denis Vlasenko456fa6c2006-10-20 18:36:55 +000020 USE_FEATURE_READLINK_FOLLOW(
21 unsigned opt;
22 /* We need exactly one non-option argument. */
23 opt_complementary = "=1";
Denis Vlasenkofe7cd642007-08-18 15:32:12 +000024 opt = getopt32(argv, "f");
Denis Vlasenko456fa6c2006-10-20 18:36:55 +000025 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 Whitley872138d2000-10-09 18:56:47 +000032
Denis Vlasenko456fa6c2006-10-20 18:36:55 +000033 /* 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 Vlasenko6ca04442007-02-11 16:19:28 +000039 buf = xmalloc_readlink_or_warn(fname);
Denis Vlasenko456fa6c2006-10-20 18:36:55 +000040 }
Ned Luddc6fbed52004-12-08 16:47:28 +000041
Eric Andersen28355a32001-05-07 17:48:28 +000042 if (!buf)
43 return EXIT_FAILURE;
Mark Whitley872138d2000-10-09 18:56:47 +000044 puts(buf);
Rob Landleyb7128c62005-09-11 01:05:30 +000045
Denis Vlasenko74324c82007-06-04 10:16:52 +000046 if (ENABLE_FEATURE_CLEAN_UP && !opt)
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +000047 free(buf);
Mark Whitley872138d2000-10-09 18:56:47 +000048
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000049 fflush_stdout_and_exit(EXIT_SUCCESS);
Mark Whitley872138d2000-10-09 18:56:47 +000050}