headermask image

header image

category archive listing Category Archives: Programming

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

A glance of core dump

在平时的测试工作中经常碰到程序产生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 [...]

An accumulation of regular expressions

在检查一行文本时,^代表一行的开始,$代表结束。
|是一个非常简捷的元字符(metacharacter),它的意思是“或”(or)。依靠它,我们能够把不同的子表达式组合成一个总的表达式,而这个总的表达式又能够匹配任意的子表达式。
egrep的命令行参数“-i”表示进行忽略大小写的匹配。
元字符+表示之前紧邻的元素出现一次或多次,而*表示之前紧邻的元素任意多次,或者不出现。
使用\+来匹配字符串中出现的“+”号。
To be continued

An accumulation of ksh

ksh的内置命令history可以显示出命令历史列表。
当给一个局部变量赋值时,等号两边不能出现空格。(今天刚犯了这个错误-_-b)
设置一个变量为空时,等号右边什么都不带即可。
双美元符号$$是一个特殊变量,它保存了当前shell的PID。
env命令列出所有环境变量(输出变量)。
To be continued