473,387 Members | 1,569 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Help with a A Perverse Standard C Program

A challenged co-worker of mine challenged me to come up with a perverse
example of a conforming Standard C program. This is by no means a gold
medal winner in a C obfuscation contest, but nevertheless it
demonstrates some arguably serious abuse of the preprocessor. If you can
make it even more perverse, I'd appreciate it.

And for those of you who might ask what my C question is ... is this
Standard C?

#include <ctype.h>
#include <stdio.h>
#include <string.h>

#define UNSIGNED unsigned
#define CHAR char
#define INT int
#define MAIN main
#define VOID void
#define RETURN return
#define STATIC static
#define BEGIN {
#define END }
#define IF if(
#define THEN ){
#define ELSE }else{
#define ELSEIF }else if(
#define ENDIF }
#define FOR for(
#define LOOP ){
#define ENDLOOP }
#define IS ==
/*lint -save -e723 Suspicious use of = */
/*(OK since I know what I am doing)*/
#define ASSIGN =
/*lint -restore*/
#define LT <
#define GTE >=
#define BUMP +=
#define CUT -=

#define PRINTF printf
#define PUTS puts
#define FFLUSH fflush
#define GETS gets
#define STDOUT stdout
#define STRLEN strlen
#define TOUPPER toupper
#define SIZE_T size_t

STATIC CHAR *MAKE_UPPER(CHAR *S);

INT MAIN(VOID)
BEGIN
INT I;
CHAR NAME[4096];

PRINTF("ENTER YOUR NAME: ");
FFLUSH(STDOUT);
PRINTF("HI %s!\n", MAKE_UPPER(GETS(NAME)));/*lint !e421 Caution -- */
/*function */
/*'gets(char *)'*/
/*is considered*/
/*dangerous*/
/*(OK, since no one*/
/*has a name greater*/
/*in length than 4095*/
/*characters).*/
PUTS("LET'S TEST A FOR LOOP WITH BUMP");
FOR I ASSIGN 0; I LT 6; I BUMP 1
LOOP
IF I IS 4
THEN
PRINTF("I IS 4\n");
ELSEIF I IS 5
THEN
PRINTF("I IS 5\n");
ELSE
PRINTF("I IS %d\n", I);
ENDIF
ENDLOOP

PUTS("LET'S TEST A FOR LOOP WITH CUT");
FOR I ASSIGN 5; I GTE 0; I CUT 1
LOOP
IF I IS 4
THEN
PRINTF("I IS 4\n");
ELSEIF I IS 5
THEN
PRINTF("I IS 5\n");
ELSE
PRINTF("I IS %d\n", I);
ENDIF
ENDLOOP

PRINTF("BYE %s!\n", MAKE_UPPER(NAME));

RETURN 0;
END

STATIC CHAR *MAKE_UPPER(CHAR *S)
BEGIN
SIZE_T I;
SIZE_T LEN;

IF S IS NULL
THEN
RETURN NULL;
ENDIF
LEN = STRLEN(S);
FOR I ASSIGN 0; I LT LEN; I BUMP 1
LOOP
S[i] = (CHAR)TOUPPER((UNSIGNED CHAR)(S[i]));
ENDLOOP
RETURN S;
END

Thanks
--
Jerret
Oct 10 '06 #1
9 2024
Jerret Johnson wrote:
And for those of you who might ask what my C question is ... is this
Standard C?
Yes. Perfectly legal ANSI C. GCC gives a warning about using gets().

This one is my all-time favorite.

#define BeginProgram void main(int argc, char *argv[])
#define CloseBrace }
#define CommandLineArgument -1
#define Declare int i,j,n,Flag=1;
#define EndOfProgram return;
#define False 0;
#define ForLoop ;for
#define GetCommandLineArgument n=atoi(argv[1]);
#define i F1ag
#define If if
#define Increment ++
#define Is ==
#define LessThan *(c&64)*
#define LessThanOrEqualTo !=
#define Modulo %
#define OpenBrace {
#define PossibleFactor j
#define PossiblePrime i
#define Possib1ePrime (c=getchar())
#define PrimeNumber (c^(!i*n%64));
#define Print putchar
#define SetTo =
#define SmallestPrime 2
#define True 1
#define Variables char c;
#define Zero i%j

BeginProgram
OpenBrace
Declare Variables
GetCommandLineArgument

ForLoop (PossiblePrime SetTo SmallestPrime ;
Possib1ePrime LessThanOrEqualTo CommandLineArgument ;
Increment PossiblePrime)
OpenBrace
F1ag SetTo True
ForLoop (PossibleFactor SetTo SmallestPrime ;
PossibleFactor LessThan PossiblePrime ;
Increment PossibleFactor)
If (PossiblePrime Modulo PossibleFactor Is Zero)
F1ag SetTo False

If (Flag Is True)
Print PrimeNumber
CloseBrace

EndOfProgram
CloseBrace
Oct 10 '06 #2
On Tue, 10 Oct 2006 00:06:13 -0700, Jerret Johnson <jj*******@aol.com>
wrote:
>A challenged co-worker of mine challenged me to come up with a perverse
example of a conforming Standard C program. This is by no means a gold
medal winner in a C obfuscation contest, but nevertheless it
demonstrates some arguably serious abuse of the preprocessor.
Fortunately you can also use the proprocessor to effortlessly
reconstruct the C program from your 'perversity'.

Best wishes,
Roland Pibinger
Oct 10 '06 #3
jmcgill said:
This one is my all-time favorite.

#define BeginProgram void main(int argc, char *argv[])
So much for Standard C.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Oct 10 '06 #4
Jerret Johnson said:

<snip>
And for those of you who might ask what my C question is ... is this
Standard C?

PRINTF("HI %s!\n", MAKE_UPPER(GETS(NAME)));/*lint !e421 Caution -- */
/*function */
/*'gets(char *)'*/
/*is considered*/
/*dangerous*/
/*(OK, since no one*/
/*has a name greater*/
/*in length than 4095*/
/*characters).*/
Not okay, because it can be attacked by a malicious user (cf Internet Worm).
It also fails to take into account the possibility that gets() returns
NULL.

<snip>
S[i] = (CHAR)TOUPPER((UNSIGNED CHAR)(S[i]));
The first cast is unnecessary. The second is sensible.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Oct 10 '06 #5

"Richard Heathfield" <in*****@invalid.invalidwrote in message
news:E5******************************@bt.com...
Jerret Johnson said:

<snip>
And for those of you who might ask what my C question is ... is this
Standard C?

PRINTF("HI %s!\n", MAKE_UPPER(GETS(NAME)));/*lint !e421 Caution -- */
/*function */
/*'gets(char *)'*/
/*is considered*/
/*dangerous*/
/*(OK, since no one*/
/*has a name greater*/
/*in length than 4095*/
/*characters).*/

Not okay, because it can be attacked by a malicious user (cf Internet
Worm).
It also fails to take into account the possibility that gets() returns
NULL.
?

Then why does MAKE_UPPER begin with this?

IF S IS NULL
THEN
RETURN NULL;
ENDIF

It appears to me that it takes the possibility into account, and then passes
the NULL along to printf()...

;)
Rod Pemberton
Oct 10 '06 #6

"Jerret Johnson" <jj*******@aol.comwrote in message
news:GD****************@newsfe03.lga...
A challenged co-worker of mine challenged me to come up with a perverse
example of a conforming Standard C program.
Why? (whiny)...
This is by no means a gold
medal winner in a C obfuscation contest, but nevertheless it
demonstrates some arguably serious abuse of the preprocessor. If you can
make it even more perverse, I'd appreciate it.
Someone a while back tried to turn C into Pascal or BASIC using the
preprocessor. So, there are two directions you could go. It might look
really interesting if you chose BASIC and managed to add line numbers...
You could then hide all the defines in a local include for your prank.

Rod Pemberton
Oct 10 '06 #7

Jerret Johnson wrote:
A challenged co-worker of mine challenged me to come up with a perverse
example of a conforming Standard C program.
Actually I've done something similar to "help" C code look a little
less like a mangy porcupine in a blender.

#define If if(
#define Then ) {
#define Else } else {
#define Equals ==
#define Is ==
#define Isnt !=
#define Procedure void
#define Function
#define NoArgs ( void )
#define For(var,lo,hi) for(var=(lo);var <=(hi);var++)
#define var {
#define begin {
#define end }
#define code
#define EndFor }
so you can write the arguably prettier and clearer:

Procedure CrapOnMem( char Buf[], int Len )
var
int i;
code
For( i, 0, Len - 1 )
If i Isnt 22 Then
Buf[i] = '%';
EndIf
EndFor
end
Of course this causes most C aficionados to raise an eyebrow, or sneer,
or feint barfing.

Oct 10 '06 #8
Richard Heathfield wrote:
jmcgill said:
>This one is my all-time favorite.

#define BeginProgram void main(int argc, char *argv[])

So much for Standard C.
Fair enough, but I still get a kick out of this program. It does
something really interesting, and not at all obvious.
Oct 10 '06 #9
Rod Pemberton said:
"Richard Heathfield" <in*****@invalid.invalidwrote in message
news:E5******************************@bt.com...
>Jerret Johnson said:

<snip>
And for those of you who might ask what my C question is ... is this
Standard C?

PRINTF("HI %s!\n", MAKE_UPPER(GETS(NAME)));/*lint !e421 Caution --
*/
<snip>
>It also fails to take into account the possibility that gets() returns
NULL.

?
!
Then why does MAKE_UPPER begin with this?

IF S IS NULL
THEN
RETURN NULL;
ENDIF

It appears to me that it takes the possibility into account, and then
passes the NULL along to printf()...
I was talking about the whole statement. Passing NULL to printf as a match
for %s invokes undefined behaviour.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Oct 10 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: John | last post by:
I am new to c++ programming and i just want to make a program that takes a users name, then converts it to a hex string, then puts it together in a fixed order. An example output would be: ...
6
by: wukexin | last post by:
Help me, good men. I find mang books that introduce bit "mang header files",they talk too bit,in fact it is my too fool, I don't learn it, I have do a test program, but I have no correct doing...
2
by: sunfox | last post by:
Please help!! I have a difficulty in writing an assignment which is related to Visual C++ V6.0. Can anybody here assist me to write a program which is able to run under DOS? The program will be...
1
by: Rahul | last post by:
Hi Everybody I have some problem in my script. please help me. This is script file. I have one *.inq file. I want run this script in XML files. But this script errors shows . If u want i am...
4
by: Sreekanth | last post by:
Hi all, I have implemented a timing out version of fgets function call. I am pasting the entire code below. I have following doubts: 1. The code which I have written does it follow standard C...
18
by: pipito | last post by:
Hi...i am a beginner in C programming... my question is like this... im using the scanf in C to accept input data in output area, printf("name: ");scanf("%s",name); printf(Address:...
1
by: simonalexander | last post by:
I have got a homework task to do and I have started the work but I cannot finish it.Can someone please help me finish the code. The help given is much appreciated. The actual specifications are...
21
by: asif929 | last post by:
I need immediate help in writing a function program. I have to write a program in functions and use array to store them. I am not familiar with functions and i tried to create it but i fails to...
9
by: xiao | last post by:
It always dumped when I tried to run it... But it compiles OK. What I want to do is to do a test: Read information from a .dat file and then write it to another file. The original DAT file is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.