<?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>My Crusade &#187; Programming</title>
	<atom:link href="http://www.jarodwang.cn/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jarodwang.cn</link>
	<description>For the future we believe in.</description>
	<lastBuildDate>Thu, 26 Nov 2009 12:29:57 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>A sample program for stat()</title>
		<link>http://www.jarodwang.cn/2008/12/11/a-sample-program-for-stat/</link>
		<comments>http://www.jarodwang.cn/2008/12/11/a-sample-program-for-stat/#comments</comments>
		<pubDate>Thu, 11 Dec 2008 15:23:09 +0000</pubDate>
		<dc:creator>jarodwang</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[stat]]></category>
		<category><![CDATA[strace]]></category>

		<guid isPermaLink="false">http://www.jarodwang.cn/?p=180</guid>
		<description><![CDATA[stat() is a Unix system call that returns useful data about a file inode.
今天在看一个 strace 的输出时，看到下面的一行：
19:42:00.545797 stat64(&#8221;/cdctest/orahome&#8221;, {st_mode=S_IFDIR&#124;0755, st_size=8192, &#8230;}) = 0
Google 之后发现 stat64 背后就是 stat() 系统调用，这里有详细的介绍。
于是一时手痒，想自己写一个简单的程序来实验一下，就在自己的 PC 机（跑的是 Debian &#8220;etch&#8221; 4.0r5）上动手。
首先是搭建编译环境：
# apt-get install gcc build-essential
然后coding如下：

?Download test_stat.c1
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
#include &#60;stdio.h&#62;
#include &#60;stdlib.h&#62;
#include &#60;string.h&#62;
#include &#60;errno.h&#62;
#include &#60;sys/stat.h&#62;
&#160;
int main&#40;&#41;
&#123;
	struct stat *buf;
	const char *path = &#34;/home/ftp/upload&#34;;
&#160;
	buf = &#40;struct stat *&#41; malloc&#40;sizeof&#40;struct stat&#41;&#41;;
	memset&#40;buf, [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p><a title="stat()" href="http://en.wikipedia.org/wiki/Stat_(Unix)" target="_blank">stat()</a> is a Unix system call that returns useful data about a file inode.</p></blockquote>
<p>今天在看一个 strace 的输出时，看到下面的一行：</p>
<p>19:42:00.545797 stat64(&#8221;/cdctest/orahome&#8221;, {st_mode=S_IFDIR|0755, st_size=8192, &#8230;}) = 0</p>
<p>Google 之后发现 stat64 背后就是 stat() 系统调用，<a title="这里" href="http://linux.die.net/man/2/stat64" target="_blank">这里</a>有详细的介绍。</p>
<p>于是一时手痒，想自己写一个简单的程序来实验一下，就在自己的 PC 机（跑的是 Debian &#8220;etch&#8221; 4.0r5）上动手。</p>
<p>首先是搭建编译环境：</p>
<p># apt-get install gcc build-essential</p>
<p>然后coding如下：</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left2">Download <a href="http://www.jarodwang.cn/wordpress/wp-content/plugins/wp-codebox/wp-codebox.php?p=180&amp;download=test_stat.c">test_stat.c</a></span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p1802"><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
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
</pre></td><td class="code" id="p180code2"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &lt;stdio.h&gt;</span>
<span style="color: #339933;">#include &lt;stdlib.h&gt;</span>
<span style="color: #339933;">#include &lt;string.h&gt;</span>
<span style="color: #339933;">#include &lt;errno.h&gt;</span>
<span style="color: #339933;">#include &lt;sys/stat.h&gt;</span>
&nbsp;
<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;">struct</span> stat <span style="color: #339933;">*</span>buf<span style="color: #339933;">;</span>
	<span style="color: #993333;">const</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>path <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;/home/ftp/upload&quot;</span><span style="color: #339933;">;</span>
&nbsp;
	buf <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">struct</span> stat <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> malloc<span style="color: #009900;">&#40;</span><span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">struct</span> stat<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</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: #993333;">sizeof</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">struct</span> stat<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #993333;">int</span> ret<span style="color: #339933;">;</span> 
&nbsp;
	ret <span style="color: #339933;">=</span> stat<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;/home/ftp/upload&quot;</span><span style="color: #339933;">,</span> buf<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>ret <span style="color: #339933;">==</span> EACCES<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;EACCES<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		free<span style="color: #009900;">&#40;</span>buf<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		buf <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">NULL</span><span style="color: #339933;">;</span>
		exit<span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span>EACCES<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> buf<span style="color: #339933;">-&gt;</span>st_mode<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>S_ISDIR<span style="color: #009900;">&#40;</span>buf<span style="color: #339933;">-&gt;</span>st_mode<span style="color: #009900;">&#41;</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;%s is a diretory.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> path<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: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%s is NOT a directory.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> path<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	free<span style="color: #009900;">&#40;</span>buf<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	buf<span style="color: #339933;">=</span><span style="color: #000000; font-weight: bold;">NULL</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>编译生成可执行文件</p>
<p>jarod@zhenwan:~$ gcc -o test_stat test_stat.c</p>
<p>运行之：</p>
<p>jarod@zhenwan:~$ ./test_stat<br />
16895<br />
/home/ftp/upload is a diretory.</p>
<p>大功告成。</p>
<p>最后还可以自己 strace 一把：</p>
<p><span id="more-180"></span></p>
<p>jarod@zhenwan:~$ strace ./test_stat<br />
execve(&#8221;./test_stat&#8221;, ["./test_stat"], [/* 27 vars */]) = 0<br />
uname({sys=&#8221;Linux&#8221;, node=&#8221;zhenwan&#8221;, &#8230;}) = 0<br />
brk(0)                                  = 0&#215;804a000<br />
access(&#8221;/etc/ld.so.nohwcap&#8221;, F_OK)      = -1 ENOENT (No such file or directory)<br />
mmap2(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb7ee9000<br />
access(&#8221;/etc/ld.so.preload&#8221;, R_OK)      = -1 ENOENT (No such file or directory)<br />
open(&#8221;/etc/ld.so.cache&#8221;, O_RDONLY)      = 3<br />
fstat64(3, {st_mode=S_IFREG|0644, st_size=45513, &#8230;}) = 0<br />
mmap2(NULL, 45513, PROT_READ, MAP_PRIVATE, 3, 0) = 0xb7edd000<br />
close(3)                                = 0<br />
access(&#8221;/etc/ld.so.nohwcap&#8221;, F_OK)      = -1 ENOENT (No such file or directory)<br />
open(&#8221;/lib/tls/i686/cmov/libc.so.6&#8243;, O_RDONLY) = 3<br />
read(3, &#8220;\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0\3\0\1\0\0\0\240O\1&#8243;&#8230;, 512) = 512<br />
fstat64(3, {st_mode=S_IFREG|0644, st_size=1241392, &#8230;}) = 0<br />
mmap2(NULL, 1247388, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0xb7dac000<br />
mmap2(0xb7ed3000, 28672, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0&#215;127) = 0xb7ed3000<br />
mmap2(0xb7eda000, 10396, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0xb7eda000<br />
close(3)                                = 0<br />
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb7dab000<br />
mprotect(0xb7ed3000, 20480, PROT_READ)  = 0<br />
set_thread_area({entry_number:-1 -&gt; 6, base_addr:0xb7dab8e0, limit:1048575, seg_32bit:1, contents:0, read_exec_only:0, limit_in_pages:1, seg_not_present:0, useable:1}) = 0<br />
munmap(0xb7edd000, 45513)               = 0<br />
brk(0)                                  = 0&#215;804a000<br />
brk(0&#215;806b000)                          = 0&#215;806b000<br />
<span style="color: #ff0000;">stat64(&#8221;/home/ftp/upload&#8221;, {st_mode=S_IFDIR|0777, st_size=4096, &#8230;}) = 0</span><br />
fstat64(1, {st_mode=S_IFCHR|0600, st_rdev=makedev(136, 5), &#8230;}) = 0<br />
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb7ee8000<br />
write(1, &#8220;16895\n&#8221;, 616895<br />
)                  = 6<br />
write(1, &#8220;/home/ftp/upload is a diretory.\n&#8221;, 32/home/ftp/upload is a diretory.<br />
) = 32<br />
munmap(0xb7ee8000, 4096)                = 0<br />
exit_group(0)                           = ?<br />
Process 32729 detached<br />
jarod@zhenwan:~$</p>
<p>可以看出来其中<span style="color: #ff0000;">红色</span>的一行即是由程序中的 stat() 系统调用所产生。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jarodwang.cn/2008/12/11/a-sample-program-for-stat/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A glance of core dump</title>
		<link>http://www.jarodwang.cn/2008/09/20/a-glance-of-core-dump/</link>
		<comments>http://www.jarodwang.cn/2008/09/20/a-glance-of-core-dump/#comments</comments>
		<pubDate>Sat, 20 Sep 2008 03:11:27 +0000</pubDate>
		<dc:creator>jarodwang</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.jarodwang.cn/?p=95</guid>
		<description><![CDATA[在平时的测试工作中经常碰到程序产生core dump的情况，什么是core dump？下面是摘自Wikipedia的core dump的定义：
A core dump is the recorded state of the working memory of a computer program at a specific time, generally when the program has terminated abnormally. In practice, other key pieces of program state are usually dumped at the same time, including the processor registers, which may include the program counter and [...]]]></description>
			<content:encoded><![CDATA[<p>在平时的测试工作中经常碰到程序产生core dump的情况，什么是core dump？下面是摘自<a title="Wikipedia" href="http://www.wikipedia.org/" target="_blank">Wikipedia</a>的core dump的定义：</p>
<p>A <strong>core dump</strong> is the recorded state of the working <span class="mw-redirect">memory</span> of a computer program at a specific time, generally when the program has terminated abnormally. In practice, other key pieces of program state are usually dumped at the same time, including the processor registers, which may include the program counter and stack pointer, memory management information, and other processor and operating system flags and information. The name comes from the once-standard <span class="mw-redirect">core memory</span> technology. Core dumps are often used to diagnose or <span class="mw-redirect">debug</span> errors in computer programs.</p>
<p>简单来说，core dump是在程序异常终止的时候对其所使用的内存内容和程序状态的一个记录。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jarodwang.cn/2008/09/20/a-glance-of-core-dump/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>An accumulation of regular expressions</title>
		<link>http://www.jarodwang.cn/2008/07/05/an-accumulation-of-regular-expressions/</link>
		<comments>http://www.jarodwang.cn/2008/07/05/an-accumulation-of-regular-expressions/#comments</comments>
		<pubDate>Sat, 05 Jul 2008 15:06:36 +0000</pubDate>
		<dc:creator>jarodwang</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[re]]></category>

		<guid isPermaLink="false">http://www.jarodwang.cn/?p=73</guid>
		<description><![CDATA[在检查一行文本时，^代表一行的开始，$代表结束。
&#124;是一个非常简捷的元字符（metacharacter），它的意思是“或”（or）。依靠它，我们能够把不同的子表达式组合成一个总的表达式，而这个总的表达式又能够匹配任意的子表达式。
egrep的命令行参数“-i”表示进行忽略大小写的匹配。
元字符+表示之前紧邻的元素出现一次或多次，而*表示之前紧邻的元素任意多次，或者不出现。
使用\+来匹配字符串中出现的“+”号。
To be continued
]]></description>
			<content:encoded><![CDATA[<p>在检查一行文本时，^代表一行的开始，$代表结束。</p>
<p>|是一个非常简捷的元字符（metacharacter），它的意思是“或”（or）。依靠它，我们能够把不同的子表达式组合成一个总的表达式，而这个总的表达式又能够匹配任意的子表达式。</p>
<p>egrep的命令行参数“-i”表示进行忽略大小写的匹配。</p>
<p>元字符+表示之前紧邻的元素出现一次或多次，而*表示之前紧邻的元素任意多次，或者不出现。</p>
<p>使用\+来匹配字符串中出现的“+”号。</p>
<p>To be continued</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jarodwang.cn/2008/07/05/an-accumulation-of-regular-expressions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>An accumulation of ksh</title>
		<link>http://www.jarodwang.cn/2008/07/03/an-accumulation-of-ksh/</link>
		<comments>http://www.jarodwang.cn/2008/07/03/an-accumulation-of-ksh/#comments</comments>
		<pubDate>Thu, 03 Jul 2008 15:34:17 +0000</pubDate>
		<dc:creator>jarodwang</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[ksh]]></category>
		<category><![CDATA[shell]]></category>

		<guid isPermaLink="false">http://www.jarodwang.cn/?p=72</guid>
		<description><![CDATA[ksh的内置命令history可以显示出命令历史列表。
当给一个局部变量赋值时，等号两边不能出现空格。（今天刚犯了这个错误-_-b）
设置一个变量为空时，等号右边什么都不带即可。
双美元符号$$是一个特殊变量，它保存了当前shell的PID。
env命令列出所有环境变量（输出变量）。
To be continued
]]></description>
			<content:encoded><![CDATA[<p>ksh的内置命令history可以显示出命令历史列表。</p>
<p>当给一个局部变量赋值时，等号两边不能出现空格。（今天刚犯了这个错误-_-b）</p>
<p>设置一个变量为空时，等号右边什么都不带即可。</p>
<p>双美元符号$$是一个特殊变量，它保存了当前shell的PID。</p>
<p>env命令列出所有环境变量（输出变量）。</p>
<p>To be continued</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jarodwang.cn/2008/07/03/an-accumulation-of-ksh/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
