|
Self-Printing Program
Last post 05-23-2009 12:04 PM by The Wolf. 64 replies.
-
-
-
meisawesome


- Joined on 02-24-2007
- Posts 5
|
Re: Self-Printing Program
DOH edit expired... Rules: The program must have at least 1 real(not whitespace) line of source code
|
|
-
-
Angstrom


- Joined on 02-15-2006
- Posts 172
|
Re: Self-Printing Program
meisawesome:Here's a tough one: Make a program that print's it own source code.
In Brainfuck: ->+>+++>>+>++>+>+++>>+>++>>>+>+>+>++>+>>>>+++>+>>++>+>+++>>++> ++>>+>>+>++>++>+>>>>+++>+>>>>++>++>>>>+>>++>+>+++>>>++>>+++++ +>>+>>++>+>>>>+++>>+++++>>+>+++>>>++>>++>>+>>++>+>+++>>>++>>+ ++++++++++++>>+>>++>+>+++>+>+++>>>++>>++++>>+>>++>+>>>>+++>>+ ++++>>>>++>>>>+>+>++>>+++>+>>>>+++>+>>>>+++>+>>>>+++>>++>++>+ >+++>+>++>++>>>>>>++>+>+++>>>>>+++>>>++>+>+++>+>+>++>>>>>>++> >>+>>>++>+>>>>+++>+>>>+>>++>+>++++++++++++++++++>>>>+>+>>>+>> ++>+>+++>>>++>>++++++++>>+>>++>+>>>>+++>>++++++>>>+>++>>+++>+ >+>++>+>+++>>>>>+++>>>+>+>>++>+>+++>>>++>>++++++++>>+>>++>+>> >>+++>>++++>>+>+++>>>>>>++>+>+++>>+>++>>>>+>+>++>+>>>>+++>>++ +>>>+[[->>+<<]<+]+++++[->+++++++++<]>.[+]>>[<<+++++++[->+++++++++<]>- .------------------->-[-<.<+>>]<[+]<+>>>]<<<[-[-[-[>>+<++++++[->+++++<]]>+++++++ +++++++<]>+++<]++++++[->+++++++<]>+<<<-[->>>++<<<]>[->>.<<]<<] Note that whitespace is not preserved, but is semantically meaningless. With apologies to Brian Raiter.
|
|
-
-
iwpg


- Joined on 05-24-2006
- Posts 258
|
Re: Self-Printing Program
In Perl:
#! /usr/bin/perl
use strict; use warnings;
use LWP::UserAgent; use HTML::PullParser;
my $ua = new LWP::UserAgent;
my $req = new HTTP::Request(GET => "http://forums.worsethanfailure.com/forums/permalink/114219/114219/ShowThread.aspx"); my $resp = $ua->request($req);
my $p = new HTML::PullParser(doc => $resp->decoded_content, report_tags => [qw/a pre br/], start => '"S", tagname, attr', end => '"E", tagname', text => '"T", dtext', unbroken_text => 1, );
while (my $token = $p->get_token()) { last if $token->[0] eq "S" && $token->[1] eq "a" && ($token->[2]->{name} || "") eq "114219"; }
while (my $token = $p->get_token()) { last if $token->[0] eq "S" && $token->[1] eq "pre"; }
my $code = ""; while (my $token = $p->get_token()) { last if $token->[0] eq "E" && $token->[1] eq "pre"; $code .= "\n" if $token->[0] eq "S" && $token->[1] eq "br"; $code .= $token->[1] if $token->[0] eq "T"; }
print($code);
;-)
|
|
-
-
GeneWitch


- Joined on 12-23-2006
- Orange County, CA
- Posts 315
|
Re: Self-Printing Program
iwpg:
In Perl:
#! /usr/bin/perl
use strict; use warnings;
use LWP::UserAgent; use HTML::PullParser;
my $ua = new LWP::UserAgent;
my $req = new HTTP::Request(GET => "http://forums.worsethanfailure.com/forums/permalink/114219/114219/ShowThread.aspx"); my $resp = $ua->request($req);
my $p = new HTML::PullParser(doc => $resp->decoded_content, report_tags => [qw/a pre br/], start => '"S", tagname, attr', end => '"E", tagname', text => '"T", dtext', unbroken_text => 1, );
while (my $token = $p->get_token()) { last if $token->[0] eq "S" && $token->[1] eq "a" && ($token->[2]->{name} || "") eq "114219"; }
while (my $token = $p->get_token()) { last if $token->[0] eq "S" && $token->[1] eq "pre"; }
my $code = ""; while (my $token = $p->get_token()) { last if $token->[0] eq "E" && $token->[1] eq "pre"; $code .= "\n" if $token->[0] eq "S" && $token->[1] eq "br"; $code .= $token->[1] if $token->[0] eq "T"; }
print($code);
;-)
HAHAHA that's fucked up.
you're gunna get a FILE-NOT-FOUND error soon though, ain't ya?
sudo make me a sandwich
|
|
-
-
asuffield


- Joined on 05-31-2006
- Posts 2,137
|
Re: Self-Printing Program
This stopped being difficult when languages gained self-referential capabilities
In Perl:
seek DATA,0,0;print while <DATA>
__END__
|
|
-
-
ammoQ


- Joined on 04-13-2005
- Vienna.Austria.Europe.Earth
- Posts 3,530
|
Re: Self-Printing Program
meisawesome:Here's a tough one: Make a program that print's it own source code.
int main(int argc, char *argv[]) { char *s="int main(int argc, char *argv[]) { char *s=%c%s%c; printf(s,34,s,34,10); return 0; }%c"; printf(s,34,s,34,10); return 0; } Disclaimer: I've written this little program all by myself, without peeking into existing solutions (but knowing the basic concept). Anyway, due to the nature of this challenge, it's likely that a very similar or even identical solution already exists somewhere on the web.
beanbag girl 4ever ... or maybe Astah girl?
|
|
-
-
ammoQ


- Joined on 04-13-2005
- Vienna.Austria.Europe.Earth
- Posts 3,530
|
Re: Self-Printing Program
Now to something less boring than C: the self-replicating SQL*PLUS script set feedback off set head off set linesize 32767 create or replace function selfrep return varchar2 is s varchar2(2000) := 'set feedback offAset head offAset linesize 32767Acreate or replace function selfrep return varchar2 isAs varchar2(2000) := BCB;AbeginAreturn replace(replace(replace(s,chr(65),chr(10)),chr(66),chr(39)),chr(67),s);Aend;A/Aselect selfrep from dual;A'; begin return replace(replace(replace(s,chr(65),chr(10)),chr(66),chr(39)),chr(67),s); end; / select selfrep from dual;
beanbag girl 4ever ... or maybe Astah girl?
|
|
-
-
sirhegel


- Joined on 01-28-2007
- Posts 23
|
Re: Self-Printing Program
I have heard, that level 2 version of this problem is to write source code for program A, which outputs source code for program B, which in turn outputs source code for program A. I am too stupid to solve these by myself, but it would be interesting to see if this could be solved by using CodeDom in .Net.
|
|
-
-
ammoQ


- Joined on 04-13-2005
- Vienna.Austria.Europe.Earth
- Posts 3,530
|
Re: Self-Printing Program
sirhegel:I have heard, that level 2 version of this problem is to write source code for program A, which outputs source code for program B, which in turn outputs source code for program A. I am too stupid to solve these by myself, but it would be interesting to see if this could be solved by using CodeDom in .Net.
Here we go, Level 2... the only difference between program A and program B is one character in a comment.. better than nothing, I guess...It's a modified version of the SQL*PLUS script.
set feedback off set head off set linesize 32767 create or replace function selfrep return varchar2 is s varchar2(2000) := 'set feedback offAset head offAset linesize 32767Acreate or replace function selfrep return varchar2 isAs varchar2(2000) := BCB;AbeginA-- DAreturn replace(replace(replace(replace(replace(replace(replace(s,chr(65),chr(10)),chr(66),chr(39)),chr(67),s),chr(68),chr(70)),chr(69),chr(71)),chr(70),chr(69)),chr(71),chr(68));Aend;A/Aselect selfrep from dual;A'; begin -- D return replace(replace(replace(replace(replace(replace(replace(s,chr(65),chr(10)),chr(66),chr(39)),chr(67),s),chr(68),chr(70)),chr(69),chr(71)),chr(70),chr(69)),chr(71),chr(68)); end; / select selfrep from dual;
beanbag girl 4ever ... or maybe Astah girl?
|
|
-
-
kryptn


- Joined on 03-06-2007
- Posts 1
|
Re: Self-Printing Program
in php:
<?php
$self = substr($_SERVER['SCRIPT_NAME'], 1); highlight_string(file_get_contents($self));
?>
|
|
-
-
Pap


- Joined on 09-12-2006
- Earf
- Posts 281
|
Re: Self-Printing Program
Who can beat 21 characters?
<?readfile(__FILE__);
|
|
-
-
iwpg


- Joined on 05-24-2006
- Posts 258
|
Re: Self-Printing Program
Pap:Who can beat 21 characters?
How about:
|
|
-
-
Pap


- Joined on 09-12-2006
- Earf
- Posts 281
|
Re: Self-Printing Program
Rules: The program must have at least 1 real(not whitespace) line of source code
|
|
-
-
Pap


- Joined on 09-12-2006
- Earf
- Posts 281
|
Re: Self-Printing Program
I'm more interested in self-replicating WTFs... you know, the ones where the comments are more retarded than the original story. Anyone have any favorites of these they would care to link to?
|
|
-
-
iwpg


- Joined on 05-24-2006
- Posts 258
|
Re: Self-Printing Program
Pap:Rules: The program must have at least 1 real(not whitespace) line of source code
Bah, how boring.
|
|
-
-
newfweiler


- Joined on 10-18-2006
- Newf Hampshire
- Posts 385
|
Re: Self-Printing Program
Anyone remember the Wang 2200? The shortest self-reproducing program for it was:
^Error 42
If you typed that in, the response would be:
^Error 42
|
|
-
-
iwpg


- Joined on 05-24-2006
- Posts 258
|
Re: Self-Printing Program
iwpg:
Pap:Who can beat 21 characters?
How about:
Seriously though, Unix shell:
cat $0
|
|
-
-
klmann


- Joined on 03-08-2007
- Posts 2
|
Re: Self-Printing Program
Python:
import sys
print file(sys.argv[0]).read()
|
|
-
-
JvdL


- Joined on 01-26-2007
- Spain
- Posts 212
|
Re: Self-Printing Program
This is a no-brainer in JavaScript:
function program() {
alert(program);
return "Brillant";
}
|
|
-
-
aikii


- Joined on 05-18-2006
- Brussels
- Posts 141
|
Re: Self-Printing Program
How about a program that opens its source in word, prints the screenshot that lands on a wooden table, triggers a shot by a digital camera and then exports the picture to a website ?
|
|
-
-
Albatross


- Joined on 10-31-2005
- Calgary, AB
- Posts 310
|
Re: Self-Printing Program
In Powershell:
function TypeMe() { Get-Content "Function:\TypeMe" }
It's funny because it's out of context.
|
|
-
-
-
-
Faxmachinen


- Joined on 03-19-2007
- Posts 383
|
In C, with nice formatting
sirhegel:
I have heard, that level 2 version of this problem is to write source code for program A, which outputs source code for program B, which in turn outputs source code for program A.
#include <stdio.h>
int main() { const int id = 0; char *prog = "#include <stdio.h>%c%cint main()%c{%c const int id = %d;%c char *prog = %c%s%c;%c printf(prog, 10, 10, 10, 10, id^1, 10, 34, prog, 34, 10, 10, 10);%c return 0;%c}"; printf(prog, 10, 10, 10, 10, id^1, 10, 34, prog, 34, 10, 10, 10); return 0; }
Mr. "I got 90% on meta-critic"
|
|
-
-
mlambir


- Joined on 03-08-2006
- Posts 1
|
Re: Self-Printing Program
Python (without using read...): s="s=%c%s%c%cprint(s%c(34,s,34,10,37))" print(s%(34,s,34,10,37))
|
|
-
-
m0ffx


- Joined on 08-15-2006
- Posts 602
|
Re: Self-Printing Program
Hehe. One year a zero-length 'quine' one the IOCCC prize for "Worst abuse of the rules". In English "Repeat exactly what I just said"
TRWTF is Community Server
|
|
-
-
Eternal Density


- Joined on 03-25-2007
- Posts 414
|
Re: Self-Printing Program
I'd really like to see a quine written *in* Whitespace.
Plz send teh coedz!
To mah dorm.
|
|
-
-
plazmo


- Joined on 12-12-2005
- Posts 100
|
Re: Self-Printing Program
<%@ Page Language="C#" %> <script runat="server"> protected void Page_Init(object sender, EventArgs e){ Response.Write(System.IO.File.ReadAllText(Request.PhysicalPath)); } </script>
|
|
-
-
Zylon


- Joined on 12-14-2006
- Posts 441
|
Re: Self-Printing Program
Atari 8K BASIC: 10 LIST Heh.
|
|
-
-
UnFleshed One


- Joined on 07-27-2006
- Posts 36
|
Re: Self-Printing Program
Bash :)
echo $BASH_COMMAND
|
|
-
-
plazmo


- Joined on 12-12-2005
- Posts 100
|
Re: Self-Printing Program
CREATE PROC SP_ME AS SELECT ROUTINE_DEFINITION FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_NAME = 'SP_ME' AND ROUTINE_TYPE='PROCEDURE'
|
|
-
-
ShawnD


- Joined on 01-10-2006
- Posts 21
|
Re: Self-Printing Program
Rexx /* */ say sourceline(1)
|
|
-
-
-
-
cangulo


- Joined on 04-23-2007
- A beautiful town, Brasil
- Posts 18
|
Re: Self-Printing Program
<?php echo implode("\n", file(__FILE__)); ?>
|
|
-
-
PSWorx


- Joined on 04-28-2006
- Posts 1,305
|
Re: Self-Printing Program
Because someone had to do it
(My apologies for the length)
<?xml version="1.0" encoding="UTF-8" ?> <?xml-stylesheet type="text/xsl" href="http://forums.worsethanfailure.com/forums/AddPost.aspx?PostID=119432#" ?> <s:stylesheet version="1.0" xmlns:s="http://www.w3.org/1999/XSL/Transform"> <s:output method="html" /> <s:strip-space elements="*" /> <s:preserve-space elements="s:text" /> <s:template name="line"> <br /> <s:for-each select="ancestor::*"> <s:text><![CDATA[ ]]></s:text> </s:for-each> </s:template> <s:template name="tagHead"> <s:text><![CDATA[<]]></s:text> <span class="tagName"> <s:value-of select="name()" /> </span> <s:for-each select="./@*"> <s:text><![CDATA[ ]]></s:text> <span class="attribName"> <s:value-of select="name()" /> </span> <s:text><![CDATA[="]]></s:text> <span class="attribValue"> <s:value-of select="." /> </span> <s:text><![CDATA["]]></s:text> </s:for-each> </s:template> <s:template name="tagTail"> <s:text><![CDATA[</]]></s:text> <span class="tagName"> <s:value-of select="name()" /> </span> <s:text><![CDATA[>]]></s:text> </s:template> <s:template match="//*"> <s:call-template name="line" /> <s:call-template name="tagHead" /> <s:text><![CDATA[ />]]></s:text> </s:template> <s:template match="//*[child::*]"> <s:call-template name="line" /> <s:call-template name="tagHead" /> <s:text><![CDATA[>]]></s:text> <s:apply-templates /> <s:call-template name="line" /> <s:call-template name="tagTail" /> </s:template> <s:template match="//s:text|//style|//title|//span[@class='system']"> <s:call-template name="line" /> <s:call-template name="tagHead" /> <s:text><![CDATA[>]]></s:text> <span class="cdataMarkup"> <s:text><![CDATA[<![CDATA[]]></s:text> </span> <span class="textValue"> <s:value-of select="text()" /> </span> <span class="cdataMarkup"> <s:text><![CDATA[]]]]></s:text> <s:text><![CDATA[>]]></s:text> </span> <s:call-template name="tagTail" /> </s:template> <s:template match="/s:stylesheet"> <html> <head> <title><![CDATA[Quine!!!]]></title> <style><![CDATA[ pre { background-color:rgb(255,255,201); border-color:rgb(255,255,0); border-width:3px; border-style:solid; padding:3px; } .tagName {color:DarkBlue;font-weight:bold;} .attribName {color:Blue;} .attribValue {color:DarkGoldenRod;font-style:italic;} .cdataMarkup {color:DarkMagenta;} .textValue {color:DarkOliveGreen;font-style:italic;} .system {color:BlueViolet;} ]]></style> </head> <body> <H1> <s:value-of select="//title" /> </H1> <code> <pre> <span class="system"><![CDATA[<?xml version="1.0" encoding="UTF-8" ?>]]></span> <br /> <span class="system"><![CDATA[<?xml-stylesheet type="text/xsl" href="http://forums.worsethanfailure.com/forums/AddPost.aspx?PostID=119432#" ?>]]></span> <br /> <s:for-each select="/s:stylesheet"> <s:call-template name="tagHead" /> </s:for-each> <span class="system"><![CDATA[ xmlns:s]]></span> <s:text><![CDATA[="]]></s:text> <span class="attribValue"> <s:text><![CDATA[http://www.w3.org/1999/XSL/Transform]]></s:text> </span> <s:text><![CDATA[">]]></s:text> <s:apply-templates /> <br /> <s:for-each select="/s:stylesheet"> <s:call-template name="tagTail" /> </s:for-each> </pre> </code> </body> </html> </s:template> </s:stylesheet>
(Apparently the WTF forum tries to be "smart" and messes up my stylesheet links... those href="http://forums.worsethanfailure.com/..."s are supposed to be just "#".)
|
|
-
-
zachninme


- Joined on 04-26-2007
- Posts 3
|
Re: Self-Printing Program
An interesting concept for all languages, here in PHP: <?php <b>Parse error</b>: syntax error, unexpected '<' in <b>/opt/lampp/htdocs/selfrep.php</b> on line <b>2</b> ?> Of course, this is if your php files are in /opt/lampp/htdocs/, and the file is called "selfrep.php", but it works :P
|
|
-
-
flop


- Joined on 04-27-2007
- Posts 127
|
Re: Self-Printing Program
For bash, ksh and probably others:
z='echo "$x$z$y\"$x\" ; y=\"$y\" ; $z"' ; x="z='" ; y="' ; x=" ; echo "$x$z$y\"$x\" ; y=\"$y\" ; $z"
|
|
-
-
Worf


- Joined on 05-15-2007
- Posts 14
|
Re: Self-Printing Program
sirhegel:I have heard, that level 2 version of this problem is to write source code for program A, which outputs source code for program B, which in turn outputs source code for program A. I am too stupid to solve these by myself, but it would be interesting to see if this could be solved by using CodeDom in .Net.
A recent (a couple years ago or so) back, the IOCCC had such an entry. It started out formatted as a Japanese character. Compile, run, outputs source code in the form of another Japanese character. Compile/run, you get a third, and so on for at least 6 times, at which point, you ended up with the beginning character.
|
|
-
-
Random832


- Joined on 05-09-2007
- Posts 484
|
Re: Self-Printing Program
[i did the #!/bin/cat, then noticed someone else had after the delete time limit ran out]
|
|
-
-
gremlin


- Joined on 01-05-2006
- Posts 80
|
Re: Self-Printing Program
dillybar1:
No, that doesn't print out its source code.
the forum software ate my colon
|
|
-
-
AbbydonKrafts


- Joined on 11-21-2006
- Carrollton, GA, USA
- Posts 1,022
|
Re: Self-Printing Program
gremlin: No, that doesn't print out its source code.
Sure it does. The only time it doesn't is if ECHO OFF was used before running it. Sample output of "TESTBAT.BAT" that only contains the statement "REM": C:\>testbat C:\>REM C:\>echo off testbat echo on C:\>
Join us at #TDWTF on irc.slashnet.org !
|
|
-
-
zzo38


- Joined on 02-10-2008
- Posts 352
|
Re: Self-Printing Program
This is called a quine and it has been done a lot already. I have made one quine program in MS-DOS:
@echo off
%1 %2
call %0 goto e %%
call %0 goto e %%3 echo.%%4
echo :f
goto f
:e
echo.%4@echo off
echo.%4%31 %32
echo.%4call %30 goto e %3%3
echo.%4call %30 goto e %3%33 echo.%3%34
echo.%4echo :f
echo.%4goto f
echo.%4:e
:f
|
|
-
-
-
-
mfah


- Joined on 12-01-2007
- Posts 117
|
Re: Self-Printing Program
AbbydonKrafts: gremlin: No, that doesn't print out its source code.
Sure it does. The only time it doesn't is if ECHO OFF was used before running it. Sample output of "TESTBAT.BAT" that only contains the statement "REM":
C:\>testbat
C:\>REM
C:\>echo off testbat echo on
C:\>
@echo off type printme.bat
|
|
-
-
Spectre


- Joined on 05-09-2007
- ::1
- Posts 1,027
|
Re: Self-Printing Program
OK, since the thread is resurrected I decided I'll contribute as well:
puts [format [set s {puts [format [set s {%s}] $s]}] $s]
Guessing the language is left as an exercise to the reader 9=]. (Note, to make it perfect, the file must be terminated by a newline.)
╩юфют√ь ёЄЁрэшЎрь яюЁр эр яхэёш■.
#TDWTF @ SlashNET was merged into #codelove @ the same network. You're still welcome to drop by. I guess.
|
|
-
-
asuffield


- Joined on 05-31-2006
- Posts 2,137
|
Re: Self-Printing Program
Spectre:puts [format [set s {puts [format [set s {%s}] $s]}] $s]
Guessing the language is left as an exercise to the reader
That's expect, you pervert. Why don't you write something in COBOL while you're at it?
|
|
-
-
Spectre


- Joined on 05-09-2007
- ::1
- Posts 1,027
|
Re: Self-Printing Program
asuffield: Spectre:
puts [format [set s {puts [format [set s {%s}] $s]}] $s]
Guessing the language is left as an exercise to the reader
That's expect, you pervert. Why don't you write something in COBOL while you're at it?
Tcl actually, but I guess Expect would run it too.
╩юфют√ь ёЄЁрэшЎрь яюЁр эр яхэёш■.
#TDWTF @ SlashNET was merged into #codelove @ the same network. You're still welcome to drop by. I guess.
|
|
|
|
|