Copy stdin to stdout: Difference between revisions

(added Ol)
 
(15 intermediate revisions by 11 users not shown)
Line 5:
=={{header|8086 Assembly}}==
 
<langsyntaxhighlight lang="asm">READ: equ 3Fh ; MS-DOS syscalls
WRITE: equ 40h
BUFSZ: equ 4000h ; Buffer size
Line 26:
done: ret
section .bss
buffer: resb BUFSZ</langsyntaxhighlight>
 
=={{header|Action!}}==
<langsyntaxhighlight Actionlang="action!">PROC Main()
CHAR c
 
Line 37:
UNTIL c=27 ;repeat until Esc key is pressed
OD
RETURN</langsyntaxhighlight>
{{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}}==
 
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO;
 
procedure Copy_Stdin_To_Stdout is
Line 56:
Put (C);
end loop;
end Copy_Stdin_To_Stdout;</langsyntaxhighlight>
 
=={{header|Aime}}==
<langsyntaxhighlight lang="aime">file f;
data b;
f.stdin;
while (f.b_line(b) ^ -1) {
o_(b, "\n");
}</langsyntaxhighlight>
 
=={{header|ALGOL 68}}==
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}}
<langsyntaxhighlight lang="algol68">BEGIN
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</langsyntaxhighlight>
 
=={{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.
<langsyntaxhighlight AWKlang="awk">awk "//"</langsyntaxhighlight>
 
=={{header|BCPL}}==
<langsyntaxhighlight lang="bcpl">get "libhdr"
 
let start() be
Line 88 ⟶ 90:
if c = endstreamch then finish
wrch(c)
$) repeat</langsyntaxhighlight>
 
=={{header|Brainf***}}==
<syntaxhighlight lang ="brainf***">,[.,]</langsyntaxhighlight>
 
=={{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">
<lang C>
#include <stdio.h>
 
Line 104 ⟶ 118:
return 0;
}
</syntaxhighlight>
</lang>
 
=={{header|C sharp|C#}}==
{{works with|.NET Framework|4.0 or later}}
<langsyntaxhighlight lang="csharp">
using System;
 
Line 118 ⟶ 132:
}
}
</syntaxhighlight>
</lang>
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <iterator>
 
Line 133 ⟶ 147:
);
return 0;
}</langsyntaxhighlight>
 
Shorter and quicker alternative:
<langsyntaxhighlight lang="cpp">#include <iostream>
 
int main() {
std::cout << std::cin.rdbuf();
}</langsyntaxhighlight>
 
=={{header|CLU}}==
<langsyntaxhighlight lang="clu">start_up = proc ()
pi: stream := stream$primary_input()
po: stream := stream$primary_output()
Line 152 ⟶ 166:
return
end
end start_up</langsyntaxhighlight>
 
=={{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.
 
<langsyntaxhighlight lang="gwbasic">10 print chr$(147);chr$(14);
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</langsyntaxhighlight>
 
{{output}}
Line 262 ⟶ 276:
 
=={{header|Common Lisp}}==
<langsyntaxhighlight lang="lisp">#|Loops while reading and collecting characters from STDIN until EOF (C-Z or C-D)
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>
</lang>
 
=={{header|Crystal}}==
<langsyntaxhighlight lang="ruby">STDIN.each_line do |line|
puts line
end</langsyntaxhighlight>
 
=={{header|D}}==
<langsyntaxhighlight lang="d">import std.stdio;
 
void main() {
Line 281 ⟶ 295:
writeln(line);
}
}</langsyntaxhighlight>
 
=={{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#}}==
<langsyntaxhighlight lang="fsharp">
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>
</lang>
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: io kernel ;
 
[ read1 dup ] [ write1 ] while drop</langsyntaxhighlight>
 
=={{header|Forth}}==
{{works with|gforth|0.7.3}}
<syntaxhighlight lang ="forth">stdin slurp-fid type bye</langsyntaxhighlight>
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">#define FIN 255 'eof is already a reserved word
#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</langsyntaxhighlight>
 
=={{header|Frink}}==
The special string "-" indicates reading from stdin.
<langsyntaxhighlight lang="frink">print[read["-"]]</langsyntaxhighlight>
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 336 ⟶ 382:
w.Flush()
}
}</langsyntaxhighlight>
 
===io.Copy===
<syntaxhighlight lang="go">
package main
 
import (
"io"
"os"
)
 
func main() {
io.Copy(os.Stdout, os.Stdin)
}
</syntaxhighlight>
 
=={{header|Groovy}}==
<langsyntaxhighlight Groovylang="groovy">class StdInToStdOut {
static void main(args) {
try (def reader = System.in.newReader()) {
Line 348 ⟶ 408:
}
}
}</langsyntaxhighlight>
 
=={{header|Haskell}}==
<langsyntaxhighlight Haskelllang="haskell">main = interact id </langsyntaxhighlight>
 
=={{header|Java}}==
Copies until no more input.
<langsyntaxhighlight lang="java">
import java.util.Scanner;
 
Line 370 ⟶ 430:
 
}
</syntaxhighlight>
</lang>
{{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 ="jq">jq -Rr .</langsyntaxhighlight>
 
=={{header|Julia}}==
<langsyntaxhighlight Julialang="julia">while !eof(stdin)
write(stdout, read(stdin, UInt8))
end</langsyntaxhighlight>
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">fun main() {
var c: Int
do {
Line 396 ⟶ 468:
System.out.write(c)
} while (c >= 0)
}</langsyntaxhighlight>
 
=={{header|Latitude}}==
<langsyntaxhighlight lang="latitude">while { $stdin eof? not. } do {
$stdout putln: $stdin readln.
}.</langsyntaxhighlight>
 
=={{header|Lua}}==
Line 407 ⟶ 479:
 
=={{header|Mercury}}==
<syntaxhighlight lang="mercury">
<lang Mercury>
 
:- module stdin_to_stdout.
Line 450 ⟶ 522:
 
%-----------------------------------------------------------------------------%
</syntaxhighlight>
</lang>
 
=={{header|Nim}}==
 
<syntaxhighlight lang ="nim">stdout.write readAll(stdin)</langsyntaxhighlight>
 
=={{header|OCaml}}==
 
<langsyntaxhighlight lang="ocaml">try
while true do
output_char stdout (input_char stdin)
done
with End_of_file -> ()</langsyntaxhighlight>
 
=={{header|Ol}}==
<langsyntaxhighlight lang="scheme">
(bytestream->port (port->bytestream stdin) stdout)
</syntaxhighlight>
</lang>
 
=={{header|Pascal}}==
<langsyntaxhighlight lang="pascal">program writeInput(input, output);
var
buffer: char;
Line 479 ⟶ 551:
write(buffer); // shorthand for write(output, buffer)
end;
end.</langsyntaxhighlight>
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">
perl -pe ''
</syntaxhighlight>
</lang>
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(notonline)-->
<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>
<!--</langsyntaxhighlight>-->
 
=={{header|PicoLisp}}==
<syntaxhighlight lang PicoLisp="picolisp">(in NIL (echo))</langsyntaxhighlight>
 
=={{header|Prolog}}==
<syntaxhighlight lang="prolog">
<lang Prolog>
%File: stdin_to_stdout.pl
:- initialization(main).
Line 509 ⟶ 581:
X == end_of_file,
fail.
</syntaxhighlight>
</lang>
 
Invocation at the command line (with Swi-prolog):
<syntaxhighlight lang="sh">
<lang sh>
swipl stdin_to_stdout.pl
</syntaxhighlight>
</lang>
 
=={{header|Python}}==
Line 523 ⟶ 595:
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">#lang racket
 
(let loop ()
Line 529 ⟶ 601:
[(? eof-object?) (void)]
[c (display c)
(loop)]))</langsyntaxhighlight>
 
=={{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" perl6line>raku -pe'.lines'</langsyntaxhighlight>
 
When invoked from a file: Lines are auto-chomped, so need to re-add newlines (hence .say rather than .print)
<syntaxhighlight lang="raku" perl6line>.say for lines</langsyntaxhighlight>
 
=={{header|REXX}}==
Line 546 ⟶ 618:
normally the console. &nbsp; So for REXX, this task equates to copying data
from the console to itself.
<langsyntaxhighlight lang="rexx">/*REXX pgm copies data from STDIN──►STDOUT (default input stream──►default output stream*/
 
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. */</langsyntaxhighlight>
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
? "give input: " give str
? "output: " + str
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 565 ⟶ 637:
</pre>
 
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">
$stdout << $stdin.gets
</syntaxhighlight>
=={{header|Rust}}==
<langsyntaxhighlight Rustlang="rust">use std::io;
 
fn main() {
io::copy(&mut io::stdin().lock(), &mut io::stdout().lock());
}</langsyntaxhighlight>
 
=={{header|Scala}}==
Line 576 ⟶ 652:
For Scala 2's compiler <code>scalac</code>, a containing object is required:
 
<langsyntaxhighlight Scalalang="scala">object CopyStdinToStdout extends App {
io.Source.fromInputStream(System.in).getLines().foreach(println)
}</langsyntaxhighlight>
 
If it's being run directly by <code>scala</code>, it can be shortened to one line, and run directly in the shell:
 
<langsyntaxhighlight lang="bash">scala -e "io.Source.fromInputStream(System.in).getLines().foreach(println)"</langsyntaxhighlight>
 
=={{header|Scheme}}==
 
<langsyntaxhighlight lang="scheme">
(do ((c (read-char) (read-char)))
((eof-object? c) 'done)
(display c))
</syntaxhighlight>
</lang>
 
=={{header|sed}}==
 
<syntaxhighlight lang="sh">
<lang sh>
sed -e ''
</syntaxhighlight>
</lang>
 
=={{header|Seed7}}==
 
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "fileutil.s7i";
 
Line 606 ⟶ 682:
begin
copyFile(IN, OUT);
end func;</langsyntaxhighlight>
 
=={{header|Smalltalk}}==
{{Works with|Smalltalk/X}}
<langsyntaxhighlight lang="smalltalk">"using Stream class's bulk copy method:"
Stdin copyToEndInto:Stdout.
 
Line 622 ⟶ 698:
[
[ Stdout nextPut:(Stdin next) ] loop.
] on: StreamError do:[]</langsyntaxhighlight>
 
=={{header|Standard ML}}==
<langsyntaxhighlight lang="sml">fun copyLoop () =
case TextIO.input TextIO.stdIn of
"" => ()
| tx => copyLoop (TextIO.output (TextIO.stdOut, tx))
 
val () = copyLoop ()</langsyntaxhighlight>
 
=={{header|Symsyn}}==
<syntaxhighlight lang="symsyn">
<lang Symsyn>
Loop [] []
go Loop
</syntaxhighlight>
</lang>
 
=={{header|Tcl}}==
<langsyntaxhighlight lang="tcl">package require Tcl 8.5
 
chan copy stdin stdout
# fcopy stdin stdout for older versions</langsyntaxhighlight>
 
=={{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.
<langsyntaxhighlight ecmascriptlang="wren">import "io" for Stdin, Stdout
 
Stdin.isRaw = true // prevents echoing to the terminal
Line 658 ⟶ 743:
}
System.print()
Stdin.isRaw = false</langsyntaxhighlight>
 
=={{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}}==
<langsyntaxhighlight lang="zkl">zkl --eval "File.stdout.write(File.stdin.read())"</langsyntaxhighlight>
56

edits