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 = [...]

Linux system calls

最近学习了在Linux平台上使用strace来观察程序所调用的系统调用,它给出的输出中涉及到了很多Linux平台的系统调用,这里我搜集了一些常见的,以备参考:
int access (const char *pathname, int mode);
检查当前用户对于文件所拥有的权限(R_OK, W_OK, X_OK),或是检查文件是否存在(F_OK)
int fcntl (int fd, int cmd, long arg);
操作文件描述符fd对应的文件
int fstat (int fd, struct stat *buf);
返回文件描述符fd对应的文件的状态
long getcwd (char *buf, unsigned long size);
获取当前工作目录的绝对路径
int getrlimit (int resource, struct rlimit *rlim);
获取资源限制
off_t lseek (int fd, off_t offset, int whence);
重定位读写文件时的偏移量(offset)
ssize_t read (int fd, void *buf, size_t count);
从文件描述符fd对应的文件读
int statfs (const char *path, struct statfs *buf);
获取文件系统统计数据
int [...]