<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Dada's Blog &#187; 程式語言</title>
	<atom:link href="http://blog.urdada.net/category/comp/language/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.urdada.net</link>
	<description>Just for fun</description>
	<lastBuildDate>Fri, 13 Jan 2012 09:14:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>一個長整數各自表述 (in 64-bit system)</title>
		<link>http://blog.urdada.net/2008/04/18/85/</link>
		<comments>http://blog.urdada.net/2008/04/18/85/#comments</comments>
		<pubDate>Fri, 18 Apr 2008 14:06:30 +0000</pubDate>
		<dc:creator>dada</dc:creator>
				<category><![CDATA[作業系統]]></category>
		<category><![CDATA[程式語言]]></category>

		<guid isPermaLink="false">http://blog.urdada.net/2008/04/18/85/</guid>
		<description><![CDATA[Size of long integer may be different between 64-bit systems (一個長整數各自表述) 不知道是不是我太落伍了... 我一直以為 C/C++ 下面 short, long, long long 三種資料型態都固定是 2, 4, 8 個 bytes 大小。只有 int 這個資料型態會因為 16-bit/32-bit 系統的不同而變成 2 或 4 bytes 的大小，所以理所當然 int 在 64-bit 的電腦也應該會變成 8 bytes (64-bit) 的大小囉 ?! 在整理前一篇文章《Bypass the 2GB file size limit on 32-bit Linux》的時候，讓我驚覺在 64-bit 的系統下，long [...]]]></description>
			<content:encoded><![CDATA[<p>Size of long integer may be different between 64-bit systems (一個長整數各自表述)</p>
<p>不知道是不是我太落伍了...</p>
<p>我一直以為 C/C++ 下面 short, long, long long 三種資料型態都固定是 2, 4, 8 個 bytes 大小。只有 int 這個資料型態會因為 16-bit/32-bit 系統的不同而變成 2 或 4 bytes 的大小，所以理所當然 int 在 64-bit 的電腦也應該會變成 8 bytes (64-bit) 的大小囉 ?!</p>
<p>在整理前一篇文章《<a href=http://blog.urdada.net/2008/04/18/84/ target=_blank>Bypass the 2GB file size limit on 32-bit Linux</a>》的時候，讓我驚覺在 64-bit 的系統下，long 的長度也是各自表述的！</p>
<p>首先，int 的大小即使到了 64-bit 的機器上，大部分的系統仍然使用 4 bytes 的大小而已，這主要是為了避免程式從 32-bit 系統轉換到 64-bit 系統需要修改太多地方</p>
<p>再來，請參考 Wikipedia: <a href=http://en.wikipedia.org/wiki/64-bit#64-bit_data_models target=_blank>64-bit data models</a> 的說明</p>
<p>絕大多數的 UNIX 系統在 64-bit 下面採用 <a href=http://www.unix.org/version2/whatsnew/lp64_wp.html target=_blank>LP64</a> 這種 data model，這時候 long 就不再是固定為 4 bytes 大小，而是變成 8 bytes 的大小了！</p>
<p>然而，Win64 卻不是使用 LP64，而是採用 LLP64 這個 data model，這時候 long 的大小仍然還是 4 bytes</p>
<p><img src=http://farm4.static.flickr.com/3219/2422521769_4f4c50c2f2.jpg /></p>
<p><strong><i>Many 64-bit compilers today use the <strong>LP64</strong> model (including Solaris, AIX, HP, Linux, Mac OS X, and IBM z/OS native compilers). Microsoft's VC++ compiler uses the <strong>LLP64</strong> model.</i></strong></p>
<p>兩種 data model 的最大差異點就是 long 這個資料型態的大小，LP64 是 64-bit，而 LLP64 則是 32-bit</p>
<p>LLP64 data model 基本上可以說跟 32-bit 的系統一樣，唯一差別只有位址(pointer)改成了 64-bit 而已。資料物件(class, structure) 等如果沒有包含 pointer 的成員的話，整個物件的大小是與 32-bit 系統一樣的！</p>
<p>而 LP64 則是除了位址(pointer)改成 64-bit 之外，long 的大小也變成了 64-bit 大小。所以在 UNIX 下面，要把 32-bit 程式 porting 到 64-bit 可能要比 Windows 多花費多一點功夫。</p>
<p>所以呢，我們觀察到兩個問題影響著程式的相容性</p>
<ol>
<li>在 UNIX 下面，long 的大小在 32-bit 與 64-bit 的系統下是不一樣的
<li>同樣是 64-bit 系統，UNIX 與 Windows 對於 long 的大小看法是不一致的
</ol>
<p>為了使程式在 32-bit 與 64-bit 之間以及 UNIX 與 Windows 之間的相容性提昇，改用固定長度的資料型態是寫程式的一個好習慣</p>
<p>在 UNIX 下面，我們可以改用 <a href=http://en.wikipedia.org/wiki/Stdint.h target=_blank>stdint.h</a> 這個 header file 中對於資料型態的定義:</p>
<pre>
int8_t     8-bit signed interger
int16_t    16-bit signed interger
int32_t    32-bit signed interger
int64_t    64-bit signed interger
uint8_t    8-bit unsigned interger
uint16_t   16-bit unsigned interger
uint32_t   32-bit unsigned interger
uint64_t   64-bit unsigned interger
</pre>
<p>在 Windows 下面，則改用下面這些整數固定大小的資料型態</p>
<pre>
INT8       8-bit signed integer
INT16      16-bit signed integer
INT32      32-bit signed integer
INT64      64-bit signed integer
UINT8      8-bit unsigned integer
UINT16     16-bit unsigned integer
UINT32     32-bit unsigned integer
UINT64     64-bit unsigned integer
</pre>
<p>絕對不要再使用 int 和 long 了！</p>
<p>尤其是寫網路程式時，很可能 client 是 Windows 而 server 是 UNIX，然後又有 32-bit 及 64-bit 系統混在裡面，一不小心就發生不相容的問題了...</p>
<p>當然，在 64-bit 的系統下寫程式，要考慮的絕對不只上面這些基本的資料型態。除了 pointer 的大小變成 64-bit 外，許多系統內建函式會用到的 size_t 及 off_t 的大小也變成 64-bit 了.... 寫程式時若有用到這些資料型態，需特別注意，尤其是 casting 時，千萬不要用 32-bit 的整數去裝這些資料，免得造成不可預期的結果！</p>
<p>最後提供一個小程式讓你得知你的系統主要資料型態的大小</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &lt;stdio.h&gt;</span>
<span style="color: #339933;">#include &lt;sys/types.h&gt;</span>
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
        <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;sizeof(short)     = %d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">short</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;sizeof(int)       = %d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;sizeof(long)      = %d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">long</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;sizeof(long long) = %d<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">long</span> <span style="color: #993333;">long</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;sizeof(size_t)    = %d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span>size_t<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;sizeof(off_t)     = %d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span>off_t<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;sizeof(void *)    = %d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span> <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>參考資料:</p>
<ol>
<li>Wikipedia: <a href=http://en.wikipedia.org/wiki/64-bit#64-bit_data_models target=_blank>64-bit data models</a>
<li><a href=http://www.unix.org/version2/whatsnew/lp64_wp.html target=_blank>64-Bit Programming Models: Why LP64?</a>
<li><a href=http://technet.microsoft.com/en-us/library/bb496995.aspx target=_blank>Introduction to Win32/Win64</a>
<li><a href=http://softwarecommunity.intel.com/articles/eng/1660.htm target=_blank>Porting 32-bit Applications to the Itanium® Architecture</a>
<li><a href=http://cache-www.intel.com/cd/00/00/01/79/17969_codeclean_r02.pdf target=_blank>Preparing Code for the IA-64 Architecture</a> (PDF)
</ol>
]]></content:encoded>
			<wfw:commentRss>http://blog.urdada.net/2008/04/18/85/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>消失的硬碟空間</title>
		<link>http://blog.urdada.net/2007/11/20/65/</link>
		<comments>http://blog.urdada.net/2007/11/20/65/#comments</comments>
		<pubDate>Mon, 19 Nov 2007 17:02:32 +0000</pubDate>
		<dc:creator>dada</dc:creator>
				<category><![CDATA[作業系統]]></category>
		<category><![CDATA[程式語言]]></category>
		<category><![CDATA[disk]]></category>
		<category><![CDATA[fstat]]></category>
		<category><![CDATA[log]]></category>
		<category><![CDATA[rotation]]></category>
		<category><![CDATA[storage]]></category>

		<guid isPermaLink="false">http://blog.urdada.net/2007/11/20/65/</guid>
		<description><![CDATA[話說某一天，一位同事發現某個在 UNIX 上用 C 寫的程式，跑一陣子後似乎會吃掉很多硬碟空間，吃掉的硬碟空間用 du 去算卻跟 df 的結果差異很大，而且把 process 停掉後，空間竟然又自動恢復正常了 最後，用 fstat 去仔細分析，終於找到原因： 已經開啟的檔案，即使開啟中被強制砍掉(unlink)，對原 file descriptor 持續寫入的部份仍會繼續佔用硬碟空間，寫得越多，佔用的空間也越多 實務上最常遇到這種狀況的就是 log rotation，尤其是 rotation 後的舊 LOG 是壓縮過的情況。因為經過 gzip 壓縮過後，原始的 LOG 會被刪除，只留下 XXX.gz。這個時候如果沒有人通知原來寫 LOG 的程式要重新開啟一次 LOG (重新寫一個檔案)，就會導致程式在不知情狀況下繼續寫 LOG，然後空間就莫名其妙被用掉了！ 例如 FreeBSD 下專門作 log rotation 的 newsyslog 設定檔 (newsyslog.conf) 就有個欄位可以設定在 log rotation 後送一個 signal 給 process，而 apache (httpd) 就接受 SIGUSR1 [...]]]></description>
			<content:encoded><![CDATA[<p>話說某一天，一位同事發現某個在 UNIX 上用 C 寫的程式，跑一陣子後似乎會吃掉很多硬碟空間，吃掉的硬碟空間用 du 去算卻跟 df 的結果差異很大，而且把 process 停掉後，空間竟然又自動恢復正常了</p>
<p>最後，用 fstat 去仔細分析，終於找到原因：</p>
<blockquote><p><strong>已經開啟的檔案，即使開啟中被強制砍掉(unlink)，對原 file descriptor 持續寫入的部份仍會繼續佔用硬碟空間，寫得越多，佔用的空間也越多</strong></p></blockquote>
<p>實務上最常遇到這種狀況的就是 log rotation，尤其是 rotation 後的舊 LOG 是壓縮過的情況。因為經過 gzip 壓縮過後，原始的 LOG 會被刪除，只留下 XXX.gz。這個時候如果沒有人通知原來寫 LOG 的程式要重新開啟一次 LOG (重新寫一個檔案)，就會導致程式在不知情狀況下繼續寫 LOG，然後空間就莫名其妙被用掉了！</p>
<p>例如 <a href=http://www.freebsd.org/ target=_blank>FreeBSD</a> 下專門作 log rotation 的 <a href=http://www.freebsd.org/cgi/man.cgi?query=newsyslog&#038;sektion=8 target=_blank>newsyslog</a> 設定檔 (<a href=http://www.freebsd.org/cgi/man.cgi?query=newsyslog.conf&#038;sektion=5&#038;apropos=0&#038;manpath=FreeBSD+6.2-RELEASE target=_blank>newsyslog.conf</a>) 就有個欄位可以設定在 log rotation 後送一個 signal 給 process，而 apache (httpd) 就接受 SIGUSR1 來當作重新開啟 LOG 檔案的訊號（事實上對 apache 而言是 graceful restart）。很多人以為這只是為了讓 LOG 能繼續寫不會漏掉，但其實更重要的是：如果不這麼作，你的硬碟可能很快就爆掉啦...</p>
<p>我們可以寫個簡單的程式來測試一下這種狀況：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
</pre></td><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &lt;stdio.h&gt;</span>
<span style="color: #339933;">#include &lt;fcntl.h&gt;</span>
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
        <span style="color: #993333;">int</span>     fd<span style="color: #339933;">,</span> i<span style="color: #339933;">;</span>
        <span style="color: #993333;">char</span>    cmd<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">32</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> buf<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1024</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        memset<span style="color: #009900;">&#40;</span> buf<span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">1024</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        snprintf<span style="color: #009900;">&#40;</span>cmd<span style="color: #339933;">,</span><span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span>cmd<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;df .&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;==&gt; open file for write and delete it ...<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        fd <span style="color: #339933;">=</span> open<span style="color: #009900;">&#40;</span> <span style="color: #ff0000;">&quot;test-file.log&quot;</span><span style="color: #339933;">,</span> O_CREAT<span style="color: #339933;">|</span>O_WRONLY<span style="color: #339933;">|</span>O_TRUNC <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        unlink<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;test-file.log&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        system<span style="color: #009900;">&#40;</span>cmd<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>==&gt; write 100MB to file ...<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span> i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span> <span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> <span style="color: #0000dd;">1000</span><span style="color: #339933;">*</span><span style="color: #0000dd;">100</span> <span style="color: #339933;">;</span> i<span style="color: #339933;">++</span> <span style="color: #009900;">&#41;</span>
                write<span style="color: #009900;">&#40;</span> fd<span style="color: #339933;">,</span> buf<span style="color: #339933;">,</span> <span style="color: #0000dd;">1024</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        system<span style="color: #009900;">&#40;</span>cmd<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>==&gt; close file ...<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        close<span style="color: #009900;">&#40;</span>fd<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        system<span style="color: #009900;">&#40;</span>cmd<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>首先，這個小程式會先開啟一個檔案，然後馬上砍掉它（但先不關閉檔案），接下來執行 "df ." 來查看目前硬碟用量。第二步驟是寫入100MB的垃圾資料到這個已開啟的檔案(file descriptor)中，然後再執行 "df ." 來取得硬碟用量。最後關閉檔案後，再執行一次 "df ."。執行結果如下：</p>
<pre class=mono>
==> open file for write and delete it ...
Filesystem  1K-blocks     Used     Avail Capacity  Mounted on
/dev/ad8s1d 144520482 <u>28011428</u> 104947416    21%    /home

==> write 100MB to file ...
Filesystem  1K-blocks     Used     Avail Capacity  Mounted on
/dev/ad8s1d 144520482 <u>28111508</u> 104847336    21%    /home

==> close file ...
Filesystem  1K-blocks     Used     Avail Capacity  Mounted on
/dev/ad8s1d 144520482 <u>28011428</u> 104947416    21%    /home
</pre>
<p>我們可以看到程式寫了100MB之後，空間真的被佔掉了，即使我們已經刪除這個檔案，且從目錄的檔案列表中無法直接看到這個檔案了。而當被開啟的檔案關掉後，這些空間也立即被釋放回來了</p>
<p>接下來我們把程式中的 df 改成 fstat，可以更清楚看到狀況</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="c" style="font-family:monospace;">        snprintf<span style="color: #009900;">&#40;</span>cmd<span style="color: #339933;">,</span><span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span>cmd<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;fstat -f -p %d .&quot;</span><span style="color: #339933;">,</span> getpid<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>這是最後的結果：</p>
<pre class=mono>
==> open file for write and delete it ...
USER     CMD     PID   FD MOUNT     INUM MODE         SZ|DV R/W
cdsheen  a.out 91475   wd /home 12694528 drwxr-xr-x    2048  r
cdsheen  a.out 91475 text /home 12694672 -rwxr-xr-x    7910  r
cdsheen  a.out 91475    3 /home 12694673 ----r-x--x       0  w

==> write 100MB to file ...
USER     CMD     PID   FD MOUNT     INUM MODE         SZ|DV R/W
cdsheen  a.out 91475   wd /home 12694528 drwxr-xr-x    2048  r
cdsheen  a.out 91475 text /home 12694672 -rwxr-xr-x    7910  r
cdsheen  a.out 91475    3 /home 12694673 ----r-x--x  <u>102400000</u>  w

==> close file ...
USER     CMD     PID   FD MOUNT     INUM MODE         SZ|DV R/W
cdsheen  a.out 91475   wd /home 12694528 drwxr-xr-x    2048  r
cdsheen  a.out 91475 text /home 12694672 -rwxr-xr-x    7910  r
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.urdada.net/2007/11/20/65/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>File handle is always global in Perl</title>
		<link>http://blog.urdada.net/2007/10/04/58/</link>
		<comments>http://blog.urdada.net/2007/10/04/58/#comments</comments>
		<pubDate>Thu, 04 Oct 2007 05:43:38 +0000</pubDate>
		<dc:creator>dada</dc:creator>
				<category><![CDATA[程式語言]]></category>

		<guid isPermaLink="false">http://blog.urdada.net/2007/10/04/58/</guid>
		<description><![CDATA[Perl 的 file handle 及 directory handle 一定是 global 的，無法宣告成 local 一般使用上沒有大問題，但是當用在遞迴式呼叫時，就會產生錯誤了，下面是一個把目錄下包含子目錄中所有檔案列出來的 perl 程式，採用遞迴式呼叫。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #!/usr/bin/perl die &#34;Usage: $0 [directory]\n&#34; unless @ARGV == 1; die &#34;ERROR: $ARGV[0] is not a directory\n&#34; unless -d $ARGV&#91;0&#93;; [...]]]></description>
			<content:encoded><![CDATA[<p>Perl 的 file handle 及 directory handle 一定是 global 的，無法宣告成 local</p>
<p>一般使用上沒有大問題，但是當用在遞迴式呼叫時，就會產生錯誤了，下面是一個把目錄下包含子目錄中所有檔案列出來的 perl 程式，採用遞迴式呼叫。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
</pre></td><td class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/usr/bin/perl</span>
<span style="color: #000066;">die</span> <span style="color: #ff0000;">&quot;Usage: $0 [directory]<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #b1b100;">unless</span> <span style="color: #339933;">@</span><span style="color: #000000; font-weight: bold;">ARGV</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
<span style="color: #000066;">die</span> <span style="color: #ff0000;">&quot;ERROR: $ARGV[0] is not a directory<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #b1b100;">unless</span> <span style="color: #339933;">-</span>d <span style="color: #0000ff;">$ARGV</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #0000ff;">&amp;show_dir</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$ARGV</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066;">exit</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">sub</span> show_dir
<span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">my</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$dir</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">@_</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">my</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$fname</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066;">opendir</span><span style="color: #009900;">&#40;</span> DIR<span style="color: #339933;">,</span> <span style="color: #0000ff;">$dir</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$fname</span> <span style="color: #339933;">=</span> <span style="color: #000066;">readdir</span><span style="color: #009900;">&#40;</span>DIR<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
                <span style="color: #b1b100;">next</span> <span style="color: #b1b100;">if</span> <span style="color: #0000ff;">$fname</span> <span style="color: #b1b100;">eq</span> <span style="color: #ff0000;">'.'</span> <span style="color: #339933;">||</span> <span style="color: #0000ff;">$fname</span> <span style="color: #b1b100;">eq</span> <span style="color: #ff0000;">'..'</span><span style="color: #339933;">;</span>
                <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #339933;">-</span>d <span style="color: #ff0000;">&quot;$dir/$fname&quot;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                        <span style="color: #0000ff;">&amp;show_dir</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;$dir/$fname&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
                <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
                        <span style="color: #000066;">print</span> <span style="color: #ff0000;">&quot;$dir/$fname<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000066;">closedir</span><span style="color: #009900;">&#40;</span>DIR<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>由於 DIR 這個 directory-handle 會被當成 global 變數，因此這段程式會出錯，無法列出所有檔案。</p>
<p>解決方法是把 DIR 這一個 directory-handle 直接用 $dir 來代替，這樣在遞迴的時候就不會因為重複使用而造成錯誤了，下面是改過的程式：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
</pre></td><td class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/usr/bin/perl</span>
<span style="color: #000066;">die</span> <span style="color: #ff0000;">&quot;Usage: $0 [directory]<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #b1b100;">unless</span> <span style="color: #339933;">@</span><span style="color: #000000; font-weight: bold;">ARGV</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
<span style="color: #000066;">die</span> <span style="color: #ff0000;">&quot;ERROR: $ARGV[0] is not a directory<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #b1b100;">unless</span> <span style="color: #339933;">-</span>d <span style="color: #0000ff;">$ARGV</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #0000ff;">&amp;show_dir</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$ARGV</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066;">exit</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">sub</span> show_dir
<span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">my</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$dir</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">@_</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">my</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$fname</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066;">opendir</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$dir</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$dir</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$fname</span> <span style="color: #339933;">=</span> <span style="color: #000066;">readdir</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$dir</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
                <span style="color: #b1b100;">next</span> <span style="color: #b1b100;">if</span> <span style="color: #0000ff;">$fname</span> <span style="color: #b1b100;">eq</span> <span style="color: #ff0000;">'.'</span> <span style="color: #339933;">||</span> <span style="color: #0000ff;">$fname</span> <span style="color: #b1b100;">eq</span> <span style="color: #ff0000;">'..'</span><span style="color: #339933;">;</span>
                <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #339933;">-</span>d <span style="color: #ff0000;">&quot;$dir/$fname&quot;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                        <span style="color: #0000ff;">&amp;show_dir</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;$dir/$fname&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
                <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
                        <span style="color: #000066;">print</span> <span style="color: #ff0000;">&quot;$dir/$fname<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000066;">closedir</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$dir</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.urdada.net/2007/10/04/58/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>使用 bcompiler 來編譯（加密）您的 PHP 原始碼</title>
		<link>http://blog.urdada.net/2006/07/25/15/</link>
		<comments>http://blog.urdada.net/2006/07/25/15/#comments</comments>
		<pubDate>Mon, 24 Jul 2006 17:25:30 +0000</pubDate>
		<dc:creator>dada</dc:creator>
				<category><![CDATA[程式語言]]></category>

		<guid isPermaLink="false">http://blog.urdada.net/2006/07/25/15/</guid>
		<description><![CDATA[Using bcomipler to compile (encode) your PHP script (in FreeBSD) eAccelerator 的 encoder 在 PHP 5.1 以後就無法運作了，使是最新的版本 0.9.5 也不行，因此如果您還想用 eAccelerator 的 encoder，乖乖待在 PHP 4.x or PHP 5.0 吧，相關資訊可參考之前的兩篇文章： eAccelerator 無法使用於 PHP 5.1 將 PHP 從 5.1 降至 5.0 (with portdowngrade) 根據 mailing list 上面作者最新的說法，短期內應該都還不會修復，甚至有可能還會放棄支援 encoder 部份，作者還推薦大家改用 bcompiler ... orz 所以如果你跟我一樣必須要用 PHP 5.1 的話，就跟我一起來玩看看 bcompiler 囉 [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Using bcomipler to  compile (encode) your PHP script (in FreeBSD)</strong></p>
<p><a href="http://eaccelerator.net/" target="_blank">eAccelerator</a> 的 encoder 在 PHP  5.1 以後就無法運作了，使是最新的版本 0.9.5 也不行，因此如果您還想用 eAccelerator 的 encoder，乖乖待在  PHP 4.x or PHP 5.0 吧，相關資訊可參考之前的兩篇文章：</p>
<ul>
<li><a href="http://blog.urdada.net/2005/12/12/16/" target="_blank">eAccelerator 無法使用於 PHP 5.1</a></li>
<li><a href="http://www.wretch.cc/blog/cdsheen&amp;article_id=3802770" target="_blank">將 PHP 從 5.1 降至 5.0 (with portdowngrade)</a></li>
</ul>
<p>根據 mailing  list 上面作者最新的<a href="http://sourceforge.net/mailarchive/message.php?msg_id=24006812" target="_blank">說法</a>，短期內應該都還不會修復，甚至有可能還會放棄支援 encoder 部份，作者還推薦大家改用 <a href="http://www.php.net/manual/en/ref.bcompiler.php" target="_blank">bcompiler</a> ... orz<br />
所以如果你跟我一樣必須要用 PHP 5.1 的話，就跟我一起來玩看看 <a href="http://www.php.net/manual/en/ref.bcompiler.php" target="_blank">bcompiler</a> 囉</p>
<p>附帶一提，原本是要買 <a href="http://www.ioncube.com/" target="_blank">ionCube</a> 公司開發的 <a href="http://www.ioncube.com/sa_encoder.php" target="_blank">PHP Encoder</a>  的，這看來是目前編譯（加密）PHP 原始碼最好的選擇，價格也不算貴，而且花的是公司的錢，不過還是先試用看看 bcompiler  好了，真的不好用的話，再來買囉</p>
<p>以下的安裝與使用說明均以 FreeBSD  為範例，其他作業系統應該也差不多，請自己試看看吧</p>
<p>在正式安裝之前提醒你，<a href="http://www.php.net/manual/en/ref.bcompiler.php" target="_blank">bcompiler</a> 會用到 <a href="http://tw.php.net/manual/en/ref.bzip2.php" target="_blank">bzip2</a> 這個  extension，所以先安裝一下 php5-bz2：</p>
<pre class="small"># cd /usr/ports/archivers/php5-bz2
# make install clean</pre>
<p><a href="http://www.php.net/manual/en/ref.bcompiler.php" target="_blank">bcompiler</a> 是一個收容於 <a href="http://pecl.php.net/" target="_blank">PECL</a> 的 PHP extension，您可以直接使用 pecl 這個命令來安裝，而 pecl 的使用方法與 <a href="http://pear.php.net/" target="_blank">pear</a> 相同，因此你必須先安裝 <a href="http://pear.php.net/" target="_blank">pear</a> :</p>
<pre class="small"># cd /usr/ports/devel/pear
# make install clean</pre>
<p>pear 安裝好後，同時會有一個 pecl 命令可用 (/usr/locel/bin/pecl)，可用它來安裝 bcompiler</p>
<pre class="small">pecl install channel://pecl.php.net/bcompiler-0.8</pre>
<p>不過呢，我發現直接執行 pecl 會發生錯誤（至少在 FreeBSD 下是如此）：</p>
<pre class="small"># pecl
Fatal error: Call to undefined function preg_match() in ...</pre>
<p>問題出在 /usr/local/bin/pecl 這個 script 的最後一行：</p>
<pre class="small">exec $PHP -C -n -q $INCARG -d output_buffering=1 -d safe_mode=0 ...</pre>
<p>這一行的 -n 這個選項代表不載入 php.ini 設定檔，因此 pcre.so  就沒有被載入，<br />
然後你就看到上面的錯誤了，所以自己去改一下程式把 -n 拿掉吧....</p>
<p>拿掉之後，安裝程式應該就可以正常運作了了：</p>
<pre class="small">pecl install channel://pecl.php.net/bcompiler-0.8</pre>
<p>安裝好後，記得要改設定把 bcompiler 這個 extension 載入</p>
<pre class="small"># echo "extension=bcompiler.so" &gt;&gt; /usr/local/etc/php/extensions.ini</pre>
<p>bcompiler 全名是 PHP bytecode Compiler，詳細的使用方法可以參考 PHP 網站上的詳細說明： PHP  bytecode Compiler: <a href="http://www.php.net/manual/en/ref.bcompiler.php" target="_blank">http://www.php.net/manual/en/ref.bcompiler.php</a></p>
<p>看不懂的話，<a href="http://www.php.net/manual/en/function.bcompiler-write-file.php" target="_blank">bcompiler_write_file()</a> 這個 function 的說明裡面有範例：</p>
<pre class="small">$fh = fopen("example.phb", "w");
bcompiler_write_header($fh);
bcompiler_write_file($fh, "example.php");
bcompiler_write_footer($fh);
fclose($fh);</pre>
<p>為了易於使用，我寫了一個簡單的 <a href="http://bencoder.urdada.net/" target="_blank">script</a>，使用方法如下：</p>
<pre class="small">BENCODER v1.3 - Encode your PHP script using bcompiler

Usage: bencoder [-f] [-q] -o FILE    file1.php
       bencoder [-f] [-q] -o OUTDIR  file1.php file2.php ...
       bencoder [-f] [-q] -o OUTDIR  -s SRCDIR  [-e SUFFIX] [-r] [-c] [-l]

  -o FILE   : the file name to write the encoded script
              (default to '-encoded.XXX' suffix)
  -o OUTDIR : the directory to write all encoded files

  -a SRCDIR
  -s SRCDIR : encode all files in this source directory

  -r        : encode directories recursively (no by default)
  -f        : force overwriting even if the target exists
  -e SUFFIX : encode the files with the SUFFIX extension only (default: php)
              (regular expression allowed, ex: "php|inc")
  -c        : copy files those shouldn't be encoded (no by default)
  -l        : follow symbolic link (no by default)
  -q        : do not print the file name while encoding or copying
  -b
  -bz2      : compress the encoded files with bz2 (needs bzip2-extension)</pre>
<p>想用的人到 <a href="http://bencoder.urdada.net/" target="_blank">這邊</a>  自己抓回去玩玩看吧</p>
<pre class="small"># wget http://bencoder.urdada.net/bencoder
# chmod 755 bencoder
# mv bencoder /usr/local/bin/</pre>
<p>在把程式使用 bcompiler 編碼之後，執行的時候發覺 $_SERVER 以及 $_ENV  的變數都不見了，後來查了資料，才發現要把 <a href="http://www.php.net/manual/en/ini.core.php#ini.auto-globals-jit" target="_blank">auto_globals_jit</a> 關掉 (Off) 才行，請直接修改您的 php.ini ：</p>
<pre class="small">; When enabled, the SERVER and ENV variables are created when they're first
; used (Just In Time) instead of when the script starts. If these variables
; are not used within a script, having this directive on will result in a
; performance gain. The PHP directives register_globals, register_long_arrays,
; and register_argc_argv must be disabled for this directive to have any affect.
auto_globals_jit = Off</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.urdada.net/2006/07/25/15/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>eAccelerator 無法使用於 PHP 5.1</title>
		<link>http://blog.urdada.net/2005/12/12/16/</link>
		<comments>http://blog.urdada.net/2005/12/12/16/#comments</comments>
		<pubDate>Mon, 12 Dec 2005 04:50:01 +0000</pubDate>
		<dc:creator>dada</dc:creator>
				<category><![CDATA[程式語言]]></category>

		<guid isPermaLink="false">http://blog.urdada.net/2005/12/12/16/</guid>
		<description><![CDATA[上個月 PHP 5.1 正式發表了 昨天一時興起，把 PHP 5.0 利用 portupgrade 升級成 PHP 5.1，過程沒遇到什麼阻礙 安裝完後也覺得應該沒什麼問題，但是後來卻發現 eAccelerator 無法使用了 不管是 eAccelerator 0.9.3 或者 0.9.4-rc1 都一樣不能使用（連編譯都會發生錯誤） 根據 討論區 上面的說法，要修復這個問題似乎還需要一段時間 即使是將要發表的 0.9.4 版本，也不會支援 PHP 5.1 因此，有重度依賴 eAccelerator 的使用者，請乖乖先待在 PHP 4.4 或者 PHP 5.0 吧 Ref: eAccelerator - PHP 網頁加速及編碼]]></description>
			<content:encoded><![CDATA[<p>上個月 <a href="http://www.php.net/" target="_blank">PHP</a> 5.1  正式發表了</p>
<p>昨天一時興起，把 PHP 5.0 利用 portupgrade 升級成 PHP  5.1，過程沒遇到什麼阻礙<br />
安裝完後也覺得應該沒什麼問題，但是後來卻發現 <a href="http://eaccelerator.net/HomeUk" target="_blank">eAccelerator</a> 無法使用了</p>
<p>不管是 eAccelerator 0.9.3 或者 0.9.4-rc1  都一樣不能使用（連編譯都會發生錯誤）</p>
<p>根據 <a href="http://sourceforge.net/forum/?group_id=122249" target="_blank">討論區</a>  上面的說法，要修復這個問題似乎還需要一段時間</p>
<p>即使是將要發表的 0.9.4 版本，也不會支援 PHP 5.1</p>
<p>因此，有重度依賴  eAccelerator 的使用者，請乖乖先待在 PHP 4.4 或者 PHP 5.0 吧</p>
<p>Ref: <a href="http://blog.urdada.net/2005/06/23/18/" target="_blank">eAccelerator - PHP 網頁加速及編碼</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.urdada.net/2005/12/12/16/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>eAccelerator - PHP 網頁加速及編碼</title>
		<link>http://blog.urdada.net/2005/06/23/18/</link>
		<comments>http://blog.urdada.net/2005/06/23/18/#comments</comments>
		<pubDate>Thu, 23 Jun 2005 06:16:58 +0000</pubDate>
		<dc:creator>dada</dc:creator>
				<category><![CDATA[程式語言]]></category>

		<guid isPermaLink="false">http://blog.urdada.net/2005/06/23/18/</guid>
		<description><![CDATA[eAccelerator - PHP 網頁加速及編碼軟體 http://eaccelerator.net/HomeUk eAccelerator 是一套開放原始碼的網頁加速軟體，它能加快 PHP 網頁的執行速度。其原理是把原始 PHP 程式碼編譯過 (compiled) 的二進位碼快取起來，下次執行同一個程式時，就不需要再編譯一次，可以節省很多時間 同時，eAccelerator 還有保護 PHP 原始碼的效果，透過 eAccelerator 提供的 encoder 程式，程式開發者不需要再把 PHP 程式碼公開出去就可以提供服務，對於商業軟體的開發有很大的幫助 以下簡介在 FreeBSD 上面安裝與使用 eAccelerator 的過程 首先，請先確定您的機器上面已經安裝了 Apache Web Server 及 PHP 模組，Web Server 部分，eAccelerator 支援 Apache 1.3 與 2.0，PHP 則是支援 mod_php4 與 mod_php5，eAccelerator 並不支援 PHP 以 CGI 模式執行。 1. 安裝 eAccelerator # [...]]]></description>
			<content:encoded><![CDATA[<p><strong>eAccelerator - PHP 網頁加速及編碼軟體</strong></p>
<p><a href="http://eaccelerator.net/HomeUk">http://eaccelerator.net/HomeUk</a></p>
<p><a href="http://eaccelerator.net/HomeUk">eAccelerator</a> 是一套開放原始碼的網頁加速軟體，它能加快 <a href="http://blog.urdada.net/wp-admin/www.php.net">PHP</a> 網頁的執行速度。其原理是把原始 PHP 程式碼編譯過 (compiled)  的二進位碼快取起來，下次執行同一個程式時，就不需要再編譯一次，可以節省很多時間</p>
<p>同時，<a href="http://eaccelerator.net/HomeUk">eAccelerator</a> 還有保護 PHP 原始碼的效果，透過 <a href="http://eaccelerator.net/HomeUk">eAccelerator</a> 提供的 encoder  程式，程式開發者不需要再把 PHP 程式碼公開出去就可以提供服務，對於商業軟體的開發有很大的幫助</p>
<p>以下簡介在 <a href="http://blog.urdada.net/wp-admin/www.freebsd.org">FreeBSD</a> 上面安裝與使用 eAccelerator  的過程</p>
<p>首先，請先確定您的機器上面已經安裝了 <a href="http://httpd.apache.org/">Apache Web  Server</a> 及 <a href="http://www.php.net/">PHP 模組</a>，Web Server 部分，<a href="http://eaccelerator.net/HomeUk">eAccelerator</a> 支援 Apache 1.3 與 2.0，PHP  則是支援 mod_php4 與 mod_php5，<a href="http://eaccelerator.net/HomeUk">eAccelerator</a> 並不支援 PHP 以 CGI  模式執行。</p>
<p><strong>1. 安裝 eAccelerator</strong></p>
<p># cd  /usr/ports/www/eaccelerator/<br />
# make install</p>
<p><strong>2. 啟用 eAccelerator  extension</strong></p>
<p># vi  /usr/local/etc/php/extensions.ini</p>
<p>在檔案的最後面加上：</p>
<p>extension=eaccelerator.so</p>
<p>請注意：  extension=eaccelerator.so 一定要加上 extension=session.so 之後，否則會發生錯誤</p>
<p><strong>3. 設定  php.ini</strong></p>
<p># cd /usr/local/etc/<br />
# vi php.ini</p>
<p>如果你還沒有設定過 php.ini  的話，建議從 php.ini-recommended 拷貝一份來改：</p>
<p># cp -i php.ini-recommended  php.ini<br />
# chmod 644 php.ini</p>
<p>在 php.ini 設定檔的最後面加上這些設定：</p>
<pre class="tiny">eaccelerator.shm_size="16"
eaccelerator.cache_dir="/tmp/eaccelerator"
eaccelerator.enable="1"
eaccelerator.optimizer="1"
eaccelerator.check_mtime="1"
eaccelerator.debug="0"
eaccelerator.filter=""
eaccelerator.shm_max="0"
eaccelerator.shm_ttl="0"
eaccelerator.shm_prune_period="0"
eaccelerator.shm_only="0"
eaccelerator.compress="1"
eaccelerator.compress_level="9"</pre>
<p>每個設定參數的功能請參考 <a href="http://eaccelerator.net/IniSettingsUk">http://eaccelerator.net/IniSettingsUk</a>  的說明</p>
<p><strong>4. 建立快取暫存目錄</strong></p>
<p># mkdir /tmp/eaccelerator<br />
# chmod  0777 /tmp/eaccelerator</p>
<p><strong>5. 重新啟動 Apache，大功告成</strong></p>
<p>#  /usr/local/rc.d/apache2.sh stop<br />
# /usr/local/rc.d/apache2.sh  start</p>
<p>安裝完畢之後，您所有的程式都不須修改，自動就可以享受速度加倍的好處了，eAccelerator 會把快取的檔案放在  /tmp/eaccelerator/ 下面</p>
<p>eAccelerator 也有提供線上監控的介面，就像 phpinfo() 一樣，只要呼叫  eaccelerator() 即可，寫一個網頁如下即可：</p>
<pre class="tiny">&lt;?
      eaccelerator();
?&gt;</pre>
<p>當然，最好還是把這個網頁經由一些安全機制保護起來。除了你自己對網頁加上權限控管外，你也可以使用 eAccelerator  提供的保護機制，設定方法如下：</p>
<p>執行  /usr/local/share/examples/eaccelerator/eaccelerator_password.php 這支程式：</p>
<pre class="small"># php /usr/local/share/examples/eaccelerator/eaccelerator_password.php
Changing password for eAccelerator Web Interface (eaccelerator.php)

Enter admin name: <font color="blue">dada</font>
New admin password: <font color="blue">12345</font>
Retype new admin password: <font color="blue">12345</font>

Add the following lines into your php.ini and restart HTTPD

<font color="green">eaccelerator.admin.name="dada"
eaccelerator.admin.password="$1$oEX8dazK$7F5FMsJQejEh2Vdazjkqd."</font></pre>
<p>輸入想要設定的帳號密碼(藍色部分)後，這支程式會產生編碼過後的資訊(綠色部分)，然後把綠色部分的兩行貼到 php.ini  中，重新啟動 Apache 就可以了。eAccelerator 提供的保護機制是使用 HTTP  認證方式..</p>
<p>這個介面長得就像這樣子：</p>
<p><a href="http://photo.giga.net.tw/photo/2029/original/20050623131051-0001.jpg" rel="lightbox[18]">http://photo.giga.net.tw/photo/2029/original/20050623131051-0001.jpg</a></p>
<p>裡面提供簡單的管理功能，同時會告訴你那個網頁已經被快取了</p>
<p><strong>eAccelerator  提供的網頁編碼功能</strong></p>
<p>eAccelerator 提供一支小程式，可對網頁做編碼，詳細使用方法可執行</p>
<p>#  /usr/local/bin/encoder</p>
<p>執行後會看到簡單的使用說明：</p>
<pre class="small">Usage:  encoder [options] source_file_name
        encoder [options] source_file_name...
        encoder [options] source_directory_name...

Options:
        -s suffix
                encode files only with following suffix (default is "php")
        -a
                encode all files (no by default)
        -l
                follow symbolic links (no by default)
        -r
                encode directories recursively (no by default)
        -c
                copy files those shouldn't be encoded (no by default)
        -f
                overwrite existing files (no by default)
        -w
                exclude check for eaccelerator_load() and subsequent warning
        -o target
                If you encode only one script then 'target' specifyes an output
                file name. If you encode directory or several files at once
                then 'target' specifyes an output directory name.

Examples:
        encoder some_file.php
        encoder some_file.php -o some_encoded_file.php
        encoder *.php -o some_dir
        encoder ~/public_html/x -rcf -sphp -sinc -o ~/public_html/y</pre>
<p>稍微玩一下應該就知道怎麼用了...</p>
<p>使用 encoder 去對 PHP  程式編碼，會產生另外一個檔案，你也可以對整個目錄作轉換，例如：</p>
<p># encoder myhome -rcf -sphp -sinc -o  myhome_encoded</p>
<p>這個命令會把 myhome/ 目錄下所有的 php / inc 結尾的檔案作編碼，並存放到  myhome_encoded 下面，同時，由於指定了 -c 這個選項，其他格式、不需編碼的檔案，如 jpg/gif/html/js  等，也會原封不動複製一份到 myhome_encoded 下面，這樣方便你直接以這個目錄來提供服務。</p>
<p>選項 -r 代表  recursively 的功能，可以幫你同時處理所有子目錄，不過有個討厭的地方就是，encoder 幫你建立的子目錄，目錄權限竟然是  0777，感覺很不好，所以我習慣上會自己去改 encoder 這支程式</p>
<p># chmod u+w  /usr/local/bin/encoder<br />
# vi /usr/local/bin/encoder</p>
<p>然後，搜尋 0777 ，改成 0755  就可以了</p>
<pre class="small">--- /usr/local/bin/encoder.orig Wed Jun 22 09:30:49 2005
+++ /usr/local/bin/encoder      Wed Jun 22 09:40:07 2005
@@ -99,7 +99,7 @@

 function eaccelerator_mkdir($dir, $f, $web) {
   if (!empty($dir)) {
<font color="blue">-    if (!@mkdir($dir,0777)) {
+    if (!@mkdir($dir,0755)) {</font>
       if (!$f) {
         $error = "Can't create destination directory \"$dir\"";
         if (file_exists($dir)) {</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.urdada.net/2005/06/23/18/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP4 fsockopen() 無法使用 ssl:// 的 bug</title>
		<link>http://blog.urdada.net/2005/04/18/31/</link>
		<comments>http://blog.urdada.net/2005/04/18/31/#comments</comments>
		<pubDate>Mon, 18 Apr 2005 07:24:59 +0000</pubDate>
		<dc:creator>dada</dc:creator>
				<category><![CDATA[程式語言]]></category>

		<guid isPermaLink="false">http://blog.urdada.net/2005/04/18/31/</guid>
		<description><![CDATA[PHP 4 有 bug 會導致 fsockopen 無法接受 "ssl://example.com" 即使有 OpenSSL support，還是會產生以下這樣的錯誤訊息： Warning: fsockopen(): no SSL support in this build PHP BUG #29934: http://bugs.php.net/bug.php?id=29934 解決方法有二: 1. Install PHP 5.x 2. FreeBSD, http://www.freebsd.org/cgi/query-pr.cgi?pr=ports/72275 重裝系統時常常會忘記.... 搞了半天才發現之前就找過答案了.. 寫下來以免以後又忘了 建議若沒有特殊需求，儘量改用 PHP 5.x 吧..]]></description>
			<content:encoded><![CDATA[<p>PHP 4 有 <a href="http://bugs.php.net/bug.php?id=29934"><strong>bug</strong></a> 會導致 <a href="http://www.php.net/manual/en/function.fsockopen.php">fsockopen</a> 無法接受  "ssl://example.com"</p>
<p>即使有 <a href="http://www.php.net/manual/en/ref.openssl.php">OpenSSL  support</a>，還是會產生以下這樣的錯誤訊息：</p>
<p><strong>  Warning: fsockopen(): no SSL support in this build</strong></p>
<p>PHP BUG #29934: <a href="http://bugs.php.net/bug.php?id=29934">http://bugs.php.net/bug.php?id=29934</a></p>
<p>解決方法有二:<br />
1. Install PHP 5.x<br />
2. FreeBSD, <a href="http://www.freebsd.org/cgi/query-pr.cgi?pr=ports/72275">http://www.freebsd.org/cgi/query-pr.cgi?pr=ports/72275</a></p>
<p>重裝系統時常常會忘記.... 搞了半天才發現之前就找過答案了..   寫下來以免以後又忘了<br />
建議若沒有特殊需求，儘量改用 PHP 5.x 吧..</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.urdada.net/2005/04/18/31/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>讓 FreeBSD 下的 PHP DBA extension 能支援 &#039;dbm&#039; handler</title>
		<link>http://blog.urdada.net/2005/03/05/20/</link>
		<comments>http://blog.urdada.net/2005/03/05/20/#comments</comments>
		<pubDate>Sat, 05 Mar 2005 04:10:14 +0000</pubDate>
		<dc:creator>dada</dc:creator>
				<category><![CDATA[程式語言]]></category>

		<guid isPermaLink="false">http://blog.urdada.net/2005/03/05/20/</guid>
		<description><![CDATA[讓 FreeBSD 下的 PHP DBA extension 能支援 'dbm' handler 在 FreeBSD 上面開發了一些程式，使用到 DBOPEN(3) 這個函數來處理一些 hash.. 原本 PHP 有支援 DBM extension 可以讀取這些 db，不過新版的 PHP 已經不支援 DBM extension 了，改用 DBA extension 配合 'dbm' handler 來存取這些 db 然而在 FreeBSD 下，使用 ports 去安裝的新版 PHP，其中的 DBA extension 卻無法支援 'dbm' handler.. 當然，如果可以的話改用 'db4' handler 可能是比較好的，不過由於歷史因素難解.. 所以就只好自己跳下去改程式，讓 PHP 在安裝時能夠利用 DBOPEN(3) 來提供 'dbm' [...]]]></description>
			<content:encoded><![CDATA[<p>讓 FreeBSD 下的 PHP DBA extension 能支援 'dbm' handler</p>
<p>在 FreeBSD  上面開發了一些程式，使用到 <a href="http://www.freebsd.org/cgi/man.cgi?query=dbopen&amp;apropos=0&amp;sektion=0&amp;manpath=FreeBSD+5.3-RELEASE+and+Ports&amp;format=html" target="_blank"><font face="Courier">DBOPEN(3)</font></a> 這個函數來處理一些 hash..</p>
<p>原本 PHP  有支援 DBM extension 可以讀取這些 db，不過新版的 PHP 已經不支援 DBM extension 了，改用 <a href="http://www.php.net/manual/en/ref.dba.php">DBA extension</a> 配合 'dbm'  handler 來存取這些 db</p>
<p>然而在 FreeBSD 下，使用 ports 去安裝的新版 PHP，其中的 DBA extension  卻無法支援 'dbm' handler..</p>
<p>當然，如果可以的話改用 'db4' handler  可能是比較好的，不過由於歷史因素難解.. 所以就只好自己跳下去改程式，讓 PHP 在安裝時能夠利用 <a href="http://www.freebsd.org/cgi/man.cgi?query=dbopen&amp;apropos=0&amp;sektion=0&amp;manpath=FreeBSD+5.3-RELEASE+and+Ports&amp;format=html" target="_blank"><font face="Courier">DBOPEN(3)</font></a> 來提供 'dbm'  handler</p>
<p>DBM 事實上只是一個 API，其所產生的 db 檔案格式在各種系統下可能不相容，而且利用不同方式提供的 DBM  handler 所產生的 db 檔案格式也可能不一樣..</p>
<p>這個 patch 我有回送給 PHP 開發小組，據說 PHP 5.1 已經恢復支援  DBM handler 了，不過我還沒空去測試</p>
<p>有了這個 patch，你就可以使用 PHP 來存取 FreeBSD 的一些系統 db 檔案了，例如  <span style="font-weight: bold">/etc/mail/aliases.db</span></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$id</span> <span style="color: #339933;">=</span> <span style="color: #990000;">dba_open</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/etc/mail/aliases.db&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;r&quot;</span><span style="color: #339933;">,</span> <span style="color: #339933;">&lt;</span>strong<span style="color: #339933;">&gt;</span><span style="color: #0000ff;">&quot;dbm&quot;</span><span style="color: #339933;">&lt;/</span>strong<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$root_alias</span> <span style="color: #339933;">=</span> <span style="color: #990000;">dba_fetch</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;root<span style="color: #660099; font-weight: bold;">\0</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$id</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>另外，這個  patch 使用 <a href="http://www.freebsd.org/cgi/man.cgi?query=hash&amp;apropos=0&amp;sektion=3&amp;manpath=FreeBSD+5.3-RELEASE+and+Ports&amp;format=html" target="_blank"><font face="Courier">HASH(3)</font></a> 當作內部的資料格式，因此如果用 C  寫程式，記得第四個參數要使用 <font face="Times"><strong>DB_HASH</strong></font> ，例如：</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">db <span style="color: #339933;">=</span> dbopen<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;/tmp/test.db&quot;</span><span style="color: #339933;">,</span> O_RDWR<span style="color: #339933;">,</span> <span style="color: #208080;">0644</span><span style="color: #339933;">,</span> DB_HASH<span style="color: #339933;">,</span> NULL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>其他詳細資訊請參考：  <a href="http://www.csie.nctu.edu.tw/%7Ecdsheen/codings/freebsd-phpdbm-patch.php">http://www.csie.nctu.edu.tw/~cdsheen/codings/freebsd-phpdbm-patch.php</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.urdada.net/2005/03/05/20/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

