[Toybox] [PATCH] head -X

Rob Landley rob at landley.net
Thu Mar 19 13:57:54 PDT 2015


On 03/17/2015 02:54 PM, Isaac Dunham wrote:
> On Mon, Mar 16, 2015 at 09:16:00PM -0700, enh wrote:
>> Allow "head -X" as well as "head -n X".
>>
>> diff --git a/toys/posix/head.c b/toys/posix/head.c
>> index e8517d4..608b93a 100644
>> --- a/toys/posix/head.c
>> +++ b/toys/posix/head.c
>> @@ -4,7 +4,7 @@
>>   *
>>   * See http://opengroup.org/onlinepubs/9699919799/utilities/head.html
>>
>> -USE_HEAD(NEWTOY(head, "n#<0=10", TOYFLAG_BIN))
>> +USE_HEAD(NEWTOY(head, "?n#<0=10", TOYFLAG_BIN))
>>
>>  config HEAD
>>    bool "head"
>> @@ -50,5 +50,10 @@ static void do_head(int fd, char *name)
>>
>>  void head_main(void)
>>  {
>> -  loopfiles(toys.optargs, do_head);
>> +  char **args = toys.optargs;
>> +  if (*args && **args == '-') {
>> +    TT.lines = xstrtol(*(args++) + 1, NULL, 10);
> 
> This will break "head -", which is valid though redundant.

Actually it's valid and useful:

  thingy | head - file2 file3

I also tried:

  head README -3

And the ubuntu version went:

  head: invalid trailing option -- 3
  Try 'head --help' for more information.

And I went "Really? They have a custom error message for this?"

So I wondered this needs a "stop at first unknown argument" flag, and tried:

  touch ./-x
  echo | head -3 - -x

Which went:

  head: invalid option -- 'x'

At which point I went "screw it, the gnu/dammit version is crazy, as
usual". (Custom error messages for crazy corner cases are seldom a good
sign.)

I guess the point is no scripts out in the wild are going to use "head
README -3" because it wouldn't work with the implementation they created
their script against, so they wold have debugged it away. (Unless bsd or
macosx behave differently, but this is a nonstandard extension already
so I'll wait for complaints.)

(P.S. Reminder to self: the toybox shell's wildcard expansion should
expand any $FILENAME with a leading - and no path to "./$FILENAME" just
so NOBODY ELSE EVER HAS TO CARE. Why this isn't standard behavior
already, I have no idea.)

> I'd suggest using
>   if (*args && **args == '-' && isdigit(*args + 1))
> or some similar check.
> 
> Same applies to the "tail -X" patch.

I rewrote them locally, testing now. Should go in later today.

Thanks,

Rob

 1426798674.0


More information about the Toybox mailing list