Copy stdin to stdout: Difference between revisions

(Frink)
 
(39 intermediate revisions by 26 users not shown)
Line 2:
 
Create an executable file that copies stdin to stdout, or else a script that does so through the invocation of an interpreter at the command line.
 
=={{header|8086 Assembly}}==
 
<syntaxhighlight lang="asm">READ: equ 3Fh ; MS-DOS syscalls
WRITE: equ 40h
BUFSZ: equ 4000h ; Buffer size
cpu 8086
bits 16
org 100h
section .text
read: mov ah,READ ; Read into buffer
xor bx,bx ; From STDIN (file 0)
mov cx,BUFSZ
mov dx,buffer
int 21h
test ax,ax ; Did we read anything?
jz done ; If not, stop
xchg ax,cx ; Write as many bytes as read
mov ah,WRITE
inc bx ; To STDOUT (file 1)
int 21h
jmp read ; Go get more
done: ret
section .bss
buffer: resb BUFSZ</syntaxhighlight>
 
=={{header|Action!}}==
<syntaxhighlight lang="action!">PROC Main()
CHAR c
 
DO
c=GetD(7)
Put(c)
UNTIL c=27 ;repeat until Esc key is pressed
OD
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Copy_stdin_to_stdout.png Screenshot from Atari 8-bit computer]
<pre>
COPY STDIN TO STDOUT
</pre>
 
=={{header|Ada}}==
 
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO;
 
procedure Copy_Stdin_To_Stdout is
Line 15 ⟶ 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 34 ⟶ 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}}==
<syntaxhighlight lang="bcpl">get "libhdr"
 
let start() be
$( let c = rdch()
if c = endstreamch then finish
wrch(c)
$) repeat</syntaxhighlight>
 
=={{header|Brainf***}}==
<syntaxhighlight lang="brainf***">,[.,]</syntaxhighlight>
 
=={{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 51 ⟶ 118:
return 0;
}
</syntaxhighlight>
</lang>
 
=={{header|C sharp|C#}}==
{{works with|.NET Framework|4.0 or later}}
<syntaxhighlight lang="csharp">
using System;
 
class Program
{
static void Main(string[] args)
{
Console.OpenStandardInput().CopyTo(Console.OpenStandardOutput());
}
}
</syntaxhighlight>
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <iterator>
 
Line 66 ⟶ 147:
);
return 0;
}</langsyntaxhighlight>
 
Shorter and quicker alternative:
<langsyntaxhighlight lang="cpp">#include <iostream>
 
int main() {
std::cout << std::cin.rdbuf();
}</langsyntaxhighlight>
 
=={{header|CLU}}==
<syntaxhighlight lang="clu">start_up = proc ()
pi: stream := stream$primary_input()
po: stream := stream$primary_output()
while true do
stream$putc(po, stream$getc(pi))
end except when end_of_file:
return
end
end start_up</syntaxhighlight>
 
=={{header|Commodore BASIC}}==
 
Commodore BASIC assigns device #0 (keyboard) as the standard input device, and device #3 (screen) as the standard output device. By opening channels for input and/or output, data can be sent to/from files, printers, and other peripheral devices. Although the keyboard and screen are the default input and output devices, they can be read from and written to using the same conventions as other devices, which is useful if a program wants to allow a user to direct output elsewhere (e.g. printing a hard copy of a report instead of displaying it on the screen)
 
{| class="wikitable"
|+Commodore Device Addresses
!Number
!Device
|-
|0
|Keyboard
|-
|1
|Datassette Tape Drive
|-
|2
|User Port (RS-232)
|-
|3
|Text Screen
|-
|4
|rowspan="2"|IEC Bus Printers
|-
|5
|-
|6
|rowspan="2"|IEC Bus Plotters
|-
|7
|-
|8-30
|Floppy/Hard Disk Drives
|}
 
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.
 
<syntaxhighlight 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"
13 print "8-11:Disk Drives":print
14 input "Input device";d1
15 if d1=1 or d1>=8 then input "Filename for INPUT";i$
16 input "Output device";d2
17 if d2=1 or d2>=8 then input "Filename for OUTPUT";o$
18 print:if d1=0 then print "Begin typing. Press CTRL-Z to end.":print
20 open 5,d1,5,"0:"+i$+",s,r"
30 open 2,d2,2,"@0:"+o$+",s,w"
40 get#5,a$
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</syntaxhighlight>
 
{{output}}
 
The output sample below demonstrates the following device input-output combinations:
* Keyboard (0) to Screen (3)
* Keyboard (0) to Disk File (8)
* Disk File (8) to Screen (3)
 
 
<pre>
0:Keyboard 1:Tape 2:RS-232 3:Screen
4-7:printers/plotters
8-11:Disk Drives
Input device? 0
Output device? 3
Begin typing. Press CTRL-Z to end.
Hello. This is so much fun on Rosetta Code!
Goodbye!
ready.
run
 
0:Keyboard 1:Tape 2:RS-232 3:Screen
4-7:printers/plotters
8-11:Disk Drives
Input device? 0
Output device? 8
Filename for OUTPUT? rosetta.txt
Begin typing. Press CTRL-Z to end.
 
[No echo of text because output is directed to file.]
ready.
run
 
0:Keyboard 1:Tape 2:RS-232 3:Screen
4-7:printers/plotters
8-11:Disk Drives
Input device? 8
Filename for INPUT? rosetta.txt
Output device? 3
These device numbers are unique to the Commodore line of 8-bit computers.
ready.
&#9608;
</pre>
 
=={{header|Common Lisp}}==
<syntaxhighlight 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>
 
=={{header|Crystal}}==
<syntaxhighlight lang="ruby">STDIN.each_line do |line|
puts line
end</syntaxhighlight>
 
=={{header|D}}==
<langsyntaxhighlight lang="d">import std.stdio;
 
void main() {
Line 82 ⟶ 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}}==
<syntaxhighlight 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
do
char = getchar()
if char = FIN then exit do else putchar(char)
loop</syntaxhighlight>
 
=={{header|Frink}}==
The special string "-" indicates reading from stdin.
<lang frink>while (line = readStdin[]) != undef
<syntaxhighlight lang="frink">print[read["-"]]</syntaxhighlight>
println[line]</lang>
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 128 ⟶ 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}}==
<syntaxhighlight lang="groovy">class StdInToStdOut {
static void main(args) {
try (def reader = System.in.newReader()) {
def line
while ((line = reader.readLine()) != null) {
println line
}
}
}
}</syntaxhighlight>
 
=={{header|Haskell}}==
<langsyntaxhighlight Haskelllang="haskell">main = interact id </langsyntaxhighlight>
 
=={{header|Java}}==
Copies until no more input.
<langsyntaxhighlight lang="java">
import java.util.Scanner;
 
Line 150 ⟶ 430:
 
}
</syntaxhighlight>
</lang>
{{out}}
Output interleaved. Stdin and Stdout are same window.
Line 160 ⟶ 440:
 
</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 .</syntaxhighlight>
 
=={{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 173 ⟶ 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 184 ⟶ 479:
 
=={{header|Mercury}}==
<syntaxhighlight lang="mercury">
<lang Mercury>
 
:- module stdin_to_stdout.
Line 227 ⟶ 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}}==
<syntaxhighlight lang="scheme">
(bytestream->port (port->bytestream stdin) stdout)
</syntaxhighlight>
 
=={{header|Pascal}}==
<langsyntaxhighlight lang="pascal">program writeInput(input, output);
var
buffer: char;
Line 251 ⟶ 551:
write(buffer); // shorthand for write(output, buffer)
end;
end.</langsyntaxhighlight>
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">
perl -pe ''
</syntaxhighlight>
</lang>
 
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(notonline)-->
<lang Phix>while true do
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span>
integer ch = wait_key()
<span style="color: #008080;">while</span> <span style="color: #004600;">true</span> <span style="color: #008080;">do</span>
if ch=#1B then exit end if
<span style="color: #004080;">integer</span> <span style="color: #000000;">ch</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">wait_key</span><span style="color: #0000FF;">()</span>
puts(1,ch)
<span style="color: #008080;">if</span> <span style="color: #000000;">ch</span><span style="color: #0000FF;">=</span><span style="color: #000000;">#1B</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end while</lang>
<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>
<!--</syntaxhighlight>-->
 
=={{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 278 ⟶ 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 292 ⟶ 595:
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">#lang racket
 
(let loop ()
Line 298 ⟶ 601:
[(? eof-object?) (void)]
[c (display c)
(loop)]))</langsyntaxhighlight>
 
=={{header|Raku}}==
Line 304 ⟶ 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 315 ⟶ 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 334 ⟶ 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}}==
 
For Scala 2's compiler <code>scalac</code>, a containing object is required:
 
<syntaxhighlight lang="scala">object CopyStdinToStdout extends App {
io.Source.fromInputStream(System.in).getLines().foreach(println)
}</syntaxhighlight>
 
If it's being run directly by <code>scala</code>, it can be shortened to one line, and run directly in the shell:
 
<syntaxhighlight lang="bash">scala -e "io.Source.fromInputStream(System.in).getLines().foreach(println)"</syntaxhighlight>
 
=={{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 363 ⟶ 682:
begin
copyFile(IN, OUT);
end func;</langsyntaxhighlight>
 
=={{header|Smalltalk}}==
{{Works with|Smalltalk/X}}
<syntaxhighlight lang="smalltalk">"using Stream class's bulk copy method:"
Stdin copyToEndInto:Stdout.
 
"line wise"
[Stdin atEnd] whileFalse:[ Stdout nextPutLine:(Stdin nextLine) ].
 
"character wise"
[Stdin atEnd] whileFalse:[ Stdout nextPut:(Stdin next) ].
 
"no EOF test, but handle EOF Exception"
[
[ Stdout nextPut:(Stdin next) ] loop.
] on: StreamError do:[]</syntaxhighlight>
 
=={{header|Standard ML}}==
<syntaxhighlight lang="sml">fun copyLoop () =
case TextIO.input TextIO.stdIn of
"" => ()
| tx => copyLoop (TextIO.output (TextIO.stdOut, tx))
 
val () = copyLoop ()</syntaxhighlight>
 
=={{header|Symsyn}}==
<syntaxhighlight lang="symsyn">
<lang Symsyn>
Loop [] []
go Loop
</syntaxhighlight>
</lang>
 
=={{header|Tcl}}==
<syntaxhighlight lang="tcl">package require Tcl 8.5
 
chan copy stdin stdout
# fcopy stdin stdout for older versions</syntaxhighlight>
 
=={{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 375 ⟶ 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 385 ⟶ 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