delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2005/03/31/08:46:59

Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com
Message-ID: <424BFEE6.6040601@byu.net>
Date: Thu, 31 Mar 2005 06:45:10 -0700
From: Eric Blake <ebb9 AT byu DOT net>
User-Agent: Mozilla Thunderbird 1.0.2 (Windows/20050317)
MIME-Version: 1.0
To: Krisztian Fekete <fkr0 AT mailbox DOT hu>
CC: cygwin AT cygwin DOT com
Subject: Re: bug: tail -c stopped working
References: <E1DGvtD-0005FS-00 AT web2 DOT mailbox DOT hu>
In-Reply-To: <E1DGvtD-0005FS-00@web2.mailbox.hu>
X-IsSubscribed: yes

-----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 AT byu DOT 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/

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019