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