Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Wed 17 Dec 2014, 16:07
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
learning by viewing contest
Post_new_topic   Reply_to_topic View_previous_topic :: View_next_topic
Page 1 of 1 Posts_count  
Author Message
technosaurus


Joined: 18 May 2008
Posts: 4423

PostPosted: Thu 13 Dec 2012, 19:58    Post_subject:  learning by viewing contest
Sub_title: the prize is pride
 

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:
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:
function F(x){var t=XMLHttpRequest();try{t.open("HEAD",x,false);t.send();return t.status!=404}catch(n){return false}}

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

_________________
Web Programming - Pet Packaging 100 & 101

Edited_times_total
Back to top
View user's profile Send_private_message 
MinHundHettePerro


Joined: 05 Feb 2009
Posts: 856
Location: SE

PostPosted: Thu 13 Dec 2012, 20:32    Post_subject:  

Right, ... Confused

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 Question , which I don't know in _any_ detail at all Embarassed Wink

Or, whatever ... Smile/ MHHP

_________________
Celeron 2.8 GHz, 1 GB, i82845, many ptns, modes 12, 13
Dual Xeon 3.2 GHz, 1 GB, nvidia quadro nvs 285

Slackos & 214X, ... and Q6xx
Nämen, vaf.... ln -s /dev/null MHHP
Back to top
View user's profile Send_private_message 
technosaurus


Joined: 18 May 2008
Posts: 4423

PostPosted: Fri 14 Dec 2012, 00:23    Post_subject:  

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:
#!/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:
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...)

_________________
Web Programming - Pet Packaging 100 & 101
Back to top
View user's profile Send_private_message 
technosaurus


Joined: 18 May 2008
Posts: 4423

PostPosted: Fri 14 Dec 2012, 01:20    Post_subject:  

here is a simple, but useful javascript function:
Code:
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

_________________
Web Programming - Pet Packaging 100 & 101
Back to top
View user's profile Send_private_message 
MinHundHettePerro


Joined: 05 Feb 2009
Posts: 856
Location: SE

PostPosted: Fri 14 Dec 2012, 17:33    Post_subject: Re: learning by viewing contest
Sub_title: the prize is pride
 

technosaurus wrote:

Current open contests:

C:
Code:
#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 Confused Shocked
Code:
#!/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 Smile/ MHHP

_________________
Celeron 2.8 GHz, 1 GB, i82845, many ptns, modes 12, 13
Dual Xeon 3.2 GHz, 1 GB, nvidia quadro nvs 285

Slackos & 214X, ... and Q6xx
Nämen, vaf.... ln -s /dev/null MHHP
Back to top
View user's profile Send_private_message 
technosaurus


Joined: 18 May 2008
Posts: 4423

PostPosted: Fri 14 Dec 2012, 18:54    Post_subject:  

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.
_________________
Web Programming - Pet Packaging 100 & 101
Back to top
View user's profile Send_private_message 
SFR


Joined: 26 Oct 2011
Posts: 1111

PostPosted: Fri 14 Dec 2012, 21:06    Post_subject: Re: learning by viewing contest
Sub_title: the prize is pride
 

technosaurus wrote:
C:
Code:
#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!

_________________
[O]bdurate [R]ules [D]estroy [E]nthusiastic [R]ebels => [C]reative [H]umans [A]lways [O]pen [S]ource
Omnia mea mecum porto.
Back to top
View user's profile Send_private_message 
technosaurus


Joined: 18 May 2008
Posts: 4423

PostPosted: Sat 15 Dec 2012, 00:47    Post_subject:  

@SFR - congrats. Yes it is the equivalent of shift in bash.
_________________
Web Programming - Pet Packaging 100 & 101
Back to top
View user's profile Send_private_message 
technosaurus


Joined: 18 May 2008
Posts: 4423

PostPosted: Thu 20 Dec 2012, 17:17    Post_subject:  

here is another javascript
Code:
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

_________________
Web Programming - Pet Packaging 100 & 101

Edited_time_total
Back to top
View user's profile Send_private_message 
technosaurus


Joined: 18 May 2008
Posts: 4423

PostPosted: Fri 09 May 2014, 04:29    Post_subject:  

without compiling, what does this code do?
Code:
#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

_________________
Web Programming - Pet Packaging 100 & 101
Back to top
View user's profile Send_private_message 
GustavoYz


Joined: 07 Jul 2010
Posts: 897
Location: .ar

PostPosted: Wed 18 Jun 2014, 21:51    Post_subject:  

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.

_________________

Back to top
View user's profile Send_private_message 
technosaurus


Joined: 18 May 2008
Posts: 4423

PostPosted: Wed 18 Jun 2014, 22:28    Post_subject:  

and we have a winner. ...not bad for a beginner.
_________________
Web Programming - Pet Packaging 100 & 101
Back to top
View user's profile Send_private_message 
Display_posts:   Sort by:   
Page 1 of 1 Posts_count  
Post_new_topic   Reply_to_topic View_previous_topic :: View_next_topic
 Forum index » Off-Topic Area » Programming
Jump to:  

Rules_post_cannot
Rules_reply_cannot
Rules_edit_cannot
Rules_delete_cannot
Rules_vote_cannot
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.0778s ][ Queries: 12 (0.0035s) ][ GZIP on ]