This is the mail archive of the cygwin mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: bug: tail -c stopped working


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

According to Krisztian Fekete on 3/31/2005 2:22 AM:
> Hi,
> 
> GNU tail has an option to output the last n bytes:
> 
> $ tail --help
> ...
>   -c, --bytes=N            output the last N bytes
> ...
> 
> In the current coreutils version (5.3.0-3) the short option version
> stopped working:
> 
> $ tail -c 30
> tail: cannot open `30' for reading: No such file or directory

This is by design.  coreutils 5.3.0 was built with POSIX 1992 semantics
(more on this later), and back then, -c took an optional argument.  Now,
how does one do an optional argument for a single letter?  Only by
omitting the space between the letter and its argument, since a space
implies that the next command-line element starts a new option or
filename.  --bytes, on the other hand, is a long option, and requires an
argument, so either = or space work.  So, the following work:
$ tail -c30 foo
$ tail --bytes=30 foo
$ tail --bytes 30 foo
while the following display the last 10 bytes of the file foo or the file 30:
$ tail -c foo
$ tail -c 30
and this fails, because the --bytes argument is not optional
$ tail --bytes foo

Now, in 2001, the POSIX folks realized that optional arguments are tough
to implement and inconsistent with the rest of POSIX, so they changed -c
to have a mandatory argument, as mentioned in the RATIONALE and Issue 6
sections of
http://www.opengroup.org/onlinepubs/009695399/utilities/tail.html.  With
2001 semantics, there are no changes to the --bytes option (it is, after
all, a GNU invention, so there are no POSIX requirements on it), but the
- -c option changes such that the following work:
$ tail -c 30 foo
$ tail -c30 foo
while this fails, because it no longer defaults to 10
$ tail -c foo

See also /usr/share/doc/coreutils-5.3.0/NEWS, and search for tail, to see
the documented difference between 1992 and 2001 semantics.  Among other
things also affected by the change in POSIX: in 1992, `tail -1' is valid,
but in 2001, it is an error and you need `tail -n 1' instead.  Also, try
`info coreutils "standards conformance"' for more details.

Now, how to switch between the two semantics?  Well, all of coreutils
first defaults to the _POSIX2_VERSION level defined in <unistd.h> (cygwin
doesn't define it, because cygwin is not fully compliant to either version
of POSIX, so it defaults to 1992).  Then, at build time this can be
overridden (I did not override it when packaging coreutils-5.3.0-3, so the
default remains at 1992; but you could download the coreutils-5.3.0-3-src
package and rebuild it yourself with the default changed).  Finally, each
coreutils utility that cares reads the _POSIX2_VERSION environment
variable, as the final say on which behavior to use.  So, if you don't
like the default of 199209, then use this in sh, bash, ksh, or zsh
$ _POSIX2_VERSION=200112; export _POSIX2_VERSION
or this in tcsh
$ setenv _POSIX2_VERSION=200112 200112

somewhere before using the utility whose behavior you want affected.  And
be prepared to find other things changed, such as `tail -1' no longer working.

- --
Life is short - so eat dessert first!

Eric Blake             ebb9@byu.net
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.0 (Cygwin)
Comment: Public key at home.comcast.net/~ericblake/eblake.gpg
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFCS/7m84KuGfSFAYARArLPAJ0cQDWLStfR+xzi0FKChADqTD3dfACfe3l9
SCnZGQLsWzAvnrYX2CTXn7Q=
=RAeR
-----END PGP SIGNATURE-----

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]