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 | |||||
Bernhard Reutner-Fischer | e15d757 | 2006-06-02 20:56:16 +0000 | [diff] [blame] | 10 | #include "busybox.h" |
Mark Whitley | 872138d | 2000-10-09 18:56:47 +0000 | [diff] [blame] | 11 | #include <errno.h> |
12 | #include <unistd.h> | ||||
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 13 | #include <stdlib.h> |
Ned Ludd | c6fbed5 | 2004-12-08 16:47:28 +0000 | [diff] [blame] | 14 | #include <getopt.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; |
Rob Landley | 07e42dc | 2005-09-17 11:13:25 +0000 | [diff] [blame] | 21 | unsigned long opt = ENABLE_FEATURE_READLINK_FOLLOW ? |
22 | bb_getopt_ulflags(argc, argv, "f") : 0; | ||||
Mark Whitley | 8a63326 | 2001-04-30 18:17:00 +0000 | [diff] [blame] | 23 | |
Rob Landley | 07e42dc | 2005-09-17 11:13:25 +0000 | [diff] [blame] | 24 | if (argc != (ENABLE_FEATURE_READLINK_FOLLOW ? optind + 1 : 2)) |
25 | bb_show_usage(); | ||||
Mark Whitley | 872138d | 2000-10-09 18:56:47 +0000 | [diff] [blame] | 26 | |
Rob Landley | 07e42dc | 2005-09-17 11:13:25 +0000 | [diff] [blame] | 27 | if (opt & READLINK_FLAG_f) |
Rob Landley | 5371a95 | 2006-05-17 04:09:14 +0000 | [diff] [blame] | 28 | buf = realpath(argv[optind], bb_common_bufsiz1); |
Rob Landley | ba50217 | 2005-09-11 23:45:28 +0000 | [diff] [blame] | 29 | else |
Rob Landley | 07e42dc | 2005-09-17 11:13:25 +0000 | [diff] [blame] | 30 | buf = xreadlink(argv[ENABLE_FEATURE_READLINK_FOLLOW ? optind : 1]); |
Ned Ludd | c6fbed5 | 2004-12-08 16:47:28 +0000 | [diff] [blame] | 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 | |
Bernhard Reutner-Fischer | e15d757 | 2006-06-02 20:56:16 +0000 | [diff] [blame] | 36 | if (ENABLE_FEATURE_CLEAN_UP && buf != bb_common_bufsiz1) |
37 | free(buf); | ||||
Mark Whitley | 872138d | 2000-10-09 18:56:47 +0000 | [diff] [blame] | 38 | |
39 | return EXIT_SUCCESS; | ||||
40 | } |