learning by viewing contest

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

learning by viewing contest

#1 Post by technosaurus »

Submit your enlightening code here for others to figure out and learn. The first one to get it right, gets added to this score card.


Point count:

Code: Select all

nc -ll -p 80 -e sh -c 'while read L;do set $L;case "$1" in [Gg][Ee][Tt])F=${2##*/};cat ${F%%\?*};break;;esac;done'
MinHundHettePerro 1 (bash/shell)

Current open contests:
javascript:

Code: Select all

function F(x){var t=XMLHttpRequest();try{t.open("HEAD",x,false);t.send();return t.status!=404}catch(n){return false}}
C:

Code: Select all

#define F(i,a) ({ int j=0;while(a[j] != NULL){a[j]=a[j+i];j++;}})
Last edited by technosaurus on Fri 14 Dec 2012, 14:12, edited 2 times in total.
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

User avatar
MinHundHettePerro
Posts: 852
Joined: Thu 05 Feb 2009, 22:22
Location: SE

#2 Post by MinHundHettePerro »

Right, ... :?

Listen on your computer's port 80 if someone is trying to connect, if there's someone actually trying that, check if any line in the header begins with (any combination of capitals and small letters) "get", if so, set variable "F" to whatever follows the last "/" following the initial "get _blank_", then print whatever precedes the first (remaining) "?" - otherwise continue ...

Yes, the parts to be read - and output - are probably part of the http-protocol :?: , which I don't know in _any_ detail at all :oops: :wink:

Or, whatever ... :)/ MHHP
[color=green]Celeron 2.8 GHz, 1 GB, i82845, many ptns, modes 12, 13
Dual Xeon 3.2 GHz, 1 GB, nvidia quadro nvs 285[/color]
Slackos & 214X, ... and Q6xx
[color=darkred]Nämen, vaf....[/color] [color=green]ln -s /dev/null MHHP[/color]

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#3 Post by technosaurus »

MinHundHettePerro wrote:then print whatever precedes the first (remaining) "?"
close enough, it uploads the file from the directory where it is run ... basically it is a file server designed to ignore directory structure (for mirroring several javascript CDNs like ajax.googleapis.com, cdnjs.cloudflare.com,ajax.aspnetcdn.com,... without having to have duplicate files for each different directory structure)

Here is a fuller implementation that assumes your mirrored sites are set up in /etc/hosts

Code: Select all

#!/bin/sh
nc -ll -p 80 -e sh -c '
while read A B DUMMY
do
	case "$A" in
		[Gg][Ee][Tt])
			FULL=$B
			F=${FULL##*/}
			F=${F%%\?*}
			[ -f "$F" ] && cat "$F" && break
		;;
		[Hh][Oo][Ss][Tt]*)
			[ -f "$F" ] && break
			HOST=${B:0:$((${#B}-1))}
			sed -i "s/hosts:\t\tfiles /hosts:\t\t/g" /etc/nsswitch.conf
			wget -t 0 -q --no-dns-cache $HOST$FULL
			sed -i "s/hosts:\t\t/hosts:\t\tfiles /g" /etc/nsswitch.conf
			cat "$F"
			break
		;;
	esac
done
'
here is an example of the hosts file

Code: Select all

127.0.0.1 localhost puppypcXXXXX
127.0.0.1 ajax.googleapis.com
0.0.0.0 pagead2.googlesyndication.com
the 127.0.0.1 sites use your local mirror instead (no more waiting for ajax.googleapis.com)
the 0.0.0.0 sites get completely ignored (ad removal etc...)
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#4 Post by technosaurus »

here is a simple, but useful javascript function:

Code: Select all

function F(x){var t=XMLHttpRequest();try{t.open("HEAD",x,false);t.send();return t.status!=404}catch(n){return false}}
obfuscated for your education
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

User avatar
MinHundHettePerro
Posts: 852
Joined: Thu 05 Feb 2009, 22:22
Location: SE

Re: learning by viewing contest

#5 Post by MinHundHettePerro »

technosaurus wrote: Current open contests:

C:

Code: Select all

#define F(i,a) ({ int j=0;while(a[j] != NULL){a[j]=a[j+i];j++;}})
Yes, right ... don't know C either but, since I enjoy deciphering code I don't know, I'll wager a stab at it :? :shock:

Code: Select all

#!/bin/sh

# C:         #define F(i,a) ({ int j=0;while(a[j] != NULL){a[j]=a[j+i];j++;}})

# C Code snippet above could probably be used for many things but I'll try a dd analogy for reading and "streaming" a file by a set chunk-size ...
# par example:

i="a number"
a="a file to transfer"

F() { dd if="$a" bs="$i"}

# the indexing variable j would correspond to the "count"-parameter for dd as of above but, the need to index (and up the index by one until there are no more chunks) would be unnecessesary using dd with an input file of finite length. 

Cheers :)/ MHHP
[color=green]Celeron 2.8 GHz, 1 GB, i82845, many ptns, modes 12, 13
Dual Xeon 3.2 GHz, 1 GB, nvidia quadro nvs 285[/color]
Slackos & 214X, ... and Q6xx
[color=darkred]Nämen, vaf....[/color] [color=green]ln -s /dev/null MHHP[/color]

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#6 Post by technosaurus »

That one is intentionally misleading, inefficient code that I actually used before I understood how c strings relate to pointers... Hint: s+=n would be functionally equivalent.
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

User avatar
SFR
Posts: 1800
Joined: Wed 26 Oct 2011, 21:52

Re: learning by viewing contest

#7 Post by SFR »

technosaurus wrote:C:

Code: Select all

#define F(i,a) ({ int j=0;while(a[j] != NULL){a[j]=a[j+i];j++;}})
I still haven't started to learn C, but I can not see any other application of that:
Does this code shifts all elements of 'a' array 'i' position(s) to the left?

Greetings!
[color=red][size=75][O]bdurate [R]ules [D]estroy [E]nthusiastic [R]ebels => [C]reative [H]umans [A]lways [O]pen [S]ource[/size][/color]
[b][color=green]Omnia mea mecum porto.[/color][/b]

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#8 Post by technosaurus »

@SFR - congrats. Yes it is the equivalent of shift in bash.
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#9 Post by technosaurus »

here is another javascript

Code: Select all

function
q(){for(var a="?",c=arguments,b=c.length;(a+=c[--b])&&b;a+="&")
return a}
//which is the same as
function
q(){return"?"+[].splice.call(arguments,0).join("&")}
Edit:
Hint - reply to this post and look up
Last edited by technosaurus on Fri 09 May 2014, 08:32, edited 1 time in total.
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#10 Post by technosaurus »

without compiling, what does this code do?

Code: Select all

#include <stdio.h>
int main(int i){return printf("%d\n",i)-5 && main(++i);}
hint - see printf manpage for its return value
hint 2 - && is a short circuit operator
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

User avatar
GustavoYz
Posts: 883
Joined: Wed 07 Jul 2010, 05:11
Location: .ar

#11 Post by GustavoYz »

C beginner here, guessing.

It prints the numbers from 1 to 1000.
The -5 is the pointer/index of the "i" that is/acts as an array, so it needs store 4 digits to eval `&&` as negative and finish the program...

If im correct, changing the 5 for a 4 will exit when gets to 3 digits printing numbers from 1 to 100 (by 2, from 1 to 10 and cant be smaller than that).

I may not resist the temptation of compile this before technosaurus' reply.

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#12 Post by technosaurus »

and we have a winner. ...not bad for a beginner.
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

Post Reply