<?xml version="1.0" encoding="utf-8"?><?xml-stylesheet title="XSL formatting" type="text/xsl" href="http://grapsus.net/blog/feed/rss2/xslt" ?><rss version="2.0"
  xmlns:dc="http://purl.org/dc/elements/1.1/"
  xmlns:wfw="http://wellformedweb.org/CommentAPI/"
  xmlns:content="http://purl.org/rss/1.0/modules/content/"
  xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
  <title>Grapsus.net</title>
  <link>http://grapsus.net/blog/</link>
  <atom:link href="http://www.grapsus.net/blog/feed/rss2" rel="self" type="application/rss+xml"/>
  <description></description>
  <language>fr</language>
  <pubDate>Sun, 20 May 2012 00:53:05 +0200</pubDate>
  <copyright></copyright>
  <docs>http://blogs.law.harvard.edu/tech/rss</docs>
  <generator>Dotclear</generator>
  
    
  <item>
    <title>Nagios plugin to check SSL certificates</title>
    <link>http://grapsus.net/blog/post/Nagios-plugin-to-check-SSL-certificates</link>
    <guid isPermaLink="false">urn:md5:148c9bd99c7f6cc63126fce5a959e32d</guid>
    <pubDate>Tue, 27 Sep 2011 11:15:00 +0200</pubDate>
    <dc:creator>Alexis Bezverkhyy</dc:creator>
        <category>bash</category><category>nagios</category>    
    <description>    &lt;p&gt;Here a &lt;em&gt;Nagios&lt;/em&gt; plugin I wrote for checking &lt;em&gt;SSL&lt;/em&gt; certificates for expiry. You can set it up for emitting a &lt;em&gt;WARNING&lt;/em&gt; or a &lt;em&gt;CRITICAL&lt;/em&gt; state &lt;em&gt;N&lt;/em&gt; days before expiry.&lt;/p&gt;


&lt;p&gt;&lt;em&gt;/root/bin/nagios-check-crt.sh&lt;/em&gt;&amp;nbsp;:&lt;/p&gt;
&lt;table class=&quot;bash codecolor bash&quot; style=&quot;font-family:inherit&quot;&gt;&lt;tbody&gt;&lt;tr class=&quot;li1&quot;&gt;&lt;td style=&quot;vertical-align:top;&quot; class=&quot;codelines&quot;&gt;&lt;pre style=&quot;vertical-align:top;&quot; class=&quot;codecontent&quot;&gt;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
&lt;/pre&gt;&lt;/td&gt;&lt;td style=&quot;vertical-align:top;&quot; class=&quot;codecontent&quot;&gt;&lt;pre style=&quot;vertical-align:top;&quot; class=&quot;codecontent&quot;&gt;&lt;span style=&quot;color: #666666; font-style: italic;&quot;&gt;#!/bin/bash&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #666666; font-style: italic;&quot;&gt;# Written by Alexis Bezverkhyy &amp;lt;alexis@grapsus.net&amp;gt; in september 2011&lt;/span&gt;
&lt;span style=&quot;color: #666666; font-style: italic;&quot;&gt;# This is free and unencumbered software released into the public domain.&lt;/span&gt;
&lt;span style=&quot;color: #666666; font-style: italic;&quot;&gt;# For more information, please refer to &amp;lt;http://unlicense.org/&amp;gt;&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;function&lt;/span&gt; PRINT_USAGE&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#123;&lt;/span&gt;
&amp;nbsp; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;echo&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;This Nagios plugin checks SSL certificates for expiration :&lt;/span&gt;
&lt;span style=&quot;color: #ff0000;&quot;&gt; &amp;nbsp;-c HOST:PORT host and port to connect&lt;/span&gt;
&lt;span style=&quot;color: #ff0000;&quot;&gt; &amp;nbsp;-d DAYS &amp;nbsp;minimum days before expiry, otherwise a WARNING is issued&lt;/span&gt;
&lt;span style=&quot;color: #ff0000;&quot;&gt; &amp;nbsp;-D DAYS &amp;nbsp;minimum days before expiry, otherwise a CRITICAL is issued&lt;/span&gt;
&lt;span style=&quot;color: #ff0000;&quot;&gt; &amp;nbsp;-h &amp;nbsp; &amp;nbsp;prints out this help&amp;quot;&lt;/span&gt;
&amp;nbsp; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;exit&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;0&lt;/span&gt;
&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #007800;&quot;&gt;CONNECT&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;''&lt;/span&gt;;&lt;span style=&quot;color: #007800;&quot;&gt;WDAYS&lt;/span&gt;=&lt;span style=&quot;color: #000000;&quot;&gt;0&lt;/span&gt;;&lt;span style=&quot;color: #007800;&quot;&gt;CDAYS&lt;/span&gt;=&lt;span style=&quot;color: #000000;&quot;&gt;0&lt;/span&gt;;
&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;declare&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-i&lt;/span&gt; CDAYS 
&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;declare&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-i&lt;/span&gt; WDAYS
&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;while&lt;/span&gt; &lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;true&lt;/span&gt; ; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;do&lt;/span&gt;
&amp;nbsp; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;getopts&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;'c:d:D:h'&lt;/span&gt; OPT 
&amp;nbsp; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#91;&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$OPT&lt;/span&gt;&amp;quot;&lt;/span&gt; = &lt;span style=&quot;color: #ff0000;&quot;&gt;'?'&lt;/span&gt; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#93;&lt;/span&gt; ; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;then&lt;/span&gt; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;break&lt;/span&gt;; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;fi&lt;/span&gt;; 
&amp;nbsp; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;case&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$OPT&lt;/span&gt;&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;in&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;c&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #007800;&quot;&gt;CONNECT&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$OPTARG&lt;/span&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;;;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;d&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #007800;&quot;&gt;WDAYS&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$OPTARG&lt;/span&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;;;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;D&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #007800;&quot;&gt;CDAYS&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$OPTARG&lt;/span&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;;;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;h&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#41;&lt;/span&gt; PRINT_USAGE&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;;;&lt;/span&gt;
&amp;nbsp; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;esac&lt;/span&gt;
&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;done&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#91;&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-z&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$CONNECT&lt;/span&gt;&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-o&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;'('&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$WDAYS&lt;/span&gt;&amp;quot;&lt;/span&gt; = &lt;span style=&quot;color: #ff0000;&quot;&gt;'0'&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-a&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$CDAYS&lt;/span&gt;&amp;quot;&lt;/span&gt; = &lt;span style=&quot;color: #ff0000;&quot;&gt;'0'&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;')'&lt;/span&gt; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#93;&lt;/span&gt; ; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;then&lt;/span&gt;
&amp;nbsp; PRINT_USAGE
&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;fi&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;function&lt;/span&gt; get_crt_expiry
&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#123;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #666666; font-style: italic;&quot;&gt;# connect to host with OpenSSL client, filter CRT, parse CRT,&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #666666; font-style: italic;&quot;&gt;# get expiry time, convert to traditionnal y-m-d h:s&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;echo&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-n&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;''&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;|&lt;/span&gt; openssl s_client &lt;span style=&quot;color: #660033;&quot;&gt;-connect&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;$1&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;gt;/&lt;/span&gt;dev&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;null \
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;|&lt;/span&gt; &lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;awk&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;'BEGIN { p = 0 }&lt;/span&gt;
&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/BEGIN CERT/ { p = 1 }&lt;/span&gt;
&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{ if (p) print $0 }&lt;/span&gt;
&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/END CERT/ { p = 0 }'&lt;/span&gt; \
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;|&lt;/span&gt; openssl asn1parse &lt;span style=&quot;color: #000000;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;gt;/&lt;/span&gt;dev&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;null \
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;|&lt;/span&gt; &lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;grep&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;'UTCTIME'&lt;/span&gt; \
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;|&lt;/span&gt; &lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;awk&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;'{ print $7 }'&lt;/span&gt; \
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;|&lt;/span&gt; &lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;tr&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-d&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;'Z:'&lt;/span&gt; \
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;|&lt;/span&gt; &lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;tail&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-n&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;1&lt;/span&gt; \
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;|&lt;/span&gt; &lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;sed&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-r&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;'s/^(..)(..)(..)(..)(..).*$/\1-\2-\3 \4:\5/'&lt;/span&gt;
&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #007800;&quot;&gt;EXPIRY&lt;/span&gt;=$&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#40;&lt;/span&gt;get_crt_expiry &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$CONNECT&lt;/span&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#41;&lt;/span&gt;
&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#91;&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-z&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$EXPIRY&lt;/span&gt;&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#93;&lt;/span&gt; ; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;then&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;echo&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;WARNING - cannot get expiry date for &lt;span style=&quot;color: #007800;&quot;&gt;$CONNECT&lt;/span&gt;&amp;quot;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;exit&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;1&lt;/span&gt;
&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;fi&lt;/span&gt;
&lt;span style=&quot;color: #007800;&quot;&gt;EPOCH_EXPIRY&lt;/span&gt;=$&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;date&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-d&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$EXPIRY&lt;/span&gt;&amp;quot;&lt;/span&gt; +&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;%&lt;/span&gt;s&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#41;&lt;/span&gt;
&lt;span style=&quot;color: #007800;&quot;&gt;EPOCH_NOW&lt;/span&gt;=$&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;date&lt;/span&gt; +&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;%&lt;/span&gt;s&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#41;&lt;/span&gt;
&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;let&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;REM_DAYS = (EPOCH_EXPIRY - EPOCH_NOW)/(24*3600)&amp;quot;&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#91;&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$CDAYS&lt;/span&gt;&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-gt&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;0&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-a&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$REM_DAYS&lt;/span&gt;&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-lt&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$CDAYS&lt;/span&gt;&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#93;&lt;/span&gt; ; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;then&lt;/span&gt;
&amp;nbsp; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;echo&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;CRITICAL - &lt;span style=&quot;color: #007800;&quot;&gt;$CONNECT&lt;/span&gt; crt expries on &lt;span style=&quot;color: #007800;&quot;&gt;$EXPIRY&lt;/span&gt; (&lt;span style=&quot;color: #007800;&quot;&gt;$REM_DAYS&lt;/span&gt; days left)&amp;quot;&lt;/span&gt; 
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;exit&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;2&lt;/span&gt;
&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;fi&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#91;&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$WDAYS&lt;/span&gt;&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-gt&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;0&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-a&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$REM_DAYS&lt;/span&gt;&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-lt&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$WDAYS&lt;/span&gt;&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#93;&lt;/span&gt; ; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;then&lt;/span&gt;
&amp;nbsp; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;echo&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;WARNING - &lt;span style=&quot;color: #007800;&quot;&gt;$CONNECT&lt;/span&gt; crt expries on &lt;span style=&quot;color: #007800;&quot;&gt;$EXPIRY&lt;/span&gt; (&lt;span style=&quot;color: #007800;&quot;&gt;$REM_DAYS&lt;/span&gt; days left)&amp;quot;&lt;/span&gt; 
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;exit&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;1&lt;/span&gt;
&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;fi&lt;/span&gt;
&amp;nbsp; 
&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;echo&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;OK - &lt;span style=&quot;color: #007800;&quot;&gt;$CONNECT&lt;/span&gt; crt expries on &lt;span style=&quot;color: #007800;&quot;&gt;$EXPIRY&lt;/span&gt; (&lt;span style=&quot;color: #007800;&quot;&gt;$REM_DAYS&lt;/span&gt; days left)&amp;quot;&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;


&lt;p&gt;Here's the configuration to check a simple &lt;em&gt;HTTPS&lt;/em&gt; service.&lt;/p&gt;


&lt;p&gt;&lt;em&gt;commands.cfg&lt;/em&gt;&amp;nbsp;:&lt;/p&gt;
&lt;table class=&quot;text codecolor text&quot; style=&quot;font-family:inherit&quot;&gt;&lt;tbody&gt;&lt;tr class=&quot;li1&quot;&gt;&lt;td style=&quot;vertical-align:top;&quot; class=&quot;codelines&quot;&gt;&lt;pre style=&quot;vertical-align:top;&quot; class=&quot;codecontent&quot;&gt;1
2
3
4
&lt;/pre&gt;&lt;/td&gt;&lt;td style=&quot;vertical-align:top;&quot; class=&quot;codecontent&quot;&gt;&lt;pre style=&quot;vertical-align:top;&quot; class=&quot;codecontent&quot;&gt;define command {
&amp;nbsp; command_name check_crt
&amp;nbsp; command_line /root/bin/nagios-check-crt.sh -c $ARG1$ -d $ARG2$ -D $ARG3$
}&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;


&lt;p&gt;&lt;em&gt;myhost.cfg&lt;/em&gt;&amp;nbsp;:&lt;/p&gt;
&lt;table class=&quot;text codecolor text&quot; style=&quot;font-family:inherit&quot;&gt;&lt;tbody&gt;&lt;tr class=&quot;li1&quot;&gt;&lt;td style=&quot;vertical-align:top;&quot; class=&quot;codelines&quot;&gt;&lt;pre style=&quot;vertical-align:top;&quot; class=&quot;codecontent&quot;&gt;1
2
3
4
5
6
&lt;/pre&gt;&lt;/td&gt;&lt;td style=&quot;vertical-align:top;&quot; class=&quot;codecontent&quot;&gt;&lt;pre style=&quot;vertical-align:top;&quot; class=&quot;codecontent&quot;&gt;define service {
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; use &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; generic-service
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; host_name &amp;nbsp;myhost
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; service_description HTTPS-CRT
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; check_command check_crt!myhost.com:443!60!30
}&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;</description>
    
    
    
          <comments>http://grapsus.net/blog/post/Nagios-plugin-to-check-SSL-certificates#comment-form</comments>
      <wfw:comment>http://grapsus.net/blog/post/Nagios-plugin-to-check-SSL-certificates#comment-form</wfw:comment>
      <wfw:commentRss>http://grapsus.net/blog/feed/atom/comments/31</wfw:commentRss>
      </item>
    
  <item>
    <title>Low-level Python debugging with GDB</title>
    <link>http://grapsus.net/blog/post/Low-level-Python-debugging-with-GDB</link>
    <guid isPermaLink="false">urn:md5:0c57c619a504409ee83621cfdf20e777</guid>
    <pubDate>Thu, 11 Aug 2011 22:08:00 +0200</pubDate>
    <dc:creator>Alexis Bezverkhyy</dc:creator>
        <category>code</category><category>debug</category><category>gdb</category><category>hack</category><category>python</category>    
    <description>    &lt;h2&gt;Introduction&lt;/h2&gt;


&lt;p&gt;Your &lt;em&gt;Python&lt;/em&gt; program is crashing or unresponsive and you can't figure it out with traditional tools (printing, &lt;em&gt;PDB&lt;/em&gt;)&amp;nbsp;? Then this tutorial might help you&amp;nbsp;!&lt;/p&gt;


&lt;p&gt;&lt;em&gt;GDB&lt;/em&gt; is a very powerful debugging tool, but it's hardly intuitive to use and moreover it doesn't understand &lt;em&gt;Python&lt;/em&gt; data structures. With no hacking it will
only be able to print the system stack and addresses of some variables whereas what you need is the &lt;em&gt;Python&lt;/em&gt; stack and &lt;em&gt;Python&lt;/em&gt; local variables in a human
readable form.&lt;/p&gt;


&lt;p&gt;To start, install &lt;em&gt;GDB&lt;/em&gt; and a debug build of your &lt;em&gt;Python&lt;/em&gt; interpreter (&lt;em&gt;python2.x-dbg&lt;/em&gt; on &lt;em&gt;Debian&lt;/em&gt;-like systems).&lt;/p&gt;


&lt;p&gt;This last file contains a few macros for &lt;em&gt;GDB&lt;/em&gt; that will enable it to print &lt;em&gt;Python&lt;/em&gt; locals and stack.&lt;/p&gt;


&lt;h2&gt;Test program&lt;/h2&gt;


&lt;p&gt;Let's pretend we don't know why this simple program doesn't stop and why it's unresponsive&amp;nbsp;:&lt;/p&gt;

&lt;table class=&quot;python codecolor python&quot; style=&quot;font-family:inherit&quot;&gt;&lt;tbody&gt;&lt;tr class=&quot;li1&quot;&gt;&lt;td style=&quot;vertical-align:top;&quot; class=&quot;codelines&quot;&gt;&lt;pre style=&quot;vertical-align:top;&quot; class=&quot;codecontent&quot;&gt;1
2
3
4
5
6
7
8
9
&lt;/pre&gt;&lt;/td&gt;&lt;td style=&quot;vertical-align:top;&quot; class=&quot;codecontent&quot;&gt;&lt;pre style=&quot;vertical-align:top;&quot; class=&quot;codecontent&quot;&gt;&lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: #dc143c;&quot;&gt;time&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;def&lt;/span&gt; next&lt;span style=&quot;color: black;&quot;&gt;&amp;#40;&lt;/span&gt;i&lt;span style=&quot;color: black;&quot;&gt;&amp;#41;&lt;/span&gt;:
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #dc143c;&quot;&gt;time&lt;/span&gt;.&lt;span style=&quot;color: black;&quot;&gt;sleep&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff4500;&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#41;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; i = &lt;span style=&quot;color: #ff4500;&quot;&gt;1&lt;/span&gt; - i
&amp;nbsp;
i = &lt;span style=&quot;color: #ff4500;&quot;&gt;1&lt;/span&gt;
&lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;while&lt;/span&gt; &lt;span style=&quot;color: #008000;&quot;&gt;True&lt;/span&gt;:
&amp;nbsp; &amp;nbsp; next&lt;span style=&quot;color: black;&quot;&gt;&amp;#40;&lt;/span&gt;i&lt;span style=&quot;color: black;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;


&lt;h2&gt;Easy case&lt;/h2&gt;


&lt;p&gt;If your problem is easily reproducible, then you're in luck.
Restart your script with the debug build of the &lt;em&gt;Python&lt;/em&gt; interpreter and attach to it with &lt;em&gt;GDB&lt;/em&gt;&amp;nbsp;:&lt;/p&gt;

&lt;table class=&quot;text codecolor text&quot; style=&quot;font-family:inherit&quot;&gt;&lt;tbody&gt;&lt;tr class=&quot;li1&quot;&gt;&lt;td style=&quot;vertical-align:top;&quot; class=&quot;codelines&quot;&gt;&lt;pre style=&quot;vertical-align:top;&quot; class=&quot;codecontent&quot;&gt;1
2
3
&lt;/pre&gt;&lt;/td&gt;&lt;td style=&quot;vertical-align:top;&quot; class=&quot;codecontent&quot;&gt;&lt;pre style=&quot;vertical-align:top;&quot; class=&quot;codecontent&quot;&gt;python2.7-dbg test.py &amp;amp;
[1] 7877
gdb -p 7877&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;


&lt;p&gt;At this point the &lt;em&gt;Python&lt;/em&gt; interpreter has been interrupted and the script is paused so we can inspect it.
First, we can see the &lt;em&gt;Python&lt;/em&gt; stack of our script&amp;nbsp;:&lt;/p&gt;

&lt;table class=&quot;text codecolor text&quot; style=&quot;font-family:inherit&quot;&gt;&lt;tbody&gt;&lt;tr class=&quot;li1&quot;&gt;&lt;td style=&quot;vertical-align:top;&quot; class=&quot;codelines&quot;&gt;&lt;pre style=&quot;vertical-align:top;&quot; class=&quot;codecontent&quot;&gt;1
2
3
4
5
&lt;/pre&gt;&lt;/td&gt;&lt;td style=&quot;vertical-align:top;&quot; class=&quot;codecontent&quot;&gt;&lt;pre style=&quot;vertical-align:top;&quot; class=&quot;codecontent&quot;&gt;(gdb) py-bt
#5 Frame 0x242ae50, for file test.py, line 4, in next (i=1)
&amp;nbsp; &amp;nbsp; time.sleep(10)
#8 Frame 0x2427e00, for file test.py, line 9, in &amp;lt;module&amp;gt; ()
&amp;nbsp; &amp;nbsp; next(i)&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;


&lt;p&gt;To avoid all confusion&amp;nbsp;: the most recent call comes first in this trace unlike when the backtrace is printed from &lt;em&gt;Python&lt;/em&gt;.
In &lt;em&gt;GDB&lt;/em&gt;, the most recent call is called active or selected. We can print &lt;em&gt;Python&lt;/em&gt; code and local variables in the selected frame&amp;nbsp;:&lt;/p&gt;

&lt;table class=&quot;text codecolor text&quot; style=&quot;font-family:inherit&quot;&gt;&lt;tbody&gt;&lt;tr class=&quot;li1&quot;&gt;&lt;td style=&quot;vertical-align:top;&quot; class=&quot;codelines&quot;&gt;&lt;pre style=&quot;vertical-align:top;&quot; class=&quot;codecontent&quot;&gt;1
2
3
4
5
6
7
8
9
10
11
12
&lt;/pre&gt;&lt;/td&gt;&lt;td style=&quot;vertical-align:top;&quot; class=&quot;codecontent&quot;&gt;&lt;pre style=&quot;vertical-align:top;&quot; class=&quot;codecontent&quot;&gt;(gdb) py-list 
&amp;nbsp; &amp;nbsp;1 &amp;nbsp; &amp;nbsp;import time
&amp;nbsp; &amp;nbsp;2 &amp;nbsp; &amp;nbsp;
&amp;nbsp; &amp;nbsp;3 &amp;nbsp; &amp;nbsp;def next(i):
&amp;nbsp; &amp;gt;4 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;time.sleep(10)
&amp;nbsp; &amp;nbsp;5 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;i = 1 - i
&amp;nbsp; &amp;nbsp;6 &amp;nbsp; &amp;nbsp;
&amp;nbsp; &amp;nbsp;7 &amp;nbsp; &amp;nbsp;i = 1
&amp;nbsp; &amp;nbsp;8 &amp;nbsp; &amp;nbsp;while True:
&amp;nbsp; &amp;nbsp;9 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;next(i)
(gdb) py-locals 
i = 1&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;


&lt;p&gt;With &lt;em&gt;py-up&lt;/em&gt; and &lt;em&gt;py-down&lt;/em&gt; macros we can change the selected &lt;em&gt;Python&lt;/em&gt; frame. You must be aware that the process is still frozen and
those commands don't actually do anything to the process.&lt;/p&gt;

&lt;table class=&quot;text codecolor text&quot; style=&quot;font-family:inherit&quot;&gt;&lt;tbody&gt;&lt;tr class=&quot;li1&quot;&gt;&lt;td style=&quot;vertical-align:top;&quot; class=&quot;codelines&quot;&gt;&lt;pre style=&quot;vertical-align:top;&quot; class=&quot;codecontent&quot;&gt;1
2
3
4
5
6
7
8
9
10
&lt;/pre&gt;&lt;/td&gt;&lt;td style=&quot;vertical-align:top;&quot; class=&quot;codecontent&quot;&gt;&lt;pre style=&quot;vertical-align:top;&quot; class=&quot;codecontent&quot;&gt;(gdb) py-up
#8 Frame 0x2427e00, for file test.py, line 9, in &amp;lt;module&amp;gt; ()
&amp;nbsp; &amp;nbsp; next(i)
(gdb) py-list 
&amp;nbsp; &amp;nbsp;4 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;time.sleep(10)
&amp;nbsp; &amp;nbsp;5 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;i = 1 - i
&amp;nbsp; &amp;nbsp;6 &amp;nbsp; &amp;nbsp;
&amp;nbsp; &amp;nbsp;7 &amp;nbsp; &amp;nbsp;i = 1
&amp;nbsp; &amp;nbsp;8 &amp;nbsp; &amp;nbsp;while True:
&amp;nbsp; &amp;gt;9 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;next(i)&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;


&lt;p&gt;At this point &lt;em&gt;GDB&lt;/em&gt; has the same behavior as &lt;em&gt;PDB&lt;/em&gt;, it is good but not so helpful.
If you were unable to figure it out with &lt;em&gt;PDB&lt;/em&gt;, then you're probably dealing with some low-level
problem in some &lt;em&gt;Python&lt;/em&gt; internal, external lib or system call. To understand what happens you will
need to explore the system stack&amp;nbsp;:&lt;/p&gt;

&lt;table class=&quot;text codecolor text&quot; style=&quot;font-family:inherit&quot;&gt;&lt;tbody&gt;&lt;tr class=&quot;li1&quot;&gt;&lt;td style=&quot;vertical-align:top;&quot; class=&quot;codelines&quot;&gt;&lt;pre style=&quot;vertical-align:top;&quot; class=&quot;codecontent&quot;&gt;1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
&lt;/pre&gt;&lt;/td&gt;&lt;td style=&quot;vertical-align:top;&quot; class=&quot;codecontent&quot;&gt;&lt;pre style=&quot;vertical-align:top;&quot; class=&quot;codecontent&quot;&gt;(gdb) backtrace
#0 &amp;nbsp;0x00007f48671cddf3 in select () from /lib/x86_64-linux-gnu/libc.so.6
#1 &amp;nbsp;0x000000000058b22c in floatsleep (secs=10) at ../Modules/timemodule.c:943
#2 &amp;nbsp;0x000000000058a05f in time_sleep (self=0x0, args=(10,)) at ../Modules/timemodule.c:206
#3 &amp;nbsp;0x000000000048867e in PyCFunction_Call (func=&amp;lt;built-in function sleep&amp;gt;, arg=(10,), kw=0x0)
&amp;nbsp; &amp;nbsp; at ../Objects/methodobject.c:81
#4 &amp;nbsp;0x0000000000525efe in call_function (pp_stack=0x7fff3ac2ca40, oparg=1) at ../Python/ceval.c:4013
#5 &amp;nbsp;0x0000000000520f59 in PyEval_EvalFrameEx (f=Frame 0xe59e50, for file test.py, line 4, in next (i=1), throwflag=0)
&amp;nbsp; &amp;nbsp; at ../Python/ceval.c:2666
#6 &amp;nbsp;0x00000000005263c5 in fast_function (func=&amp;lt;function at remote 0xe074f8&amp;gt;, pp_stack=0x7fff3ac2cdc0, n=1, na=1, nk=0)
&amp;nbsp; &amp;nbsp; at ../Python/ceval.c:4099
#7 &amp;nbsp;0x00000000005260d5 in call_function (pp_stack=0x7fff3ac2cdc0, oparg=1) at ../Python/ceval.c:4034
#8 &amp;nbsp;0x0000000000520f59 in PyEval_EvalFrameEx (f=Frame 0xe56e00, for file test.py, line 9, in &amp;lt;module&amp;gt; (), throwflag=0)
&amp;nbsp; &amp;nbsp; at ../Python/ceval.c:2666
#9 &amp;nbsp;0x0000000000523744 in PyEval_EvalCodeEx (co=0xdb90f0, globals=
&amp;nbsp; &amp;nbsp; {'__builtins__': &amp;lt;module at remote 0x7f4868754470&amp;gt;, '__file__': 'test.py', '__package__': None, 'i': 1, 'time': &amp;lt;module at remote 0xe0e678&amp;gt;, '__name__': '__main__', 'next': &amp;lt;function at remote 0xe074f8&amp;gt;, '__doc__': None}, locals=
&amp;nbsp; &amp;nbsp; {'__builtins__': &amp;lt;module at remote 0x7f4868754470&amp;gt;, '__file__': 'test.py', '__package__': None, 'i': 1, 'time': &amp;lt;module at remote 0xe0e678&amp;gt;, '__name__': '__main__', 'next': &amp;lt;function at remote 0xe074f8&amp;gt;, '__doc__': None}, args=0x0, 
&amp;nbsp; &amp;nbsp; argcount=0, kws=0x0, kwcount=0, defs=0x0, defcount=0, closure=0x0) at ../Python/ceval.c:3253
#10 0x0000000000519b86 in PyEval_EvalCode (co=0xdb90f0, globals=
&amp;nbsp; &amp;nbsp; {'__builtins__': &amp;lt;module at remote 0x7f4868754470&amp;gt;, '__file__': 'test.py', '__package__': None, 'i': 1, 'time': &amp;lt;module at remote 0xe0e678&amp;gt;, '__name__': '__main__', 'next': &amp;lt;function at remote 0xe074f8&amp;gt;, '__doc__': None}, locals=
[...]&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;


&lt;p&gt;We can now see that our program is stuck in a call to &lt;em&gt;select()&lt;/em&gt;, in the &lt;em&gt;libc&lt;/em&gt; (you might not actually see exactly where the last call was made unless you have a debug version of that external library).
Now you should probably use &lt;em&gt;GDB&lt;/em&gt; commands &lt;em&gt;finish&lt;/em&gt; and &lt;em&gt;return&lt;/em&gt; to see if the execution thread comes back into the &lt;em&gt;Python&lt;/em&gt; interpreter. If not, it's probably a bug with an external library which should be reproducible outside of &lt;em&gt;Python&lt;/em&gt;.&lt;/p&gt;


&lt;h2&gt;Hard case&lt;/h2&gt;


&lt;p&gt;You might not be able to trigger systematically the bug which may be happening like once a day on one of your production servers. In this case we absolutely need to perform the analysis right on the production server where you found the unresponsive process.
As this process is running on an optimized and stripped version of the &lt;em&gt;Python&lt;/em&gt; interpreter, the stack trace will give you very few info&amp;nbsp;:&lt;/p&gt;

&lt;table class=&quot;text codecolor text&quot; style=&quot;font-family:inherit&quot;&gt;&lt;tbody&gt;&lt;tr class=&quot;li1&quot;&gt;&lt;td style=&quot;vertical-align:top;&quot; class=&quot;codelines&quot;&gt;&lt;pre style=&quot;vertical-align:top;&quot; class=&quot;codecontent&quot;&gt;1
2
3
4
5
6
7
8
9
10
11
12
13
&lt;/pre&gt;&lt;/td&gt;&lt;td style=&quot;vertical-align:top;&quot; class=&quot;codecontent&quot;&gt;&lt;pre style=&quot;vertical-align:top;&quot; class=&quot;codecontent&quot;&gt;(gdb) bt
#0 &amp;nbsp;0x00007f1b7f02cdf3 in select () from /lib/x86_64-linux-gnu/libc.so.6
#1 &amp;nbsp;0x00000000005048c2 in ?? ()
#2 &amp;nbsp;0x00000000004b90a9 in PyEval_EvalFrameEx ()
#3 &amp;nbsp;0x00000000004b9673 in PyEval_EvalFrameEx ()
#4 &amp;nbsp;0x00000000004bf600 in PyEval_EvalCodeEx ()
#5 &amp;nbsp;0x00000000004c0082 in PyEval_EvalCode ()
#6 &amp;nbsp;0x00000000004df2d2 in ?? ()
#7 &amp;nbsp;0x00000000004dfe64 in PyRun_FileExFlags ()
#8 &amp;nbsp;0x00000000004e096e in PyRun_SimpleFileExFlags ()
#9 &amp;nbsp;0x00000000004f09dd in Py_Main ()
#10 0x00007f1b7ef7cead in __libc_start_main () from /lib/x86_64-linux-gnu/libc.so.6
#11 0x000000000041f0a1 in _start ()&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;


&lt;p&gt;Only public symbols of &lt;em&gt;libpython&lt;/em&gt; are visible, we absolutely don't know where we are in the &lt;em&gt;Python&lt;/em&gt; script and we have no idea of the &lt;em&gt;Python&lt;/em&gt; stack.
Let's install the debug version of &lt;em&gt;Python&lt;/em&gt;, it will at least install GDB symbols for the &lt;em&gt;Python&lt;/em&gt; interpreter&amp;nbsp;:&lt;/p&gt;

&lt;table class=&quot;text codecolor text&quot; style=&quot;font-family:inherit&quot;&gt;&lt;tbody&gt;&lt;tr class=&quot;li1&quot;&gt;&lt;td style=&quot;vertical-align:top;&quot; class=&quot;codelines&quot;&gt;&lt;pre style=&quot;vertical-align:top;&quot; class=&quot;codecontent&quot;&gt;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
&lt;/pre&gt;&lt;/td&gt;&lt;td style=&quot;vertical-align:top;&quot; class=&quot;codecontent&quot;&gt;&lt;pre style=&quot;vertical-align:top;&quot; class=&quot;codecontent&quot;&gt;(gdb) bt
#0 &amp;nbsp;0x00007f0b1773bdf3 in select () from /lib/x86_64-linux-gnu/libc.so.6
#1 &amp;nbsp;0x00000000005048c2 in floatsleep (self=&amp;lt;value optimized out&amp;gt;, args=&amp;lt;value optimized out&amp;gt;)
&amp;nbsp; &amp;nbsp; at ../Modules/timemodule.c:943
#2 &amp;nbsp;time_sleep (self=&amp;lt;value optimized out&amp;gt;, args=&amp;lt;value optimized out&amp;gt;) at ../Modules/timemodule.c:206
#3 &amp;nbsp;0x00000000004b90a9 in call_function (f=&amp;lt;value optimized out&amp;gt;, throwflag=&amp;lt;value optimized out&amp;gt;)
&amp;nbsp; &amp;nbsp; at ../Python/ceval.c:4013
#4 &amp;nbsp;PyEval_EvalFrameEx (f=&amp;lt;value optimized out&amp;gt;, throwflag=&amp;lt;value optimized out&amp;gt;) at ../Python/ceval.c:2666
#5 &amp;nbsp;0x00000000004b9673 in fast_function (f=&amp;lt;value optimized out&amp;gt;, throwflag=&amp;lt;value optimized out&amp;gt;)
&amp;nbsp; &amp;nbsp; at ../Python/ceval.c:4099
#6 &amp;nbsp;call_function (f=&amp;lt;value optimized out&amp;gt;, throwflag=&amp;lt;value optimized out&amp;gt;) at ../Python/ceval.c:4034
#7 &amp;nbsp;PyEval_EvalFrameEx (f=&amp;lt;value optimized out&amp;gt;, throwflag=&amp;lt;value optimized out&amp;gt;) at ../Python/ceval.c:2666
#8 &amp;nbsp;0x00000000004bf600 in PyEval_EvalCodeEx (co=0x7f0b18c0f8b0, globals=&amp;lt;value optimized out&amp;gt;, 
&amp;nbsp; &amp;nbsp; locals=&amp;lt;value optimized out&amp;gt;, args=&amp;lt;value optimized out&amp;gt;, argcount=&amp;lt;value optimized out&amp;gt;, 
&amp;nbsp; &amp;nbsp; kws=&amp;lt;value optimized out&amp;gt;, kwcount=0, defs=0x0, defcount=0, closure=0x0) at ../Python/ceval.c:3253
#9 &amp;nbsp;0x00000000004c0082 in PyEval_EvalCode (co=&amp;lt;value optimized out&amp;gt;, globals=&amp;lt;value optimized out&amp;gt;, 
&amp;nbsp; &amp;nbsp; locals=&amp;lt;value optimized out&amp;gt;) at ../Python/ceval.c:667
#10 0x00000000004df2d2 in run_mod (mod=&amp;lt;value optimized out&amp;gt;, filename=&amp;lt;value optimized out&amp;gt;, globals=
&amp;nbsp; &amp;nbsp; {'__builtins__': &amp;lt;module at remote 0x7f0b18c8cad0&amp;gt;, '__file__': 'test.py', '__package__': None, 'i': 1, 'time': &amp;lt;module at remote 0x7f0b18bc7210&amp;gt;, '__name__': '__main__', 'next': &amp;lt;function at remote 0x7f0b18bc2578&amp;gt;, '__doc__': None}, locals=
&amp;nbsp; &amp;nbsp; {'__builtins__': &amp;lt;module at remote 0x7f0b18c8cad0&amp;gt;, '__file__': 'test.py', '__package__': None, 'i': 1, 'time': &amp;lt;module at remote 0x7f0b18bc7210&amp;gt;, '__name__': '__main__', 'next': &amp;lt;function at remote 0x7f0b18bc2578&amp;gt;, '__doc__': None}, flags=
&amp;lt;value optimized out&amp;gt;, arena=&amp;lt;value optimized out&amp;gt;) at ../Python/pythonrun.c:1346
#11 0x00000000004dfe64 in PyRun_FileExFlags (fp=0x2693690, filename=0x7ffffc54e67b &amp;quot;test.py&amp;quot;, 
&amp;nbsp; &amp;nbsp; start=&amp;lt;value optimized out&amp;gt;, globals=
&amp;nbsp; &amp;nbsp; {'__builtins__': &amp;lt;module at remote 0x7f0b18c8cad0&amp;gt;, '__file__': 'test.py', '__package__': None, 'i': 1, 'time': &amp;lt;module at remote 0x7f0b18bc7210&amp;gt;, '__name__': '__main__', 'next': &amp;lt;function at remote 0x7f0b18bc2578&amp;gt;, '__doc__': None}, locals=
&amp;nbsp; &amp;nbsp; {'__builtins__': &amp;lt;module at remote 0x7f0b18c8cad0&amp;gt;, '__file__': 'test.py', '__package__': None, 'i': 1, 'time': &amp;lt;module at remote 0x7f0b18bc7210&amp;gt;, '__name__': '__main__', 'next': &amp;lt;function at remote 0x7f0b18bc2578&amp;gt;, '__doc__': None}, closeit=1,
flags=0x7ffffc54ce30) at ../Python/pythonrun.c:1332
#12 0x00000000004e096e in PyRun_SimpleFileExFlags (fp=0x2693690, filename=&amp;lt;value optimized out&amp;gt;, closeit=1, flags=
&amp;nbsp; &amp;nbsp; 0x7ffffc54ce30) at ../Python/pythonrun.c:936
#13 0x00000000004f09dd in Py_Main (argc=&amp;lt;value optimized out&amp;gt;, argv=&amp;lt;value optimized out&amp;gt;) at ../Modules/main.c:599
#14 0x00007f0b1768bead in __libc_start_main () from /lib/x86_64-linux-gnu/libc.so.6
#15 0x000000000041f0a1 in _start ()&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;


&lt;p&gt;It is better, we now know the module and the file, but nothing about local variables or &lt;em&gt;Python&lt;/em&gt; stack.
&lt;strong&gt;Do not try to use &lt;em&gt;py-*&lt;/em&gt; macros&lt;/strong&gt;, they will not work as almost all &lt;em&gt;Python&lt;/em&gt; internals are &quot;optimized out&quot;, they will probably
trigger a segmentation fault by trying to print &lt;em&gt;Python&lt;/em&gt; objects with _PyObject_Dump.&lt;/p&gt;


&lt;p&gt;The only chance you have to find exactly where the code is failing is by carefully inspecting all the internal &lt;em&gt;Python&lt;/em&gt; variables, some of them are still usable and can be used to find out what's going on. For example&amp;nbsp;:&lt;/p&gt;

&lt;table class=&quot;text codecolor text&quot; style=&quot;font-family:inherit&quot;&gt;&lt;tbody&gt;&lt;tr class=&quot;li1&quot;&gt;&lt;td style=&quot;vertical-align:top;&quot; class=&quot;codelines&quot;&gt;&lt;pre style=&quot;vertical-align:top;&quot; class=&quot;codecontent&quot;&gt;1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
&lt;/pre&gt;&lt;/td&gt;&lt;td style=&quot;vertical-align:top;&quot; class=&quot;codecontent&quot;&gt;&lt;pre style=&quot;vertical-align:top;&quot; class=&quot;codecontent&quot;&gt;(gdb) select-frame 2
(gdb) info locals
secs = 10
(gdb) select-frame 3
(gdb) info locals
callargs = (10,)
flags = &amp;lt;value optimized out&amp;gt;
tstate = &amp;lt;value optimized out&amp;gt;
func = &amp;lt;built-in function sleep&amp;gt;
w = &amp;lt;value optimized out&amp;gt;
na = &amp;lt;value optimized out&amp;gt;
nk = &amp;lt;value optimized out&amp;gt;
n = &amp;lt;value optimized out&amp;gt;
pfunc = 0x26aace0
x = &amp;lt;value optimized out&amp;gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;


&lt;p&gt;Frame 2 was a call in &lt;em&gt;timemodule.c&lt;/em&gt; and showed us that the argument of the function call was &lt;em&gt;10 secs&lt;/em&gt;.&lt;/p&gt;


&lt;p&gt;Frame 3 is in &lt;em&gt;PyEval_EvalFrameEx()&lt;/em&gt; (main &lt;em&gt;Python&lt;/em&gt; bytecode interpretation routine) it brings us back into the interpreter. Almost all local variables were optimized, func tells us that the function call was for the function &lt;em&gt;sleep&lt;/em&gt;. Finally&amp;nbsp;:&lt;/p&gt;

&lt;table class=&quot;text codecolor text&quot; style=&quot;font-family:inherit&quot;&gt;&lt;tbody&gt;&lt;tr class=&quot;li1&quot;&gt;&lt;td style=&quot;vertical-align:top;&quot; class=&quot;codelines&quot;&gt;&lt;pre style=&quot;vertical-align:top;&quot; class=&quot;codecontent&quot;&gt;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
&lt;/pre&gt;&lt;/td&gt;&lt;td style=&quot;vertical-align:top;&quot; class=&quot;codecontent&quot;&gt;&lt;pre style=&quot;vertical-align:top;&quot; class=&quot;codecontent&quot;&gt;(gdb) select-frame 4
(gdb) info locals 
sp = 0x26aace8
stack_pointer = &amp;lt;value optimized out&amp;gt;
next_instr = 0x7f0b18bc0760 &amp;quot;\001d\002&amp;quot;
opcode = &amp;lt;value optimized out&amp;gt;
oparg = &amp;lt;value optimized out&amp;gt;
why = WHY_NOT
err = 0
x = &amp;lt;value optimized out&amp;gt;
v = &amp;lt;value optimized out&amp;gt;
w = &amp;lt;value optimized out&amp;gt;
u = &amp;lt;value optimized out&amp;gt;
t = &amp;lt;value optimized out&amp;gt;
stream = 0x0
fastlocals = &amp;lt;value optimized out&amp;gt;
freevars = 0x26aace0
retval = &amp;lt;value optimized out&amp;gt;
tstate = 0x25d20a0
co = 0x7f0b18c0fa30
instr_ub = -1
instr_lb = 0
instr_prev = -1
first_instr = 0x7f0b18bc0754 &amp;quot;t&amp;quot;
names = ('time', 'sleep')
consts = (None, 10, 1)
(gdb) p tstate-&amp;gt;frame-&amp;gt;f_globals 
$5 = 
&amp;nbsp; &amp;nbsp; {'__builtins__': &amp;lt;module at remote 0x7f0b18c8cad0&amp;gt;, '__file__': 'test.py', '__package__': None, 'i': 1, 'time': &amp;lt;module at remote 0x7f0b18bc7210&amp;gt;, '__name__': '__main__', 'next': 
&amp;lt;function at remote 0x7f0b18bc2578&amp;gt;, '__doc__': None}
(gdb) p tstate-&amp;gt;frame-&amp;gt;f_lineno 
$6 = 3&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;


&lt;p&gt;Here we go &lt;em&gt;test.py&lt;/em&gt; line 3, &lt;em&gt;i = 1&lt;/em&gt;, function call to &lt;em&gt;time.sleep(10)&lt;/em&gt;&amp;nbsp;!&lt;/p&gt;


&lt;h2&gt;Insanely hard case&lt;/h2&gt;


&lt;p&gt;If this those steps are still insufficient, you might try to set breakpoints on &lt;em&gt;call_function()&lt;/em&gt; and let the script run a little bit with &lt;em&gt;continue&lt;/em&gt; or &lt;em&gt;step&lt;/em&gt;.&lt;/p&gt;


&lt;p&gt;The final and ultimate solution is to run &lt;em&gt;PyEval_EvalFrameEx()&lt;/em&gt; step by step. Grab the source of &lt;em&gt;CPython&lt;/em&gt; and go to the &lt;em&gt;Python&lt;/em&gt; directory before launching &lt;em&gt;GDB&lt;/em&gt; (it must be the source of that exact same version of the &lt;em&gt;Python&lt;/em&gt; interpreter that runs your script)&amp;nbsp;:&lt;/p&gt;

&lt;table class=&quot;text codecolor text&quot; style=&quot;font-family:inherit&quot;&gt;&lt;tbody&gt;&lt;tr class=&quot;li1&quot;&gt;&lt;td style=&quot;vertical-align:top;&quot; class=&quot;codelines&quot;&gt;&lt;pre style=&quot;vertical-align:top;&quot; class=&quot;codecontent&quot;&gt;1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
&lt;/pre&gt;&lt;/td&gt;&lt;td style=&quot;vertical-align:top;&quot; class=&quot;codecontent&quot;&gt;&lt;pre style=&quot;vertical-align:top;&quot; class=&quot;codecontent&quot;&gt;(gdb) finish
Run till exit from #0 &amp;nbsp;0x00007fc13a60adf3 in select () from /lib/x86_64-linux-gnu/libc.so.6
0x00000000004dc17b in floatsleep (self=&amp;lt;value optimized out&amp;gt;, args=&amp;lt;value optimized out&amp;gt;)
&amp;nbsp; &amp;nbsp; at ../Modules/timemodule.c:914
914 &amp;nbsp; &amp;nbsp; ../Modules/timemodule.c: No such file or directory.
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; in ../Modules/timemodule.c
(gdb) next
time_sleep (self=&amp;lt;value optimized out&amp;gt;, args=&amp;lt;value optimized out&amp;gt;) at ../Modules/timemodule.c:206
206 &amp;nbsp; &amp;nbsp; in ../Modules/timemodule.c
(gdb) cd python2.7-2.7.2/Python/
Working directory /home/grapsus/ludia/python2.7-2.7.2/Python.
(gdb) next
208 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Py_INCREF(Py_None);
(gdb) next
209 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; return Py_None;
(gdb) next
210 &amp;nbsp; &amp;nbsp; }&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;


&lt;p&gt;Notice that it doesn't work until you &lt;em&gt;cd&lt;/em&gt; to the &lt;em&gt;Python&lt;/em&gt; directory of &lt;em&gt;CPython&lt;/em&gt; source tree. Same thing if you want to debug step by step some &lt;em&gt;Python&lt;/em&gt; module, like &lt;em&gt;gevent&lt;/em&gt;, you will need the source code of the very same version that's running the script.&lt;/p&gt;


&lt;p&gt;It is very time-consuming and you'll probably need a &lt;em&gt;Python&lt;/em&gt; bytecode reference to follow what's going on but you'll eventually find the issue.&lt;/p&gt;


&lt;h2&gt;Conclusion&lt;/h2&gt;


&lt;p&gt;Even with a strongly optimized and stripped &lt;em&gt;Python&lt;/em&gt; interpreter it is possible to debug or at least analyze a buggy &lt;em&gt;Python&lt;/em&gt; script.&lt;/p&gt;


&lt;h2&gt;References&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://docs.python.org/devguide/gdb.html&quot; title=&quot;http://docs.python.org/devguide/gdb.html&quot;&gt;http://docs.python.org/devguide/gdb...&lt;/a&gt; Debugging &lt;em&gt;Python&lt;/em&gt; with &lt;em&gt;GDB&lt;/em&gt; and the new &lt;em&gt;Python&lt;/em&gt; &lt;em&gt;GDB API&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://tromey.com/blog/?p=548&quot; title=&quot;http://tromey.com/blog/?p=548&quot;&gt;http://tromey.com/blog/?p=548&lt;/a&gt; About &lt;em&gt;GDB 7+ Python API&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://hg.python.org/cpython/file/e88362fb4950/Misc/gdbinit&quot; title=&quot;http://hg.python.org/cpython/file/e88362fb4950/Misc/gdbinit&quot;&gt;http://hg.python.org/cpython/file/e...&lt;/a&gt; Old &lt;em&gt;GDB&lt;/em&gt; macros for &lt;em&gt;Python&lt;/em&gt; (before &lt;em&gt;GDB 7&lt;/em&gt; and &lt;em&gt;Python API&lt;/em&gt;)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://marcioandreyoliveira.blogspot.com/2008/03/how-to-debug-striped-programs-with-gdb.html&quot; title=&quot;http://marcioandreyoliveira.blogspot.com/2008/03/how-to-debug-striped-programs-with-gdb.html&quot;&gt;http://marcioandreyoliveira.blogspo...&lt;/a&gt; Article about debugging stripped programs&lt;/li&gt;
&lt;li&gt;&lt;em&gt;CPython&lt;/em&gt; source code in general&lt;/li&gt;
&lt;/ul&gt;</description>
    
    
    
          <comments>http://grapsus.net/blog/post/Low-level-Python-debugging-with-GDB#comment-form</comments>
      <wfw:comment>http://grapsus.net/blog/post/Low-level-Python-debugging-with-GDB#comment-form</wfw:comment>
      <wfw:commentRss>http://grapsus.net/blog/feed/atom/comments/30</wfw:commentRss>
      </item>
    
  <item>
    <title>Sleep sort performance test</title>
    <link>http://grapsus.net/blog/post/Sleep-sort</link>
    <guid isPermaLink="false">urn:md5:bfa5b8585e62d3862907e4cb5a7a5c8d</guid>
    <pubDate>Mon, 25 Jul 2011 01:38:00 +0200</pubDate>
    <dc:creator>Alexis Bezverkhyy</dc:creator>
        <category>algorithm</category><category>gevent</category><category>python</category><category>sort</category>    
    <description>    &lt;h2&gt;Introduction&lt;/h2&gt;


&lt;p&gt;&lt;em&gt;Sleep sort&lt;/em&gt; is a very curious and original algorithm. It brings several interesting questions. Such as, how to measure its complexity or in what cases does it perform the best.&lt;/p&gt;


&lt;h2&gt;Principle&lt;/h2&gt;


&lt;p&gt;In a nutshell: for each element to sort, start a thread or a process which sleeps a time proportional to the element and then pushes it back to the main thread.&lt;/p&gt;


&lt;p&gt;Trivial Bash implementation:&lt;/p&gt;
&lt;table class=&quot;bash codecolor bash&quot; style=&quot;font-family:inherit&quot;&gt;&lt;tbody&gt;&lt;tr class=&quot;li1&quot;&gt;&lt;td style=&quot;vertical-align:top;&quot; class=&quot;codelines&quot;&gt;&lt;pre style=&quot;vertical-align:top;&quot; class=&quot;codecontent&quot;&gt;1
2
3
4
5
6
7
8
9
10
11
12
&lt;/pre&gt;&lt;/td&gt;&lt;td style=&quot;vertical-align:top;&quot; class=&quot;codecontent&quot;&gt;&lt;pre style=&quot;vertical-align:top;&quot; class=&quot;codecontent&quot;&gt;&lt;span style=&quot;color: #666666; font-style: italic;&quot;&gt;#!/bin/bash&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;function&lt;/span&gt; f &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#123;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;sleep&lt;/span&gt; &lt;span style=&quot;color: #007800;&quot;&gt;$1&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;echo&lt;/span&gt; &lt;span style=&quot;color: #007800;&quot;&gt;$1&lt;/span&gt;
&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;while&lt;/span&gt; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#91;&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-n&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;$1&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#93;&lt;/span&gt; ; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;do&lt;/span&gt; 
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; f &lt;span style=&quot;color: #007800;&quot;&gt;$1&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;amp;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;shift&lt;/span&gt;
&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;done&lt;/span&gt;
&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;wait&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;


&lt;h2&gt;Test suite&lt;/h2&gt;


&lt;p&gt;In order to study it and measure its performance we (me and &lt;a href=&quot;http://blog.hartok.fr/&quot;&gt;Hartok&lt;/a&gt;) decided to implement the &lt;em&gt;Sleep Sort&lt;/em&gt; in &lt;em&gt;Python&lt;/em&gt; and &lt;a href=&quot;http://www.gevent.org/gevent.html&quot;&gt;gevent&lt;/a&gt;.&lt;/p&gt;


&lt;p&gt;Our program generates a set of random numbers and sorts it with &lt;em&gt;Sleep sort&lt;/em&gt; then with the &lt;em&gt;Python&lt;/em&gt; built-in &lt;em&gt;Quick sort&lt;/em&gt; and records timings.
It works with three parameters:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;set size&lt;/li&gt;
&lt;li&gt;maximum value in the set&lt;/li&gt;
&lt;li&gt;the ratio between sleep time and values in the set (for example with 10 the thread for 42 sleeps for 42/10 seconds)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This last parameter is pretty tricky: the higher it is, the faster the &lt;em&gt;Sleep sort&lt;/em&gt; works. But if it is too high, the time to start a new thread (or &lt;em&gt;greenlet&lt;/em&gt;) will be more important than the sleep time for low values and the result will be incorrect. It seems to be strongly hardware dependent and a little tied to the other parameters.&lt;/p&gt;

&lt;table class=&quot;python codecolor python&quot; style=&quot;font-family:inherit&quot;&gt;&lt;tbody&gt;&lt;tr class=&quot;li1&quot;&gt;&lt;td style=&quot;vertical-align:top;&quot; class=&quot;codelines&quot;&gt;&lt;pre style=&quot;vertical-align:top;&quot; class=&quot;codecontent&quot;&gt;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
&lt;/pre&gt;&lt;/td&gt;&lt;td style=&quot;vertical-align:top;&quot; class=&quot;codecontent&quot;&gt;&lt;pre style=&quot;vertical-align:top;&quot; class=&quot;codecontent&quot;&gt;&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;#!/usr/bin/python&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: #dc143c;&quot;&gt;sys&lt;/span&gt;
&lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: #dc143c;&quot;&gt;random&lt;/span&gt;
&lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: #dc143c;&quot;&gt;time&lt;/span&gt;
&lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: #dc143c;&quot;&gt;copy&lt;/span&gt;
&lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;import&lt;/span&gt; gevent
&amp;nbsp;
divisor = &lt;span style=&quot;color: #ff4500;&quot;&gt;900000&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;def&lt;/span&gt; sort&lt;span style=&quot;color: black;&quot;&gt;&amp;#40;&lt;/span&gt;numbers&lt;span style=&quot;color: black;&quot;&gt;&amp;#41;&lt;/span&gt;:
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;&amp;quot;&amp;quot; Sorts a list with Sleep Sort algorithm and measures time&lt;/span&gt;
&lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; begin = &lt;span style=&quot;color: #dc143c;&quot;&gt;time&lt;/span&gt;.&lt;span style=&quot;color: #dc143c;&quot;&gt;time&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#41;&lt;/span&gt;
&amp;nbsp;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;def&lt;/span&gt; f&lt;span style=&quot;color: black;&quot;&gt;&amp;#40;&lt;/span&gt;n, result&lt;span style=&quot;color: black;&quot;&gt;&amp;#41;&lt;/span&gt;:
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; gevent.&lt;span style=&quot;color: black;&quot;&gt;sleep&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;float&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#40;&lt;/span&gt;n&lt;span style=&quot;color: black;&quot;&gt;&amp;#41;&lt;/span&gt;/divisor&lt;span style=&quot;color: black;&quot;&gt;&amp;#41;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; result.&lt;span style=&quot;color: black;&quot;&gt;append&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#40;&lt;/span&gt;n&lt;span style=&quot;color: black;&quot;&gt;&amp;#41;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; result = &lt;span style=&quot;color: black;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#93;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; greenlets = &lt;span style=&quot;color: black;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#93;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# spawn a sleep greenlet for each value&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;for&lt;/span&gt; n &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;in&lt;/span&gt; numbers:
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; greenlets.&lt;span style=&quot;color: black;&quot;&gt;append&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#40;&lt;/span&gt;gevent.&lt;span style=&quot;color: black;&quot;&gt;Greenlet&lt;/span&gt;.&lt;span style=&quot;color: black;&quot;&gt;spawn&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#40;&lt;/span&gt;f, n, result&lt;span style=&quot;color: black;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#41;&lt;/span&gt;
&amp;nbsp;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# wait for all greenlets to end&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; gevent.&lt;span style=&quot;color: black;&quot;&gt;joinall&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#40;&lt;/span&gt;greenlets&lt;span style=&quot;color: black;&quot;&gt;&amp;#41;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: black;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #dc143c;&quot;&gt;time&lt;/span&gt;.&lt;span style=&quot;color: #dc143c;&quot;&gt;time&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#41;&lt;/span&gt;-begin, result&lt;span style=&quot;color: black;&quot;&gt;&amp;#41;&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;def&lt;/span&gt; systemsort&lt;span style=&quot;color: black;&quot;&gt;&amp;#40;&lt;/span&gt;numbers&lt;span style=&quot;color: black;&quot;&gt;&amp;#41;&lt;/span&gt;:
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;&amp;quot;&amp;quot; Sorts a list with system sort function and measures time&lt;/span&gt;
&lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; begin = &lt;span style=&quot;color: #dc143c;&quot;&gt;time&lt;/span&gt;.&lt;span style=&quot;color: #dc143c;&quot;&gt;time&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#41;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; result = &lt;span style=&quot;color: #dc143c;&quot;&gt;copy&lt;/span&gt;.&lt;span style=&quot;color: #dc143c;&quot;&gt;copy&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#40;&lt;/span&gt;numbers&lt;span style=&quot;color: black;&quot;&gt;&amp;#41;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; result.&lt;span style=&quot;color: black;&quot;&gt;sort&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#41;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: black;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #dc143c;&quot;&gt;time&lt;/span&gt;.&lt;span style=&quot;color: #dc143c;&quot;&gt;time&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#41;&lt;/span&gt;-begin, result&lt;span style=&quot;color: black;&quot;&gt;&amp;#41;&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;def&lt;/span&gt; generate&lt;span style=&quot;color: black;&quot;&gt;&amp;#40;&lt;/span&gt;n, m&lt;span style=&quot;color: black;&quot;&gt;&amp;#41;&lt;/span&gt;: 
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;&amp;quot;&amp;quot; Generates a list of n random numbers, each being less than m&lt;/span&gt;
&lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; numbers = &lt;span style=&quot;color: black;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#93;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;for&lt;/span&gt; i &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: #008000;&quot;&gt;range&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#40;&lt;/span&gt;n&lt;span style=&quot;color: black;&quot;&gt;&amp;#41;&lt;/span&gt;:
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; numbers.&lt;span style=&quot;color: black;&quot;&gt;append&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #dc143c;&quot;&gt;random&lt;/span&gt;.&lt;span style=&quot;color: black;&quot;&gt;randint&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff4500;&quot;&gt;1&lt;/span&gt;, m&lt;span style=&quot;color: black;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#41;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;return&lt;/span&gt; numbers
&amp;nbsp;
&lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;if&lt;/span&gt; __name__ == &lt;span style=&quot;color: #483d8b;&quot;&gt;'__main__'&lt;/span&gt;:
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# read params&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;try&lt;/span&gt;:
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; n = &lt;span style=&quot;color: #008000;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #dc143c;&quot;&gt;sys&lt;/span&gt;.&lt;span style=&quot;color: black;&quot;&gt;argv&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #ff4500;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#41;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; m = &lt;span style=&quot;color: #008000;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #dc143c;&quot;&gt;sys&lt;/span&gt;.&lt;span style=&quot;color: black;&quot;&gt;argv&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #ff4500;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#41;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;except&lt;/span&gt; BaseException:
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;print&lt;/span&gt; &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;usage: %s n m [d]&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;%&lt;/span&gt; &lt;span style=&quot;color: #dc143c;&quot;&gt;sys&lt;/span&gt;.&lt;span style=&quot;color: black;&quot;&gt;argv&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #ff4500;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#93;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;print&lt;/span&gt; &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot; - n number of random numbers&amp;quot;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;print&lt;/span&gt; &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot; - m maximum random value&amp;quot;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;print&lt;/span&gt; &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot; - d time divisor (optionnal), default is %d&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;%&lt;/span&gt; divisor
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #dc143c;&quot;&gt;sys&lt;/span&gt;.&lt;span style=&quot;color: black;&quot;&gt;exit&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff4500;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#41;&lt;/span&gt;
&amp;nbsp;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;try&lt;/span&gt;:
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; divisor = &lt;span style=&quot;color: #008000;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #dc143c;&quot;&gt;sys&lt;/span&gt;.&lt;span style=&quot;color: black;&quot;&gt;argv&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #ff4500;&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#41;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;except&lt;/span&gt;:
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;pass&lt;/span&gt;
&amp;nbsp;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# generate random values&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; numbers = generate&lt;span style=&quot;color: black;&quot;&gt;&amp;#40;&lt;/span&gt;n, m&lt;span style=&quot;color: black;&quot;&gt;&amp;#41;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# sleep sort them&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: black;&quot;&gt;&amp;#40;&lt;/span&gt;sleep_time, sleep_result&lt;span style=&quot;color: black;&quot;&gt;&amp;#41;&lt;/span&gt; = sort&lt;span style=&quot;color: black;&quot;&gt;&amp;#40;&lt;/span&gt;numbers&lt;span style=&quot;color: black;&quot;&gt;&amp;#41;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;# system sort them&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: black;&quot;&gt;&amp;#40;&lt;/span&gt;system_time, system_result&lt;span style=&quot;color: black;&quot;&gt;&amp;#41;&lt;/span&gt; = systemsort&lt;span style=&quot;color: black;&quot;&gt;&amp;#40;&lt;/span&gt;numbers&lt;span style=&quot;color: black;&quot;&gt;&amp;#41;&lt;/span&gt;
&amp;nbsp;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;if&lt;/span&gt; system_result == sleep_result:
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;print&lt;/span&gt; &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;sleep sort succeed&amp;quot;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;print&lt;/span&gt; &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;sleep: %f&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;%&lt;/span&gt; sleep_time
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;print&lt;/span&gt; &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;system: %f&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;%&lt;/span&gt; system_time
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;else&lt;/span&gt;:
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;print&lt;/span&gt; &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;sleep sort failed (time divisor too ambitious ?)&amp;quot;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;print&lt;/span&gt; &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;sleep: (%d elements)&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;%&lt;/span&gt; &lt;span style=&quot;color: #008000;&quot;&gt;len&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#40;&lt;/span&gt;sleep_result&lt;span style=&quot;color: black;&quot;&gt;&amp;#41;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;#print sleep_result.__str__()&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;print&lt;/span&gt; &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;system: (%d elements)&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;%&lt;/span&gt; &lt;span style=&quot;color: #008000;&quot;&gt;len&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#40;&lt;/span&gt;system_result&lt;span style=&quot;color: black;&quot;&gt;&amp;#41;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;#print system_result.__str__()&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; errors = &lt;span style=&quot;color: #ff4500;&quot;&gt;0&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;for&lt;/span&gt; n &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: #008000;&quot;&gt;range&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;#40;&lt;/span&gt;n&lt;span style=&quot;color: black;&quot;&gt;&amp;#41;&lt;/span&gt;:
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;if&lt;/span&gt; system_result&lt;span style=&quot;color: black;&quot;&gt;&amp;#91;&lt;/span&gt;n&lt;span style=&quot;color: black;&quot;&gt;&amp;#93;&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;!&lt;/span&gt;= sleep_result&lt;span style=&quot;color: black;&quot;&gt;&amp;#91;&lt;/span&gt;n&lt;span style=&quot;color: black;&quot;&gt;&amp;#93;&lt;/span&gt;:
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; errors = errors + &lt;span style=&quot;color: #ff4500;&quot;&gt;1&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #ff7700;font-weight:bold;&quot;&gt;print&lt;/span&gt; &lt;span style=&quot;color: #483d8b;&quot;&gt;&amp;quot;%d errors&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;%&lt;/span&gt; errors&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;


&lt;h2&gt;Test results&lt;/h2&gt;


&lt;p&gt;&lt;img src=&quot;http://grapsus.net/blog/public/sleepsort/quicksort.png&quot; alt=&quot;quicksort.png&quot; style=&quot;display:block; margin:0 auto;&quot; title=&quot;quicksort.png, juil. 2011&quot; /&gt;&lt;/p&gt;


&lt;p&gt;&lt;img src=&quot;http://grapsus.net/blog/public/sleepsort/sleepsort.png&quot; alt=&quot;sleepsort.png&quot; style=&quot;display:block; margin:0 auto;&quot; title=&quot;sleepsort.png, juil. 2011&quot; /&gt;&lt;/p&gt;


&lt;p&gt;&lt;img src=&quot;http://grapsus.net/blog/public/sleepsort/ratio.png&quot; alt=&quot;ratio.png&quot; style=&quot;display:block; margin:0 auto;&quot; title=&quot;ratio.png, juil. 2011&quot; /&gt;&lt;/p&gt;


&lt;p&gt;The overall performance of the &lt;em&gt;Sleep sort&lt;/em&gt; is awful, but it has interesting performance when it comes to sets with few different values.&lt;/p&gt;</description>
    
    
    
          <comments>http://grapsus.net/blog/post/Sleep-sort#comment-form</comments>
      <wfw:comment>http://grapsus.net/blog/post/Sleep-sort#comment-form</wfw:comment>
      <wfw:commentRss>http://grapsus.net/blog/feed/atom/comments/29</wfw:commentRss>
      </item>
    
  <item>
    <title>Blog migré !</title>
    <link>http://grapsus.net/blog/post/Blog-migr%C3%A9-%21</link>
    <guid isPermaLink="false">urn:md5:01ba6d5b1909b1e7c93da5dce2cb1c31</guid>
    <pubDate>Fri, 22 Jul 2011 11:20:00 +0200</pubDate>
    <dc:creator>Alexis Bezverkhyy</dc:creator>
        <category>blog</category><category>réseau</category><category>xen</category>    
    <description>    &lt;p&gt;Plusieurs internautes reconnus ont retwitté mon dernier billet. Merci Stéphane Bortzmeyer et Korben&amp;nbsp;! Mon blog était devenu affreusement lent à consulter, la ligne ADSL ne tenant plus la charge avec son petit mégabit d'upload. Plus de 50 personnes se sont rabattues sur une copie que j'ai envoyée sur &lt;em&gt;pastebin&lt;/em&gt;.&lt;/p&gt;


&lt;p&gt;J'ai décidé de migrer mon blog immédiatement vers un serveur &lt;em&gt;OVH&lt;/em&gt;. Merci à &lt;em&gt;Ekinox&lt;/em&gt; pour ce gros &lt;em&gt;domU&lt;/em&gt;&amp;nbsp;! Tout est de nouveau opérationnel et rapide si les &lt;em&gt;DNS&lt;/em&gt; se sont bien propagés chez vous. Je migrerai le reste de mes services demain, Mercurial, SVN, etc. L'ancien serveur est toujours accessible sur&lt;/p&gt;

&lt;pre&gt;home.grapsus.net&lt;/pre&gt;</description>
    
    
    
          <comments>http://grapsus.net/blog/post/Blog-migr%C3%A9-%21#comment-form</comments>
      <wfw:comment>http://grapsus.net/blog/post/Blog-migr%C3%A9-%21#comment-form</wfw:comment>
      <wfw:commentRss>http://grapsus.net/blog/feed/atom/comments/28</wfw:commentRss>
      </item>
    
  <item>
    <title>Bouygues Télécom modifie en douce ses CGS peu après mon premier billet</title>
    <link>http://grapsus.net/blog/post/Bouygues-Telecom-modifie-en-douce-ses-CGS</link>
    <guid isPermaLink="false">urn:md5:214a0a32d3eed6059acba642bb6895ec</guid>
    <pubDate>Fri, 22 Jul 2011 07:20:00 +0200</pubDate>
    <dc:creator>Alexis Bezverkhyy</dc:creator>
        <category>bouygues</category><category>internet</category><category>neutralité</category><category>réseau</category><category>télécom</category>    
    <description>    &lt;h2&gt;Avant-propos&lt;/h2&gt;


&lt;p&gt;Tout d'abord je voudrais remercier tous ceux qui ont promu mon billet précédent sur &lt;em&gt;Twitter&lt;/em&gt; ou dans leurs blogs&amp;nbsp;: en deux jours, je suis passé de 50 à 1000 visiteurs journaliers, avec une pointe à 3500 hier. Ça me donne vraiment envie d'écrire plus souvent.&lt;/p&gt;


&lt;p&gt;Mon blog a d'ailleurs eu une petite coupure causée par cette affluence soudaine. Il est actuellement hébergé à la maison avec une machine à la retraite et une connexion &lt;em&gt;ADSL&lt;/em&gt;. Je fais ça plus par conviction que par manque de moyens. Ce système marchait parfaitement pour 50 visiteurs journaliers mais il est évident que ça ne peut plus marcher avec 1000 et au-delà. Je vais donc le déplacer sur un serveur dédié plus traditionnel dans les jours qui viennent et il n'y aura plus de problèmes de ce côté.&lt;/p&gt;


&lt;p&gt;Des personnes ont mentionné le &lt;em&gt;tethering&lt;/em&gt; employé pour faire mes tests comme un non respect des conditions d'utilisation. Oui c'est vrai, mais techniquement, la provenance d'un paquet n'a aucune espèce d'importance et je me suis servi d'un PC pour une raison purement pratique, pour avoir accès à des outils comme &lt;em&gt;tcpdump&lt;/em&gt;, &lt;em&gt;wireshark&lt;/em&gt;, &lt;em&gt;wget&lt;/em&gt;, &lt;em&gt;nmap&lt;/em&gt; ou &lt;em&gt;netcat&lt;/em&gt;. On pourrait refaire les mêmes tests depuis le terminal du téléphone et l'on trouvera rigoureusement la même chose. D'ailleurs la raison qui m'a poussé à faire ces tests est que je n'arrivais regarder presqu'aucune vidéo proposée par un site web depuis mon téléphone. D'autres personnes se plaignent de ne pas pouvoir consulter des documents tels que des journaux ou des BD depuis leur téléphone. Ceci démontre bien que ce n'est pas un problème de &lt;em&gt;tethering&lt;/em&gt;, mais une véritable limitation pour ceux qui utilisent légitiment le service.&lt;/p&gt;


&lt;p&gt;N'hésitez pas à me contacter si vous avez des questions &lt;strong&gt;techniques&lt;/strong&gt; concernant le test de votre connexion. Je pourrai même vous aider si vous me donnez un accès &lt;em&gt;SSH&lt;/em&gt;.&lt;/p&gt;


&lt;h2&gt;Introduction&lt;/h2&gt;


&lt;p&gt;De nombreuses personnes (et trolls) ont commenté mon billet précédent dans lequel je dénonçais la coupure des sessions &lt;em&gt;TCP&lt;/em&gt; dépassant 10 Mo et l'interdiction, par un proxy transparent, de l'accès aux fichiers dépassant 10 Mo. Plusieurs personnes ont dit que ces limites étaient annoncées dans les conditions générales de service de l'opérateur. À ma grande surprise c'est vrai&amp;nbsp;! Mais pourtant, je me rappelle, en janvier 2011, avoir épluché ces conditions, avant de souscrire à mon forfait &lt;em&gt;Internet&lt;/em&gt; et il n'y avait rien qui parlait de ce genre de limites.&lt;/p&gt;


&lt;h2&gt;Pratiques douteuses avec les CGS&lt;/h2&gt;


&lt;p&gt;Je ne suis pas du tout juriste, mais je sais que lorsqu'on souscrit à un service, surtout avec de l'engagement de durée, on signe un très long contrat décrivant très scrupuleusement le service souscrit et les conditions de son utilisation. Lorsque l'opérateur ne respecte pas ces conditions, ou lorsqu'il décide de changer de manière unilatérale ces conditions, le client n'est plus tenu à respecter son engagement. C'est exactement ce qui était arrivée avec l'augmentation de la &lt;em&gt;TVA&lt;/em&gt; sur les services multimédias en février 2011.&lt;/p&gt;


&lt;p&gt;Chez Bouygues Télécom on a une vision très particulière du contrat avec le client. Après des heures de recherche, la seule page qui contient des informations à valeur légale (et non pas des brochures ou des informations indicatives) est la suivante&amp;nbsp;: &lt;a href=&quot;http://www.laboutique.bouyguestelecom.fr/tarifs_conditions.html&quot; title=&quot;http://www.laboutique.bouyguestelecom.fr/tarifs_conditions.html&quot;&gt;http://www.laboutique.bouyguestelec...&lt;/a&gt;&lt;/p&gt;


&lt;p&gt;La première chose qui m'a frappée, c'est que ces documents ne sont pas versionnés&amp;nbsp;: il sont régulièrement mis à jour et toute trace des versions précédentes est effacée. Or c'est les conditions de service à la date où l'on a souscrit au service qui doivent être appliquées à un client. Le moindre changement doit être explicitement accepté par le client. C'est quelque chose que l'on voit régulièrement chez &lt;em&gt;Free&lt;/em&gt;, sur la page d'accueil de l'espace client on nous propose d'accepter ou non des modifications apportées au contrat (souvent pour profiter de nouveaux tarifs ou services). Chez Bouygues, il est donc quasi impossible d'obtenir le texte des conditions de services à la date où l'on s'est abonné au service.&lt;/p&gt;


&lt;p&gt;Deuxième point noir&amp;nbsp;: &quot;Conditions Générales de Service&quot; est tout sauf un document légal, c'est encore une brochure de 4 pages qui prend la forme d'une FAQ et où on vous dit en gros deux choses&amp;nbsp;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Les seules raisons valables pour résilier avant la fin de l'engagement c'est le décès ou l'emprisonnement à longue durée du client.&lt;/li&gt;
&lt;li&gt;Tous les tarifs et les services sont définis dans le &quot;Guide des tarifs&quot;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Alors là c'est très fort&amp;nbsp;: pour la description et les tarifs des services souscrits on vous renvoie à une brochure&amp;nbsp;!&lt;/p&gt;


&lt;h2&gt;Changement des CGS en douce&lt;/h2&gt;


&lt;p&gt;Je tombe alors sur le fameux &quot;Guide des tarifs&quot; version &quot;mai/août 2011&quot; qui prend la forme d'une brochure explicative des prix et des services avec, à la page 74, &quot;les informations contractuelles&quot;. Cette dernière partie se contente de reprendre le texte des &quot;Conditions Générales de Service&quot; sous forme de FAQ.&lt;/p&gt;


&lt;p&gt;Dans la partie non contractuelle on trouve effectivement la phrase suivante&amp;nbsp;:&lt;/p&gt;

&lt;pre&gt;INTERNET
Correspond à la navigation sur tous les sites Internet ou wap. Cette navigation s'effectue soit :
sur votre mobile ou sur votre ordinateur/tablette grâce à l'option modem 24/24. Chaque télé-
chargement ou accès en streaming est de 10Mo maximum.&lt;/pre&gt;


&lt;p&gt;Première remarque&amp;nbsp;: on est en train de nous dire que &lt;em&gt;Internet&lt;/em&gt; c'est naviguer sur des sites &lt;em&gt;Internet&lt;/em&gt; ou &lt;em&gt;WAP&lt;/em&gt;. On m'a appris à l'école, que &lt;em&gt;Internet&lt;/em&gt; est un réseau qui interconnecte des millions de réseaux et leur permet d'échanger des données au moyen du protocole &lt;em&gt;IP&lt;/em&gt;. Chez Bouygues &lt;em&gt;Internet&lt;/em&gt; c'est la consultation d'un seul service, le web, au moyen du protocole &lt;em&gt;HTTP&lt;/em&gt;. Soit.&lt;/p&gt;


&lt;p&gt;Deuxième remarque&amp;nbsp;: toutes les limites d'utilisation du service &lt;em&gt;Internet&lt;/em&gt; sont répertoriées dans la partie contractuelle de la brochure, mais pas celle-là&amp;nbsp;! Ça sent l'ajout à la va-vite, sans avoir consulté le juriste pour inclure cette condition dans les véritables CGS...&lt;/p&gt;


&lt;p&gt;J'avais vraiment le sentiment que cette phrase ne figurait pas dans le &quot;Guide des tarifs&quot; en janvier 2011. Heureusement que le &lt;strong&gt;web&lt;/strong&gt; conserve et archive des documents&amp;nbsp;: j'ai pu retrouver les &lt;em&gt;PDF&lt;/em&gt; des versions précédentes du &quot;Guide des tarifs&quot;, effacées du site officiel&amp;nbsp;:&lt;/p&gt;

&lt;pre&gt;guide des tarifs bouygues &quot;Offres mobile&quot; filetype:pdf&lt;/pre&gt;


&lt;p&gt;Résultats&amp;nbsp;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;12729PHR novembre/janvier 2010&amp;nbsp;: pas de paragraphe &quot;INTERNET&quot;, mais un modeste paragraphe &quot;SURF&quot; qui donne des exemples d'utilisation&lt;/li&gt;
&lt;li&gt;12755PHR mars/mai 2010&amp;nbsp;: +&quot;L’Internet depuis un mobile correspond à l’échange de données (dont les e-mails) sur les sites Internet ou WAP.&quot;&lt;/li&gt;
&lt;li&gt;12783PHR août/novembre 2010&amp;nbsp;: &quot;Correspond à la navigation sur tous les sites Internet ou wap. Cette navigation s’effectue exclusivement sur votre mobile non relié à un ordinateur ou avec une clé 3G+ relié à un seul ordinateur.&quot;&lt;/li&gt;
&lt;li&gt;12820PHR janvier/mars 2011&amp;nbsp;: pas de changement&lt;/li&gt;
&lt;li&gt;12830PHR mars/mai 2011&amp;nbsp;: pas de changement&lt;/li&gt;
&lt;li&gt;12846PHR mai/août 2011 (version actuelle)&amp;nbsp;: bingo&amp;nbsp;! +&quot;Chaque télé-chargement ou accès en streaming est de 10Mo maximum.&quot;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Je ne veux pas soutenir une quelconque théorie du complot, ou me donner de l'importance&amp;nbsp;: mais mon billet précédent a été publié le 19 avril 2011 et à l'époque je l'avais twitté à &lt;em&gt;@bouyguestelecom&lt;/em&gt; et il avait fait déjà un peu de bruit.&lt;/p&gt;


&lt;h2&gt;Conclusion&lt;/h2&gt;


&lt;p&gt;Les Conditions Générales de Service de Bouygues ont été modifiées en douce en mai 2011 pour rendre contractuelles des limitations techniques intolérables. Les versions précédentes des documents contractuels sont effacées pour faire comme si tous les clients acceptaient tacitement n'importe quel changement.&lt;/p&gt;


&lt;p&gt;Le contrat que j'ai signé en janvier 2011 ne mentionne pas ces limitations, pourtant je les subis et je n'ai jamais accepté les nouvelles conditions. Dans la mesure où ce changement est une régression du service proposé, ceci constitue une modification unilatérale de mon contrat. Je suis actuellement en stage à l'étranger et ma ligne est temporairement suspendue. À mon retour je compte envoyer une lettre de résiliation.&lt;/p&gt;


&lt;p&gt;Je voudrais vraiment entendre des vrais avis juridiques sur la question. L'idéal serait de faire étudier la problématique à quelqu'un comme Maître Éolas...
Il y a peut-être aussi du matériel pour une action de l'UFC Que choisir&amp;nbsp;?&lt;/p&gt;


&lt;h2&gt;Edit&lt;/h2&gt;


&lt;p&gt;J'ai retrouvé plusieurs autres &lt;em&gt;PDF&lt;/em&gt; 12***PHR, surtout le &quot;mars/mai 2011&quot; qui était la pièce manquante pour avoir la preuve que le changement a été introduit en mai 2011. J'ai aussi ajouté une évolution plus détaillé du paragraphe problématique&amp;nbsp;: on s'aperçoit qu'il a été remanié plus d'une fois&amp;nbsp;!&lt;/p&gt;


&lt;p&gt;Remarquez le passage de &quot;SURF&quot; à &quot;INTERNET&quot; après janvier 2010. Bizarrement ce changement n'a pas entraîné l'ouverture d'autres services que le web ou l'attribution d'adresses &lt;em&gt;IP&lt;/em&gt; &lt;em&gt;Internet&lt;/em&gt; aux clients...&lt;/p&gt;</description>
    
    
    
          <comments>http://grapsus.net/blog/post/Bouygues-Telecom-modifie-en-douce-ses-CGS#comment-form</comments>
      <wfw:comment>http://grapsus.net/blog/post/Bouygues-Telecom-modifie-en-douce-ses-CGS#comment-form</wfw:comment>
      <wfw:commentRss>http://grapsus.net/blog/feed/atom/comments/27</wfw:commentRss>
      </item>
    
  <item>
    <title>Bouygues Télécom filtre malhonnêtement son réseau 3G et inspecte vos données</title>
    <link>http://grapsus.net/blog/post/Bouygues-Telecom-filtre-malhonnetement-son-reseau-3G-et-inspecte-vos-donnees</link>
    <guid isPermaLink="false">urn:md5:00a522fa317fc0a452b0fefc9790479b</guid>
    <pubDate>Tue, 19 Apr 2011 11:10:00 +0200</pubDate>
    <dc:creator>Alexis Bezverkhyy</dc:creator>
        <category>bouygues</category><category>internet</category><category>neutralité</category><category>réseau</category><category>telecom</category>    
    <description>    &lt;p&gt;Une jolie leçon en matière de neutralité de réseau de la part de Bouygues Télécom. Voici &quot;l'accès Internet illimité 2Go&quot; que obtenez pour 45 euros par mois.&lt;/p&gt;


&lt;h2&gt;Introduction&lt;/h2&gt;


&lt;p&gt;Mon PC sous &lt;em&gt;Debian SID&lt;/em&gt; est connecté au réseau 3G de Bouygues Télécom à travers la fonctionnalité &quot;point d'accès Wifi&quot; du téléphone &lt;em&gt;HTC Desire Z&lt;/em&gt; (&lt;em&gt;Android Froyo&lt;/em&gt;).&lt;/p&gt;


&lt;h2&gt;Filtrage de contenus vidéo&lt;/h2&gt;


&lt;p&gt;Tous les lecteurs de vidéos &lt;em&gt;flash&lt;/em&gt; que vous rencontrez sur internet affichent mystérieusement une erreur. Avec &lt;em&gt;Firebug&lt;/em&gt; on voit aisément que c'est le flux &lt;em&gt;FLV&lt;/em&gt; en lui même qui ne passe pas&amp;nbsp;: &lt;em&gt;ERREUR 403&amp;nbsp;: Forbidden&lt;/em&gt; avec un message d'erreur en français, quelque soit le site. On a donc affaire à un &lt;em&gt;proxy&lt;/em&gt; transparent. Pourquoi on trouverait ça choquant ou inadmissible de la part d'un FAI &lt;em&gt;ADSL&lt;/em&gt; alors que c'est monnaie courante chez les revendeurs de minitels mobiles&amp;nbsp;? Essayons d'en savoir plus...&lt;/p&gt;


&lt;p&gt;Voici un faux contenu &lt;em&gt;FLV&lt;/em&gt;&amp;nbsp;:&lt;/p&gt;

&lt;table class=&quot;php codecolor php&quot; style=&quot;font-family:inherit&quot;&gt;&lt;tbody&gt;&lt;tr class=&quot;li1&quot;&gt;&lt;td style=&quot;vertical-align:top;&quot; class=&quot;codelines&quot;&gt;&lt;pre style=&quot;vertical-align:top;&quot; class=&quot;codecontent&quot;&gt;1
2
&lt;/pre&gt;&lt;/td&gt;&lt;td style=&quot;vertical-align:top;&quot; class=&quot;codecontent&quot;&gt;&lt;pre style=&quot;vertical-align:top;&quot; class=&quot;codecontent&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;?php&lt;/span&gt; &lt;span style=&quot;color: #990000;&quot;&gt;header&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;&amp;quot;Content-Type: video/x-flv&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;?&amp;gt;&lt;/span&gt;
lol&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;


&lt;p&gt;Bizarrement tout va bien&amp;nbsp;:&lt;/p&gt;


&lt;pre&gt; grapsus@escher:~$ wget grapsus.net/test.php
 &lt;a href=&quot;http://grapsus.net/blog/post/...&quot; title=&quot;...&quot;&gt;...&lt;/a&gt;
 2011-04-18 12:28:36 (417 KB/s) - «test.php» sauvegardé &lt;a href=&quot;http://grapsus.net/blog/post/6/6&quot; title=&quot;6/6&quot;&gt;6/6&lt;/a&gt;&lt;/pre&gt;


&lt;p&gt;Mais alors pourquoi une vraie vidéo bloque&amp;nbsp;? Essayons d'enregistrer un vrai fichier &lt;em&gt;FLV&lt;/em&gt; de 20 Mo&amp;nbsp;:&lt;/p&gt;


&lt;pre&gt; wget http://grapsus.net/test.flv
 &lt;a href=&quot;http://grapsus.net/blog/post/...&quot; title=&quot;...&quot;&gt;...&lt;/a&gt;
 requête HTTP transmise, en attente de la réponse...403 Forbidden
 2011-04-18 22:09:56 ERREUR 403: Forbidden.&lt;/pre&gt;


&lt;p&gt;Et les &lt;em&gt;logs&lt;/em&gt; du serveur&amp;nbsp;:&lt;/p&gt;


&lt;pre&gt; 62.201.XXX.XXX - - &lt;a href=&quot;http://grapsus.net/blog/post/18/Apr/2011:22:07:24 +0200&quot; title=&quot;18/Apr/2011:22:07:24 +0200&quot;&gt;18/Apr/2011:22:07:24 +0200&lt;/a&gt; &quot;GET /test.flv HTTP/1.0&quot; 200 22466703 &quot;-&quot; &quot;Wget/1.12 (linux-gnu)&quot;&lt;/pre&gt;


&lt;p&gt;Toujours pas convaincus du &lt;em&gt;proxy&lt;/em&gt; transparent&amp;nbsp;?&lt;/p&gt;


&lt;p&gt;Essayons de tronquer le fichier pour voir si la taille joue un rôle&amp;nbsp;:&lt;/p&gt;


&lt;pre&gt; $ dd if=test.flv of=test2.flv bs=1 count=8000000
 8000000+0 enregistrements lus
 8000000+0 enregistrements écrits
 8000000 bytes (8,0 MB) copied, 26,4951 s, 302 kB/s&lt;/pre&gt;


&lt;p&gt;Et miracle, ça passe !!&lt;/p&gt;


&lt;p&gt;Avec une recherche dichotomique, on comprend très vite que la taille limite est précisément de 10 Mo (10*2^20 octets), au délà, un contenu de type &lt;em&gt;video/x-flv&lt;/em&gt; est remplacé par la réponse &lt;em&gt;403 Forbidden&lt;/em&gt;.&lt;/p&gt;


&lt;h2&gt;Coupure de sessions &lt;em&gt;TCP&lt;/em&gt; trop gourmandes&lt;/h2&gt;


&lt;p&gt;Un &lt;em&gt;proxy&lt;/em&gt; ça reste gentil me direz-vous, mais voici quelque chose d'encore plus fort. Bouygues coupe pour vous toutes les sessions &lt;em&gt;TCP&lt;/em&gt; qui dépassent 10 Mo en termes de volume reçu !!!&lt;/p&gt;


&lt;p&gt;Prenons l'exemple d'une connexion &lt;em&gt;SSH&lt;/em&gt; qui atteint lesdits 10 Mo&amp;nbsp;:&lt;/p&gt;


&lt;pre&gt; root@XXX:~# Write failed: Broken pipe&lt;/pre&gt;


&lt;p&gt;Et le magnifique dump 'Wireshark' sur la carte réseau du client&amp;nbsp;:&lt;/p&gt;


&lt;pre&gt; 11158	214.451936	192.168.1.108	XXX			SSHv2	Encrypted request packet len=48
 11159	214.461449	XXX		192.168.1.108	SSHv2	Encrypted response packet len=1408
 11160	214.461604	XXX		192.168.1.108	TCP	ssh &amp;gt; 48450 &lt;a href=&quot;http://grapsus.net/blog/post/RST, PSH, ACK&quot; title=&quot;RST, PSH, ACK&quot;&gt;RST, PSH, ACK&lt;/a&gt; Seq=9593089 Ack=88369 Win=92628 Len=128
 11161	214.531079	XXX		192.168.1.108	TCP	ssh &amp;gt; 48450 &lt;a href=&quot;http://grapsus.net/blog/post/RST, ACK&quot; title=&quot;RST, ACK&quot;&gt;RST, ACK&lt;/a&gt; Seq=9591681 Ack=88417 Win=0 Len=0
 11162	214.553142	XXX		192.168.1.108	TCP	ssh &amp;gt; 48450 &lt;a href=&quot;http://grapsus.net/blog/post/RST, ACK&quot; title=&quot;RST, ACK&quot;&gt;RST, ACK&lt;/a&gt; Seq=9591681 Ack=88465 Win=0 Len=0&lt;/pre&gt;


&lt;p&gt;Au segment 11159 on dépasse le quota des 10 Mo, notez bien le segment suivant qui arrive avec un flag &lt;em&gt;RST&lt;/em&gt; (la présence de &lt;em&gt;PSH&lt;/em&gt; suggère qu'il a juste été violemment ajouté par les équipements du réseau Bouygues). Et comme ce n'est pas assez, les segments restants (11161 et 11162) se prennent aussi des flags &lt;em&gt;RST&lt;/em&gt; pour être bien sûr d'achever la connexion, du grand art&amp;nbsp;!&lt;/p&gt;


&lt;h2&gt;Conclusion&lt;/h2&gt;


&lt;p&gt;Il est tout simplement scandaleux que ce service soit appelé &quot;accès à Internet&quot;&amp;nbsp;! Le réseau de Bouygues vous ment en essayent de se substituer aux réponses données par les vrais destinataires de vos connexions dans l'irrespect le plus total des protocoles réseau. On peut comprendre (mais sûrement pas accepter !) que les opérateurs essayent de défendre une économie vouée à mourir dans laquelle on vend séparément à prix d'or des services inclus dans une vraie connexion Internet&amp;nbsp;: mail, vidéos ou même la téléphonie&amp;nbsp;!&lt;/p&gt;


&lt;p&gt;Ce genre de pratiques fait honte au domaine des télécoms et me rend très songeur quant à la perspective d'y travailler à la fin de mes études...&lt;/p&gt;


&lt;h2&gt;Notes&lt;/h2&gt;


&lt;p&gt;Mon article n'est en aucun cas une diffamation ou une accusation infondée, dans ma mesure où toutes les manipulations sont décrites avec précision et reproductibles avec un résultat sans équivoque.&lt;/p&gt;


&lt;p&gt;La question générale de la neutralité d'Internet étant très intéressante, mais hors de la portée de cet article (plutôt technique), je vous invite fortement à voir les conférences de Benjamin Bayart&amp;nbsp;:
&lt;a href=&quot;http://www.fdn.fr/minitel.avi&quot; title=&quot;http://www.fdn.fr/minitel.avi&quot;&gt;http://www.fdn.fr/minitel.avi&lt;/a&gt;&lt;/p&gt;


&lt;p&gt;Si vous êtes chez d'autres opérateurs, je vous suggère d'étudier en détail votre connexion et à poster les détails ici, afin de comparer les différents réseaux.&lt;/p&gt;</description>
    
    
    
          <comments>http://grapsus.net/blog/post/Bouygues-Telecom-filtre-malhonnetement-son-reseau-3G-et-inspecte-vos-donnees#comment-form</comments>
      <wfw:comment>http://grapsus.net/blog/post/Bouygues-Telecom-filtre-malhonnetement-son-reseau-3G-et-inspecte-vos-donnees#comment-form</wfw:comment>
      <wfw:commentRss>http://grapsus.net/blog/feed/atom/comments/26</wfw:commentRss>
      </item>
    
  <item>
    <title>Hexadecimal dump in C</title>
    <link>http://grapsus.net/blog/post/Hexadecimal-dump-in-C</link>
    <guid isPermaLink="false">urn:md5:72e725d0864284289466572a21b270fe</guid>
    <pubDate>Thu, 13 Jan 2011 01:38:00 +0100</pubDate>
    <dc:creator>Alexis Bezverkhyy</dc:creator>
        <category>C</category><category>code</category>    
    <description>    &lt;p&gt;It can be very handy, in your C programs, to be able to print raw data in a UNIX &lt;em&gt;xxd&lt;/em&gt; fashion.
Here is a short function to do that :&lt;/p&gt;


&lt;p&gt;hexdump.c :&lt;/p&gt;

&lt;table class=&quot;c codecolor c&quot; style=&quot;font-family:inherit&quot;&gt;&lt;tbody&gt;&lt;tr class=&quot;li1&quot;&gt;&lt;td style=&quot;vertical-align:top;&quot; class=&quot;codelines&quot;&gt;&lt;pre style=&quot;vertical-align:top;&quot; class=&quot;codecontent&quot;&gt;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
&lt;/pre&gt;&lt;/td&gt;&lt;td style=&quot;vertical-align:top;&quot; class=&quot;codecontent&quot;&gt;&lt;pre style=&quot;vertical-align:top;&quot; class=&quot;codecontent&quot;&gt;&lt;span style=&quot;color: #339933;&quot;&gt;#include &amp;lt;stdio.h&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #339933;&quot;&gt;#include &amp;lt;ctype.h&amp;gt;&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #339933;&quot;&gt;#ifndef HEXDUMP_COLS&lt;/span&gt;
&lt;span style=&quot;color: #339933;&quot;&gt;#define HEXDUMP_COLS 8&lt;/span&gt;
&lt;span style=&quot;color: #339933;&quot;&gt;#endif&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #993333;&quot;&gt;void&lt;/span&gt; hexdump&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #993333;&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: #339933;&quot;&gt;*&lt;/span&gt;mem&lt;span style=&quot;color: #339933;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #993333;&quot;&gt;unsigned&lt;/span&gt; &lt;span style=&quot;color: #993333;&quot;&gt;int&lt;/span&gt; len&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt;
&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#123;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #993333;&quot;&gt;unsigned&lt;/span&gt; &lt;span style=&quot;color: #993333;&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: #339933;&quot;&gt;,&lt;/span&gt; j&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #b1b100;&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;i &lt;span style=&quot;color: #339933;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt; i &lt;span style=&quot;color: #339933;&quot;&gt;&amp;lt;&lt;/span&gt; len &lt;span style=&quot;color: #339933;&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;len &lt;span style=&quot;color: #339933;&quot;&gt;%&lt;/span&gt; HEXDUMP_COLS&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #339933;&quot;&gt;?&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;HEXDUMP_COLS &lt;span style=&quot;color: #339933;&quot;&gt;-&lt;/span&gt; len &lt;span style=&quot;color: #339933;&quot;&gt;%&lt;/span&gt; HEXDUMP_COLS&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #339933;&quot;&gt;:&lt;/span&gt; &lt;span style=&quot;color: #0000dd;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt; i&lt;span style=&quot;color: #339933;&quot;&gt;++&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&amp;#123;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;/* print offset */&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #b1b100;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;i &lt;span style=&quot;color: #339933;&quot;&gt;%&lt;/span&gt; HEXDUMP_COLS &lt;span style=&quot;color: #339933;&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: #0000dd;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&amp;#123;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000066;&quot;&gt;printf&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;0x%06x: &amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;,&lt;/span&gt; i&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;/* print hex data */&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #b1b100;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;i &lt;span style=&quot;color: #339933;&quot;&gt;&amp;lt;&lt;/span&gt; len&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&amp;#123;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000066;&quot;&gt;printf&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;%02x &amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #208080;&quot;&gt;0xFF&lt;/span&gt; &lt;span style=&quot;color: #339933;&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #993333;&quot;&gt;char&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt;mem&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#91;&lt;/span&gt;i&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #b1b100;&quot;&gt;else&lt;/span&gt; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;/* end of block, just aligning for ASCII dump */&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&amp;#123;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000066;&quot;&gt;printf&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot; &amp;nbsp; &amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;/* print ASCII dump */&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #b1b100;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;i &lt;span style=&quot;color: #339933;&quot;&gt;%&lt;/span&gt; HEXDUMP_COLS &lt;span style=&quot;color: #339933;&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;HEXDUMP_COLS &lt;span style=&quot;color: #339933;&quot;&gt;-&lt;/span&gt; &lt;span style=&quot;color: #0000dd;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&amp;#123;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #b1b100;&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;j &lt;span style=&quot;color: #339933;&quot;&gt;=&lt;/span&gt; i &lt;span style=&quot;color: #339933;&quot;&gt;-&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;HEXDUMP_COLS &lt;span style=&quot;color: #339933;&quot;&gt;-&lt;/span&gt; &lt;span style=&quot;color: #0000dd;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt; j &lt;span style=&quot;color: #339933;&quot;&gt;&amp;lt;=&lt;/span&gt; i&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt; j&lt;span style=&quot;color: #339933;&quot;&gt;++&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&amp;#123;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #b1b100;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;j &lt;span style=&quot;color: #339933;&quot;&gt;&amp;gt;=&lt;/span&gt; len&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;/* end of block, not really printing */&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&amp;#123;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; putchar&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;' '&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #b1b100;&quot;&gt;else&lt;/span&gt; &lt;span style=&quot;color: #b1b100;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;isprint&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #993333;&quot;&gt;char&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt;mem&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#91;&lt;/span&gt;j&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;/* printable char */&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&amp;#123;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; putchar&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #208080;&quot;&gt;0xFF&lt;/span&gt; &lt;span style=&quot;color: #339933;&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #993333;&quot;&gt;char&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt;mem&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#91;&lt;/span&gt;j&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #b1b100;&quot;&gt;else&lt;/span&gt; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;/* other char */&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&amp;#123;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; putchar&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'.'&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; putchar&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\n&lt;/span&gt;'&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&amp;#125;&lt;/span&gt;
&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #339933;&quot;&gt;#ifdef HEXDUMP_TEST&lt;/span&gt;
&lt;span style=&quot;color: #993333;&quot;&gt;int&lt;/span&gt; main&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #993333;&quot;&gt;int&lt;/span&gt; argc&lt;span style=&quot;color: #339933;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #993333;&quot;&gt;char&lt;/span&gt; &lt;span style=&quot;color: #339933;&quot;&gt;*&lt;/span&gt;argv&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt;
&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#123;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; hexdump&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;argv&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #0000dd;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #0000dd;&quot;&gt;20&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #b1b100;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #0000dd;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#125;&lt;/span&gt;
&lt;span style=&quot;color: #339933;&quot;&gt;#endif&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;


&lt;p&gt;hexdump.h&lt;/p&gt;
&lt;table class=&quot;c codecolor c&quot; style=&quot;font-family:inherit&quot;&gt;&lt;tbody&gt;&lt;tr class=&quot;li1&quot;&gt;&lt;td style=&quot;vertical-align:top;&quot; class=&quot;codelines&quot;&gt;&lt;pre style=&quot;vertical-align:top;&quot; class=&quot;codecontent&quot;&gt;1
2
3
4
5
6
&lt;/pre&gt;&lt;/td&gt;&lt;td style=&quot;vertical-align:top;&quot; class=&quot;codecontent&quot;&gt;&lt;pre style=&quot;vertical-align:top;&quot; class=&quot;codecontent&quot;&gt;&lt;span style=&quot;color: #339933;&quot;&gt;#ifndef _HEXDUMP_H&lt;/span&gt;
&lt;span style=&quot;color: #339933;&quot;&gt;#define _HEXDUMP_H&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #993333;&quot;&gt;void&lt;/span&gt; hexdump&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #993333;&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: #339933;&quot;&gt;*&lt;/span&gt;mem&lt;span style=&quot;color: #339933;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #993333;&quot;&gt;unsigned&lt;/span&gt; &lt;span style=&quot;color: #993333;&quot;&gt;int&lt;/span&gt; len&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #339933;&quot;&gt;#endif&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;


&lt;p&gt;It can be tested as a standalone program (which dumps 20 bytes of memory strating at &lt;em&gt;*argv&lt;/em&gt;) :&lt;/p&gt;

&lt;pre class=&quot;code&quot;&gt;cc --ansi -DHEXDUMP_TEST -o hexdump hexdump.c
./hexdump
0x000000: 2e 2f 68 65 78 64 75 6d ./hexdum
0x000008: 70 00 53 53 48 5f 41 47 p.SSH_AG
0x000010: 45 4e 54 5f             ENT_&lt;/pre&gt;</description>
    
    
    
          <comments>http://grapsus.net/blog/post/Hexadecimal-dump-in-C#comment-form</comments>
      <wfw:comment>http://grapsus.net/blog/post/Hexadecimal-dump-in-C#comment-form</wfw:comment>
      <wfw:commentRss>http://grapsus.net/blog/feed/atom/comments/25</wfw:commentRss>
      </item>
    
  <item>
    <title>A script for splitting videos using ffmpeg</title>
    <link>http://grapsus.net/blog/post/A-script-for-splitting-videos-using-ffmpeg</link>
    <guid isPermaLink="false">urn:md5:24223a349f00ab359d64ef97d10aa98c</guid>
    <pubDate>Sat, 01 Jan 2011 22:07:00 +0100</pubDate>
    <dc:creator>Alexis Bezverkhyy</dc:creator>
        <category>bash</category><category>ffmpeg</category><category>video</category>    
    <description>    &lt;p&gt;Here is a small bash script for automatically cutting a video file into smaller chunks of fixed length.
ffmpeg cannot output multiple files, but it has start offset and duration parameters which my script uses to actually split the file.&lt;/p&gt;


&lt;p&gt;For example, if we have a video called 'video.mpeg' which is 3000 seconds long and we run&lt;/p&gt;

&lt;pre class=&quot;code&quot;&gt;ffsplit.sh video.mpeg 1200&lt;/pre&gt;

&lt;p&gt;we will obtain three files : video-001.mpeg (1200 seconds), video-002.mpeg (1200 seconds) and video-003.mpeg (600 seconds).&lt;/p&gt;

&lt;table class=&quot;bash codecolor bash&quot; style=&quot;font-family:inherit&quot;&gt;&lt;tbody&gt;&lt;tr class=&quot;li1&quot;&gt;&lt;td style=&quot;vertical-align:top;&quot; class=&quot;codelines&quot;&gt;&lt;pre style=&quot;vertical-align:top;&quot; class=&quot;codecontent&quot;&gt;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
&lt;/pre&gt;&lt;/td&gt;&lt;td style=&quot;vertical-align:top;&quot; class=&quot;codecontent&quot;&gt;&lt;pre style=&quot;vertical-align:top;&quot; class=&quot;codecontent&quot;&gt;&lt;span style=&quot;color: #666666; font-style: italic;&quot;&gt;#!/bin/bash&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #666666; font-style: italic;&quot;&gt;# Written by Alexis Bezverkhyy &amp;lt;alexis@grapsus.net&amp;gt; in 2011&lt;/span&gt;
&lt;span style=&quot;color: #666666; font-style: italic;&quot;&gt;# This is free and unencumbered software released into the public domain.&lt;/span&gt;
&lt;span style=&quot;color: #666666; font-style: italic;&quot;&gt;# For more information, please refer to &amp;lt;http://unlicense.org/&amp;gt;&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;function&lt;/span&gt; usage &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#123;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;echo&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;Usage : ffsplit.sh input.file chunk-duration [output-filename-format]&amp;quot;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;echo&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-e&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\t&lt;/span&gt; - input file may be any kind of file reconginzed by ffmpeg&amp;quot;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;echo&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-e&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\t&lt;/span&gt; - chunk duration must be in seconds&amp;quot;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;echo&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-e&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\t&lt;/span&gt; - output filename format must be printf-like, for example myvideo-part-%04d.avi&amp;quot;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;echo&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-e&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\t&lt;/span&gt; - if no output filename format is given, it will be computed&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\&lt;/span&gt;
 automatically from input filename&amp;quot;&lt;/span&gt;
&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #007800;&quot;&gt;IN_FILE&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;$1&amp;quot;&lt;/span&gt;
&lt;span style=&quot;color: #007800;&quot;&gt;OUT_FILE_FORMAT&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;$3&amp;quot;&lt;/span&gt;
&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;typeset&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-i&lt;/span&gt; CHUNK_LEN
&lt;span style=&quot;color: #007800;&quot;&gt;CHUNK_LEN&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;$2&amp;quot;&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #007800;&quot;&gt;DURATION_HMS&lt;/span&gt;=$&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;ffmpeg&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-i&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$IN_FILE&lt;/span&gt;&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;gt;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;1&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;|&lt;/span&gt; &lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;grep&lt;/span&gt; Duration &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;|&lt;/span&gt; &lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;cut&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-f&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;4&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-d&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;' '&lt;/span&gt;&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#41;&lt;/span&gt;
&lt;span style=&quot;color: #007800;&quot;&gt;DURATION_H&lt;/span&gt;=$&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;echo&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$DURATION_HMS&lt;/span&gt;&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;|&lt;/span&gt; &lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;cut&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-d&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;':'&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-f&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#41;&lt;/span&gt;
&lt;span style=&quot;color: #007800;&quot;&gt;DURATION_M&lt;/span&gt;=$&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;echo&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$DURATION_HMS&lt;/span&gt;&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;|&lt;/span&gt; &lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;cut&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-d&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;':'&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-f&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#41;&lt;/span&gt;
&lt;span style=&quot;color: #007800;&quot;&gt;DURATION_S&lt;/span&gt;=$&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;echo&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$DURATION_HMS&lt;/span&gt;&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;|&lt;/span&gt; &lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;cut&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-d&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;':'&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-f&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;3&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;|&lt;/span&gt; &lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;cut&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-d&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;'.'&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-f&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#41;&lt;/span&gt;
&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;let&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;DURATION = ( DURATION_H * 60 + DURATION_M ) * 60 + DURATION_S&amp;quot;&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#91;&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$DURATION&lt;/span&gt;&amp;quot;&lt;/span&gt; = &lt;span style=&quot;color: #ff0000;&quot;&gt;'0'&lt;/span&gt; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#93;&lt;/span&gt; ; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;then&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;echo&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;Invalid input video&amp;quot;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; usage
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;exit&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;1&lt;/span&gt;
&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;fi&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#91;&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$CHUNK_LEN&lt;/span&gt;&amp;quot;&lt;/span&gt; = &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;0&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#93;&lt;/span&gt; ; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;then&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;echo&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;Invalid chunk size&amp;quot;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; usage
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;exit&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;2&lt;/span&gt;
&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;fi&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#91;&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-z&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$OUT_FILE_FORMAT&lt;/span&gt;&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#93;&lt;/span&gt; ; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;then&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #007800;&quot;&gt;FILE_EXT&lt;/span&gt;=$&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;echo&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$IN_FILE&lt;/span&gt;&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;|&lt;/span&gt; &lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;sed&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;'s/^.*\.\([a-zA-Z0-9]\+\)$/\1/'&lt;/span&gt;&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#41;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #007800;&quot;&gt;FILE_NAME&lt;/span&gt;=$&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;echo&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$IN_FILE&lt;/span&gt;&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;|&lt;/span&gt; &lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;sed&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;'s/^\(.*\)\.[a-zA-Z0-9]\+$/\1/'&lt;/span&gt;&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#41;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #007800;&quot;&gt;OUT_FILE_FORMAT&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;${FILE_NAME}&lt;/span&gt;-%03d.&lt;span style=&quot;color: #007800;&quot;&gt;${FILE_EXT}&lt;/span&gt;&amp;quot;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;echo&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;Using default output file format : &lt;span style=&quot;color: #007800;&quot;&gt;$OUT_FILE_FORMAT&lt;/span&gt;&amp;quot;&lt;/span&gt;
&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;fi&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #007800;&quot;&gt;N&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;'1'&lt;/span&gt;
&lt;span style=&quot;color: #007800;&quot;&gt;OFFSET&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;'0'&lt;/span&gt;
&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;let&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;'N_FILES = DURATION / CHUNK_LEN + 1'&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;while&lt;/span&gt; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#91;&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$OFFSET&lt;/span&gt;&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-lt&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$DURATION&lt;/span&gt;&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#93;&lt;/span&gt; ; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;do&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #007800;&quot;&gt;OUT_FILE&lt;/span&gt;=$&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;printf&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$OUT_FILE_FORMAT&lt;/span&gt;&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$N&lt;/span&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#41;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;echo&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;writing &lt;span style=&quot;color: #007800;&quot;&gt;$OUT_FILE&lt;/span&gt; (&lt;span style=&quot;color: #007800;&quot;&gt;$N&lt;/span&gt;/&lt;span style=&quot;color: #007800;&quot;&gt;$N_FILES&lt;/span&gt;)...&amp;quot;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;ffmpeg&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-i&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$IN_FILE&lt;/span&gt;&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-vcodec&lt;/span&gt; copy &lt;span style=&quot;color: #660033;&quot;&gt;-acodec&lt;/span&gt; copy &lt;span style=&quot;color: #660033;&quot;&gt;-ss&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$OFFSET&lt;/span&gt;&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-t&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$CHUNK_LEN&lt;/span&gt;&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$OUT_FILE&lt;/span&gt;&amp;quot;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;let&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;N = N + 1&amp;quot;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;let&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;OFFSET = OFFSET + CHUNK_LEN&amp;quot;&lt;/span&gt;
&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;done&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;</description>
    
    
    
          <comments>http://grapsus.net/blog/post/A-script-for-splitting-videos-using-ffmpeg#comment-form</comments>
      <wfw:comment>http://grapsus.net/blog/post/A-script-for-splitting-videos-using-ffmpeg#comment-form</wfw:comment>
      <wfw:commentRss>http://grapsus.net/blog/feed/atom/comments/24</wfw:commentRss>
      </item>
    
  <item>
    <title>Lightweight HTTP server in BASH with PHP support</title>
    <link>http://grapsus.net/blog/post/Lightweight-HTTP-server-in-BASH-with-PHP-support</link>
    <guid isPermaLink="false">urn:md5:85c2a2a2f81633fdf34f32cc5aeca6d9</guid>
    <pubDate>Wed, 15 Sep 2010 00:06:00 +0200</pubDate>
    <dc:creator>Alexis Bezverkhyy</dc:creator>
        <category>bash</category><category>hack</category>    
    <description>    &lt;p&gt;No kidding, I wrote this &lt;em&gt;HTTP&lt;/em&gt; server in &lt;em&gt;Bourne Shell&lt;/em&gt;. It supports most of &lt;em&gt;HTTP 1.0&lt;/em&gt; headers, &lt;em&gt;Keep-alive&lt;/em&gt; requests, directory listing and &lt;em&gt;PHP&lt;/em&gt; scripts. By its nature, this piece of software is &lt;strong&gt;not secure&lt;/strong&gt; (it is fun though) and isn't intended for production purposes : &lt;em&gt;&amp;lt;insert the usual NO WARRANTY boilerplate bullshit here&amp;gt;&lt;/em&gt;.&lt;/p&gt;


&lt;p&gt;I tested it with &lt;em&gt;PHPMyAdmin&lt;/em&gt; which I consider to be &lt;em&gt;heavy&lt;/em&gt; &lt;em&gt;PHP&lt;/em&gt; software and it works pretty well. It is not well commented, really I just wrote it for fun, learning &lt;em&gt;BASH&lt;/em&gt; and &lt;em&gt;HTTP&lt;/em&gt; protocol.&lt;/p&gt;

&lt;table class=&quot;bash codecolor bash&quot; style=&quot;font-family:inherit&quot;&gt;&lt;tbody&gt;&lt;tr class=&quot;li1&quot;&gt;&lt;td style=&quot;vertical-align:top;&quot; class=&quot;codelines&quot;&gt;&lt;pre style=&quot;vertical-align:top;&quot; class=&quot;codecontent&quot;&gt;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
140
141
142
143
144
145
146
147
148
149
150
&lt;/pre&gt;&lt;/td&gt;&lt;td style=&quot;vertical-align:top;&quot; class=&quot;codecontent&quot;&gt;&lt;pre style=&quot;vertical-align:top;&quot; class=&quot;codecontent&quot;&gt;&lt;span style=&quot;color: #666666; font-style: italic;&quot;&gt;#!/bin/bash&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #666666; font-style: italic;&quot;&gt;# Written by Alexis Bezverkhyy &amp;lt;alexis@grapsus.net&amp;gt; in 2008&lt;/span&gt;
&lt;span style=&quot;color: #666666; font-style: italic;&quot;&gt;# This is free and unencumbered software released into the public domain.&lt;/span&gt;
&lt;span style=&quot;color: #666666; font-style: italic;&quot;&gt;# For more information, please refer to &amp;lt;http://unlicense.org/&amp;gt;&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #666666; font-style: italic;&quot;&gt;# This script should be run via inetd, first parameter is WWW root path&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #666666; font-style: italic;&quot;&gt;# Uncomment for debugging&lt;/span&gt;
&lt;span style=&quot;color: #666666; font-style: italic;&quot;&gt;#exec 2&amp;gt;/tmp/log ; set -x&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #007800;&quot;&gt;NUM&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$RANDOM&lt;/span&gt;&amp;quot;&lt;/span&gt;
&lt;span style=&quot;color: #007800;&quot;&gt;DOCUMENT_ROOT&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;$1&amp;quot;&lt;/span&gt;
&lt;span style=&quot;color: #007800;&quot;&gt;KEEP_ALIVE&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;keep-alive&amp;quot;&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;while&lt;/span&gt; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#91;&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$KEEP_ALIVE&lt;/span&gt;&amp;quot;&lt;/span&gt; == &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;keep-alive&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#93;&lt;/span&gt; ; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;do&lt;/span&gt;
&lt;span style=&quot;color: #007800;&quot;&gt;KEEP_ALIVE&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;close&amp;quot;&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;seq&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;1&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;5&lt;/span&gt;; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;do&lt;/span&gt;
&amp;nbsp; &lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;read&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-t&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;5&lt;/span&gt; line
&amp;nbsp; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#91;&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-n&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$line&lt;/span&gt;&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#93;&lt;/span&gt; ; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;then&lt;/span&gt; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;break&lt;/span&gt;; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;fi&lt;/span&gt;
&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;done&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;grep&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-sqv&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;'HTTP'&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$line&lt;/span&gt;&amp;quot;&lt;/span&gt; ; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;then&lt;/span&gt; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;exit&lt;/span&gt; ; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;fi&lt;/span&gt;
&lt;span style=&quot;color: #666666; font-style: italic;&quot;&gt;#echo `date`&amp;quot; BEGIN $line&amp;quot; &amp;gt;&amp;gt; /tmp/&amp;quot;$NUM&amp;quot;-log&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #007800;&quot;&gt;REQUEST_METHOD&lt;/span&gt;=&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;`&lt;/span&gt;&lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;cut&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-d&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;' '&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-f&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;1&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$line&lt;/span&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;`&lt;/span&gt;
&lt;span style=&quot;color: #007800;&quot;&gt;REQUEST_URI&lt;/span&gt;=&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;`&lt;/span&gt;&lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;cut&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-d&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;' '&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-f&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;2&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$line&lt;/span&gt;&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;|&lt;/span&gt; &lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;sed&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;'s/%20/ /'&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;`&lt;/span&gt;
&lt;span style=&quot;color: #007800;&quot;&gt;SCRIPT_NAME&lt;/span&gt;=&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;`&lt;/span&gt;&lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;cut&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-d&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;'?'&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-f&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;1&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$REQUEST_URI&lt;/span&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;`&lt;/span&gt;
&lt;span style=&quot;color: #007800;&quot;&gt;SCRIPT_FILENAME&lt;/span&gt;=&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;`&lt;/span&gt;&lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;sed&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-e&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;'s#//#/#'&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-e&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;'s#/$##'&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$DOCUMENT_ROOT&lt;/span&gt;&lt;span style=&quot;color: #007800;&quot;&gt;$SCRIPT_NAME&lt;/span&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;`&lt;/span&gt;
&lt;span style=&quot;color: #007800;&quot;&gt;QUERY_STRING&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;''&lt;/span&gt;
&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;grep&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-sq&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;'?'&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$REQUEST_URI&lt;/span&gt;&amp;quot;&lt;/span&gt; ; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;then&lt;/span&gt;
&amp;nbsp; &lt;span style=&quot;color: #007800;&quot;&gt;QUERY_STRING&lt;/span&gt;=&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;`&lt;/span&gt;&lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;cut&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-d&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;'?'&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-f&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;2&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$REQUEST_URI&lt;/span&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;`&lt;/span&gt;
&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;fi&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;while&lt;/span&gt; &lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;read&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-t&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;1&lt;/span&gt; line ; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;do&lt;/span&gt;
&amp;nbsp; &lt;span style=&quot;color: #007800;&quot;&gt;line&lt;/span&gt;=&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;`&lt;/span&gt;&lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;strings&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$line&lt;/span&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;`&lt;/span&gt;
&amp;nbsp; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;grep&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-sqi&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;'^Content-length'&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$line&lt;/span&gt;&amp;quot;&lt;/span&gt; ; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;then&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #007800;&quot;&gt;CONTENT_LENGTH&lt;/span&gt;=&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;`&lt;/span&gt;&lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;cut&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-d&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;' '&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-f&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;2&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$line&lt;/span&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;`&lt;/span&gt;
&amp;nbsp; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;elif&lt;/span&gt; &lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;grep&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-sqi&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;'^Content-type'&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$line&lt;/span&gt;&amp;quot;&lt;/span&gt; ; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;then&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #007800;&quot;&gt;CONTENT_TYPE&lt;/span&gt;=&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;`&lt;/span&gt;&lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;cut&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-d&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;' '&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-f&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;2&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$line&lt;/span&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;`&lt;/span&gt;
&amp;nbsp; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;elif&lt;/span&gt; &lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;grep&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-sqi&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;'^Connection'&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$line&lt;/span&gt;&amp;quot;&lt;/span&gt; ; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;then&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #007800;&quot;&gt;KEEP_ALIVE&lt;/span&gt;=&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;`&lt;/span&gt;&lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;cut&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-d&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;' '&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-f&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;2&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$line&lt;/span&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;`&lt;/span&gt;
&amp;nbsp; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;elif&lt;/span&gt; &lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;grep&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-sqi&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;'^Cookie'&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$line&lt;/span&gt;&amp;quot;&lt;/span&gt; ; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;then&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #007800;&quot;&gt;HTTP_COOKIE&lt;/span&gt;=&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;`&lt;/span&gt;&lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;sed&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;'s/Cookie:[ ]*//i'&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$line&lt;/span&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;`&lt;/span&gt;
&amp;nbsp; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;fi&lt;/span&gt;
&amp;nbsp; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#91;&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-z&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$line&lt;/span&gt;&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-a&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$REQUEST_METHOD&lt;/span&gt;&amp;quot;&lt;/span&gt; == &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;POST&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-a&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-n&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$CONTENT_LENGTH&lt;/span&gt;&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#93;&lt;/span&gt; ; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;then&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;read&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-n&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$CONTENT_LENGTH&lt;/span&gt;&amp;quot;&lt;/span&gt; line
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;echo&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$line&lt;/span&gt;&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;tmp&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$NUM&lt;/span&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #660033;&quot;&gt;-post&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;break&lt;/span&gt;
&amp;nbsp; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;elif&lt;/span&gt; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#91;&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-z&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$line&lt;/span&gt;&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#93;&lt;/span&gt; ; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;then&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;break&lt;/span&gt;
&amp;nbsp; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;fi&lt;/span&gt;
&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;done&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #666666; font-style: italic;&quot;&gt;# some security&lt;/span&gt;
&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;grep&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-sq&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;'\.\.'&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$SCRIPT_FILENAME&lt;/span&gt;&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;||&lt;/span&gt; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#40;&lt;/span&gt; &lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;namei&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$SCRIPT_FILENAME&lt;/span&gt;&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;|&lt;/span&gt; &lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;grep&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-sq&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;'\-&amp;gt;'&lt;/span&gt;&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#41;&lt;/span&gt; ; 
&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;then&lt;/span&gt;
&amp;nbsp; &lt;span style=&quot;color: #007800;&quot;&gt;SCRIPT_FILENAME&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;'./'&lt;/span&gt; &amp;nbsp;
&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;fi&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#91;&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-d&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$SCRIPT_FILENAME&lt;/span&gt;&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#93;&lt;/span&gt; ; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;then&lt;/span&gt;
&amp;nbsp; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;echo&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-en&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;'HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n'&lt;/span&gt;
&amp;nbsp; &lt;span style=&quot;color: #007800;&quot;&gt;dir&lt;/span&gt;=&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;`&lt;/span&gt;&lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;sed&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;'s#'&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$DOCUMENT_ROOT&lt;/span&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'##'&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$SCRIPT_FILENAME&lt;/span&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;`&lt;/span&gt;
&amp;nbsp; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#91;&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-z&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$dir&lt;/span&gt;&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#93;&lt;/span&gt; ; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;then&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #007800;&quot;&gt;dir&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;'/'&lt;/span&gt; ; &lt;span style=&quot;color: #007800;&quot;&gt;parent&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;'/'&lt;/span&gt;
&amp;nbsp; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;else&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #007800;&quot;&gt;parent&lt;/span&gt;=&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;`&lt;/span&gt;&lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;sed&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;'s#/[^/]\+$##'&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$dir&lt;/span&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;`&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#91;&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-z&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$parent&lt;/span&gt;&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#93;&lt;/span&gt; ; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;then&lt;/span&gt; &lt;span style=&quot;color: #007800;&quot;&gt;parent&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;'/'&lt;/span&gt; ; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;fi&lt;/span&gt;
&amp;nbsp; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;fi&lt;/span&gt;
&amp;nbsp; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;echo&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&amp;lt;html&amp;gt;&amp;lt;head&amp;gt;&amp;lt;title&amp;gt;Index of &lt;span style=&quot;color: #007800;&quot;&gt;$dir&lt;/span&gt;&amp;lt;/title&amp;gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #ff0000;&quot;&gt; &amp;nbsp;&amp;lt;body&amp;gt;&amp;lt;h3&amp;gt;Index of &lt;span style=&quot;color: #007800;&quot;&gt;$dir&lt;/span&gt;&amp;lt;/h3&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #ff0000;&quot;&gt; &amp;nbsp;&amp;lt;table&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #ff0000;&quot;&gt; &amp;nbsp; &amp;nbsp;&amp;lt;tr&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #ff0000;&quot;&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;lt;td&amp;gt;&amp;lt;b&amp;gt;Name&amp;lt;/b&amp;gt;&amp;lt;/td&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #ff0000;&quot;&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;lt;td&amp;gt;&amp;lt;b&amp;gt;Last modified&amp;lt;/b&amp;gt;&amp;lt;/td&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #ff0000;&quot;&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;lt;td&amp;gt;&amp;lt;b&amp;gt;Size&amp;lt;/b&amp;gt;&amp;lt;/td&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #ff0000;&quot;&gt; &amp;nbsp; &amp;nbsp;&amp;lt;/tr&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #ff0000;&quot;&gt; &amp;nbsp; &amp;nbsp;&amp;lt;tr&amp;gt;&amp;lt;td colspan=&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\&amp;quot;&lt;/span&gt;3&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\&amp;quot;&lt;/span&gt;&amp;gt;[D] &amp;lt;a href=&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #007800;&quot;&gt;$parent&lt;/span&gt;&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\&amp;quot;&lt;/span&gt;&amp;gt;..&amp;lt;/a&amp;gt;&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&amp;quot;&lt;/span&gt;
&amp;nbsp; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;for&lt;/span&gt; item &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$SCRIPT_FILENAME&lt;/span&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/*&lt;/span&gt; ; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;do&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#91;&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$item&lt;/span&gt;&amp;quot;&lt;/span&gt; == &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$SCRIPT_FILENAME&lt;/span&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'/*'&lt;/span&gt; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#93;&lt;/span&gt; ; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;then&lt;/span&gt; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;break&lt;/span&gt; ; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;fi&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #007800;&quot;&gt;name&lt;/span&gt;=&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;`&lt;/span&gt;&lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;basename&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$item&lt;/span&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;`&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #007800;&quot;&gt;link&lt;/span&gt;=&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;`&lt;/span&gt;&lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;sed&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;'s#'&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$DOCUMENT_ROOT&lt;/span&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'##'&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$item&lt;/span&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;`&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #007800;&quot;&gt;stat&lt;/span&gt;=&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;`&lt;/span&gt;&lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;ls&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-lhd&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;--time-style&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;'+%d-%m-%y#%H:%m'&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$item&lt;/span&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;`&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #007800;&quot;&gt;mtime&lt;/span&gt;=&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;`&lt;/span&gt;&lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;cut&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-d&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;' '&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-f&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;6&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$stat&lt;/span&gt;&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;|&lt;/span&gt; &lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;sed&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;'s/#/ /'&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;`&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #007800;&quot;&gt;size&lt;/span&gt;=&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;`&lt;/span&gt;&lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;cut&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-d&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;' '&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-f&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;5&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$stat&lt;/span&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;`&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;echo&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;&amp;quot;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#91;&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-L&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$item&lt;/span&gt;&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#93;&lt;/span&gt; ; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;then&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;echo&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;[S] &lt;span style=&quot;color: #007800;&quot;&gt;$name&lt;/span&gt;&amp;lt;br/&amp;gt;&amp;quot;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;elif&lt;/span&gt; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#91;&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-d&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$item&lt;/span&gt;&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#93;&lt;/span&gt; ; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;then&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;echo&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;'[D] &amp;lt;a href=&amp;quot;'&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$link&lt;/span&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'&amp;quot;&amp;gt;'&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$name&lt;/span&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'&amp;lt;/a&amp;gt;&amp;lt;br/&amp;gt;'&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;else&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;echo&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;'[F] &amp;lt;a href=&amp;quot;'&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$link&lt;/span&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'&amp;quot;&amp;gt;'&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$name&lt;/span&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'&amp;lt;/a&amp;gt;&amp;lt;br/&amp;gt;'&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;fi&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;echo&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;&lt;span style=&quot;color: #007800;&quot;&gt;$mtime&lt;/span&gt;&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;&lt;span style=&quot;color: #007800;&quot;&gt;$size&lt;/span&gt;&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&amp;quot;&lt;/span&gt;
&amp;nbsp; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;done&lt;/span&gt;
&amp;nbsp; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;echo&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&amp;lt;/table&amp;gt;&amp;lt;/body&amp;gt;&amp;lt;/html&amp;gt;&amp;quot;&lt;/span&gt;
&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;elif&lt;/span&gt; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#91;&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-f&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$SCRIPT_FILENAME&lt;/span&gt;&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#93;&lt;/span&gt; ; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;then&lt;/span&gt;
&amp;nbsp; &lt;span style=&quot;color: #007800;&quot;&gt;mime&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;'text/html'&lt;/span&gt;
&amp;nbsp; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;grep&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-Esqv&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;'\.(php|htm|html)$'&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$SCRIPT_FILENAME&lt;/span&gt;&amp;quot;&lt;/span&gt; ; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;then&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #007800;&quot;&gt;mime&lt;/span&gt;=&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;`&lt;/span&gt;&lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;file&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-b&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;--mime-type&lt;/span&gt; &lt;span style=&quot;color: #007800;&quot;&gt;$SCRIPT_FILENAME&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;`&lt;/span&gt;
&amp;nbsp; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;fi&lt;/span&gt;
&amp;nbsp; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;grep&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-sq&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;'\.php$'&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$SCRIPT_FILENAME&lt;/span&gt;&amp;quot;&lt;/span&gt; ; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;then&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;for&lt;/span&gt; var &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;`&lt;/span&gt;&lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;env&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;|&lt;/span&gt; &lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;cut&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-d&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;'='&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-f&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;`&lt;/span&gt; ; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;do&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#91;&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$var&lt;/span&gt;&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;!&lt;/span&gt;= &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;PATH&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-a&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$var&lt;/span&gt;&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;!&lt;/span&gt;= &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;PWD&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-a&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$var&lt;/span&gt;&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;!&lt;/span&gt;= &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;LANG&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-a&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$var&lt;/span&gt;&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;!&lt;/span&gt;= &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;SHLVL&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#93;&lt;/span&gt; ; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;then&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;export&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-n&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$var&lt;/span&gt;&amp;quot;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;fi&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;done&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;export&lt;/span&gt; REQUEST_URI REQUEST_METHOD QUERY_STRING DOCUMENT_ROOT SCRIPT_FILENAME \
&amp;nbsp; &amp;nbsp; SCRIPT_NAME CONTENT_LENGTH CONTENT_TYPE &lt;span style=&quot;color: #007800;&quot;&gt;GATEWAY_INTERFACE&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;'CGI/1.1'&lt;/span&gt; \
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #007800;&quot;&gt;HTTP_HOST&lt;/span&gt;=&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;`&lt;/span&gt;&lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;hostname&lt;/span&gt; -i&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;`&lt;/span&gt; HTTP_COOKIE &lt;span style=&quot;color: #007800;&quot;&gt;REDIRECT_STATUS&lt;/span&gt;=&lt;span style=&quot;color: #000000;&quot;&gt;1&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#91;&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$REQUEST_METHOD&lt;/span&gt;&amp;quot;&lt;/span&gt; == &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;GET&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#93;&lt;/span&gt; ; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;then&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; php-cgi &lt;span style=&quot;color: #007800;&quot;&gt;$SCRIPT_FILENAME&lt;/span&gt; \
&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;`&lt;/span&gt;&lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;tr&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;'&amp;amp;'&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;' '&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$QUERY_STRING&lt;/span&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;`&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;tmp&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$NUM&lt;/span&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #660033;&quot;&gt;-php&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;else&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; php-cgi &lt;span style=&quot;color: #007800;&quot;&gt;$SCRIPT_FILENAME&lt;/span&gt; \
&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;`&lt;/span&gt;&lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;tr&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;'&amp;amp;'&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;' '&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$QUERY_STRING&lt;/span&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;`&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;tmp&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$NUM&lt;/span&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #660033;&quot;&gt;-php&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;tmp&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$NUM&lt;/span&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #660033;&quot;&gt;-post&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;fi&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #007800;&quot;&gt;HTTP_STATUS&lt;/span&gt;=&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;`&lt;/span&gt;&lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;grep&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-i&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;'^Status: .*$'&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;tmp&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$NUM&lt;/span&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #660033;&quot;&gt;-php&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;|&lt;/span&gt; &lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;cut&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-d&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;' '&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-f&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;`&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#91;&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-z&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$HTTP_STATUS&lt;/span&gt;&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#93;&lt;/span&gt; ; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;then&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #007800;&quot;&gt;HTTP_STATUS&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;'200'&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;fi&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #007800;&quot;&gt;OUT&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;head&amp;quot;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;cat&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;tmp&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$NUM&lt;/span&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #660033;&quot;&gt;-php&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;|&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;while&lt;/span&gt; &lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;read&lt;/span&gt; ; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;do&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#91;&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$OUT&lt;/span&gt;&amp;quot;&lt;/span&gt; = &lt;span style=&quot;color: #ff0000;&quot;&gt;'head'&lt;/span&gt; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#93;&lt;/span&gt; ; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;then&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #007800;&quot;&gt;REPLY&lt;/span&gt;=$&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;strings&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$REPLY&lt;/span&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#41;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#91;&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-z&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$REPLY&lt;/span&gt;&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#93;&lt;/span&gt; ; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;then&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #007800;&quot;&gt;OUT&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;'body'&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;continue&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;fi&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;fi&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;echo&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$REPLY&lt;/span&gt;&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;tmp&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$NUM&lt;/span&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #660033;&quot;&gt;-php-&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$OUT&lt;/span&gt;&amp;quot;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;done&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;echo&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-en&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;HTTP/1.0 &lt;span style=&quot;color: #007800;&quot;&gt;$HTTP_STATUS&lt;/span&gt; OK&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\r&lt;/span&gt;&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\n&lt;/span&gt;Content-type: &lt;span style=&quot;color: #007800;&quot;&gt;$mime&lt;/span&gt;&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\r&lt;/span&gt;&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\n&lt;/span&gt;Content-length:&amp;quot;&lt;/span&gt;\
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;`&lt;/span&gt;&lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;ls&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-l&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;tmp&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$NUM&lt;/span&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #660033;&quot;&gt;-php-body&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;|&lt;/span&gt; &lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;cut&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-d&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;' '&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-f&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;`&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\r&lt;/span&gt;&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\n&lt;/span&gt;Connection: &lt;span style=&quot;color: #007800;&quot;&gt;$KEEP_ALIVE&lt;/span&gt;&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\r&lt;/span&gt;&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\n&lt;/span&gt;&amp;quot;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;cat&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;tmp&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$NUM&lt;/span&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #660033;&quot;&gt;-php-head&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;echo&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-en&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\r&lt;/span&gt;&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\n&lt;/span&gt;&amp;quot;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;cat&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;tmp&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$NUM&lt;/span&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #660033;&quot;&gt;-php-body&lt;/span&gt;
&amp;nbsp; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;else&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;echo&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-en&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;HTTP/1.0 200 OK&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\r&lt;/span&gt;&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\n&lt;/span&gt;Content-type: &lt;span style=&quot;color: #007800;&quot;&gt;$mime&lt;/span&gt;&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\r&lt;/span&gt;&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\n&lt;/span&gt;Content-length: &amp;quot;&lt;/span&gt;\
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;`&lt;/span&gt;&lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;ls&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-l&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$SCRIPT_FILENAME&lt;/span&gt;&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;|&lt;/span&gt; &lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;cut&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-d&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;' '&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-f&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;`&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\r&lt;/span&gt;&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\n&lt;/span&gt;Connection: &lt;span style=&quot;color: #007800;&quot;&gt;$KEEP_ALIVE&lt;/span&gt;&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\r&lt;/span&gt;&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\r&lt;/span&gt;&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\n&lt;/span&gt;&amp;quot;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;cat&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$SCRIPT_FILENAME&lt;/span&gt;&amp;quot;&lt;/span&gt;
&amp;nbsp; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;fi&lt;/span&gt;
&amp;nbsp; &lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;rm&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-f&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;tmp&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$NUM&lt;/span&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #660033;&quot;&gt;-php&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;tmp&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$NUM&lt;/span&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #660033;&quot;&gt;-php-body&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;tmp&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$NUM&lt;/span&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #660033;&quot;&gt;-php-head&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;tmp&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #007800;&quot;&gt;$NUM&lt;/span&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #660033;&quot;&gt;-post&lt;/span&gt; 
&amp;nbsp; &lt;span style=&quot;color: #666666; font-style: italic;&quot;&gt;# /tmp/&amp;quot;$NUM&amp;quot;-log&lt;/span&gt;
&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;else&lt;/span&gt;
&amp;nbsp; &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;echo&lt;/span&gt; &lt;span style=&quot;color: #660033;&quot;&gt;-en&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;'HTTP/1.0 404 NOT FOUND\n\rContent-type: text/plain\r\n\r\n404 File not found'&lt;/span&gt;
&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;fi&lt;/span&gt;
&lt;span style=&quot;color: #666666; font-style: italic;&quot;&gt;#echo `date`&amp;quot; END&amp;quot; &amp;gt;&amp;gt; /tmp/&amp;quot;$NUM&amp;quot;-log&lt;/span&gt;
&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;done&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;


&lt;p&gt;Here's the &lt;em&gt;inetd&lt;/em&gt; configuration I use to run it :&lt;/p&gt;

&lt;pre class=&quot;code&quot;&gt;8080	stream	tcp	nowait	grapsus	/usr/sbin/tcpd /home/grapsus/bin/http.sh /home/grapsus/www&lt;/pre&gt;


&lt;p&gt;I know &lt;em&gt;BASH&lt;/em&gt; supports &lt;em&gt;sockets&lt;/em&gt;, but this support is disabled in most &lt;em&gt;Unix&lt;/em&gt; distributions (especially on &lt;em&gt;Debian&lt;/em&gt;).&lt;/p&gt;


&lt;p&gt;Let me know what you think about it or the improvements you made.&lt;/p&gt;</description>
    
    
    
          <comments>http://grapsus.net/blog/post/Lightweight-HTTP-server-in-BASH-with-PHP-support#comment-form</comments>
      <wfw:comment>http://grapsus.net/blog/post/Lightweight-HTTP-server-in-BASH-with-PHP-support#comment-form</wfw:comment>
      <wfw:commentRss>http://grapsus.net/blog/feed/atom/comments/23</wfw:commentRss>
      </item>
    
  <item>
    <title>PHP tricks : generate an MS Excel file</title>
    <link>http://grapsus.net/blog/post/PHP-tricks-generate-an-MS-Excel-file</link>
    <guid isPermaLink="false">urn:md5:4cb7bfd64d92dbafd77805ad5c0d7b1a</guid>
    <pubDate>Sun, 12 Sep 2010 10:31:00 +0200</pubDate>
    <dc:creator>Alexis Bezverkhyy</dc:creator>
        <category>hack</category><category>php</category>    
    <description>    &lt;p&gt;Here's the shortest and the fastest way I found to convert a &lt;em&gt;CSV&lt;/em&gt; file to &lt;em&gt;MS Excel&lt;/em&gt; format. It supports string and numeric fields. I hope this function can avoid you using huge &lt;em&gt;Excel&lt;/em&gt; &lt;em&gt;PHP&lt;/em&gt; classes which are too complicated and slow (and require reading a lot of documentation) for such a basic task.&lt;/p&gt;

&lt;table class=&quot;php codecolor php&quot; style=&quot;font-family:inherit&quot;&gt;&lt;tbody&gt;&lt;tr class=&quot;li1&quot;&gt;&lt;td style=&quot;vertical-align:top;&quot; class=&quot;codelines&quot;&gt;&lt;pre style=&quot;vertical-align:top;&quot; class=&quot;codecontent&quot;&gt;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
&lt;/pre&gt;&lt;/td&gt;&lt;td style=&quot;vertical-align:top;&quot; class=&quot;codecontent&quot;&gt;&lt;pre style=&quot;vertical-align:top;&quot; class=&quot;codecontent&quot;&gt;&lt;span style=&quot;color: #666666; font-style: italic;&quot;&gt;/* Written by Alexis Bezverkhyy &amp;lt;alexis@grapsus.net&amp;gt; in September 2010&lt;/span&gt;
&lt;span style=&quot;color: #666666; font-style: italic;&quot;&gt;&amp;nbsp;* This is free and unencumbered software released into the public domain.&lt;/span&gt;
&lt;span style=&quot;color: #666666; font-style: italic;&quot;&gt;&amp;nbsp;* For more information, please refer to &amp;lt;http://unlicense.org/&amp;gt; */&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #009933; font-style: italic;&quot;&gt;/** Convert a CSV file to MS Excel format&lt;/span&gt;
&lt;span style=&quot;color: #009933; font-style: italic;&quot;&gt;&amp;nbsp;* &lt;/span&gt;
&lt;span style=&quot;color: #009933; font-style: italic;&quot;&gt;&amp;nbsp;* @param string $in input file&lt;/span&gt;
&lt;span style=&quot;color: #009933; font-style: italic;&quot;&gt;&amp;nbsp;* @param string $out output&lt;/span&gt;
&lt;span style=&quot;color: #009933; font-style: italic;&quot;&gt;&amp;nbsp;* @param string $glue CSV glue&lt;/span&gt;
&lt;span style=&quot;color: #009933; font-style: italic;&quot;&gt;&amp;nbsp;* @param string $enclosure CSV enclosure character&lt;/span&gt;
&lt;span style=&quot;color: #009933; font-style: italic;&quot;&gt;&amp;nbsp;*/&lt;/span&gt;
&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;function&lt;/span&gt; csv2xls&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000088;&quot;&gt;$in&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #000088;&quot;&gt;$out&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #000088;&quot;&gt;$glue&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;&amp;quot;;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #000088;&quot;&gt;$enclosure&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;'&amp;quot;'&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt;
&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#123;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000088;&quot;&gt;$fp_in&lt;/span&gt; &lt;span style=&quot;color: #339933;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #990000;&quot;&gt;fopen&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000088;&quot;&gt;$in&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;&amp;quot;r&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000088;&quot;&gt;$fp_out&lt;/span&gt; &lt;span style=&quot;color: #339933;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #990000;&quot;&gt;fopen&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000088;&quot;&gt;$out&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;&amp;quot;w&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #666666; font-style: italic;&quot;&gt;/* write Excel BOF */&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #990000;&quot;&gt;fputs&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000088;&quot;&gt;$fp_out&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #990000;&quot;&gt;pack&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;&amp;quot;ssssss&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #208080;&quot;&gt;0x809&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #208080;&quot;&gt;0x8&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #208080;&quot;&gt;0x0&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #208080;&quot;&gt;0x10&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #208080;&quot;&gt;0x0&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #208080;&quot;&gt;0x0&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #666666; font-style: italic;&quot;&gt;/* Read CSV fields */&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #b1b100;&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000088;&quot;&gt;$row&lt;/span&gt; &lt;span style=&quot;color: #339933;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #cc66cc;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt; &lt;span style=&quot;color: #000088;&quot;&gt;$fields&lt;/span&gt; &lt;span style=&quot;color: #339933;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #990000;&quot;&gt;fgetcsv&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000088;&quot;&gt;$fp_in&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #cc66cc;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #000088;&quot;&gt;$glue&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #000088;&quot;&gt;$enclosure&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt; &lt;span style=&quot;color: #000088;&quot;&gt;$row&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;++&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&amp;#123;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #b1b100;&quot;&gt;foreach&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000088;&quot;&gt;$fields&lt;/span&gt; &lt;span style=&quot;color: #b1b100;&quot;&gt;as&lt;/span&gt; &lt;span style=&quot;color: #000088;&quot;&gt;$col&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #000088;&quot;&gt;$value&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&amp;#123;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000088;&quot;&gt;$value&lt;/span&gt; &lt;span style=&quot;color: #339933;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #990000;&quot;&gt;trim&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000088;&quot;&gt;$value&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000088;&quot;&gt;$value&lt;/span&gt; &lt;span style=&quot;color: #339933;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #990000;&quot;&gt;utf8_decode&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000088;&quot;&gt;$value&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #666666; font-style: italic;&quot;&gt;/* string cell */&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #b1b100;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;!&lt;/span&gt;&lt;span style=&quot;color: #990000;&quot;&gt;is_numeric&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000088;&quot;&gt;$value&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&amp;#123;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000088;&quot;&gt;$l&lt;/span&gt; &lt;span style=&quot;color: #339933;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #990000;&quot;&gt;strlen&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000088;&quot;&gt;$value&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #990000;&quot;&gt;fputs&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000088;&quot;&gt;$fp_out&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;,&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #990000;&quot;&gt;pack&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;&amp;quot;ssssss&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #208080;&quot;&gt;0x204&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #cc66cc;&quot;&gt;8&lt;/span&gt; &lt;span style=&quot;color: #339933;&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color: #000088;&quot;&gt;$l&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #000088;&quot;&gt;$row&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #000088;&quot;&gt;$col&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #208080;&quot;&gt;0x0&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #000088;&quot;&gt;$l&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #000088;&quot;&gt;$value&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #666666; font-style: italic;&quot;&gt;/* numeric cell */&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #b1b100;&quot;&gt;else&lt;/span&gt; 
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&amp;#123;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #990000;&quot;&gt;fputs&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000088;&quot;&gt;$fp_out&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;,&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #990000;&quot;&gt;pack&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;&amp;quot;sssss&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #208080;&quot;&gt;0x203&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #cc66cc;&quot;&gt;14&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #000088;&quot;&gt;$row&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #000088;&quot;&gt;$col&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #208080;&quot;&gt;0x0&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #990000;&quot;&gt;pack&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;&amp;quot;d&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #000088;&quot;&gt;$value&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #666666; font-style: italic;&quot;&gt;/* write Excel EOF */&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #990000;&quot;&gt;fputs&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000088;&quot;&gt;$fp_out&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #990000;&quot;&gt;pack&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;&amp;quot;ss&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #208080;&quot;&gt;0x0A&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #208080;&quot;&gt;0x00&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #990000;&quot;&gt;fclose&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000088;&quot;&gt;$fp_out&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #990000;&quot;&gt;fclose&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000088;&quot;&gt;$fp_in&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #009900;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;</description>
    
    
    
          <comments>http://grapsus.net/blog/post/PHP-tricks-generate-an-MS-Excel-file#comment-form</comments>
      <wfw:comment>http://grapsus.net/blog/post/PHP-tricks-generate-an-MS-Excel-file#comment-form</wfw:comment>
      <wfw:commentRss>http://grapsus.net/blog/feed/atom/comments/22</wfw:commentRss>
      </item>
    
</channel>
</rss>
