blob: 8f9cfe4313e1a39e9e3dee97d8698e2eccb32c28 [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
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +000010#include "busybox.h"
Mark Whitley872138d2000-10-09 18:56:47 +000011#include <errno.h>
12#include <unistd.h>
Eric Andersened3ef502001-01-27 08:24:39 +000013#include <stdlib.h>
Ned Luddc6fbed52004-12-08 16:47:28 +000014#include <getopt.h>
Mark Whitley872138d2000-10-09 18:56:47 +000015
Denis Vlasenko06af2162007-02-03 17:28:39 +000016int readlink_main(int argc, char **argv);
Mark Whitley872138d2000-10-09 18:56:47 +000017int readlink_main(int argc, char **argv)
18{
Rob Landleyba502172005-09-11 23:45:28 +000019 char *buf;
Denis Vlasenko456fa6c2006-10-20 18:36:55 +000020 char *fname;
Mark Whitley8a633262001-04-30 18:17:00 +000021
Denis Vlasenko456fa6c2006-10-20 18:36:55 +000022 USE_FEATURE_READLINK_FOLLOW(
23 unsigned opt;
24 /* We need exactly one non-option argument. */
25 opt_complementary = "=1";
26 opt = getopt32(argc, argv, "f");
27 fname = argv[optind];
28 )
29 SKIP_FEATURE_READLINK_FOLLOW(
30 const unsigned opt = 0;
31 if (argc != 2) bb_show_usage();
32 fname = argv[1];
33 )
Mark Whitley872138d2000-10-09 18:56:47 +000034
Denis Vlasenko456fa6c2006-10-20 18:36:55 +000035 /* compat: coreutils readlink reports errors silently via exit code */
36 logmode = LOGMODE_NONE;
37
38 if (opt) {
39 buf = realpath(fname, bb_common_bufsiz1);
40 } else {
41 buf = xreadlink(fname);
42 }
Ned Luddc6fbed52004-12-08 16:47:28 +000043
Eric Andersen28355a32001-05-07 17:48:28 +000044 if (!buf)
45 return EXIT_FAILURE;
Mark Whitley872138d2000-10-09 18:56:47 +000046 puts(buf);
Rob Landleyb7128c62005-09-11 01:05:30 +000047
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +000048 if (ENABLE_FEATURE_CLEAN_UP && buf != bb_common_bufsiz1)
49 free(buf);
Mark Whitley872138d2000-10-09 18:56:47 +000050
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000051 fflush_stdout_and_exit(EXIT_SUCCESS);
Mark Whitley872138d2000-10-09 18:56:47 +000052}