Live data from Hacker News

Show HN: A simple Unix cat implementation in various languages

github.com

1–7 of 7 posts

Re: Show HN: A simple Unix cat implementation in various languages

#3
Here is another implementation using Node.js (although it does not explicitly check for EOF):

  require("fs").createReadStream(process.argv[2]).pipe(process.stdout)
Here is a implementation in PostScript (it does explicitly check for EOF, and specifically deals with one byte at a time):

  /o (%stdout) (w) file def
  ARGUMENTS 0 get (r) file
  {
    dup read {
      o exch write
    } {
      quit
    } ifelse
  } bind loop
(I have no GitHub account. But if the owner of that repository want to add, they can add what I have; I post this message (and all of my other messages on Hacker News) to public domain.)

Re: Show HN: A simple Unix cat implementation in various languages

#4
post #2

Feel free to add your own implementation. :)

FWIW, iter() handles the logic of testing for an explicit EOF marker, so another way to write it is:

    import sys

    with open(sys.argv[1]) as fin:
        for c in iter((lambda: fin.read(1)), ""):
            sys.stdout.write(c)

Re: Show HN: A simple Unix cat implementation in various languages

#5
post #4
post #2

Feel free to add your own implementation. :)

FWIW, iter() handles the logic of testing for an explicit EOF marker, so another way to write it is: import sys with open(sys.argv[1]) as fin: for c in iter((lambda: fin.read(1)), ""): sys.stdout.write(c)

That's a good point. Thanks!

Re: Show HN: A simple Unix cat implementation in various languages

#6

Here is another implementation using Node.js (although it does not explicitly check for EOF): require("fs").createReadStream(process.argv[2]).pipe(process.stdout) Here is a implementation in PostScript (it does explicitly check for EOF, and specifically deals with one byte at a time): /o (%stdout) (w) file def ARGUMENTS 0 get (r) file { dup read { o exch write } { quit } ifelse } bind loop (I have no GitHub account.…

Thank you for the implementations! Do you have any suggestions how to test the postscript code on mac/linux? I tried on macos using ghostscript, but I get the following error:

$ gs cat.ps helloworld.txt

Error: /undefined in ARGUMENTS ...

Thanks!

Re: Show HN: A simple Unix cat implementation in various languages

#7
post #6

Here is another implementation using Node.js (although it does not explicitly check for EOF): require("fs").createReadStream(process.argv[2]).pipe(process.stdout) Here is a implementation in PostScript (it does explicitly check for EOF, and specifically deals with one byte at a time): /o (%stdout) (w) file def ARGUMENTS 0 get (r) file { dup read { o exch write } { quit } ifelse } bind loop (I have no GitHub account.…

Thank you for the implementations! Do you have any suggestions how to test the postscript code on mac/linux? I tried on macos using ghostscript, but I get the following error: $ gs cat.ps helloworld.txt Error: /undefined in ARGUMENTS ... Thanks!

To give command-line arguments to a PostScript program in Ghostscript, you must prefix the name of the PostScript file with two minus signs, such as:

  gs -dNODISPLAY -q -- cat.ps helloworld.txt
(The -dNODISPLAY and -q are not needed to get this PostScript program to work, but -dNODISPLAY disables graphical output, and -q avoids Ghostscript's messages getting mixed in with the PostScript program's output. You might also need -dNOSAFER; if you get a invalidaccess error, try that. As far as I know, ARGUMENTS and -- are a feature specific to Ghostscript.)