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, "<1vnf(canonicalize)emqz[-mef][-qv]", TOYFLAG_USR|TOYFLAG_BIN))
6 USE_REALPATH(NEWTOY(realpath, "<1(relative-base):R(relative-to):s(no-symlinks)LPemqz[-Ps][-LP][-me]", 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 error messages)
23 -z NUL instead of newline
24
25 config REALPATH
26 bool "realpath"
27 default y
28 help
29 usage: realpath [-LPemqsz] [--relative-base DIR] [-R DIR] FILE...
30
31 Display the canonical absolute pathname
32
33 -R Show ../path relative to DIR (--relative-to)
34 -L Logical path (resolve .. before symlinks)
35 -P Physical path (default)
36 -e Canonical path to existing entry (fail if missing)
37 -m Ignore missing entries, show where it would be
38 -q Quiet (no error messages)
39 -s Don't expand symlinks
40 -z NUL instead of newline
41 --relative-base If path under DIR trim off prefix
42 */
43
44 #define FOR_realpath
45 #define FORCE_FLAGS
46 #define TT this.readlink // workaround: first FOR_ doesn't match filename
47 #include "toys.h"
48
49 GLOBALS(
50 char *R, *relative_base;
51 )
52
53 // test TT.relative_base -RsmLP
54 // Trim .. out early for -s and -L. TODO: in place in the input string.
55
resolve(char * arg)56 static char *resolve(char *arg)
57 {
58 int flags = FLAG(e) ? ABS_FILE : FLAG(m) ? 0 : ABS_PATH;
59 char *s, *ss = 0, *dd = 0;
60
61 if (FLAG(s)) flags |= ABS_KEEP;
62 else if (FLAG(L)) arg = dd = xabspath(arg, ABS_KEEP);
63 if (!(s = xabspath(arg, flags)) && !FLAG(q)) perror_msg("%s", arg);
64 free(dd);
65
66 // Trim off this prefix if path under here
67
68 if (TT.relative_base) {
69 ss = s;
70 if (strstart(&ss, TT.relative_base) && (!*ss || *ss=='/')) {
71 if (*ss=='/') ss++;
72 ss = xstrdup(!*ss ? "." : ss);
73 } else ss = 0;
74 } else if (TT.R) ss = relative_path(TT.R, s, 0);
75 if (ss) {
76 free(s);
77 s = ss;
78 }
79
80 return s;
81 }
82
83 // Resolve command line arguments that can't take part in their own resolution
presolve(char ** s)84 static char *presolve(char **s)
85 {
86 char *ss = *s;
87
88 if (ss) {
89 *s = 0;
90 if (!(*s = resolve(ss))) xexit();
91 }
92
93 return ss;
94 }
95
96 // Uses realpath flag context: flags (1 = resolve, 2 = -n)
do_paths(int flags)97 static void do_paths(int flags)
98 {
99 char **arg, *s;
100
101 if (!presolve(&TT.relative_base)) presolve(&TT.R);
102
103 for (arg = toys.optargs; *arg; arg++) {
104 if (!(s = (flags&1) ? resolve(*arg) : xreadlink(*arg))) toys.exitval = 1;
105 else xprintf(((flags&2) && !arg[1]) ? "%s" : "%s%c", s, '\n'*!FLAG(z));
106 free(s);
107 }
108 }
109
realpath_main(void)110 void realpath_main(void)
111 {
112 do_paths(1);
113 }
114
115 #define FOR_readlink
116 #include "generated/flags.h"
117
118 // Convert readlink flag context to realpath (feeding in -nf separately)
readlink_main(void)119 void readlink_main(void)
120 {
121 int nf = (toys.optflags/FLAG_f)|!!(FLAG(m)|FLAG(e));
122
123 toys.optflags &= FLAG_f-1;
124 if (!FLAG(v)) toys.optflags |= FLAG_q;
125 do_paths(nf);
126 }
127