⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 x2.c

📁 Advanced UNIX Programming is the long-awaited (19 years!) update to the 1985 original. Maybe "update
💻 C
📖 第 1 页 / 共 2 页
字号:
/*[]*//*[testlock]*/void testlock(void){	int i;	for (i = 1; i <= 4; i++) {		if (lock("accounts")) {			printf("Process %ld got the lock\n", (long)getpid());			sleep(rand() % 5 + 1);			ec_false( unlock("accounts") )		}		else {			if (errno == EAGAIN) {				printf("Process %ld tired of waiting\n", (long)getpid());				ec_reinit(); /* forget this error */			}			else				EC_FAIL /* something serious */		}		sleep(rand() % 5 + 5);	}	return;EC_CLEANUP_BGN	EC_FLUSH("testlock")EC_CLEANUP_END}/*[]*/void junk(void){	int fd, n;	char *path, acctrec[50];/*[tempfile]*/ec_neg1( fd = open("temp", O_RDWR | O_CREAT | O_TRUNC | O_EXCL, 0) )ec_neg1( unlink("temp") )/*[]*/	ec_neg1( write(fd, &fd, sizeof(fd)) )	ec_neg1( lseek(fd, SEEK_SET, 0) )	ec_neg1( read(fd, &n, sizeof(n)) )	assert(fd == n);	ec_neg1( close(fd) )/*[tempfile-lock]*/ec_false( lock("opentemp") )ec_neg1(  fd = open("temp", O_RDWR | O_CREAT | O_TRUNC | O_EXCL, 0) )ec_neg1(  unlink("temp") )ec_false( unlock("opentemp") )/*[]*/	ec_neg1( write(fd, &fd, sizeof(fd)) )	ec_neg1( lseek(fd, SEEK_SET, 0) )	ec_neg1( read(fd, &n, sizeof(n)) )	assert(fd == n);	ec_neg1( close(fd) ){/*[tempfile2]*/char *pathname;ec_null( pathname = tmpnam(NULL) )ec_neg1( fd = open(pathname, O_RDWR | O_CREAT | O_TRUNC | O_EXCL, 0) )ec_neg1( unlink(pathname) )/*[]*/	ec_neg1( write(fd, &fd, sizeof(fd)) )	ec_neg1( lseek(fd, SEEK_SET, 0) )	ec_neg1( read(fd, &n, sizeof(n)) )	assert(fd == n);	ec_neg1( close(fd) )}{/*[tempfile3]*/char pathname[] = "/tmp/dataXXXXXX";errno = 0; /* mkstemp may not set it on error */ec_neg1( fd = mkstemp(pathname) )ec_neg1( unlink(pathname) )printf("%s\n", pathname);/*[]*/	ec_neg1( write(fd, &fd, sizeof(fd)) )	ec_neg1( lseek(fd, SEEK_SET, 0) )	ec_neg1( read(fd, &n, sizeof(n)) )	assert(fd == n);	ec_neg1( close(fd) )}{char pathname[] = "/tmp/dataXXXXXX";/*[tempfile4]*/int status;char cmd[100];ec_neg1( fd = mkstemp(pathname) )/* code to write text lines to fd not shown *//*[]*//* following writes not in book */ec_neg1( write(fd, "zzz\n", 4) )ec_neg1( write(fd, "mmm\n", 4) )ec_neg1( write(fd, "qqq\n", 4) )ec_neg1( write(fd, "aaa\n", 4) )ec_neg1( write(fd, "rrr\n", 4) )/*[tempfile4a]*/snprintf(cmd, sizeof(cmd), "sort %s", pathname);ec_neg1( status = system(cmd) )/*[]*/	ec_neg1( close(fd) )}{/*[fileoffset]*/int fd1, fd2, fd3;ec_neg1( fd1 = open("myfile", O_WRONLY | O_CREAT | O_TRUNC, PERM_FILE) )ec_neg1( fd2 = open("myfile", O_RDONLY) )ec_neg1( fd3 = open("yourfile", O_RDWR | O_CREAT | O_TRUNC, PERM_FILE) )/*[]*/}return;/*2-8*/if (unlink(path) == -1)	syserr("unlink");/**//*2-7*/if ((fd = creat(path, 0600)) == -1)	syserr("creat");if (close(fd) == -1 || (fd = open(path, 2)) == -1)	syserr("reopen");/**//*2-9*/if (!lock("accts"))	syserr("lock");if (lseek(fd, 0L, 2) == -1) /* seek to end; see Section 2.11 */	syserr("lseek");if (write(fd, acctrec, sizeof(acctrec)) == -1)	syserr("write");unlock("accts");/**//*2-10*/if ((fd = open(path, 1)) == -1) {	if (errno == ENOENT) {		if ((fd = creat(path, 0666)) == -1)			syserr("creat");	}	else		syserr("open");}/**//*2-11*/if ((fd = open(path, O_WRONLY | O_CREAT, 0666)) == -1)	syserr("open");/**/	return;EC_CLEANUP_BGNEC_CLEANUP_END}/*[backward0]*/void backward0(char *path){	char s[256], c;	int i, fd;	ssize_t nread;	off_t where;	ec_neg1( fd = open(path, O_RDONLY) )	ec_neg1( where = lseek(fd, -1, SEEK_END) )	i = sizeof(s) - 1;	s[i] = '\0';	while ((nread = read(fd, &c, 1)) == 1) {		if (c == '\n') {			printf("%s", &s[i]);			i = sizeof(s) - 1;		}		if (i <= 0) {			errno = E2BIG;			EC_FAIL		}		s[--i] = c;		if (where == 0)			break;		ec_neg1( where = lseek(fd, -2, SEEK_CUR) )	}	if (nread == -1)		EC_FAIL	else if (nread != 1){ /* impossible */		errno = 0;		EC_FAIL	}	printf("%s", &s[i]);	ec_neg1( close(fd) )	return;EC_CLEANUP_BGN	EC_FLUSH("backward");EC_CLEANUP_END}/*[]*//*[backward]*/void backward(char *path){	char s[256], c;	int i, fd;	off_t where;	ec_neg1( fd = open(path, O_RDONLY) )	ec_neg1( where = lseek(fd, 1, SEEK_END) )	i = sizeof(s) - 1;	s[i] = '\0';	do {		ec_neg1( where = lseek(fd, -2, SEEK_CUR) )		switch (read(fd, &c, 1)) {		case 1:			if (c == '\n') {				printf("%s", &s[i]);				i = sizeof(s) - 1;			}			if (i <= 0) {				errno = E2BIG;				EC_FAIL			}			s[--i] = c;			break;		case -1:			EC_FAIL			break;		default: /* impossible */			errno = 0;			EC_FAIL		}	} while (where > 0);	printf("%s", &s[i]);	ec_neg1( close(fd) )	return;EC_CLEANUP_BGN	EC_FLUSH("backward");EC_CLEANUP_END}/*[]*//*[backward2]*/void backward2(char *path){	char s[256], c;	int i, fd;	off_t file_size, where;	ec_neg1( fd = open(path, O_RDONLY) )	ec_neg1( file_size = lseek(fd, 0, SEEK_END) )	i = sizeof(s) - 1;	s[i] = '\0';	for (where = file_size - 1; where >= 0; where--)		switch (pread(fd, &c, 1, where)) {		case 1:			if (c == '\n') {				printf("%s", &s[i]);				i = sizeof(s) - 1;			}			if (i <= 0) {				errno = E2BIG;				EC_FAIL			}			s[--i] = c;			break;		case -1:			EC_FAIL			break;		default: /* impossible */			errno = 0;			EC_FAIL		}	printf("%s", &s[i]);	ec_neg1( close(fd) )	return;EC_CLEANUP_BGN	EC_FLUSH("backward2");EC_CLEANUP_END}/*[]*//*[ftruncate_test]*/void ftruncate_test(void){	int fd;	const char s[] = "Those are my principles.\n"	  "If you don't like them I have others.\n"	  "\t--Groucho Marx\n";	ec_neg1( fd = open("tmp", O_WRONLY | O_CREAT | O_TRUNC, PERM_FILE) )	errno = 0;	ec_false( write(fd, s, sizeof(s)) == sizeof(s) )	(void)system("ls -l tmp; cat tmp");	ec_neg1( ftruncate(fd, 25) )	(void)system("ls -l tmp; cat tmp");	ec_neg1( close(fd) )	return;EC_CLEANUP_BGN	EC_FLUSH("ftruncate_test");EC_CLEANUP_END}/*[]*/void pwrite_test(void){	int fd;	ec_neg1( fd = open("tmp", O_WRONLY | O_CREAT | O_TRUNC | O_APPEND,	  PERM_FILE) )	ec_neg1( pwrite(fd, "hello1\n", 7, 0) )	ec_neg1( pwrite(fd, "hello2\n", 7, 0) )	ec_neg1( pwrite(fd, "hello3\n", 7, 0) )	ec_neg1( pwrite(fd, "hello4\n", 7, 0) )	system("echo '========'; cat tmp");	ec_neg1( lseek(fd, 0, SEEK_SET) )	ec_neg1( write(fd, "hello5\n", 7) )	ec_neg1( write(fd, "hello6\n", 7) )	ec_neg1( write(fd, "hello7\n", 7) )	ec_neg1( write(fd, "hello8\n", 7) )	ec_neg1( close(fd) )	system("echo '========'; cat tmp; echo '========'");	return;EC_CLEANUP_BGN	EC_FLUSH("pwrite_test");EC_CLEANUP_END}void backward_test(void){	FILE *f;	ec_null( f = fopen("backward.tmp", "w") )	ec_eof(  fputs("dog\nbites\nman\n", f) )	ec_eof(  fclose(f) )	backward0("backward.tmp");	backward("backward.tmp");	backward2("backward.tmp");	(void)unlink("backward.tmp");	return;EC_CLEANUP_BGN	EC_FLUSH("backward");EC_CLEANUP_END}int main(void){	struct stat sbuf;	ftruncate_test();	printf("_SC_OPEN_MAX = %ld\n", sysconf(_SC_OPEN_MAX));	pwrite_test();	//synctest();	scatter();	backward_test();	print_C_info();printf("BUFSIZ = %d\n", BUFSIZ);printf("getblksize = %lu\n", getblksize(NULL));	ec_neg1( stat(".", &sbuf) )printf("st_blksize = %lu\n", (unsigned long)sbuf.st_blksize);#if 0	copy("a.tmp", "b.tmp");	copy2("b.tmp", "c.tmp");	system("cmp a.tmp c.tmp");	junk();	testlock();	ec_false( temp_open() )#endif	unlink("b.tmp");	unlink("c.tmp");	unlink("d.tmp");	unlink("e.tmp");	system("ls -l file_of_4mb"); /* AUP2 (was "li", whatever that was) */	timestart();	ec_false( copy3("file_of_4mb", "d.tmp") )	timestop("BUFIO pkg");	system("cmp file_of_4mb d.tmp");	timestart();	ec_false( copy4("file_of_4mb", "e.tmp") )	timestop("Standard I/O");	system("cmp file_of_4mb e.tmp");	timestart();	ec_false( copy2("file_of_4mb", "b.tmp") )	timestop("BUFSIZ blocks");	system("cmp file_of_4mb b.tmp");	timestart();	ec_false( copy2a("file_of_4mb", "c.tmp") )	timestop("1-byte blocks");	system("cmp file_of_4mb c.tmp");return 0;	system("ls -l a.tmp");	if (!lock("lock"))		syserr("lock1 failed");	system("ls -l /tmp/lock");	if (lock("lock"))		syserr("lock2 failed");	system("ls -l /tmp/lock");	unlock("lock");	system("ls -l /tmp/lock");	if (!lock("lock"))		syserr("lock3 failed");	if (lock("lock"))		syserr("lock4 failed");	unlock("lock");	printf("Done.\n"); /* AUP2 */EC_CLEANUP_BGN	return 1;EC_CLEANUP_END}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -