chapter 3 -- file hole 文件的黑洞

apue 里介绍了文洞(英文是hole,中文不知道怎么说,我理解为“文件的黑洞”,感觉它挺神秘的,像黑洞一样……),所以做了一个程序来产生一个有黑洞的文件,一个没有黑洞的文件。用法是 

a.out filename1 filename2,filename1 

代表有黑洞的文件名,filename2 代表没黑洞的文件名。注意设计程序的时候这个“黑洞”一定要给得足够大,否则看不见……

所以我的 DELTA 设置为 1000 了,一开始设置为 100,结果 ls -asl  的时候两个文件竟然是一模一样的……

#include <stdio.h>
//#include <unistd.h>
#include "apue.h"
#include "myerr.c"
//#include <pwd.h>
#include <fcntl.h>

#define BUFSIZE	11
#define DELTA	1000

int
main (int argc, char *argv[])
{
	int 	i, fd;
	char	buf[BUFSIZE] = {"abcdefghij"} ;


	if (argc != 3)
		err_quit ("usage: a.out filename1 filename2\n") ;

	// write a holed file

	if ((fd = open (argv[1], O_CREAT | O_RDWR, S_IRWXU | S_IRWXG | S_IRWXO)) == -1)
		err_sys ("holed file open error") ;
//	printf ("%d\n", fd) ;
	if (write (fd, buf, 10) != 10)
		err_sys ("holed file 1 write error") ;

	if (lseek (fd, 10 * DELTA, SEEK_SET) == -1)
		err_sys ("holed file lseek error") ;

	if (write (fd, buf, 10) != 10)
		err_sys ("holed file 2 write error") ;

	close (fd) ;
	
	// write a no hole file

	if ((fd = open (argv[2], O_CREAT | O_RDWR, S_IRWXU | S_IRWXG | S_IRWXO)) == -1)
		err_sys ("noholed file open error") ;

	if (write (fd, buf, 10) != 10)
		err_sys ("noholed file 1 write error") ;

	for (i = 0 ; i < DELTA ; ++i) {
		if (write (fd, buf, 10) != 10)
			err_sys ("noholed file 2 write error") ;
	}
	close (fd) ;
    exit (0) ;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

写代码的安徒生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值