On Fri, Nov 21, 2014 at 12:24:22PM -0500, David Braun wrote:
> ...
>
> For this reason and to maintain backward compatibility maybe it would be
> better to introduce another format specifier (%L maybe) defined as
> producing an unadorned link value (no quotes or "->" stuff) or file name if
> not a symbolic link similar in formatting to %n. Alternatively a flag
> argument ('-l' perhaps) could be introduced that causes %N to produce the
> unadorned link value.
I copied the %N code in stat.c to create a new %L format as suggested
above, patch attached.
ken@kimball:~/coreutils-hacks/coreutils/
$ src/stat -c "%N" INSTALL
âINSTALLâ -> âgnulib/doc/INSTALLâ
$ src/stat -c "'%n' -> '%L'" INSTALL
'INSTALL' -> 'gnulib/doc/INSTALL'
Like %N, %L does nothing if the file is not a symlink:
$ src/stat -c "%F %N" gnulib configure
directory âgnulibâ
regular file âconfigureâ
$ src/stat -c "%F '%n' -> '%L'" gnulib configure
directory 'gnulib' -> ''
regular file 'configure' -> ''
I don't see much point in complaining if %L is used for non-symlinks,
but that could be done or a warning emitted. The symlink value is
unconstrained, so an in-band message also makes no sense (the last
example would be poor practice since it makes the regular files look
like empty symlinks).
This was not my idea, so David should be attributed in the unlikely
event that this patch is accepted.
Ken
>From 81cd015244bdcaff4578abd0c9c3ff29b9b6533f Mon Sep 17 00:00:00 2001
From: Ken Irving <[email protected]>
Date: Sun, 23 Nov 2014 12:59:42 -0900
Subject: [PATCH] stat: added %L format
---
doc/coreutils.texi | 1 +
src/stat.c | 15 +++++++++++++++
2 files changed, 16 insertions(+)
diff --git a/doc/coreutils.texi b/doc/coreutils.texi
index db24a75..9c731b5 100644
--- a/doc/coreutils.texi
+++ b/doc/coreutils.texi
@@ -11923,6 +11923,7 @@ The valid @var{format} directives for files with
@option{--format} and
@item %G - Group name of owner
@item %h - Number of hard links
@item %i - Inode number
+@item %L - Dereference symbolic link
@item %m - Mount point (See note below)
@item %n - File name
@item %N - Quoted file name with dereference if symbolic link
diff --git a/src/stat.c b/src/stat.c
index a739ae7..987f579 100644
--- a/src/stat.c
+++ b/src/stat.c
@@ -989,6 +989,20 @@ print_stat (char *pformat, size_t prefix_len, unsigned int
m,
free (linkname);
}
break;
+ case 'L':
+ if (S_ISLNK (statbuf->st_mode))
+ {
+ char *linkname = areadlink_with_size (filename, statbuf->st_size);
+ if (linkname == NULL)
+ {
+ error (0, errno, _("cannot read symbolic link %s"),
+ quote (filename));
+ return true;
+ }
+ out_string (pformat, prefix_len, linkname);
+ free (linkname);
+ }
+ break;
case 'd':
out_uint (pformat, prefix_len, statbuf->st_dev);
break;
@@ -1450,6 +1464,7 @@ The valid format sequences for files (without
--file-system):\n\
fputs (_("\
%h number of hard links\n\
%i inode number\n\
+ %L dereference symbolic link\n\
%m mount point\n\
%n file name\n\
%N quoted file name with dereference if symbolic link\n\
--
1.9.1