81
\$\begingroup\$

One of the most common standard tasks (especially when showcasing esoteric programming languages) is to implement a "cat program": read all of STDIN and print it to STDOUT. While this is named after the Unix shell utility cat it is of course much less powerful than the real thing, which is normally used to print (and concatenate) several files read from disc.

Task

You should write a full program which reads the contents of the standard input stream and writes them verbatim to the standard output stream. If and only if your language does not support standard input and/or output streams (as understood in most languages), you may instead take these terms to mean their closest equivalent in your language (e.g. JavaScript's prompt and alert). These are the only admissible forms of I/O, as any other interface would largely change the nature of the task and make answers much less comparable.

The output should contain exactly the input and nothing else. The only exception to this rule is constant output of your language's interpreter that cannot be suppressed, such as a greeting, ANSI color codes or indentation. This also applies to trailing newlines. If the input does not contain a trailing newline, the output shouldn't include one either! (The only exception being if your language absolutely always prints a trailing newline after execution.)

Output to the standard error stream is ignored, so long as the standard output stream contains the expected output. In particular, this means your program can terminate with an error upon hitting the end of the stream (EOF), provided that doesn't pollute the standard output stream. If you do this, I encourage you to add an error-free version to your answer as well (for reference).

As this is intended as a challenge within each language and not between languages, there are a few language specific rules:

  • If it is at all possible in your language to distinguish null bytes in the standard input stream from the EOF, your program must support null bytes like any other bytes (that is, they have to be written to the standard output stream as well).
  • If it is at all possible in your language to support an arbitrary infinite input stream (i.e. if you can start printing bytes to the output before you hit EOF in the input), your program has to work correctly in this case. As an example yes | tr -d \\n | ./my_cat should print an infinite stream of ys. It is up to you how often you print and flush the standard output stream, but it must be guaranteed to happen after a finite amount of time, regardless of the stream (this means, in particular, that you cannot wait for a specific character like a linefeed before printing).

Please add a note to your answer about the exact behaviour regarding null-bytes, infinite streams, and extraneous output.

Additional rules

  • This is not about finding the language with the shortest solution for this (there are some where the empty program does the trick) - this is about finding the shortest solution in every language. Therefore, no answer will be marked as accepted.

  • Submissions in most languages will be scored in bytes in an appropriate preexisting encoding, usually (but not necessarily) UTF-8.

    Some languages, like Folders, are a bit tricky to score. If in doubt, please ask on Meta.

  • Feel free to use a language (or language version) even if it's newer than this challenge. Languages specifically written to submit a 0-byte answer to this challenge are fair game but not particularly interesting.

    Note that there must be an interpreter so the submission can be tested. It is allowed (and even encouraged) to write this interpreter yourself for a previously unimplemented language.

    Also note that languages do have to fulfil our usual criteria for programming languages.

  • If your language of choice is a trivial variant of another (potentially more popular) language which already has an answer (think BASIC or SQL dialects, Unix shells or trivial Brainfuck derivatives like Headsecks or Unary), consider adding a note to the existing answer that the same or a very similar solution is also the shortest in the other language.

  • Unless they have been overruled earlier, all standard rules apply, including the http://meta.codegolf.stackexchange.com/q/1061.

As a side note, please don't downvote boring (but valid) answers in languages where there is not much to golf; these are still useful to this question as it tries to compile a catalogue as complete as possible. However, do primarily upvote answers in languages where the author actually had to put effort into golfing the code.

Catalogue

The Stack Snippet at the bottom of this post generates the catalogue from the answers a) as a list of shortest solution per language and b) as an overall leaderboard.

To make sure that your answer shows up, please start your answer with a headline, using the following Markdown template:

## Language Name, N bytes

where N is the size of your submission. If you improve your score, you can keep old scores in the headline, by striking them through. For instance:

## Ruby, <s>104</s> <s>101</s> 96 bytes

If there you want to include multiple numbers in your header (e.g. because your score is the sum of two files or you want to list interpreter flag penalties separately), make sure that the actual score is the last number in the header:

## Perl, 43 + 2 (-p flag) = 45 bytes

You can also make the language name a link which will then show up in the snippet:

## [><>](http://esolangs.org/wiki/Fish), 121 bytes

<style>body { text-align: left !important} #answer-list { padding: 10px; width: 290px; float: left; } #language-list { padding: 10px; width: 290px; float: left; } table thead { font-weight: bold; } table td { padding: 5px; }</style><script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b"> <div id="language-list"> <h2>Shortest Solution by Language</h2> <table class="language-list"> <thead> <tr><td>Language</td><td>User</td><td>Score</td></tr> </thead> <tbody id="languages"> </tbody> </table> </div> <div id="answer-list"> <h2>Leaderboard</h2> <table class="answer-list"> <thead> <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr> </thead> <tbody id="answers"> </tbody> </table> </div> <table style="display: none"> <tbody id="answer-template"> <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr> </tbody> </table> <table style="display: none"> <tbody id="language-template"> <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr> </tbody> </table><script>var QUESTION_ID = 62230; var ANSWER_FILTER = "!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe"; var COMMENT_FILTER = "!)Q2B_A2kjfAiU78X(md6BoYk"; var OVERRIDE_USER = 8478; var answers = [], answers_hash, answer_ids, answer_page = 1, more_answers = true, comment_page; function answersUrl(index) { return "//api.stackexchange.com/2.2/questions/" + QUESTION_ID + "/answers?page=" + index + "&pagesize=100&order=desc&sort=creation&site=codegolf&filter=" + ANSWER_FILTER; } function commentUrl(index, answers) { return "//api.stackexchange.com/2.2/answers/" + answers.join(';') + "/comments?page=" + index + "&pagesize=100&order=desc&sort=creation&site=codegolf&filter=" + COMMENT_FILTER; } function getAnswers() { jQuery.ajax({ url: answersUrl(answer_page++), method: "get", dataType: "jsonp", crossDomain: true, success: function (data) { answers.push.apply(answers, data.items); answers_hash = []; answer_ids = []; data.items.forEach(function(a) { a.comments = []; var id = +a.share_link.match(/\d+/); answer_ids.push(id); answers_hash[id] = a; }); if (!data.has_more) more_answers = false; comment_page = 1; getComments(); } }); } function getComments() { jQuery.ajax({ url: commentUrl(comment_page++, answer_ids), method: "get", dataType: "jsonp", crossDomain: true, success: function (data) { data.items.forEach(function(c) { if (c.owner.user_id === OVERRIDE_USER) answers_hash[c.post_id].comments.push(c); }); if (data.has_more) getComments(); else if (more_answers) getAnswers(); else process(); } }); } getAnswers(); var SCORE_REG = /<h\d>\s*([^\n,<]*(?:<(?:[^\n>]*>[^\n<]*<\/[^\n>]*>)[^\n,<]*)*),.*?(\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/; var OVERRIDE_REG = /^Override\s*header:\s*/i; function getAuthorName(a) { return a.owner.display_name; } function process() { var valid = []; answers.forEach(function(a) { var body = a.body; a.comments.forEach(function(c) { if(OVERRIDE_REG.test(c.body)) body = '<h1>' + c.body.replace(OVERRIDE_REG, '') + '</h1>'; }); var match = body.match(SCORE_REG); if (match) valid.push({ user: getAuthorName(a), size: +match[2], language: match[1], link: a.share_link, }); else console.log(body); }); valid.sort(function (a, b) { var aB = a.size, bB = b.size; return aB - bB }); var languages = {}; var place = 1; var lastSize = null; var lastPlace = 1; valid.forEach(function (a) { if (a.size != lastSize) lastPlace = place; lastSize = a.size; ++place; var answer = jQuery("#answer-template").html(); answer = answer.replace("{{PLACE}}", lastPlace + ".") .replace("{{NAME}}", a.user) .replace("{{LANGUAGE}}", a.language) .replace("{{SIZE}}", a.size) .replace("{{LINK}}", a.link); answer = jQuery(answer); jQuery("#answers").append(answer); var lang = a.language; lang = jQuery('<a>'+lang+'</a>').text(); languages[lang] = languages[lang] || {lang: a.language, lang_raw: lang.toLowerCase(42), user: a.user, size: a.size, link: a.link}; }); var langs = []; for (var lang in languages) if (languages.hasOwnProperty(lang)) langs.push(languages[lang]); langs.sort(function (a, b) { if (a.lang_raw > b.lang_raw) return 1; if (a.lang_raw < b.lang_raw) return -1; return 0; }); for (var i = 0; i < langs.length; ++i) { var language = jQuery("#language-template").html(); var lang = langs[i]; language = language.replace("{{LANGUAGE}}", lang.lang) .replace("{{NAME}}", lang.user) .replace("{{SIZE}}", lang.size) .replace("{{LINK}}", lang.link); language = jQuery(language); jQuery("#languages").append(language); } }</script>

\$\endgroup\$
  • 52
    \$\begingroup\$ Bash, 3 bytes: cat \$\endgroup\$ – TheDoctor Oct 31 '15 at 19:02
  • 3
    \$\begingroup\$ @TheDoctor I guess this would fall into the "don't use a builtin which does exactly what is needed" rule. \$\endgroup\$ – Paŭlo Ebermann Oct 31 '15 at 19:46
  • 5
    \$\begingroup\$ @PaŭloEbermann There is no such rule, and the corresponding standard loophole is no longer accepted. (In fact, there is already a sh answer using cat which also contains a shorter solution using dd.) \$\endgroup\$ – Martin Ender Oct 31 '15 at 20:25
  • 1
    \$\begingroup\$ If only it used standard methods of input and output: ///, 0 bytes. \$\endgroup\$ – Comrade SparklePony Mar 31 '17 at 19:33
  • 1
    \$\begingroup\$ @SparklePony Except, you'd have to escape slashes and backslashes. \$\endgroup\$ – Martin Ender Mar 31 '17 at 20:35

261 Answers 261

1
\$\begingroup\$

Prelude, 5 bytes

?(!?)

This works essentially like the Brainfuck solution, except that the value is pushed onto/popped from a stack instead of written to/read from a tape.

Prelude cannot possibly handle null bytes, as EOF is signified as 0.

\$\endgroup\$
1
\$\begingroup\$

Pip, 1 + 2 = 3 bytes

One byte for the code plus two for the flags -rn:

g

The -r flag reads all of stdin, splits into lines, and assigns the list to g. The code then prints g, with -n adding newlines between the elements.

This works for null bytes, but it doesn't allow for infinite input, and it will always have a newline at the end whether the input ends with one or not. It's possible to write a version that can handle infinite streams as long as they contain newlines:

W##YqPy

(See revision history for explanation.)

The rules state, "If it is at all possible in your language to support an arbitrary infinite input stream, your program has to work correctly in this case." However, Pip cannot echo an infinite stream that doesn't contain newlines. So the 3-byte version, which can't handle any infinite streams, is still valid according to the rules (thanks to Dennis for pointing this out).

\$\endgroup\$
  • \$\begingroup\$ The longer version supports infinite lines of finite length, but not an arbitrary, infinite stream of bytes. For example, yes | tr -d \\n | ./pip.py cat.pip prints nothing before getting killed by the OOM manager. \$\endgroup\$ – Dennis Oct 31 '15 at 20:41
  • \$\begingroup\$ If it is at all possible in your language to support an arbitrary infinite input stream (i.e. if you can start printing bytes to the output before you hit EOF in the input), your program has to work correctly in this case. That means your 3 byte solution is valid, doesn't it? \$\endgroup\$ – Dennis Nov 1 '15 at 0:50
  • \$\begingroup\$ @Dennis You're right--edited. \$\endgroup\$ – DLosc Nov 1 '15 at 3:47
1
\$\begingroup\$

Cat, 31 bytes

[readch 1new_str write]-1repeat

A concatenative programming language inspired by Joy. It seems to be dead...

Something something right tool for the job.

(repeat is implemented as decrementing a repeat counter until it reaches zero. If you pass it -1, it'll count downwards from there and never reach zero, looping forever. This is shorter than a while loop.)

\$\endgroup\$
1
\$\begingroup\$

X86_64, 37 bytes

Disassembly:

0000000000000000 <a>:
   0:   31 c0                   xor    %eax,%eax
   2:   31 ff                   xor    %edi,%edi
   4:   48 89 e6                mov    %rsp,%rsi
   7:   ba 01 00 00 00          mov    $0x1,%edx
   c:   0f 05                   syscall 
   e:   48 85 c0                test   %rax,%rax
  11:   74 0b                   je     1e <b>
  13:   b8 01 00 00 00          mov    $0x1,%eax
  18:   ff c7                   inc    %edi
  1a:   0f 05                   syscall 
  1c:   eb e2                   jmp    0 <a>

000000000000001e <b>:
  1e:   b8 3c 00 00 00          mov    $0x3c,%eax
  23:   0f 05                   syscall 

Source:

a:
xor eax, eax
xor edi, edi
mov rsi, rsp
mov edx, 1
syscall
test rax, rax
jz b
mov eax, 1
inc edi
syscall
jmp a
b:
mov eax, 60
syscall
\$\endgroup\$
  • \$\begingroup\$ Insted of mov edx,1, you should be able to do cdq;inc dl. Instead of mov eax,60, you could mov al,60 \$\endgroup\$ – ninjalj Nov 2 '15 at 20:26
1
\$\begingroup\$

DarkBASIC Classic/Pro, 16 bytes

DarkBASIC Pro is a language targeted primarily at game development, and it doesn't have stdin/out. Instead this snippet takes text input from the keyboard and draws it directly to the screen (the input command draws input characters to the screen as they are received, and stores them in the specified variable after return is pressed).

do
input a$
loop
\$\endgroup\$
1
\$\begingroup\$

LiveScript, 25 bytes

while true
 alert prompt!
\$\endgroup\$
1
\$\begingroup\$

Jasmin, 355 bytes

To be honest, I didn't golf this program much myself. I wrote some java with simple java code that aimed to have a low JVM instruction count, compiled it with javac, disassembled it with javap and translated the resulting pseudo-assembly into Jasmin code. Jasmin being an assembler for the java virtual machine, there wasn't a whole lot of work to be done there.

This code works for infinite inputs and can handle null bytes.

.class C
.super java/lang/Object
.method public static main([Ljava/lang/String;)V
.limit stack 2
T:
getstatic java/lang/System/in Ljava/io/InputStream;
invokevirtual java/io/InputStream/read()I
dup
istore_0
iconst_0
if_icmple R
getstatic java/lang/System/out Ljava/io/PrintStream;
iload_0
i2c
invokevirtual java/io/PrintStream/print(C)V
goto T
R:
return
.end method
\$\endgroup\$
1
\$\begingroup\$

Japt, 1 byte

Japt (JavaScript shortened) is a language I published last night. Interpreter

N

Like TeaScript, all input is stored in a single variable (although here it's N). Also like TeaScript, output is implicit.

Side note: The first (up to) 6 items in the input are stored in variables U through Z. If we knew there was only one item in the input, this code would work just as well:

U
\$\endgroup\$
  • \$\begingroup\$ Is this the first ever Japt solution posted here? \$\endgroup\$ – Shaggy Aug 16 '17 at 11:26
  • \$\begingroup\$ @Shaggy Nope, I actually posted this one before I had even written the interpreter :P Then there's this one and this one, so this is the fourth Japt answer. \$\endgroup\$ – ETHproductions Aug 16 '17 at 11:54
1
\$\begingroup\$

Gnu Forth, 56 bytes

Gnu Forth has immediate control structure words, usable outside word definitions:

[begin] pad pad 1 stdin read-file + tuck type 1- [until]

The [until] can be dropped, and Gforth kindly assumes it was there:

[begin] pad pad 1 stdin read-file + tuck type 1-

Of course, invoking external programs is shorter:

sh dd
\$\endgroup\$
1
\$\begingroup\$

𝔼𝕊𝕄𝕚𝕟, 2 chars / 4 bytes

ôï

Try it here (Firefox only).

\$\endgroup\$
1
\$\begingroup\$

Acc!, 27 bytes

Also works in Acc!!.

Count q while 1 {
Write N
}

This code can handle null bytes, but will error when input runs out, as it is impossible to handle that case in Acc!

\$\endgroup\$
1
\$\begingroup\$

DStack, 5 bytes

0kckt

Language created by my few days ago

\$\endgroup\$
  • \$\begingroup\$ What does kc do when you hit EOF? \$\endgroup\$ – Martin Ender Nov 13 '15 at 7:14
  • \$\begingroup\$ @MartinBüttner At the moment, the language does not say what happens in that case, and my implementation not see if it tried to read EOF (which I have to fix). In the case of this cat program, the only way to stop it is sending the null character. \$\endgroup\$ – DarkPhantom Nov 13 '15 at 18:57
1
\$\begingroup\$

Fishing, Dock length 2, 9 bytes

v+CC
  IP

I takes an input and P prints it. Note that N can be substituted for P to produce a trailing newline.

\$\endgroup\$
  • 1
    \$\begingroup\$ You might want to note that Fishing is not capable of a) preserving linefeeds, b) determining EOF, c) reading arbitrary infinite streams, because it always reads lines (waiting for a linefeed character)... at least as far as the reference interpreter goes. Also I don't think it's fair to count this by the dock length. The byte count of this code is 9 bytes. \$\endgroup\$ – Martin Ender Nov 13 '15 at 7:06
1
\$\begingroup\$

Go, 66 bytes

package main
import("io"
."os")
func main(){io.Copy(Stdout,Stdin)}

Go has an io.Copy function which writes what reads; until EOF.

\$\endgroup\$
1
\$\begingroup\$

ResPlicate, 36 bytes

4 2 0 -1 4 2 4 2 4 2 4 2 4 2 1 0 0 0

This was one of the earliest examples I made after implementing this language, so I have simply copied it from the linked wiki article, which I largely wrote. As was mentioned there, this program does not halt on EOF, and will continue asking for input until the interpreter is killed by force.

ResPlicate in a nutshell

Programs in ResPlicate are all comprised of a list of integers, which are inserted into a queue in order. Each step, the first two integers are popped as x and y. Then a list of x integers is popped (and padded with zeros if the queue had fewer than x elements) and re-enqueued y times. This is quite sufficient to ensure Turing-completeness. Indeed, it is even Turing-complete in the limited case that y is not allowed to exceed 2.

This simple language is extended with I/O in the following way: If x is zero and y is positive, y is output as a character. If x is zero and y is negative, a character is read from input, y+1 is added to it, and the result is enqueued.

Ungolfed

4 2 [0 -1 4 2]                  Enqueue 0 -1 (read one character) followed by this command.
4 2 [4 2 4 2]                   Enqueue 4 2 followed by this command.
4 2 [1 0 0 0]                   Put two copies of 1 0 0 0 on the cue.
                                This sequence and the one preceding it can be considered
                                together to be a single command which appends
                                1 0 0 0 to itself.

After these commands, the queue looks like this:

0 -1 4 2 0 -1 4 2 4 2 4 2 4 2 4 2 1 0 0 0 1 0 0 0

In other words, the program has decompressed itself into a copy of itself with 0 -1 prepended and 1 0 0 0 appended, and since the program will eventually (and forever) return to this state, this could be considered the "real" cat program. The execution continues like so:

0 -1                            Get a character from stdin, append it to the queue.
4 2 [0 -1 4 2]
4 2 [4 2 4 2]
4 2 [1 0 0 0]                   As above.
1 0 [0]                         Pop and discard the zero.
0 (x)                           Print the inputted character x.

After which the program is once again in the state I said it would return to.

\$\endgroup\$
1
\$\begingroup\$

Aubergine, 9 bytes

=ii=oo=ib

Aubergine in a nutshell

Aubergine has four variables and four 2-argument instructions. The variables a and b can be indirected as A and B to point to locations in the program, which can be read and written just like data, making it inherently self-modifying. The other variables are o, which refers to input or output depending on whether it is the second or first argument of the assignment instruction, and i, which is the instruction pointer. The four instructions are assignment (=), addition (+), subtraction (-), and conditional jump (:). The only constant literal available is 1. All variables are initialized to zero.

Ungolfed:

=ii                      NOP placeholder, since instruction pointer moves after jumps
=oo                      Read a character from stdin, print it to stdout.
=ib                      Send the instruction pointer to value of b, which is 0 by default

This version runs forever and must be killed manually. To make it so that it exits upon receiving a null byte, we must add 3 bytes:

=ii=ao=oa:ba
\$\endgroup\$
1
\$\begingroup\$

Chaîne, 2 bytes

Because strings.

|i
|  ; perform next character as instruction
 i ; takes input to the stack
   ; implicit: output stack
\$\endgroup\$
1
\$\begingroup\$

ROOP, 5 bytes

I
W
O

Never ends.

\$\endgroup\$
1
\$\begingroup\$

WhoScript, 39 bytes

1pr;v;#0 1;-;i;>;e;<;t=;ti;o;tl;pw;pr;d

I think its best explained by watching the stack change over time.

psychic_paper read          @ [input]
time_vortex                 @ [input]
  # 0 1                     @ [input, 0, 1]
  -                         @ [input, -1.0]
  integer                   @ [input, -1]
  pop                       @ [input]
  duplicate                 @ [input, input]
  push                      @ [input, input, -1]
  TARDIS =                  @ [input, input==-1]
  TARDIS if                 @ [input]
    opening                 @ [-1] (it won't come this far otherwise)
  TARDIS landing            @ [input | -1]
  psychic_paper write       @ []
  psychic_paper read        @ [input]
paradox                     @ Phew!

The program will accept any input so long as you don't find a character whose ASCII code is -1. The program works best if input is provided from the command line, else it will just take one character at a time until you quit providing new ones.

\$\endgroup\$
1
\$\begingroup\$

AnnieFlow, 2 bytes

11

The first 1 indicates that the program accepts input, and the second 1 indicates that there is one stack. The output stack has no changeable behavior, so that's the only information the program needs. Because the output stack is always the first stack and the input stack is always the last stack, they coincide in this case, so when the input stack is filled, it outputs the same to STDOUT instead of storing it. In any program, after the input stack is filled it is popped to start the program. However, popping from the output stack halts the program. Therefore the program halts immediately after outputting the input. This was not an intended feature, it just happened to work out the way it does because of how the programming language works.

\$\endgroup\$
1
\$\begingroup\$

Y, 4 bytes

i:gF

Simple enough. This two-link program takes input characters and prints them out as you feed input. Input an empty string to terminate. Explanation:

i:g F 
i      take input (string)
 :     duplicate
  g    print one item
    F  conditional link: pop N, and if N is zero (falsey), i.e. empty string, continue.
       otherwise, move to beginning of current link
\$\endgroup\$
1
\$\begingroup\$

SPARC (V9) assembly, 64 bytes

This runs on NetBSD (I seem to be running version 6.1.5). Disassembly with objdump:

0000000000100078 <.text>:
  100078:   9c 23 a0 08     sub  %sp, 8, %sp
  10007c:   82 10 20 03     mov  3, %g1
  100080:   90 10 00 00     mov  %g0, %o0
  100084:   92 10 00 0e     mov  %sp, %o1
  100088:   94 10 20 01     mov  1, %o2
  10008c:   91 d0 20 00     ta  0
  100090:   02 ca 00 07     brz  %o0, 0x1000ac
  100094:   82 10 20 04     mov  4, %g1
  100098:   90 10 20 01     mov  1, %o0
  10009c:   92 10 00 0e     mov  %sp, %o1
  1000a0:   94 10 20 01     mov  1, %o2
  1000a4:   10 bf ff f6     b  0x10007c
  1000a8:   91 d0 20 00     ta  0
  1000ac:   9c 03 a0 08     add  %sp, 8, %sp
  1000b0:   82 10 20 01     mov  1, %g1
  1000b4:   91 d0 20 00     ta  0

Explanation (line-by-line):

First of all, the SPARC architecture has a ton of registers. You have access to 32 of them at a time, called %gX, %iX, %lX, and %oX for X in [0..7]. There are also %pc for the program counter and %sp for the stack pointer.

sub %sp, 8, %sp

The stack grows downward. I didn't want to include a .data section, so I'm using the stack as storage. This expands it by 8 bytes.

mov 3, %g1
mov %g0, %o0
mov %sp, %o1
mov 1, %o2

Getting ready to do a syscall. The ABI in use here requires the syscall number in %g1 and the arguments in %o0, %o1, etc. According to syscall.h, read is 3. Register %g0 is always 0, so these lines equate to read(0, sp, 1).

ta 0

Those familiar with x86 assembly might want int 0x80 to do the syscall. In SPARC, we use a user-mode trap, and in NetBSD specifically it is the first one. Thus, ta 0.

brz %o0, 0x1000ac

A conditional branch: jump to address 0x1000ac if %o0 contains zero. We sometimes have to be careful with instruction order though, because every time the program encounters a branch, the next instruction is executed as well. In this case it doesn't make a difference, but it will later.

mov 4, %g1
mov 1, %o0
mov %sp, %o1
mov 1, %o2

Just like with read before, except we're using write (4) and stdout (1).

b 0x10007c
ta 0

An unconditional branch. Remember that thing about branches being weird? This executes the ta 0 to call the write we just prepared, then jumps back to address 0x10007c (to prepare to read again).

add %sp, 8, %sp

The target of the first branch we saw, here we prepare to exit. Start by putting the stack pointer back where it was.

mov 1, %g1
ta 0

We know that %o0 contains zero, since that was the condition of the branch that got us here. We want to exit with that status, so we simply call exit (1).

Source:

.global _start
.section .text
_start:
sub %sp,8,%sp
1:
mov 3,%g1
mov %g0,%o0
mov %sp,%o1
mov 1,%o2
t 0
brz %o0,2f
mov 4,%g1
mov 1,%o0
mov %sp,%o1
mov 1,%o2
b 1b
t 0
2:
add %sp,8,%sp
mov 1,%g1
t 0
\$\endgroup\$
1
\$\begingroup\$

Reng v.1.2, 4 bytes

is~o

This takes input, skips if not a negative one (i.e. no input found). When s find a regular character, it skips over the ~ and outputs the input with o. Otherwise, it meets the ~ and ends execution. Input is like "string1" "string2" ... "stringN". Try it here!

The following 13-byte program allows you to feed input more than once, but only yields correct output provided you feed it in at the right time.

>isvo
/$$>is!

Input "Meow":

meoowww

\$\endgroup\$
1
\$\begingroup\$

Jelly, 1 byte

¹

Simply the identity function.

\$\endgroup\$
1
\$\begingroup\$

Lua, 30 Bytes

Heavily base upon Lynn's answer, but as it is not updated anymore, I'll feel free ot post this 30 bytes answer.

::a::io.write(io.read())goto a
\$\endgroup\$
1
\$\begingroup\$

Fuzzy Octo Guacamole, 3 bytes

(non-competing, FOG is newer than the challenge)

(^)

Looks emotish (that's totally a word).

The ^ gets input, the X pops it and prints it.

The ( and ) denote the start and end of an infinite loop.

4 byte solution with a for loop:

?[?^]

The [ and ] denote a for loop, and the ? increments the counter, so it never runs out.

Weird implicit outputs means it prints the outputs automatically.

\$\endgroup\$
  • \$\begingroup\$ Cool!!!!!!!!!!! \$\endgroup\$ – Conor O'Brien Mar 18 '16 at 22:28
  • \$\begingroup\$ This is the infinite cat challenge. If it is at all possible in your language to support an arbitrary infinite input stream (i.e. if you can start printing bytes to the output before you hit EOF in the input), your program has to work correctly in this case. \$\endgroup\$ – Dennis Mar 19 '16 at 4:57
1
\$\begingroup\$

Javascript (browser), 16 15 bytes

alert(prompt())
\$\endgroup\$
  • 1
    \$\begingroup\$ I like the rules abuse here... :-) By the way, you can save a byte by dropping the semicolon - JavaScript has automatic semicolon insertion, with only a few exceptions. Also, you probably should specify state this is for browsers only, since Node.js doesn't have either of these, but it does support console IO. \$\endgroup\$ – Isiah Meadows May 20 '16 at 1:50
  • \$\begingroup\$ Done! Thanks for the edit btw \$\endgroup\$ – OldBunny2800 May 20 '16 at 1:52
  • 1
    \$\begingroup\$ Welcome! <filler to make SE happy> \$\endgroup\$ – Isiah Meadows May 20 '16 at 1:53
1
\$\begingroup\$

CoffeeScript/LiveScript/etc. (Node.js), 31 bytes

I'm not sure if any of those languages have been used yet, but here it is.

p=process;p.stdin.pipe p.stdout

Because it takes advantage of so few features, most of the CoffeeScript descendants with any notability would work with this code, including LiveScript, Coco, and IcedCoffeeScript.

Oh, and it does deal with infinite streams.

\$\endgroup\$
1
\$\begingroup\$

Archway, 7 bytes

/.\
\,/

According to Esolangs, this is the only useful program that can be written in the original Archway language.

\$\endgroup\$
1
\$\begingroup\$

Archway 2, 19 bytes

   \
// .
  , /
+/\
\$\endgroup\$

protected by a spaghetto Dec 22 '15 at 17:35

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.