blob: 0c45e1234a06b3c25996946780de1f361cc030ec [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
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000010#include "libbb.h"
11
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000012int readlink_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenko68404f12008-03-17 09:00:54 +000013int readlink_main(int argc ATTRIBUTE_UNUSED, char **argv)
Mark Whitley872138d2000-10-09 18:56:47 +000014{
Rob Landleyba502172005-09-11 23:45:28 +000015 char *buf;
Denis Vlasenko456fa6c2006-10-20 18:36:55 +000016 char *fname;
Denis Vlasenkob4f608d2007-11-10 01:31:19 +000017 char pathbuf[PATH_MAX];
Mark Whitley8a633262001-04-30 18:17:00 +000018
Denis Vlasenko456fa6c2006-10-20 18:36:55 +000019 USE_FEATURE_READLINK_FOLLOW(
20 unsigned opt;
21 /* We need exactly one non-option argument. */
22 opt_complementary = "=1";
Denis Vlasenkofe7cd642007-08-18 15:32:12 +000023 opt = getopt32(argv, "f");
Denis Vlasenko456fa6c2006-10-20 18:36:55 +000024 fname = argv[optind];
25 )
26 SKIP_FEATURE_READLINK_FOLLOW(
27 const unsigned opt = 0;
28 if (argc != 2) bb_show_usage();
29 fname = argv[1];
30 )
Mark Whitley872138d2000-10-09 18:56:47 +000031
Denis Vlasenko456fa6c2006-10-20 18:36:55 +000032 /* compat: coreutils readlink reports errors silently via exit code */
33 logmode = LOGMODE_NONE;
34
35 if (opt) {
Denis Vlasenkob4f608d2007-11-10 01:31:19 +000036 buf = realpath(fname, pathbuf);
Denis Vlasenko456fa6c2006-10-20 18:36:55 +000037 } else {
Denis Vlasenko6ca04442007-02-11 16:19:28 +000038 buf = xmalloc_readlink_or_warn(fname);
Denis Vlasenko456fa6c2006-10-20 18:36:55 +000039 }
Ned Luddc6fbed52004-12-08 16:47:28 +000040
Eric Andersen28355a32001-05-07 17:48:28 +000041 if (!buf)
42 return EXIT_FAILURE;
Mark Whitley872138d2000-10-09 18:56:47 +000043 puts(buf);
Rob Landleyb7128c62005-09-11 01:05:30 +000044
Denis Vlasenko74324c82007-06-04 10:16:52 +000045 if (ENABLE_FEATURE_CLEAN_UP && !opt)
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +000046 free(buf);
Mark Whitley872138d2000-10-09 18:56:47 +000047
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000048 fflush_stdout_and_exit(EXIT_SUCCESS);
Mark Whitley872138d2000-10-09 18:56:47 +000049}