toybox/toys/other/readlink.c
<<
>>
Prefs
   1/* readlink.c - Return string representation of a symbolic link.
   2 *
   3 * Copyright 2007 Rob Landley <rob@landley.net>
   4
   5USE_READLINK(NEWTOY(readlink, "<1>1nqmef(canonicalize)[-mef]", TOYFLAG_USR|TOYFLAG_BIN))
   6
   7config 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
  27void readlink_main(void)
  28{
  29  char *s;
  30
  31  // Calculating full canonical path?
  32  // Take advantage of flag positions to calculate m = -1, f = 0, e = 1
  33  if (toys.optflags & (FLAG_f|FLAG_e|FLAG_m))
  34    s = xabspath(*toys.optargs, (toys.optflags&(FLAG_f|FLAG_e))-1);
  35  else s = xreadlink(*toys.optargs);
  36
  37  if (s) {
  38    if (!(toys.optflags & FLAG_q))
  39      xprintf((toys.optflags & FLAG_n) ? "%s" : "%s\n", s);
  40    if (CFG_TOYBOX_FREE) free(s);
  41  } else toys.exitval = 1;
  42}
  43