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

📄 aupls.c

📁 高级unix编程第二版
💻 C
📖 第 1 页 / 共 2 页
字号:
	while (errno = 0, ((dp = readdir(sp)) != NULL)) {		if (strcmp(dp->d_name, ".") == 0 ||		  strcmp(dp->d_name, "..") == 0)			continue;		p->ti_name = dp->d_name;		ec_false( do_entry(p, true) )	}	if (errno != 0)		syserr_print("Reading directory (Pass 1)");	if (p->ti_recursive) {		rewinddir(sp);		while (errno = 0, ((dp = readdir(sp)) != NULL)) {			if (strcmp(dp->d_name, ".") == 0 ||			  strcmp(dp->d_name, "..") == 0)				continue;			p->ti_name = dp->d_name;			ec_false( do_entry(p, false) )		}		if (errno != 0)			syserr_print("Reading directory (Pass 2)");	}	result = true;	EC_CLEANUPEC_CLEANUP_BGN	if (dirfd != -1) {		(void)fchdir(dirfd);		(void)close(dirfd);	}	if (sp != NULL)		(void)closedir(sp);	return result;EC_CLEANUP_END}/*[aupls-do_entry]*/static bool do_entry(struct traverse_info *p, bool stat_only){	bool is_dir;	ec_neg1( lstat(p->ti_name, &p->ti_stat) )	is_dir = S_ISDIR(p->ti_stat.st_mode);	if (stat_only/* || !is_dir*/) { /* Bug fix: 24-March-2004 */		total_entries++;		if (is_dir)			total_dirs++;		ec_false( (p->ti_fcn)(p, SHOW_INFO) )	}	else if (is_dir)		ec_false( do_dir(p) )	return true;EC_CLEANUP_BGN	return false;EC_CLEANUP_END}/*[]*/#ifdef WANT_LSDIR_BUGGY/*[ls_dir]*/int main(int argc, char *argv[]) /* has a bug */{	bool ok = false;	int i;	DIR *dir = NULL;	struct dirent *entry;	struct stat statbuf;	for (i = 1; i < argc; i++) {		ec_neg1( lstat(argv[i], &statbuf) )		if (!S_ISDIR(statbuf.st_mode)) {			ls_long(&statbuf, argv[i]);			ok = true;			EC_CLEANUP		}		ec_null( dir = opendir(argv[i]) )		while (errno = 0, ((entry = readdir(dir)) != NULL)) {			ec_neg1( lstat(entry->d_name, &statbuf) )			ls_long(&statbuf, entry->d_name);		}		ec_nzero( errno )	}	ok = true;	EC_CLEANUPEC_CLEANUP_BGN	if (dir != NULL)		(void)closedir(dir);	exit(ok ? EXIT_SUCCESS : EXIT_FAILURE);EC_CLEANUP_END}/*[]*/#endif /* WANT_LSDIR_BUGGY */#ifdef WANT_LSDIR_FIXEDint main(int argc, char *argv[]){	bool ok = false;	int i, fd;	DIR *dir = NULL;	struct dirent *entry;	struct stat statbuf;	for (i = 1; i < argc; i++) {		ec_neg1( lstat(argv[i], &statbuf) )		if (!S_ISDIR(statbuf.st_mode)) {			ls_long(&statbuf, argv[i]);			ok = true;			EC_CLEANUP		}/*[ls_dir_fixed]*/		ec_null( dir = opendir(argv[i]) )		ec_neg1( fd = open(".", O_RDONLY) )		ec_neg1( chdir(argv[i]) )		while (errno = 0, ((entry = readdir(dir)) != NULL)) {			ec_neg1( lstat(entry->d_name, &statbuf) )			ls_long(&statbuf, entry->d_name);		}		ec_nzero( errno )		ec_neg1( fchdir(fd) )/*[]*/	}	ok = true;	EC_CLEANUPEC_CLEANUP_BGN	if (dir != NULL)		(void)closedir(dir);	exit(ok ? EXIT_SUCCESS : EXIT_FAILURE);EC_CLEANUP_END}#endif /* WANT_LSDIR_FIXED */#ifdef WANT_LSDIR_ALTERNATIVEint main(int argc, char *argv[]){	bool ok = false;	int i, fd, lstat_result, errno_save;	DIR *dir = NULL;	struct dirent *entry;	struct stat statbuf;	for (i = 1; i < argc; i++) {		ec_neg1( lstat(argv[i], &statbuf) )		if (!S_ISDIR(statbuf.st_mode)) {			ls_long(&statbuf, argv[i]);			ok = true;			EC_CLEANUP		}/*[ls_dir_fixed2]*/		ec_null( dir = opendir(argv[i]) )		ec_neg1( fd = open(".", O_RDONLY) )		ec_neg1( chdir(argv[i]) ) /* no jumps allowed until fchdir! */		while (errno = 0, ((entry = readdir(dir)) != NULL)) {			if ((lstat_result = lstat(entry->d_name, &statbuf)) == -1)				break;			ls_long(&statbuf, entry->d_name);		}		errno_save = errno;		ec_neg1( fchdir(fd) )		errno = errno_save;		ec_neg1( lstat_result )		ec_nzero( errno )/*[]*/	}	ok = true;	EC_CLEANUPEC_CLEANUP_BGN	if (dir != NULL)		(void)closedir(dir);	exit(ok ? EXIT_SUCCESS : EXIT_FAILURE);EC_CLEANUP_END}#endif /* WANT_LSDIR_ALTERNATIVE */static bool ls_one(const char *name){	struct stat statbuf;	ec_neg1( lstat(name, &statbuf) )	ls_long(&statbuf, name);	return true;EC_CLEANUP_BGN	return false;EC_CLEANUP_END}static char *big_cwd = NULL;static char big_name[FILENAME_MAX + 1];static off_t big_size = 0;static bool biggest(struct traverse_info *p, SHOW_OP op){	char *cwd;	if (p->ti_stat.st_size > big_size && S_ISREG(p->ti_stat.st_mode)) {		ec_null( cwd = get_cwd(false) )		free(big_cwd);		ec_null( big_cwd = malloc(strlen(cwd) + 1) )		strcpy(big_cwd, cwd);		strncpy(big_name, p->ti_name, sizeof(big_name));		big_name[sizeof(big_name) - 1] = '\0';		big_size = p->ti_stat.st_size;	}	return true;EC_CLEANUP_BGN	return false;EC_CLEANUP_END}static void biggest_file(void){	struct traverse_info ti = {0};	ti.ti_fcn = biggest;	ti.ti_recursive = true;	ti.ti_name = "/";	ec_false( do_entry(&ti, false) )	printf("Biggest file:\n%s\n%s\n%lu\n", big_cwd, big_name,	  (unsigned long)big_size);	return;EC_CLEANUP_BGN	EC_FLUSH("biggest_file");EC_CLEANUP_END}static bool runtest(char testtype, const char *name){	struct stat statbuf;	ec_neg1( lstat(name, &statbuf) )	switch (testtype) {	case 'a':		printf("get_max_pathname = %ld\n", get_max_pathname("."));		printf("get_max_pathname = %ld\n", get_max_pathname("/aup"));		/* Following line failed on DARWIN */		printf("get_max_pathname = %ld\n", get_max_pathname("/dev/fd"));		break;	case 'b':		biggest_file();		break;	case 'y':		ec_false( ls_one(name) )		break;	case 'c':	{/*[test_get_cwd]*/char *cwd;ec_null( cwd = get_cwd(false) )printf("%s\n", cwd);(void) get_cwd(true);/*[]*/ec_null( cwd = get_cwd(false) )printf("%s\n", cwd);(void) get_cwd(true);ec_null( cwd = get_cwd(false) )printf("%s\n", cwd);(void) get_cwd(true);ec_null( cwd = get_cwd(false) )printf("%s\n", cwd);(void) get_cwd(true);	}		break;	case 'M':{/*[test_print_mode]*/struct stat statbuf;ec_neg1( lstat("fifo", &statbuf) )print_mode(&statbuf);putchar('\n');ec_neg1( system("ls -l fifo") )/*[]*/}		break;	case 'm':		print_mode(&statbuf);		break;	case 'l':		print_numlinks(&statbuf);		break;	case 'o':		print_owner(&statbuf);		break;	case 'g':		print_group(&statbuf);		break;	case 's':		print_size(&statbuf);		break;	case 'd':		print_date(&statbuf);		break;	case 'n':		print_name(&statbuf, name);		break;	default:		fprintf(stderr, "Unknown test letter: %c\n", testtype);		errno = 0;		EC_FAIL	}	printf("\n");	return true;EC_CLEANUP_BGN	return false;EC_CLEANUP_END}#ifdef AUPLS_FULL/*[aupls-main1]*/int main(int argc, char *argv[]){	struct traverse_info ti = {0};	int c, status = EXIT_FAILURE;	bool stat_only = false;/*[]*/	char testtype = '\0';/*[aupls-main2]*/	ti.ti_fcn = show_stat;	while ((c = getopt(argc, argv, "dRt:")) != -1)		switch(c) {		case 'd':			stat_only = true;			break;		case 'R':			ti.ti_recursive = true;			break;/*[]*/		case 't':			testtype = *optarg;			break;/*[aupls-main3]*/		default:			fprintf(stderr, USAGE);			EC_CLEANUP		}	switch (argc - optind) {	case 0:		ti.ti_name = ".";		break;	case 1:		ti.ti_name = argv[optind];		break;	default:		fprintf(stderr, USAGE);		EC_CLEANUP	}/*[]*/	if (testtype != '\0')		ec_false( runtest(testtype, ti.ti_name) )	else {/*[aupls-main4]*/	ec_false( do_entry(&ti, stat_only) )	printf("\nTotal entries: %ld; directories = %ld\n", total_entries,	  total_dirs);/*[]*/	}/*[aupls-main5]*/	status = EXIT_SUCCESS;	EC_CLEANUPEC_CLEANUP_BGN	print_cwd(true);	exit(status);EC_CLEANUP_END}/*[]*/#endif

⌨️ 快捷键说明

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