Print itself
Create a program, which prints its source code to the stdout!
- Related tasks
11l
<lang 11l>
- start:
-V sourceFileName = fs:path:split_ext(:argv[0])[0]‘.11l’ print(File(sourceFileName).read()) </lang>
Batch File
<lang dos> @echo off for /f "tokens=*" %%s in (%~n0%~x0) do (echo %%s) </lang>
FreeBASIC
This works for Linux. <lang freebasic>shell( "cat quine.bas" )</lang>
Furor
<lang Furor> 1 argv getfile dup sprint free end </lang>
Go
<lang go>package main
import (
"fmt" "io/ioutil" "log" "os" "path"
)
func main() {
self := path.Base(os.Args[0]) + ".go" bytes, err := ioutil.ReadFile(self) if err != nil { log.Fatal(err) } fmt.Print(string(bytes))
}</lang>
- Output:
Just the invoking line as remainder is, of course, as above.
$ go run self_print.go
Julia
The running program's filename is referenced as the builtin PROGRAM_FILE variable in Julia. <lang julia>""" Read the program file and print it. """ printitself() = print(read(PROGRAM_FILE, String))
printitself() </lang>
Nanoquery
<lang Nanoquery>println new(Nanoquery.IO.File).open(args[1]).readAll()</lang>
Nim
We suppose that the source file is in the same directory as the executable file. <lang Nim>import os
let execFile = getAppFilename() let sourceFile = execFile.addFileExt("nim") stdout.write sourceFile.readFile()</lang>
Perl
<lang Perl># 20201011 added Perl programming solution
use strict; use warnings;
open my $in, '<', $0 or die; print while <$in>; close($in)
- @ARGV=$0; print <> # slurp without an explicit open()</lang>
Phix
Interpreted only: <lang Phix>puts(1,get_text(command_line()[2]))</lang>
- Output:
puts(1,get_text(command_line()[2]))
Interpreted or compiled - latter only works while executable and source are still in the same directory, and not renamed. <lang Phix>puts(1,get_text(substitute(command_line()[2],".exe",".exw")))</lang>
- Output:
>p test ;; or p -c test puts(1,get_text(substitute(command_line()[2],".exe",".exw")))
Alternative - see the docs (ie phix.chm) for an explanation of the ("") and [1][2]: <lang Phix>?get_text(include_path("")&include_files()[1][2])</lang>
- Output:
"?get_text(include_path("")&include_files()[1][2])"
PowerShell
<lang PowerShell> Write-Host $MyInvocation.MyCommand </lang>
Python
<lang python>import sys with open(sys.argv[0],'r') as input:
for row in input: print(row, end=)</lang>
Raku
Not really sure what the point of this task is.
Is it supposed to be a quine? <lang perl6>my &f = {say $^s, $^s.raku;}; f "my \&f = \{say \$^s, \$^s.raku;}; f " </lang>
Or just a program that when executed echoes its source to STDOUT? (Here's probably the simplest valid program that when executed, echoes its source to STDOUT. It is exceptionally short: zero bytes; and when executed echoes zero bytes to STDOUT.)
<lang perl6></lang>
Or are we supposed to demonstrate how to locate the currently executing source code file and incidentally, print it.
<lang perl6>print $*PROGRAM.slurp</lang>
Whatever. Any of these satisfy the rather vague specifications.
REXX
<lang rexx>/*REXX program prints its own multi─line source to the standard output (stdout). */
do j=1 for sourceline() call lineout , sourceline(j) end /*j*/ /*stick a fork in it, we're all done. */</lang>
Ring
<lang ring> fileName = filename() fp = fopen(fileName,"r") ? read(filename()) fclose(fp) </lang>
- Output:
fileName = filename() fp = fopen(fileName,"r") ? read(filename()) fclose(fp)
Wren
<lang ecmascript>import "os" for Process import "io" for File
var args = Process.allArguments System.write(File.read(args[1]))</lang>
- Output:
Just the invoking line as remainder is, of course, as above.
$ wren self_print.wren
Z80 Assembly
This program prints its bytecode as text to the Amstrad CPC terminal. The program is run by typing call &1000
to the screen.
<lang z80>org &1001
ld hl,&1001 ;&1000 won't work since that contains &00. ld e,&ff ;&00 is the terminator so we can't use zero as an immediate directly inc e ;Must abuse 8-bit underflow to get it. loop: ld a,(hl) cp e jp z,ProgramEnd
call UnpackNibbles ld a,b cp &0A jr c,noCorrectHex_B add &07 noCorrectHex_B: add &30 call &bb5a
ld a,c cp &0A jr c,noCorrectHex_C add &07 noCorrectHex_C: add &30 call &bb5a inc hl jp loop ProgramEnd: ld a,&30 call &bb5a ld a,&30 call &bb5a ret ;return to basic
UnpackNibbles:
- splits a into its component nibbles, storing high nibble in B and low in C.
push af and &0f ld c,a pop af and &f0 rrca rrca rrca rrca ld b,a ret
db 0 ;null terminator. No instructions or operands above compile to &00. </lang>
- Output: