unix programming

Pages: 123
moorecm's description is still shorter and simpler, and therefore less complex.
Do note that my solution was also somewhat obfuscated, as simpler, more coherent solutions are available. It started out as a sort of "over simplification", because we know this assignment was meant to be solved with nested for loops.
Hehehe, I'm having way too much fun with this one. Here's an even "simpler" solution than my first one:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>

unsigned r( unsigned x, unsigned y ) {
    return ( y & 1 ) && !( y & ~1 ) ? x : ( y ^ ( ( y >> 1 ) << 1 ) ) * x +
        ( r( x, ( y & ~1 ) >> 1 ) << 1 );
}

int main() {
    unsigned x = !!( r( 10, 1 ) ^ ~r( 1, 10 ) );
    unsigned c = !( ( x & ~x ) + 1 );

    again:
        switch( ( ( ( c >> 1 ) << 1 ) ^ c ) + ( ( ( ( ( c ^ ( c >> 1 ) ) + 1 ) >> 1 >> 1 ) & ~1 ) ) << 1 << 1 ) {
            default: goto try_again;
            case_0: std::cout << std::endl; goto again;
            case 0: case 1 << 1: case 1 << 1 << 1: if( c - ( ( c >> 1 ) << 1 ) )
            case 1: case 1 + ( 1 << 1 ): case ( 1 + 1 ) << 1 + 1: std::cout << 0;
        }
        std::cout << x;
        x = r( x, c & 1 ? ( ( ( 011 << 1 ) + ( 010 << 1 << 1 ) )
            << 1 ) : 1 ) | ( c ^ ( ( c >> 1 ) << 1 ) );

    try_again:
        if( ++c <= ( ( ( 1 + 1 ) ^ 1 ) << 1 ) - 1 )
            goto case_0;
}

jsmith, you have a sick and twisted mind. :-)

I was worried that the OP may have a non portable character set,
so I've converted jsmith's code to trigraphs. (see stroustrup)
hope this helps.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
??=include <iostream>

unsigned r( unsigned x, unsigned y ) ??<
    return ( y & 1 ) && !( y & ??-1 ) ? x : ( y ??' ( ( y >> 1 ) << 1 ) ) * x +
        ( r( x, ( y & ??-1 ) >> 1 ) << 1 );
??>

int main() ??<
    unsigned x = !!( r( 10, 1 ) ??' ??-r( 1, 10 ) );
    unsigned c = !( ( x & ??-x ) + 1 );
    do ??<
        switch( ( ( c >> 1 ) << 1 ) ??' c ) ??<
            default: std::cout << 1;
            case 0: case 1 << 1: case 1 << 1 << 1: if( c - ( ( c >> 1 ) << 1 ) )
            case 1: case 1 + ( 1 << 1 ): case ( 1 + 1 ) << 1 + 1: std::cout << 0;
        ??>
        std::cout << x << std::endl;
        x = r( x, c & 1 ? ( ( ( 011 << 1 ) + ( 010 << 1 << 1 ) )
            << 1 ) : 1 ) ??! ( c ??' ( ( c >> 1 ) << 1 ) );
    ??> while( ++c < ( ( ( 1 + 1 ) ??' 1 ) << 1 ) - 1 );
??> 
Since the OP hasn't reacted on Athar's offer (and even his free services!) I will post my attempt here:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <string> // Include underwear
#define cast_static(v) static_cast<v>((u
#define static_ static_cast<
#define definitely_true >(true)))
#define Hey typedef
#define there unsigned v

typedef char d;

Hey there;

inline void f(const v& i) {
  const bool p = (i^((i>>1)<<static_ int definitely_true;
           v u = !!((v)p & ~p);
    bool c = p;
             p;
          // you will be amazed by how fast this works!
    do std::cout << !!!(~(c=!c)+!
            (!c & c)*(!(!u && u) << 1));
    while(u++ < cast_static(v) & ~u)+i));
    std::cout << char((signed)~d(245));
    
}

int main()
{
    for(int i = ((~1 & 1) << 1); i<((i & ~i)+5);)
        f(i++);
    system("pause");
}


Kudos when you find the easter egg.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include <cstdio>

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <cstdarg>
#include <csetjmp>
std::map<bool,std::map<int,std::vector<std::pair<int,char> > > > magic_line;
#define one magic_line[1]
#define zero magic_line[0]
#define printf not_from_C_library
namespace
{


    class
    {
        public: void operator++(int)
        {
            {
                std::vector<std::pair<int,char> > &n = zero[0];
                std::pair<int,char> arr [] = {
                    std::make_pair(2,' '),
                    std::make_pair(2,'-'),
                    std::make_pair(2,' '),
                };
                n = std::vector<std::pair<int,char> > ( arr, arr+sizeof(arr)/sizeof(arr[0]) );
            }
            {
                std::vector<std::pair<int,char> > &n = zero[1];
                std::pair<int,char> arr [] = {
                    std::make_pair(1,' '),
                    std::make_pair(1,'|'),
                    std::make_pair(2,' '),
                    std::make_pair(1,'|'),
                    std::make_pair(1,' '),
                };
                n = std::vector<std::pair<int,char> > ( arr, arr+sizeof(arr)/sizeof(arr[0]) );
            }
            {
                std::vector<std::pair<int,char> > &
                n = zero[2];
                n = zero[1];
            }
            {
                std::vector<std::pair<int,char> > &
                n = zero[3];
                n = zero[0];
            }
            {
                std::vector<std::pair<int,char> > &n = one[0];
                std::pair<int,char> arr [] = {
                    std::make_pair(2,' '),
                    std::make_pair(1,'/'),
                    std::make_pair(1,'|'),
                    std::make_pair(2,' ')
                };
                n = std::vector<std::pair<int,char> > ( arr, arr+sizeof(arr)/sizeof(arr[0]) );
            }
            {
                std::vector<std::pair<int,char> > &n = one[1];
                std::pair<int,char> arr [] = {
                    std::make_pair(1,' '),
                    std::make_pair(1,'/'),
                    std::make_pair(1,' '),
                    std::make_pair(1,'|'),
                    std::make_pair(2,' ')
                };
                n = std::vector<std::pair<int,char> > ( arr, arr+sizeof(arr)/sizeof(arr[0]) );
            }
            {
                std::vector<std::pair<int,char> > &n = one[2];
                std::pair<int,char> arr [] = {
                    std::make_pair(3,' '),
                    std::make_pair(1,'|'),
                    std::make_pair(2,' ')
                };
                n = std::vector<std::pair<int,char> > ( arr, arr+sizeof(arr)/sizeof(arr[0]) );
            }
            {
                std::vector<std::pair<int,char> > &n = one[3];
                std::pair<int,char> arr [] = {
                    std::make_pair(1,' '),
                    std::make_pair(4,'-'),
                    std::make_pair(1,' ')
                };
                n = std::vector<std::pair<int,char> > ( arr, arr+sizeof(arr)/sizeof(arr[0]) );
            }

        }
    }c;

    class _
    {
        public: _() { c++ ; }
    }d;

}

void print_number ( const char *format, ... )
{
    va_list vl;
    va_start(vl,format);
    bool va_bool = va_arg ( vl, int );
    int va_int = va_arg ( vl, int );
    va_end(vl);
    std::vector<std::pair<int,char> > &va_vector = magic_line[va_bool][va_int];
    jmp_buf back;
    int i = 0;
    setjmp(back);
    std::cout << std::string ( va_vector[i].first, va_vector[i].second );
    if ( ! ( i++ != va_vector.size() ) || ( longjmp(back,0), i++ != va_vector.size() ) );
}

int printf ( const char *format, ... )
{
    print_number ( format, 1, 0 );
    const char *back = format;
    for ( int i = 0; i < 4; i++, format = back-1, std::cout << std::endl )
        while ( *++format )
            if ( ! ( *format - '0' ) )
                print_number ( format, 0, i );
            else if ( ! ( *format - '1' ) )
                print_number ( format, 1, i );
            else if ( ! ( *format - '\n' ) )
            {
                if ( i == 3 )
                    i = -1, *( back = format+1 ), std::cout << std::endl;
                break;
            }

    return 0;
}

int main()
{
    printf ( "1\n01\n101\n0101\n10101" );
}
  /|  
 / |  
   |  
 ---- 

  --    /|  
 |  |  / |  
 |  |    |  
  --   ---- 

  /|    --    /|  
 / |   |  |  / |  
   |   |  |    |  
 ----   --   ---- 

  --    /|    --    /|  
 |  |  / |   |  |  / |  
 |  |    |   |  |    |  
  --   ----   --   ---- 

  /|    --    /|    --    /|  
 / |   |  |  / |   |  |  / |  
   |   |  |    |   |  |    |  
 ----   --   ----   --   ----
For small output comment out lines 3 to 134
Just in case OP needs more exemplary examples, I made my own. It utilizes a simple client-server architecture.
10.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python

import socket
import sys

ADDRESS	= "127.0.0.1"
PORT	= 9000 + 1
CLIENTS	= 1

def server():
	global ADDRESS
	global PORT
	global CLIENTS

	sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	sock.bind((ADDRESS, PORT))
	sock.listen(CLIENTS)

	# Wait for connections
	running = True
	print "Waiting for connections..."
	while running:	
		conn, info = sock.accept()
		conn.setblocking(1)
		
		data = conn.recv(2048)
		print data
		
		conn.close()


def client():
	global ADDRESS
	global PORT
	
	sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	sock.connect((ADDRESS, PORT))
	
	sock.send("1\n01\n101\n0101\n10101")

usagemsg = "Usage: " + sys.argv[0] + " [options]\n"	+\
	   "Options:\n"					+\
	   "\t-h,--help\n"				+\
	   "\t\tShow this help and quit\n"		+\
	   "\t-c,--client\n"				+\
	   "\t\tRun in client mode\n"			+\
	   "\t-s,--server\n"				+\
	   "\t\tRun in server mode\n"
runmode = 0

if len(sys.argv) < 2:
	print(usagemsg)
	exit(1)

for arg in sys.argv:
	if arg in ["-h", "--help"]:
		print(usagemsg)
		exit(0)
	elif arg in ["-s", "--server"]:
		runmode = "S"
	elif arg in ["-c", "--client"]:
		runmode = "C"
		
if runmode == "S":
	exit(server())
elif runmode == "C":
	exit(client())
else:
	print(usagemsg)
	exit(1)

You need to run one in client mode and one in server mode (start the server one first) for it to work.

Screenshot: http://i35.tinypic.com/5a3qm8.png

Edit: Figured out why it didn't work with one computer (used socket.bind() instead of socket.connect() in client()) and got screenshot.
Last edited on
Very nice, Bazzy. Don't take this the wrong way, but you are quite good at writing obfuscated code.
Writing obfuscated code is easier than writing readable code...
Nice codes, really like the big letter and networking. Kind of doubt the OP does, but whatever. This should be moved to lounge, this is kind of fun. :P Oh, just one last thing considering the bitwise operations. When using signed types, the first bit defines the sign, but.. when is it set and when is it clear?
1 = negative
0 = non-negative
I doubt the OP is even looking at this (or even has signed in ever since he/she joined and posted this question)...
I doubt it.

I fixed my Python code above and posted a screenshot of it working, btw.

@jsmith & Bazzy
I agree; like the program you (Bazzy) wrote in response to the Perl program I found.
Here's my 100% C++ code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>

const int memory_size = 256;
const int stack_size = 32;

void brainfuck(const char* code){
    char memory[memory_size] = {0};
    int ip = 0, bp = 0, sp = 0;

    sp = 0; ip = 0;
    while(code[ip]){
        switch(code[ip]){
            case '+': ++memory[bp]; break;
            case '-': --memory[bp]; break;
            case '>': ++bp; break;
            case '<': --bp; break;

            case '[':
                if(memory[bp] == 0){
                    int tmp_sp = 0;
                    do{
                        if(code[ip] == '[') tmp_sp++;
                        if(code[ip] == ']') tmp_sp--;
                        ip++;
                    }while(code[ip] && tmp_sp != 0);
                }else ++sp;
                break;

            case ']':
                if(memory[bp] != 0){
                    int tmp_sp = 0;
                    do{
                        if(code[ip] == '[') tmp_sp++;
                        if(code[ip] == ']') tmp_sp--;
                        ip--;
                    }while(ip >= 0 && tmp_sp != 0);
                }else ++sp;
                break;

            case ',': memory[bp] = std::cin.get(); break;
            case '.': std::cout.put(memory[bp]); break;
        }
        ip++;
    }
}

int main(){
    brainfuck(">++++++++[->++++++<]>[-<+<+>>]<+>++++++++++"
              "<.>.<<.>.>.<.<.>.>.<<.>.<.>.>.<.<.>.<.>.>.,");
    return 0;
}
That's impressive, hamsterman, very impressive. I like it.
why don't we write it entirely in binary scripts? There used to be a language where everything was binary...I can't remember it's name or if it even still exists. But it was designed for the i286 architecture, and was about ten times as difficult to program in as assembly.
machine code?
I thought they wrote that in hexadecimal.

If I knew how to write punched cards...
I thought they wrote that in hexadecimal.

Why do people keep saying that? They probably did, but that's not the point. Machine code is a bunch of numbers. You could write it with decimal or even ASCII if you're really insane. I don't really know machine code, but I assume that in binary it could be easier to understand, as significant groups of bits may not be at convenient spots.
Pages: 123