headermask image

header image

A sample program for stat()

stat() is a Unix system call that returns useful data about a file inode.
今天在看一个 strace 的输出时,看到下面的一行:
19:42:00.545797 stat64(”/cdctest/orahome”, {st_mode=S_IFDIR|0755, st_size=8192, …}) = 0
Google 之后发现 stat64 背后就是 stat() 系统调用,这里有详细的介绍。
于是一时手痒,想自己写一个简单的程序来实验一下,就在自己的 PC 机(跑的是 Debian “etch” 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
41
#include <stdio .h>
#include <stdlib .h>
#include <string .h>
#include <errno .h>
#include <sys /stat.h>
 
int main()
{
struct stat *buf;
const char *path = "/home/ftp/upload";
 
buf = [...]