aboutsummaryrefslogtreecommitdiff
path: root/toys/other/readlink.c
blob: b237a51055a5382405fe794afcf892621d1bde28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/* readlink.c - Return string representation of a symbolic link.
 *
 * Copyright 2007 Rob Landley <rob@landley.net>

USE_READLINK(NEWTOY(readlink, "<1>1femnq[-fem]", TOYFLAG_BIN))

config READLINK
  bool "readlink"
  default n
  help
    usage: readlink FILE

    With no options, show what symlink points to, return error if not symlink.

    Options for producing cannonical paths (all symlinks/./.. resolved):

    -e	cannonical path to existing file (fail if does not exist)
    -f	cannonical path to creatable file (fail if directory does not exist)
    -m	cannonical path
    -n	no trailing newline
    -q	quiet (no output, just error code)
*/

#define FOR_readlink
#include "toys.h"

void readlink_main(void)
{
  char *s;

  // Calculating full cannonical path?

  if (toys.optflags & (FLAG_f|FLAG_e|FLAG_m)) {
    unsigned u = 0;

    if (toys.optflags & FLAG_f) u++;
    if (toys.optflags & FLAG_m) u=999999999;

    s = xabspath(*toys.optargs, u);
  } else s = xreadlink(*toys.optargs);

  if (s) {
    if (!(toys.optflags & FLAG_q))
      xprintf((toys.optflags & FLAG_n) ? "%s" : "%s\n", s);
    if (CFG_TOYBOX_FREE) free(s);
  } else toys.exitval = 1;
}