<?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; stat</title>
	<atom:link href="http://www.jarodwang.cn/tag/stat/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>
	</channel>
</rss>
