blob: 3042b83ec8d03a2795cba51cec62a62a6319dd59 [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
Mark Whitley872138d2000-10-09 18:56:47 +000010#include <errno.h>
11#include <unistd.h>
Eric Andersened3ef502001-01-27 08:24:39 +000012#include <stdlib.h>
Ned Luddc6fbed52004-12-08 16:47:28 +000013#include <getopt.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000014#include "busybox.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;
21 unsigned long opt = bb_getopt_ulflags(argc, argv,
22 ENABLE_FEATURE_READLINK_FOLLOW ? "f" : "");
Mark Whitley8a633262001-04-30 18:17:00 +000023
Ned Luddc6fbed52004-12-08 16:47:28 +000024 if (optind + 1 != argc)
Manuel Novoa III cad53642003-03-19 09:13:01 +000025 bb_show_usage();
Mark Whitley872138d2000-10-09 18:56:47 +000026
Rob Landleyba502172005-09-11 23:45:28 +000027 if (ENABLE_FEATURE_READLINK_FOLLOW && (opt & READLINK_FLAG_f))
28 buf = realpath(argv[optind], NULL);
29 else
Ned Luddc6fbed52004-12-08 16:47:28 +000030 buf = xreadlink(argv[optind]);
31
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
36 if (ENABLE_FEATURE_CLEAN_UP) free(buf);
Mark Whitley872138d2000-10-09 18:56:47 +000037
38 return EXIT_SUCCESS;
39}