blob: 10fb01ebf9840bcf73f557d7379611f84def0f5c [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 *
Rob Landleyba502172005-09-11 23:45:28 +00007 * Licensed under GPL v2, 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
Rob Landleyba502172005-09-11 23:45:28 +000016#define READLINK_FLAG_f (1 << 0)
Ned Luddc6fbed52004-12-08 16:47:28 +000017
Mark Whitley872138d2000-10-09 18:56:47 +000018int readlink_main(int argc, char **argv)
19{
Rob Landleyba502172005-09-11 23:45:28 +000020 char *buf;
Rob Landley07e42dc2005-09-17 11:13:25 +000021 unsigned long opt = ENABLE_FEATURE_READLINK_FOLLOW ?
22 bb_getopt_ulflags(argc, argv, "f") : 0;
Mark Whitley8a633262001-04-30 18:17:00 +000023
Rob Landley07e42dc2005-09-17 11:13:25 +000024 if (argc != (ENABLE_FEATURE_READLINK_FOLLOW ? optind + 1 : 2))
25 bb_show_usage();
Mark Whitley872138d2000-10-09 18:56:47 +000026
Rob Landley07e42dc2005-09-17 11:13:25 +000027 if (opt & READLINK_FLAG_f)
Rob Landley5371a952006-05-17 04:09:14 +000028 buf = realpath(argv[optind], bb_common_bufsiz1);
Rob Landleyba502172005-09-11 23:45:28 +000029 else
Rob Landley07e42dc2005-09-17 11:13:25 +000030 buf = xreadlink(argv[ENABLE_FEATURE_READLINK_FOLLOW ? optind : 1]);
Ned Luddc6fbed52004-12-08 16:47:28 +000031
Eric Andersen28355a32001-05-07 17:48:28 +000032 if (!buf)
33 return EXIT_FAILURE;
Mark Whitley872138d2000-10-09 18:56:47 +000034 puts(buf);
Rob Landleyb7128c62005-09-11 01:05:30 +000035
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +000036 if (ENABLE_FEATURE_CLEAN_UP && buf != bb_common_bufsiz1)
37 free(buf);
Mark Whitley872138d2000-10-09 18:56:47 +000038
39 return EXIT_SUCCESS;
40}