Programs that print their own source code

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

Re: Programs that print their own source code

Postby You, sir, name? » Thu Nov 19, 2009 7:49 pm UTC

TheChewanater wrote:
You, sir, name? wrote:
TheChewanater wrote:Creative, but a quine is "A program that prints it's own source code when run". These neither run nor print their source code (the compilers do).


That's compiled langauges though. In interpreted languages, there is very little to distinguish between "the program caused the interpreter to print" and "the program prints".


Well, it translates it into machine code, right? If it cannot do this and the interpreter prints this out, I don't think it's a quine.


Interpreters do not translate to machine code, they just evaluate the program as it is written. It may be translated to bytecode along the way, but that is not required.
Blag.
Ternary computer emulator. Latest version is 0.5 [Nov 29 2008].

Good morning, that's a nice tnetennba.
User avatar
You, sir, name?
 
Posts: 6128
Joined: Sun Apr 22, 2007 10:07 am UTC
Location: Chako Paul City

Re: Programs that print their own source code

Postby TheChewanater » Thu Nov 19, 2009 8:05 pm UTC

You, sir, name? wrote:
TheChewanater wrote:
You, sir, name? wrote:
TheChewanater wrote:Creative, but a quine is "A program that prints it's own source code when run". These neither run nor print their source code (the compilers do).


That's compiled langauges though. In interpreted languages, there is very little to distinguish between "the program caused the interpreter to print" and "the program prints".


Well, it translates it into machine code, right? If it cannot do this and the interpreter prints this out, I don't think it's a quine.


Interpreters do not translate to machine code, they just evaluate the program as it is written. It may be translated to bytecode along the way, but that is not required.


Well, then can an interpreted language be a quine, since the interpreter prints it out, and the interpreter uses an external file? (the source code)
ImageImage
http://internetometer.com/give/4279
No one can agree how to count how many types of people there are. You could ask two people and get 10 different answers.
User avatar
TheChewanater
 
Posts: 1261
Joined: Sat Aug 08, 2009 5:24 am UTC

Re: Programs that print their own source code

Postby You, sir, name? » Thu Nov 19, 2009 8:17 pm UTC

TheChewanater wrote:
Spoiler:
You, sir, name? wrote:
TheChewanater wrote:
You, sir, name? wrote:
TheChewanater wrote:Creative, but a quine is "A program that prints it's own source code when run". These neither run nor print their source code (the compilers do).


That's compiled langauges though. In interpreted languages, there is very little to distinguish between "the program caused the interpreter to print" and "the program prints".


Well, it translates it into machine code, right? If it cannot do this and the interpreter prints this out, I don't think it's a quine.


Interpreters do not translate to machine code, they just evaluate the program as it is written. It may be translated to bytecode along the way, but that is not required.


Well, then can an interpreted language be a quine, since the interpreter prints it out, and the interpreter uses an external file? (the source code)


I say it depends on the language. If the language offers access to the actual source code from within the language would probably be disqualified.

A simple thought experiment that illustrates that it, despite that, is possible: Say you write a C interpreter. It behaves like C, in every way conceivable, except it isn't compiled but interpreted on the fly. It seems very strange that a program's quineness should depend on the environment it's run in (interpreted vs. compiled), when the language behaves exactly the same way (save for run speed).
Blag.
Ternary computer emulator. Latest version is 0.5 [Nov 29 2008].

Good morning, that's a nice tnetennba.
User avatar
You, sir, name?
 
Posts: 6128
Joined: Sun Apr 22, 2007 10:07 am UTC
Location: Chako Paul City

Re: Programs that print their own source code

Postby TheChewanater » Thu Nov 19, 2009 8:32 pm UTC

It would, due to the reason pointed out earlier that you can't distinguish an error message from output, unless you don't count error messages, which was my point.

Also, it must "produce its own source code". That's not producing its source code, is it? Although I guess the definition of "produce" in this context is debatable.
ImageImage
http://internetometer.com/give/4279
No one can agree how to count how many types of people there are. You could ask two people and get 10 different answers.
User avatar
TheChewanater
 
Posts: 1261
Joined: Sat Aug 08, 2009 5:24 am UTC

Re: Programs that print their own source code

Postby You, sir, name? » Mon Nov 30, 2009 11:50 pm UTC

TheChewanater wrote:It would, due to the reason pointed out earlier that you can't distinguish an error message from output, unless you don't count error messages, which was my point.

Also, it must "produce its own source code". That's not producing its source code, is it? Although I guess the definition of "produce" in this context is debatable.


Well, the same considerations apply to compiled languages, because they can invoke behavior from the calling process. You could for example create a program that prints "Segmentation fault" to stderr and then exits abnormally. Depending on how devious you want to get (you could for example cause a real segmentation fault if standard error is piped to something that isn't a terminal), it will be very difficult to tell whether such a program is actually crashing, or just faking it.
Blag.
Ternary computer emulator. Latest version is 0.5 [Nov 29 2008].

Good morning, that's a nice tnetennba.
User avatar
You, sir, name?
 
Posts: 6128
Joined: Sun Apr 22, 2007 10:07 am UTC
Location: Chako Paul City

Re: Programs that print their own source code

Postby TheChewanater » Tue Dec 01, 2009 12:01 am UTC

You, sir, name? wrote:
TheChewanater wrote:It would, due to the reason pointed out earlier that you can't distinguish an error message from output, unless you don't count error messages, which was my point.

Also, it must "produce its own source code". That's not producing its source code, is it? Although I guess the definition of "produce" in this context is debatable.


Well, the same considerations apply to compiled languages, because they can invoke behavior from the calling process. You could for example create a program that prints "Segmentation fault" to stderr and then exits abnormally. Depending on how devious you want to get (you could for example cause a real segmentation fault if standard error is piped to something that isn't a terminal), it will be very difficult to tell whether such a program is actually crashing, or just faking it.


Funny you should mention that...

Code: Select all
#define Segmentation ;
#define fault }
#include <iostream>
main(){
const char string[900]="#define Segmentation ;nn#define fault }nn#include <iostream>nnmain(){nnconst char string[900]=qqssqq;nnnnfor(int i = 0; i < 900; i++){nnif(string[i]=='s'&&string[i+1] == 's'){std::cout << string;i++;}nnelse if(string[i]=='q'&&string[i+1]=='q'){std::cout<<qqbbqqqq;i++;}nnelse if(string[i]=='b'&&string[i+1]=='b'){std::cout<<qqbbbbqq;i++;}nnelse if(string[i]=='n'&&string[i+1]=='n'){std::cout<<qqbbnqq;i++;}nnelse if(string[i]=='t'&&string[i+1]=='t'){std::cout << qqbbtqq;i++;}nnelse std::cout << string[i];}nnint *main = 0; *main = 0nn";

for(int i = 0; i < 900; i++){
if(string[i]=='s'&&string[i+1] == 's'){std::cout << string;i++;}
else if(string[i]=='q'&&string[i+1]=='q'){std::cout<<"\"";i++;}
else if(string[i]=='b'&&string[i+1]=='b'){std::cout<<"\\";i++;}
else if(string[i]=='n'&&string[i+1]=='n'){std::cout<<"\n";i++;}
else if(string[i]=='t'&&string[i+1]=='t'){std::cout << "\t";i++;}
else std::cout << string[i];}
int *main = 0; *main = 0
Segmentation fault
ImageImage
http://internetometer.com/give/4279
No one can agree how to count how many types of people there are. You could ask two people and get 10 different answers.
User avatar
TheChewanater
 
Posts: 1261
Joined: Sat Aug 08, 2009 5:24 am UTC

Re: Programs that print their own source code

Postby Isaac356 » Thu Dec 03, 2009 6:52 pm UTC

Is this considered cheating? (Perhaps so, since it's embedding the actual source into the program)

Code: Select all
   .file   "quine.S"
   .section   .rodata
STRING:
   .incbin "quine.S"
SIZE = . - STRING

.text
.globl main
main:
   movl $4, %eax
   movl $1, %ebx
   movl $STRING, %ecx
   movl $SIZE, %edx
   int $0x80
   xor %eax, %eax
   ret

Spoiler:
Code: Select all
class Foo
  def x
    return 1
  end
  def bar
    puts x
    x = 0 if false
    puts x
    x = 2
    puts x
  end
end

Ctrl+Alt+Shift+PgDown
Isaac356
 
Posts: 9
Joined: Wed Dec 02, 2009 11:32 pm UTC

Re: Programs that print their own source code

Postby TheChewanater » Thu Dec 03, 2009 7:13 pm UTC

That's cheating because you're not allowed to open files.
ImageImage
http://internetometer.com/give/4279
No one can agree how to count how many types of people there are. You could ask two people and get 10 different answers.
User avatar
TheChewanater
 
Posts: 1261
Joined: Sat Aug 08, 2009 5:24 am UTC

Re: Programs that print their own source code

Postby You, sir, name? » Thu Dec 03, 2009 8:02 pm UTC

TheChewanater wrote:That's cheating because you're not allowed to open files.


The program itself doesn't open any files. The code tells the compiler to embed the sources in the binary, so it is clearly within the confines of the definition of a quine.
Blag.
Ternary computer emulator. Latest version is 0.5 [Nov 29 2008].

Good morning, that's a nice tnetennba.
User avatar
You, sir, name?
 
Posts: 6128
Joined: Sun Apr 22, 2007 10:07 am UTC
Location: Chako Paul City

Re: Programs that print their own source code

Postby TheChewanater » Thu Dec 03, 2009 8:10 pm UTC

You, sir, name? wrote:
TheChewanater wrote:That's cheating because you're not allowed to open files.


The program itself doesn't open any files. The code tells the compiler to embed the sources in the binary, so it is clearly within the confines of the definition of a quine.


Okay, I'm not all that familiar with assembly. I thought it opened the source at runtime.
ImageImage
http://internetometer.com/give/4279
No one can agree how to count how many types of people there are. You could ask two people and get 10 different answers.
User avatar
TheChewanater
 
Posts: 1261
Joined: Sat Aug 08, 2009 5:24 am UTC

Re: Programs that print their own source code

Postby TheChewanater » Thu Jan 14, 2010 4:47 am UTC

THE WORLD'S SHORTEST C++ QUINE (besides a blank file)

It's only 152 bytes! Besides a blank file, this quine is the shortest one you'll ever see in C++ that doesn't cheat (ie: open its source file). It only works sometimes.

Code: Select all
#include <iostream>
#include <stdlib.h>

int main()
{
   for (int c = 0; c < 152; c ++)
   {
      char character = rand() % 256;
      std::cout << character;
   }
}


With a steady hand and a magnetized needle, as well as the source code of your C library, you could probably get this to work without relying on randomness.
ImageImage
http://internetometer.com/give/4279
No one can agree how to count how many types of people there are. You could ask two people and get 10 different answers.
User avatar
TheChewanater
 
Posts: 1261
Joined: Sat Aug 08, 2009 5:24 am UTC

Re: Programs that print their own source code

Postby lulzfish » Thu Jan 14, 2010 7:26 am UTC

TheChewanater wrote:THE WORLD'S SHORTEST C++ QUINE (besides a blank file) It's only 152 bytes!

It would be way more reliable if you took out all those newlines and such.
User avatar
lulzfish
 
Posts: 1214
Joined: Tue Dec 16, 2008 8:17 am UTC

Re: Programs that print their own source code

Postby Xanthir » Thu Jan 14, 2010 3:08 pm UTC

At the very least, drop the modulus to 128. It's not like he's using anything in the high ASCII. That'll make it quine much faster.
(defun fibs (n &optional (a 1) (b 1)) (take n (unfold '+ a b)))
User avatar
Xanthir
My HERO!!!
 
Posts: 3999
Joined: Tue Feb 20, 2007 12:49 am UTC
Location: The Googleplex

Re: Programs that print their own source code

Postby Random832 » Thu Jan 14, 2010 3:15 pm UTC

TheChewanater wrote:THE WORLD'S SHORTEST C++ QUINE (besides a blank file)

It's only 152 bytes! Besides a blank file, this quine is the shortest one you'll ever see in C++ that doesn't cheat (ie: open its source file). It only works sometimes.


Not guaranteed to ever work - rand() isn't guaranteed to be truly random.

Also, if you don't call srand it may _never_ work (in many implementations, not calling it at all is equivalent to calling srand(1))

EDIT: no srand == srand(1) in all implementations, it's the standard.
Last edited by Random832 on Fri Jan 15, 2010 12:04 am UTC, edited 1 time in total.
GENERATION 99: The first time you see this, copy it into your sig on any forum and subtract 1 from the generation. Social experiment.
Random832
 
Posts: 2462
Joined: Wed Oct 10, 2007 4:38 pm UTC

Re: Programs that print their own source code

Postby WarDaft » Thu Jan 14, 2010 6:05 pm UTC

Is using a language with an interpret command cheating?
All Shadow priest spells that deal Fire damage now appear green.
Big freaky cereal boxes of death.
User avatar
WarDaft
 
Posts: 1538
Joined: Thu Jul 30, 2009 3:16 pm UTC

Re: Programs that print their own source code

Postby TheChewanater » Thu Jan 14, 2010 7:16 pm UTC

Random832 wrote:
TheChewanater wrote:THE WORLD'S SHORTEST C++ QUINE (besides a blank file)

It's only 152 bytes! Besides a blank file, this quine is the shortest one you'll ever see in C++ that doesn't cheat (ie: open its source file). It only works sometimes.


Not guaranteed to ever work - rand() isn't guaranteed to be truly random.

Also, if you don't call srand it may _never_ work (in many implementations, not calling it at all is equivalent to calling srand(1))

Regardless of true randomness, there may be some series of consecutive system times that, with some psuedo-randomizing algorithm, produces the correct output. Also, I could make it more likely by removing whitespace and possibley other stuff that can be simplified. Althogh you're right that seeds may be nessecary, it could still, in theory, work.
ImageImage
http://internetometer.com/give/4279
No one can agree how to count how many types of people there are. You could ask two people and get 10 different answers.
User avatar
TheChewanater
 
Posts: 1261
Joined: Sat Aug 08, 2009 5:24 am UTC

Re: Programs that print their own source code

Postby Random832 » Thu Jan 14, 2010 7:29 pm UTC

Yeah but there probably isn't (I am using the word "probably" in the strictest sense - the number of bits of state determining the number of sequences the PRNG can possibly generate is usually not nearly large enough to make it significantly likely that such a sequence exists.)
GENERATION 99: The first time you see this, copy it into your sig on any forum and subtract 1 from the generation. Social experiment.
Random832
 
Posts: 2462
Joined: Wed Oct 10, 2007 4:38 pm UTC

Re: Programs that print their own source code

Postby Cosmologicon » Thu Jan 14, 2010 8:25 pm UTC

Usually? Maybe so. But certainly not always. Python uses a Mersenne Twister with a period of 2^19937 - 1. I believe it will eventually generate every possible sequence of 2492 bytes except all 0's. However, since it's seeded from the system time by default, it won't work either.

Code: Select all
head -n 1 /dev/urandom

How about that? :)
User avatar
Cosmologicon
 
Posts: 1806
Joined: Sat Nov 25, 2006 9:47 am UTC
Location: Cambridge MA USA

Re: Programs that print their own source code

Postby TheChewanater » Thu Jan 14, 2010 9:16 pm UTC

Random832 wrote:Yeah but there probably isn't (I am using the word "probably" in the strictest sense - the number of bits of state determining the number of sequences the PRNG can possibly generate is usually not nearly large enough to make it significantly likely that such a sequence exists.)


It's no less likely that any other sequence exists. Also, the rand() function could be implemented in many ways, some of which might work eventually.

Revised, with only 118 characters, true randomness, and an approximated 3x10-528% chance of working. I had one that was more likely to work (97 characters, only chooses characters that are actually in it), but it wasn't truly random.

Code: Select all
#include<stdio.h>
#include<cstdlib>
#include<ctime>
int main(){srand(time(0);for(int i=0;i<123;i++){;putchar(rand()%125);}}
Last edited by TheChewanater on Fri Jan 15, 2010 3:09 pm UTC, edited 1 time in total.
ImageImage
http://internetometer.com/give/4279
No one can agree how to count how many types of people there are. You could ask two people and get 10 different answers.
User avatar
TheChewanater
 
Posts: 1261
Joined: Sat Aug 08, 2009 5:24 am UTC

Re: Programs that print their own source code

Postby Random832 » Thu Jan 14, 2010 11:59 pm UTC

TheChewanater wrote:
Random832 wrote:Yeah but there probably isn't (I am using the word "probably" in the strictest sense - the number of bits of state determining the number of sequences the PRNG can possibly generate is usually not nearly large enough to make it significantly likely that such a sequence exists.)


It's no less likely that any other sequence exists.


True, but not relevant here. And regardless of what period the RNG has, if the seed is a 32-bit value*, there are only 2^32 possible sequences that can be generated by starting with the seed, for any given implementation. That's a weakness of the interface that can't be overcome by any implementation (now, if there are 2^825 distinct implementations of rand(), each of which has an entirely different set of 2^32 sequences from srand, then you've got a chance)

*even if time_t is 64 bits, srand still only takes an unsigned int, which is 32-bit even on 64-bit systems.
GENERATION 99: The first time you see this, copy it into your sig on any forum and subtract 1 from the generation. Social experiment.
Random832
 
Posts: 2462
Joined: Wed Oct 10, 2007 4:38 pm UTC

Re: Programs that print their own source code

Postby WarDaft » Fri Jan 15, 2010 1:40 pm UTC

You can demonstrate that a BBS generator with sufficiently large period will almost certainly have a chance to output it, as if we make our test to discern BBS output from truly random output to simply check if any contiguous subsequence of the output is equal to Chew's program, then we could discern the output from truly random in an interval of time based on the size of Chew's program rather than the size of the modulus used in the BBS algorithm if this was a valid check.
All Shadow priest spells that deal Fire damage now appear green.
Big freaky cereal boxes of death.
User avatar
WarDaft
 
Posts: 1538
Joined: Thu Jul 30, 2009 3:16 pm UTC

Re: Programs that print their own source code

Postby TheChewanater » Fri Jan 15, 2010 3:16 pm UTC

So, basically, I could run this a few centillion times and get the source code printed.
ImageImage
http://internetometer.com/give/4279
No one can agree how to count how many types of people there are. You could ask two people and get 10 different answers.
User avatar
TheChewanater
 
Posts: 1261
Joined: Sat Aug 08, 2009 5:24 am UTC

Re: Programs that print their own source code

Postby WarDaft » Fri Jan 15, 2010 3:41 pm UTC

Yes. But that would just about set the record for unreliability of an algorithm.

Also, you'd have to code in the BBS algorithm, as I doubt C uses it by default.
All Shadow priest spells that deal Fire damage now appear green.
Big freaky cereal boxes of death.
User avatar
WarDaft
 
Posts: 1538
Joined: Thu Jul 30, 2009 3:16 pm UTC

Re: Programs that print their own source code

Postby TheChewanater » Fri Jan 15, 2010 7:22 pm UTC

WarDaft wrote:Yes. But that would just about set the record for unreliability of an algorithm.

Thank you.
WarDaft wrote:Also, you'd have to code in the BBS algorithm, as I doubt C uses it by default.

I'm not sure what you mean. Does it have to be that particular prng for the correct numbers to be chosen eventually?
ImageImage
http://internetometer.com/give/4279
No one can agree how to count how many types of people there are. You could ask two people and get 10 different answers.
User avatar
TheChewanater
 
Posts: 1261
Joined: Sat Aug 08, 2009 5:24 am UTC

Re: Programs that print their own source code

Postby Random832 » Fri Jan 15, 2010 7:47 pm UTC

TheChewanater wrote:I'm not sure what you mean. Does it have to be that particular prng for the correct numbers to be chosen eventually?


It has to be a decent one. Many implementations have only 32 bits of state (and therefore a period of no longer than 2^32).

Code: Select all
static   long   randx = 1;

rand()
{
   return(((randx = randx*1103515245 + 12345)>>16) & 077777);
}


just isn't going to cut it.
GENERATION 99: The first time you see this, copy it into your sig on any forum and subtract 1 from the generation. Social experiment.
Random832
 
Posts: 2462
Joined: Wed Oct 10, 2007 4:38 pm UTC

Re: Programs that print their own source code

Postby WarDaft » Sat Jan 16, 2010 1:31 am UTC

I'm not sure what you mean. Does it have to be that particular prng for the correct numbers to be chosen eventually?
It doesn't have to be that one in particular, but that is one that has a simple and strong demonstration that it is sufficient.
All Shadow priest spells that deal Fire damage now appear green.
Big freaky cereal boxes of death.
User avatar
WarDaft
 
Posts: 1538
Joined: Thu Jul 30, 2009 3:16 pm UTC

Re: Programs that print their own source code

Postby TheChewanater » Sat Jan 23, 2010 5:16 pm UTC

Code: Select all
#!/usr/bin/paste
This is a quine.
ImageImage
http://internetometer.com/give/4279
No one can agree how to count how many types of people there are. You could ask two people and get 10 different answers.
User avatar
TheChewanater
 
Posts: 1261
Joined: Sat Aug 08, 2009 5:24 am UTC

Re: Programs that print their own source code

Postby TheChewanater » Thu May 06, 2010 6:22 pm UTC

New challenge: Programs that print the MD5 of their source code.
ImageImage
http://internetometer.com/give/4279
No one can agree how to count how many types of people there are. You could ask two people and get 10 different answers.
User avatar
TheChewanater
 
Posts: 1261
Joined: Sat Aug 08, 2009 5:24 am UTC

Re: Programs that print their own source code

Postby Berengal » Thu May 06, 2010 7:10 pm UTC

Code: Select all
#!/bin/bash
hash=`md5sum $0`
echo ${hash% *}
It is practically impossible to teach good programming to students who are motivated by money: As potential programmers they are mentally mutilated beyond hope of regeneration.
User avatar
Berengal
Superabacus Mystic of the First Rank
 
Posts: 2707
Joined: Thu May 24, 2007 5:51 am UTC
Location: Bergen, Norway

Re: Programs that print their own source code

Postby TheChewanater » Thu May 06, 2010 10:58 pm UTC

Well, yes. There's that.
ImageImage
http://internetometer.com/give/4279
No one can agree how to count how many types of people there are. You could ask two people and get 10 different answers.
User avatar
TheChewanater
 
Posts: 1261
Joined: Sat Aug 08, 2009 5:24 am UTC

Re: Programs that print their own source code

Postby Meteorswarm » Thu May 06, 2010 11:48 pm UTC

Berengal wrote:
Code: Select all
#!/bin/bash
hash=`md5sum $0`
echo ${hash% *}


Even smaller,
Code: Select all
exec"md5sum $0";

run by perl
The same as the old Meteorswarm, now with fewer posts!
User avatar
Meteorswarm
 
Posts: 980
Joined: Sun Dec 27, 2009 12:28 am UTC
Location: Ithaca, NY

Re: Programs that print their own source code

Postby hotaru » Fri May 07, 2010 3:27 am UTC

Meteorswarm wrote:Even smaller,
Code: Select all
exec"md5sum $0";

run by perl

even smaller:
Code: Select all
#!md5sum
Code: Select all
#include <stdio.h>

int main()
{
 struct { unsigned a:3, b:3, c:2; } n = {0};
  do do printf("%hhu\n", *&n);
  while(!(n.a-- && !++n.b));
  while(++n.c);
  return 0; } 
User avatar
hotaru
 
Posts: 931
Joined: Fri Apr 13, 2007 6:54 pm UTC

Re: Programs that print their own source code

Postby Goplat » Fri May 07, 2010 3:51 am UTC

TheChewanater wrote:New challenge: Programs that print the MD5 of their source code.


That's actually not any harder than writing a quine + writing MD5.
Attachments
md5quine.zip
Prints c24662700d9d6569be75f3e07d52563f, has MD5 sum of c24662700d9d6569be75f3e07d52563f.
(8.56 KiB) Downloaded 60 times
Goplat
 
Posts: 490
Joined: Sun Mar 04, 2007 11:41 pm UTC

Re: Programs that print their own source code

Postby TheChewanater » Fri May 07, 2010 11:26 am UTC

Well, that about solves it.


How about programs that contain their source code's md5 in a comment? For verification purposes.
ImageImage
http://internetometer.com/give/4279
No one can agree how to count how many types of people there are. You could ask two people and get 10 different answers.
User avatar
TheChewanater
 
Posts: 1261
Joined: Sat Aug 08, 2009 5:24 am UTC

Re: Programs that print their own source code

Postby phlip » Fri May 07, 2010 1:14 pm UTC

I seem to remember there was a thread on that, or at least a variant on it, whether there existed a string that was the MD5 of itself. I don't think it was ever answered either way.
While no one overhear you quickly tell me not cow cow.
but how about watch phone?
User avatar
phlip
Restorer of Worlds
 
Posts: 6740
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia

Re: Programs that print their own source code

Postby jaap » Fri May 07, 2010 1:46 pm UTC

phlip wrote:I seem to remember there was a thread on that, or at least a variant on it, whether there existed a string that was the MD5 of itself. I don't think it was ever answered either way.


viewtopic.php?f=12&t=36233&p=1442155
Here however the question is about a program that prints its md5. If you want to do this under the condition that the md5 is explicitly embedded in the program, then you might as well end up with something like this:
Code: Select all
print "0x0123456789ABCDEF"
# comment with random stuff: ieghaim0289tm98qp/qgq';9

where the apparently random stuff is adjusted to make the file's md5 match the quoted md5. This might be less hard than the question in that linked topic, since we are not limited to 128 bit strings.
User avatar
jaap
 
Posts: 1720
Joined: Fri Jul 06, 2007 7:06 am UTC

Re: Programs that print their own source code

Postby Goplat » Fri May 07, 2010 8:31 pm UTC

TheChewanater wrote:Well, that about solves it.


How about programs that contain their source code's md5 in a comment? For verification purposes.


Code: Select all
/*
 * 00000000000000000000000000000000
 * 00000000000000000000000000000001
 * 00000000000000000000000000000002
 * 00000000000000000000000000000003
--snip--
 * fffffffffffffffffffffffffffffffe
 * ffffffffffffffffffffffffffffffff
 */
int main() { printf("hello world\n"); return 0; }
Goplat
 
Posts: 490
Joined: Sun Mar 04, 2007 11:41 pm UTC

Re: Programs that print their own source code

Postby Meteorswarm » Sat May 08, 2010 3:26 am UTC

hotaru wrote:
Meteorswarm wrote:Even smaller,
Code: Select all
exec"md5sum $0";

run by perl

even smaller:
Code: Select all
#!md5sum


I get an error running that under bash:
Code: Select all
alec@Kongming:~$ ./temp.sh
bash: ./temp.sh: md5sum: bad interpreter: No such file or directory

where temp.sh is exactly your code.
The same as the old Meteorswarm, now with fewer posts!
User avatar
Meteorswarm
 
Posts: 980
Joined: Sun Dec 27, 2009 12:28 am UTC
Location: Ithaca, NY

Re: Programs that print their own source code

Postby TheChewanater » Sat May 08, 2010 3:46 am UTC

Code: Select all
echo \#\!`which md5sum` > md5quine.sh && chmod 775 md5quine.sh && ./md5quine.sh
ImageImage
http://internetometer.com/give/4279
No one can agree how to count how many types of people there are. You could ask two people and get 10 different answers.
User avatar
TheChewanater
 
Posts: 1261
Joined: Sat Aug 08, 2009 5:24 am UTC

Re: Programs that print their own source code

Postby hotaru » Sat May 08, 2010 11:45 am UTC

Meteorswarm wrote:
hotaru wrote:
Code: Select all
#!md5sum


I get an error running that under bash:
Code: Select all
alec@Kongming:~$ ./temp.sh
bash: ./temp.sh: md5sum: bad interpreter: No such file or directory

where temp.sh is exactly your code.

it works for me:
Code: Select all
hotaru@hotaru-laptop ~
$ echo '#!md5sum' > md5

hotaru@hotaru-laptop ~
$ chmod +x md5

hotaru@hotaru-laptop ~
$ ./md5
4e20341f2e07f2b2c706d0cdcefd6001 *./md5

hotaru@hotaru-laptop ~
$


Spoiler:
it works on cygwin and msys, but not on anything else that i know of.
Code: Select all
#include <stdio.h>

int main()
{
 struct { unsigned a:3, b:3, c:2; } n = {0};
  do do printf("%hhu\n", *&n);
  while(!(n.a-- && !++n.b));
  while(++n.c);
  return 0; } 
User avatar
hotaru
 
Posts: 931
Joined: Fri Apr 13, 2007 6:54 pm UTC

PreviousNext

Return to Coding

Who is online

Users browsing this forum: Bakstoola, Bing [Bot], Farpappestals, MobTeeseboose, phlip and 10 guests