1 /* readlink.c - Return string representation of a symbolic link.
2 *
3 * Copyright 2007 Rob Landley <rob@landley.net>
4
5 USE_READLINK(NEWTOY(readlink, "<1nqmef(canonicalize)[-mef]", TOYFLAG_USR|TOYFLAG_BIN))
6
7 config READLINK
8 bool "readlink"
9 default y
10 help
11 usage: readlink FILE...
12
13 With no options, show what symlink points to, return error if not symlink.
14
15 Options for producing canonical paths (all symlinks/./.. resolved):
16
17 -e Canonical path to existing entry (fail if missing)
18 -f Full path (fail if directory missing)
19 -m Ignore missing entries, show where it would be
20 -n No trailing newline
21 -q Quiet (no output, just error code)
22 */
23
24 #define FOR_readlink
25 #include "toys.h"
26
readlink_main(void)27 void readlink_main(void)
28 {
29 char **arg, *s;
30
31 for (arg = toys.optargs; *arg; arg++) {
32 // Calculating full canonical path?
33 // Take advantage of flag positions to calculate m = -1, f = 0, e = 1
34 if (toys.optflags & (FLAG_f|FLAG_e|FLAG_m))
35 s = xabspath(*arg, (toys.optflags&(FLAG_f|FLAG_e))-1);
36 else s = xreadlink(*arg);
37
38 if (s) {
39 if (!FLAG(q)) xprintf(FLAG(n) ? "%s" : "%s\n", s);
40 if (CFG_TOYBOX_FREE) free(s);
41 } else toys.exitval = 1;
42 }
43 }
44