Hacker News new | past | comments | ask | show | jobs | submit login

The extremely odd for-loop structure feels to me like someone was trying very hard to avoid a goto, and in the process obfuscated the flow quite a bit more. The special case of head'ing stdin is similar to the classic "loop and a half problem" (which is really only a problem if you religiously abstain from goto use.) I'd consider this a more straightforward way to do it:

    FILE *f;
    int i = 1;
    ...
    if(argc < 2) {
       f = stdin;
       goto do_head;
    }
    ...
    while(i<argc) {
       f = fopen(argv[i], "r");
       /* check for failure to open */
       ...
      do_head:
       /* head'ing code goes here */
       ...
       fclose(f);
       i++;
    }

That being said, OpenBSD code is still far more concise and readable than GNU; compare coreutils' head.c here:

http://code.metager.de/source/xref/gnu/coreutils/src/head.c

GNU head has a bit more functionality, but I don't think the increase in complexity - nearly 10x more lines - is proportional to that.




Here's the version used in OS X. It's a bit bigger but not GNUishly big.

http://www.opensource.apple.com/source/text_cmds/text_cmds-8...


Looks like a small extension to the BSD one, that can also handle head'ing by byte count.

(Although I think the use of malloc() and strcpy() to handle the -[0-9]+ case seems rather unnecessary...)


That's almost certainly the FreeBSD one.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: