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 USE_REALPATH(NEWTOY(realpath, "<1", TOYFLAG_USR|TOYFLAG_BIN))
7 
8 config READLINK
9   bool "readlink"
10   default y
11   help
12     usage: readlink FILE...
13 
14     With no options, show what symlink points to, return error if not symlink.
15 
16     Options for producing canonical paths (all symlinks/./.. resolved):
17 
18     -e	Canonical path to existing entry (fail if missing)
19     -f	Full path (fail if directory missing)
20     -m	Ignore missing entries, show where it would be
21     -n	No trailing newline
22     -q	Quiet (no output, just error code)
23 
24 config REALPATH
25   bool "realpath"
26   default y
27   help
28     usage: realpath FILE...
29 
30     Display the canonical absolute pathname
31 */
32 
33 #define FOR_readlink
34 #define FORCE_FLAGS
35 #include "toys.h"
36 
readlink_main(void)37 void readlink_main(void)
38 {
39   char **arg, *s;
40 
41   for (arg = toys.optargs; *arg; arg++) {
42     // Calculating full canonical path?
43     // Take advantage of flag positions to calculate m = -1, f = 0, e = 1
44     if (toys.optflags & (FLAG_f|FLAG_e|FLAG_m))
45       s = xabspath(*arg, (toys.optflags&(FLAG_f|FLAG_e))-1);
46     else s = xreadlink(*arg);
47 
48     if (s) {
49       if (!FLAG(q)) xprintf(FLAG(n) ? "%s" : "%s\n", s);
50       if (CFG_TOYBOX_FREE) free(s);
51     } else toys.exitval = 1;
52   }
53 }
54 
realpath_main(void)55 void realpath_main(void)
56 {
57   toys.optflags = FLAG_f;
58   readlink_main();
59 }
60