Solved

Just for fun III - Prints self out ...

Posted on 1998-08-19
8
176 Views
Last Modified: 2010-04-15
Simple exercise: Program that prints itself out (Only executable version is available at runtime!!!)

Objective: a suitable add-on for copyright software? (just kidding)

There is a solution ...
0
Comment
Question by:newexpert
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 2

Expert Comment

by:kellyjj
ID: 1252176
Are you talking about printing source code? or the
binary???  
0
 
LVL 6

Accepted Solution

by:
snoegler earned 0 total points
ID: 1252177
#include <stdio.h>
char *text[]={"char *text[]={","%s%c%s%c,%c%s%c,%c%s%c,%c%s%c,%c%s%c,%c%s%c};%c%c","void main(){","puts(text[5]);printf(text[1],text[0],34,text[0],34,34,text[1],34,34,text[2],34,34,text[3],34,34,text[4],34,34,text[5],34,13,10);","puts(text[2]);puts(text[3]);printf(text[4]);}","#include <stdio.h>"};
void main(){
puts(text[5]);printf(text[1],text[0],34,text[0],34,34,text[1],34,34,text[2],34,34,text[3],34,34,text[4],34,34,text[5],34,13,10);
puts(text[2]);puts(text[3]);printf(text[4]);}

I've tested it, it works - but it's pretty ugly :)
0
 
LVL 84

Expert Comment

by:ozo
ID: 1252178
The standard example, fron the comp.lang.c FAQ, is:

char*s="char*s=%c%s%c;main(){printf(s,34,s,34);}";main(){printf(s,34,s,34);}
0
 
LVL 6

Expert Comment

by:snoegler
ID: 1252179
that is a bit shorter :)
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 1

Author Comment

by:newexpert
ID: 1252180
How about the one I had in mind (it even does some extra things):
#include "pself.h"
PH(int main(void) {)
PA(int i; int sum = 0; for (i = 1; i <= 100; i++) sum += i;)
PB(return sum; })

The actual code that does the hard work is conveniently hidden in pself.h
#define PH(x) x puts("#include \"pself.h\"\nPH("#x")");
#define PA(x) x; puts("PA("#x")");
#define PB(x) puts("PB("#x")"); x

0
 
LVL 84

Expert Comment

by:ozo
ID: 1252181
Hmm, if you allow that, then how about just this:

#include "pself.h"

with pself.h containg:
main(){
  int i; int sum = 0;
  for (i = 1; i <= 100; i++) sum += i;
  puts("#include \"pself.h\"\n");
  return sum;
}

Or this:

X

compiled with
cc -DX='main(){puts("X");}'


0
 
LVL 1

Author Comment

by:newexpert
ID: 1252182
Good point.  Wonder how hard would it be to create a .h which source dump any .c file that includes it?
0
 
LVL 1

Author Comment

by:newexpert
ID: 1252183
Where did the last word "it" go?
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now