Mark Whitley | 872138d | 2000-10-09 18:56:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
2 | /* | ||||
3 | * Mini readlink implementation for busybox | ||||
4 | * | ||||
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 5 | * Copyright (C) 2000,2001 Matt Kraai <kraai@alumni.carnegiemellon.edu> |
Mark Whitley | 872138d | 2000-10-09 18:56:47 +0000 | [diff] [blame] | 6 | * |
Rob Landley | ba50217 | 2005-09-11 23:45:28 +0000 | [diff] [blame] | 7 | * Licensed under GPL v2, see file LICENSE in this tarball for details. |
Mark Whitley | 872138d | 2000-10-09 18:56:47 +0000 | [diff] [blame] | 8 | */ |
9 | |||||
Mark Whitley | 872138d | 2000-10-09 18:56:47 +0000 | [diff] [blame] | 10 | #include <errno.h> |
11 | #include <unistd.h> | ||||
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 12 | #include <stdlib.h> |
Ned Ludd | c6fbed5 | 2004-12-08 16:47:28 +0000 | [diff] [blame] | 13 | #include <getopt.h> |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 14 | #include "busybox.h" |
Mark Whitley | 872138d | 2000-10-09 18:56:47 +0000 | [diff] [blame] | 15 | |
Rob Landley | ba50217 | 2005-09-11 23:45:28 +0000 | [diff] [blame] | 16 | #define READLINK_FLAG_f (1 << 0) |
Ned Ludd | c6fbed5 | 2004-12-08 16:47:28 +0000 | [diff] [blame] | 17 | |
Mark Whitley | 872138d | 2000-10-09 18:56:47 +0000 | [diff] [blame] | 18 | int readlink_main(int argc, char **argv) |
19 | { | ||||
Rob Landley | ba50217 | 2005-09-11 23:45:28 +0000 | [diff] [blame] | 20 | char *buf; |
21 | unsigned long opt = bb_getopt_ulflags(argc, argv, | ||||
22 | ENABLE_FEATURE_READLINK_FOLLOW ? "f" : ""); | ||||
Mark Whitley | 8a63326 | 2001-04-30 18:17:00 +0000 | [diff] [blame] | 23 | |
Ned Ludd | c6fbed5 | 2004-12-08 16:47:28 +0000 | [diff] [blame] | 24 | if (optind + 1 != argc) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 25 | bb_show_usage(); |
Mark Whitley | 872138d | 2000-10-09 18:56:47 +0000 | [diff] [blame] | 26 | |
Rob Landley | ba50217 | 2005-09-11 23:45:28 +0000 | [diff] [blame] | 27 | if (ENABLE_FEATURE_READLINK_FOLLOW && (opt & READLINK_FLAG_f)) |
28 | buf = realpath(argv[optind], NULL); | ||||
29 | else | ||||
Ned Ludd | c6fbed5 | 2004-12-08 16:47:28 +0000 | [diff] [blame] | 30 | buf = xreadlink(argv[optind]); |
31 | |||||
Eric Andersen | 28355a3 | 2001-05-07 17:48:28 +0000 | [diff] [blame] | 32 | if (!buf) |
33 | return EXIT_FAILURE; | ||||
Mark Whitley | 872138d | 2000-10-09 18:56:47 +0000 | [diff] [blame] | 34 | puts(buf); |
Rob Landley | b7128c6 | 2005-09-11 01:05:30 +0000 | [diff] [blame] | 35 | |
36 | if (ENABLE_FEATURE_CLEAN_UP) free(buf); | ||||
Mark Whitley | 872138d | 2000-10-09 18:56:47 +0000 | [diff] [blame] | 37 | |
38 | return EXIT_SUCCESS; | ||||
39 | } |