blob: 1ab94595a9164c069f19e91f79bd597c17f694ac [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>
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000011#include "libbb.h"
12
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000013int readlink_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Mark Whitley872138d2000-10-09 18:56:47 +000014int readlink_main(int argc, char **argv)
15{
Rob Landleyba502172005-09-11 23:45:28 +000016 char *buf;
Denis Vlasenko456fa6c2006-10-20 18:36:55 +000017 char *fname;
Denis Vlasenkob4f608d2007-11-10 01:31:19 +000018 char pathbuf[PATH_MAX];
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) {
Denis Vlasenkob4f608d2007-11-10 01:31:19 +000037 buf = realpath(fname, pathbuf);
Denis Vlasenko456fa6c2006-10-20 18:36:55 +000038 } 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}