blob: 9536d3200a6e9b533bb320718ca4e518fa2a9608 [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
16int readlink_main(int argc, char **argv)
17{
Rob Landleyba502172005-09-11 23:45:28 +000018 char *buf;
Denis Vlasenko456fa6c2006-10-20 18:36:55 +000019 char *fname;
Mark Whitley8a633262001-04-30 18:17:00 +000020
Denis Vlasenko456fa6c2006-10-20 18:36:55 +000021 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 Whitley872138d2000-10-09 18:56:47 +000033
Denis Vlasenko456fa6c2006-10-20 18:36:55 +000034 /* 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 Luddc6fbed52004-12-08 16:47:28 +000042
Eric Andersen28355a32001-05-07 17:48:28 +000043 if (!buf)
44 return EXIT_FAILURE;
Mark Whitley872138d2000-10-09 18:56:47 +000045 puts(buf);
Rob Landleyb7128c62005-09-11 01:05:30 +000046
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +000047 if (ENABLE_FEATURE_CLEAN_UP && buf != bb_common_bufsiz1)
48 free(buf);
Mark Whitley872138d2000-10-09 18:56:47 +000049
Denis Vlasenko456fa6c2006-10-20 18:36:55 +000050 bb_fflush_stdout_and_exit(EXIT_SUCCESS);
Mark Whitley872138d2000-10-09 18:56:47 +000051}