`
txf2004
  • 浏览: 6874882 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

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) ;
}


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics