Copy stdin to stdout: Difference between revisions
→{{header|Binary Lambda Calculus}}
(added Ol) |
|||
(15 intermediate revisions by 11 users not shown) | |||
Line 5:
=={{header|8086 Assembly}}==
<
WRITE: equ 40h
BUFSZ: equ 4000h ; Buffer size
Line 26:
done: ret
section .bss
buffer: resb BUFSZ</
=={{header|Action!}}==
<
CHAR c
Line 37:
UNTIL c=27 ;repeat until Esc key is pressed
OD
RETURN</
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Copy_stdin_to_stdout.png Screenshot from Atari 8-bit computer]
Line 46:
=={{header|Ada}}==
<
procedure Copy_Stdin_To_Stdout is
Line 56:
Put (C);
end loop;
end Copy_Stdin_To_Stdout;</
=={{header|Aime}}==
<
data b;
f.stdin;
while (f.b_line(b) ^ -1) {
o_(b, "\n");
}</
=={{header|ALGOL 68}}==
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}}
<
BOOL at eof := FALSE;
# set the EOF handler for stand in to a procedure that sets "at eof" to true #
Line 75:
# copy stand in to stand out #
WHILE STRING line; read( ( line, newline ) ); NOT at eof DO write( ( line, newline ) ) OD
END</
=={{header|Applesoft BASIC}}==
<syntaxhighlight lang="gwbasic">0 GET C$ : PRINT C$; : GOTO</syntaxhighlight>
=={{header|AWK}}==
Using the awk interpreter, the following command uses the pattern // (which matches anything) with the default action (which is to print the current line) and so copy lines from stdin to stdut.
<
=={{header|BCPL}}==
<
let start() be
Line 88 ⟶ 90:
if c = endstreamch then finish
wrch(c)
$) repeat</
=={{header|Brainf***}}==
<syntaxhighlight lang
=={{header|Binary Lambda Calculus}}==
As explained on https://www.ioccc.org/2012/tromp/hint.html, `cat' is the 4-bit program
<pre>0010</pre>
in bit-wise BLC, or any one of the 16 characters in the ASCII range from space to slash
<pre> !"#$%&'()*+,-./</pre>
in byte-wise BLC.
=={{header|C}}==
<syntaxhighlight lang="c">
#include <stdio.h>
Line 104 ⟶ 118:
return 0;
}
</syntaxhighlight>
=={{header|C sharp|C#}}==
{{works with|.NET Framework|4.0 or later}}
<
using System;
Line 118 ⟶ 132:
}
}
</syntaxhighlight>
=={{header|C++}}==
<
#include <iterator>
Line 133 ⟶ 147:
);
return 0;
}</
Shorter and quicker alternative:
<
int main() {
std::cout << std::cin.rdbuf();
}</
=={{header|CLU}}==
<
pi: stream := stream$primary_input()
po: stream := stream$primary_output()
Line 152 ⟶ 166:
return
end
end start_up</
=={{header|Commodore BASIC}}==
Line 191 ⟶ 205:
The following program opens channels to devices chosen by the user, then uses the GET# and PRINT# I/O statements instead of the standard GET and PRINT (for typical keyboard/screen interaction.) When keyboard and/or screen are chosen, BASIC ignores the extra filename and file type parameters normally used for other devices. Peripheral devices (tape, disk drive) use STATUS register to flag end of file, however the keyboard does not. When using the keyboard, the program terminates on a CTRL-Z.
<
11 print "0:Keyboard 1:Tape 2:RS-232 3:Screen"
12 print "4-7:printers/plotters"
Line 205 ⟶ 219:
50 if (d1=0 and a$=chr$(26)) or (d1>0 and st>0) then close 5:close 2:end
60 print#2,a$;
70 goto 40</
{{output}}
Line 262 ⟶ 276:
=={{header|Common Lisp}}==
<
Then concatenates the characters into a string|#
(format t
(concatenate 'string
(loop for x = (read-char *query-io*) until (or (char= x #\Sub) (char= x #\Eot)) collecting x)))
</syntaxhighlight>
=={{header|Crystal}}==
<
puts line
end</
=={{header|D}}==
<
void main() {
Line 281 ⟶ 295:
writeln(line);
}
}</
=={{header|Dart}}==
<syntaxhighlight lang="dart">import 'dart:io';
void main() {
var line = stdin.readLineSync();
stdout.write(line);
}</syntaxhighlight>
=={{header|Delphi}}==
''→ See [[#Pascal|Pascal]]''
=={{header|Draco}}==
<syntaxhighlight lang="draco">\util.g
proc nonrec main() void:
char c;
while
/* I/O is line-oriented, so first read characters
* from the current line while that is possible */
while read(c) do write(c) od;
case ioerror()
/* Then once it fails, if the line is empty,
* try to go to the next line. */
incase CH_MISSING:
readln();
writeln();
true
/* If it failed for another reason (which will be
* EOF here), stop. */
default:
false
esac
do od
corp</syntaxhighlight>
=={{header|F_Sharp|F#}}==
<
let copy()=let n,g=stdin,stdout
let rec fN()=match n.ReadLine() with "EOF"->g.Write "" |i->g.WriteLine i; fN()
fN()
copy()
</syntaxhighlight>
=={{header|Factor}}==
<
[ read1 dup ] [ write1 ] while drop</
=={{header|Forth}}==
{{works with|gforth|0.7.3}}
<syntaxhighlight lang
=={{header|FreeBASIC}}==
<
#include "crt/stdio.bi" 'provides the C functions getchar and putchar
dim as ubyte char
Line 310 ⟶ 356:
char = getchar()
if char = FIN then exit do else putchar(char)
loop</
=={{header|Frink}}==
The special string "-" indicates reading from stdin.
<
=={{header|Go}}==
<
import (
Line 336 ⟶ 382:
w.Flush()
}
}</
===io.Copy===
<syntaxhighlight lang="go">
package main
import (
"io"
"os"
)
func main() {
io.Copy(os.Stdout, os.Stdin)
}
</syntaxhighlight>
=={{header|Groovy}}==
<
static void main(args) {
try (def reader = System.in.newReader()) {
Line 348 ⟶ 408:
}
}
}</
=={{header|Haskell}}==
<
=={{header|Java}}==
Copies until no more input.
<
import java.util.Scanner;
Line 370 ⟶ 430:
}
</syntaxhighlight>
{{out}}
Output interleaved. Stdin and Stdout are same window.
Line 379 ⟶ 439:
Line 2.
</pre>
=={{header|JavaScript}}==
JavaScript in the browser does not have a stdin or stdout, but using Node.js we have:
<syntaxhighlight lang="javascript">process.stdin.resume();
process.stdin.pipe(process.stdout);</syntaxhighlight>
<syntaxhighlight lang="bash">node index.js < index.js</syntaxhighlight>
{{out}}
<pre>
process.stdin.resume();
process.stdin.pipe(process.stdout);
</pre>
=={{header|jq}}==
<syntaxhighlight lang
=={{header|Julia}}==
<
write(stdout, read(stdin, UInt8))
end</
=={{header|Kotlin}}==
<
var c: Int
do {
Line 396 ⟶ 468:
System.out.write(c)
} while (c >= 0)
}</
=={{header|Latitude}}==
<
$stdout putln: $stdin readln.
}.</
=={{header|Lua}}==
Line 407 ⟶ 479:
=={{header|Mercury}}==
<syntaxhighlight lang="mercury">
:- module stdin_to_stdout.
Line 450 ⟶ 522:
%-----------------------------------------------------------------------------%
</syntaxhighlight>
=={{header|Nim}}==
<syntaxhighlight lang
=={{header|OCaml}}==
<
while true do
output_char stdout (input_char stdin)
done
with End_of_file -> ()</
=={{header|Ol}}==
<
(bytestream->port (port->bytestream stdin) stdout)
</syntaxhighlight>
=={{header|Pascal}}==
<
var
buffer: char;
Line 479 ⟶ 551:
write(buffer); // shorthand for write(output, buffer)
end;
end.</
=={{header|Perl}}==
<
perl -pe ''
</syntaxhighlight>
=={{header|Phix}}==
<!--<
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span>
<span style="color: #008080;">while</span> <span style="color: #004600;">true</span> <span style="color: #008080;">do</span>
Line 494 ⟶ 566:
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ch</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<!--</
=={{header|PicoLisp}}==
<syntaxhighlight lang
=={{header|Prolog}}==
<syntaxhighlight lang="prolog">
%File: stdin_to_stdout.pl
:- initialization(main).
Line 509 ⟶ 581:
X == end_of_file,
fail.
</syntaxhighlight>
Invocation at the command line (with Swi-prolog):
<syntaxhighlight lang="sh">
swipl stdin_to_stdout.pl
</syntaxhighlight>
=={{header|Python}}==
Line 523 ⟶ 595:
=={{header|Racket}}==
<
(let loop ()
Line 529 ⟶ 601:
[(? eof-object?) (void)]
[c (display c)
(loop)]))</
=={{header|Raku}}==
Line 535 ⟶ 607:
When invoked at a command line: Slightly less magical than Perl / sed. The p flag means automatically print each line of output to STDOUT. The e flag means execute what follows inside quotes. ".lines" reads lines from the assigned pipe (file handle), STDIN by default.
<syntaxhighlight lang="raku"
When invoked from a file: Lines are auto-chomped, so need to re-add newlines (hence .say rather than .print)
<syntaxhighlight lang="raku"
=={{header|REXX}}==
Line 546 ⟶ 618:
normally the console. So for REXX, this task equates to copying data
from the console to itself.
<
do while chars()\==0 /*repeat loop until no more characters.*/
call charin , x /*read a char from the input stream. */
call charout , x /*write " " " " output " */
end /*while*/ /*stick a fork in it, we're all done. */</
=={{header|Ring}}==
<
? "give input: " give str
? "output: " + str
</syntaxhighlight>
{{out}}
<pre>
Line 565 ⟶ 637:
</pre>
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">
$stdout << $stdin.gets
</syntaxhighlight>
=={{header|Rust}}==
<
fn main() {
io::copy(&mut io::stdin().lock(), &mut io::stdout().lock());
}</
=={{header|Scala}}==
Line 576 ⟶ 652:
For Scala 2's compiler <code>scalac</code>, a containing object is required:
<
io.Source.fromInputStream(System.in).getLines().foreach(println)
}</
If it's being run directly by <code>scala</code>, it can be shortened to one line, and run directly in the shell:
<
=={{header|Scheme}}==
<
(do ((c (read-char) (read-char)))
((eof-object? c) 'done)
(display c))
</syntaxhighlight>
=={{header|sed}}==
<syntaxhighlight lang="sh">
sed -e ''
</syntaxhighlight>
=={{header|Seed7}}==
<
include "fileutil.s7i";
Line 606 ⟶ 682:
begin
copyFile(IN, OUT);
end func;</
=={{header|Smalltalk}}==
{{Works with|Smalltalk/X}}
<
Stdin copyToEndInto:Stdout.
Line 622 ⟶ 698:
[
[ Stdout nextPut:(Stdin next) ] loop.
] on: StreamError do:[]</
=={{header|Standard ML}}==
<
case TextIO.input TextIO.stdIn of
"" => ()
| tx => copyLoop (TextIO.output (TextIO.stdOut, tx))
val () = copyLoop ()</
=={{header|Symsyn}}==
<syntaxhighlight lang="symsyn">
Loop [] []
go Loop
</syntaxhighlight>
=={{header|Tcl}}==
<
chan copy stdin stdout
# fcopy stdin stdout for older versions</
=={{header|VBScript}}==
VBScript can't get single chars from stdin, so we have to implement it line to line. Ctrl-Z+Enter stops.
<syntaxhighlight lang="vb">
do
s=wscript.stdin.readline
wscript.stdout.writeline s
loop until asc(left(s,1))=26
</syntaxhighlight>
=={{header|Wren}}==
Line 648 ⟶ 733:
Bytes are read from stdin and written to stdout until the return key is pressed.
<
Stdin.isRaw = true // prevents echoing to the terminal
Line 658 ⟶ 743:
}
System.print()
Stdin.isRaw = false</
=={{header|XPL0}}==
Device 1 is stdin without echoing a character to the screen. Device 0 (or 1) is
stdout, which displays the character on the monitor. This program can
list a file to the screen like this: stdio <file.txt
<syntaxhighlight lang="xpl0">int C;
loop [C:= ChIn(1);
if C = $1A \EOF\ then quit;
ChOut(0, C);
]</syntaxhighlight>
=={{header|zkl}}==
<
|