Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Polyglot (computing)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===C, PHP, and Bash=== Two commonly used techniques for constructing a polyglot program are to make use of languages that use different [[Character (computing)|characters]] for [[comment (computer programming)|comments]], and to redefine various tokens as others in different languages. These are demonstrated in this [[public domain]] polyglot written in [[ANSI C]], [[PHP]] and [[Bash (Unix shell)|bash]]: {{columns-start|num=3}} '''Highlighted for Bash''' <syntaxhighlight lang="bash"> #define a /* #<?php echo "\010Hello, world!\n";// 2> /dev/null > /dev/null \ ; // 2> /dev/null; x=a; $x=5; // 2> /dev/null \ ; if (($x)) // 2> /dev/null; then return 0; // 2> /dev/null; fi #define e ?> #define b */ #include <stdio.h> #define main() int main(void) #define printf printf( #define true ) #define function function main() { printf "Hello, world!\n"true/* 2> /dev/null | grep -v true*/; return 0; } #define c /* main #*/ </syntaxhighlight> {{column}} '''Highlighted for PHP''' <syntaxhighlight lang="PHP"> #define a /* #<?php echo "\010Hello, world!\n";// 2> /dev/null > /dev/null \ ; // 2> /dev/null; x=a; $x=5; // 2> /dev/null \ ; if (($x)) // 2> /dev/null; then return 0; // 2> /dev/null; fi #define e ?> #define b */ #include <stdio.h> #define main() int main(void) #define printf printf( #define true ) #define function function main() { printf "Hello, world!\n"true/* 2> /dev/null | grep -v true*/; return 0; } #define c /* main #*/ </syntaxhighlight> {{column}} '''Highlighted for C''' <syntaxhighlight lang="c"> #define a /* #<?php echo "\010Hello, world!\n";// 2> /dev/null > /dev/null \ ; // 2> /dev/null; x=a; $x=5; // 2> /dev/null \ ; if (($x)) // 2> /dev/null; then return 0; // 2> /dev/null; fi #define e ?> #define b */ #include <stdio.h> #define main() int main(void) #define printf printf( #define true ) #define function function main() { printf "Hello, world!\n"true/* 2> /dev/null | grep -v true*/; return 0; } #define c /* main #*/ </syntaxhighlight> {{columns-end}} Note the following: *A hash sign marks a [[preprocessor]] statement in C, but is a comment in both bash and PHP. *"//" is a comment in both PHP and C and the [[root directory]] in bash. *Shell [[Redirection (computing)|redirection]] is used to eliminate undesirable outputs. *Even on commented out lines, the "<code><?php</code>" and "<code>?></code>" PHP indicators still have effect. *The statement "<code>function main()</code>" is valid in both PHP and bash; C #defines are used to convert it into "<code>int main(void)</code>" at compile time. *Comment indicators can be combined to perform various operations. *"<code>if (($x))</code>" is a valid statement in both bash and PHP. *<code>[[printf (Unix)|printf]]</code> is a bash [[shell builtin]] which is identical to the [[printf format string|C printf]] except for its omission of brackets (which the [[C preprocessor]] adds if this is compiled with a [[C compiler]]). *The final three lines are only used by bash, to call the <code>main</code> function. In PHP the <code>main</code> function is defined but not called and in C there is no need to explicitly call the <code>main</code> function.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)