Re: [dev] less lines of code suck less

From: Mateusz Okulus <mmokulus_AT_gmail.com>
Date: Fri, 7 May 2021 11:59:15 +0200

On 21/05/03 04:28PM, Greg Reagle wrote:
> Would sbase suck less if the program head, which is currently a C program of 77 lines, were replaced with something like
> #!/bin/sh
> sed "$1"q
>
> I know that it would need to be a bit more elaborate than that to handle the -n flag, but still. Is there any advantage to having a separate C program?
>

It's for performance or/and convenience reasons. It could be a shell
script.

head is a utility as defined by POSIX

https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_22

A utility can be a shell script and there shouldn't be a difference in
behavior.

I think it's easier to write and maintain a POSIX compliant head when
it's written in C, because it has to handle -n option and multiple files
with a special output header.

https://pubs.opengroup.org/onlinepubs/9699919799/utilities/head.html

The head is more for convenience too. In RATIONALE:
> Although it is possible to simulate head with sed 10q for a single
> file, the standard developers decided that the popularity of head on
> historical BSD systems warranted its inclusion alongside tail.

From https://en.wikipedia.org/wiki/Head_(Unix)#Other:
> Many early versions of Unix did not have this command, and
> documentation and books used sed instead:

You could write POSIX compliant head in POSIX compliant shell and it
would be correct.

Of course if you don't need to be POSIX compliant you could drop head
altogether.

I would say most people write these tools in C for performance reasons.
For example look at GNU cat code.

Regards,
mat
Received on Fri May 07 2021 - 11:59:15 CEST

This archive was generated by hypermail 2.3.0 : Fri May 07 2021 - 13:48:09 CEST