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

📄 dasdfmt.c

📁 Linux内核源代码 为压缩文件 是<<Linux内核>>一书中的源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
	 * first, check filesystems	 */	if (!(f = fopen(_PATH_MOUNTED, "r")))		ERRMSG_EXIT(EXIT_FAILURE, "%s: %s\n", _PATH_MOUNTED,			strerror(errno));	while ((ment = getmntent(f))) {		if (stat(ment->mnt_fsname, &stbuf) == 0)			if ((major == MAJOR(stbuf.st_rdev)) &&				(minor == (MINOR(stbuf.st_rdev)>>ishift))) {				ERRMSG("%s: device is mounted on %s!!\n",					prog_name,ment->mnt_dir);				ERRMSG_EXIT(EXIT_BUSY, "If you really want to "					"format it, please unmount it.\n");			}	}	fclose(f);	/*	 * second, check active swap spaces	 */	if (!(f = fopen("/proc/swaps", "r")))		ERRMSG_EXIT(EXIT_FAILURE, "/proc/swaps: %s", strerror(errno));	/*	 * skip header line	 */	fgets(line, sizeof(line), f);	while (fgets(line, sizeof(line), f)) {		char *p;		for (p = line; *p && (!isspace(*p)); p++) ;		*p = '\0';		if (stat(line, &stbuf) == 0)			if ((major == MAJOR(stbuf.st_rdev)) &&				(minor == (MINOR(stbuf.st_rdev)>>ishift))) {				ERRMSG("%s: the device is in use for "					"swapping!!\n",prog_name);				ERRMSG_EXIT(EXIT_BUSY, "If you really want to "					"format it, please use swapoff %s.\n",					line);			}	}	fclose(f);}voiddo_format_dasd(char *dev_name,format_data_t format_params,int testmode,	int verbosity,int withoutprompt){	int fd,rc;	struct stat stat_buf;	kdev_t minor_no,major_no;	int devno;	char inp_buffer[5]; /* to contain yes */	fd=open(dev_name,O_RDWR);	if (fd==-1)		ERRMSG_EXIT(EXIT_FAILURE,"%s: error opening device %s: " \			"%s\n",prog_name,dev_name,strerror(errno));	if (verbosity>=1) {	}	rc=stat(dev_name,&stat_buf);	if (rc) {		ERRMSG_EXIT(EXIT_FAILURE,"%s: error occured during stat: " \			"%s\n",prog_name,strerror(errno));	} else {		if (!S_ISBLK(stat_buf.st_mode))			ERRMSG_EXIT(EXIT_FAILURE,"%s: file is not a " \				"blockdevice.\n",prog_name);		major_no=MAJOR(stat_buf.st_rdev);		minor_no=MINOR(stat_buf.st_rdev);	}	check_mounted(major_no, minor_no);		if ( ((withoutprompt)&&(verbosity>=1)) ||		(!withoutprompt) ) {		get_xno_from_xno(&devno,&major_no,&minor_no,			GIVEN_MAJOR|GIVEN_MINOR);		printf("\nI am going to format the device %s in the " \			"following way:\n",dev_name);		printf("   Device number of device : 0x%x\n",devno);		printf("   Major number of device  : %u\n",major_no);		printf("   Minor number of device  : %u\n",minor_no);		printf("   Start track             : %d\n" \			,format_params.start_unit);		printf("   End track               : ");		if (format_params.stop_unit==-1)			printf("last track of disk\n");		else			printf("%d\n",format_params.stop_unit);		printf("   Blocksize               : %d\n" \			,format_params.blksize);		if (testmode) printf("Test mode active, omitting ioctl.\n");	}	while (!testmode) {		if (!withoutprompt) {			printf("\n--->> ATTENTION! <<---\n");			printf("All data in the specified range of that " \				"device will be lost.\nType yes to continue" \				", no will leave the disk untouched: ");			fgets(inp_buffer,sizeof(inp_buffer),stdin);			if (strcasecmp(inp_buffer,"yes") &&				strcasecmp(inp_buffer,"yes\n")) {				printf("Omitting ioctl call (disk will " \					"NOT be formatted).\n");				break;			}		}		if ( !(  (withoutprompt)&&(verbosity<1) ))			printf("Formatting the device. This may take a " \				"while (get yourself a coffee).\n");		rc=ioctl(fd,IOCTL_COMMAND,format_params);		if (rc)			ERRMSG_EXIT(EXIT_FAILURE,"%s: the dasd driver " \				"returned with the following error " \				"message:\n%s\n",prog_name,strerror(errno));		printf("Finished formatting the device.\n");		break;	}	rc=close(fd);	if (rc)		ERRMSG("%s: error during close: " \			"%s; continuing.\n",prog_name,strerror(errno));}int main(int argc,char *argv[]) {	int verbosity;	int testmode;	int withoutprompt;	char *dev_name;	int devno;	char *dev_filename,*devno_param_str,*range_param_str;	char *start_param_str,*end_param_str,*blksize_param_str;		format_data_t format_params;	int rc;	int oc;	char *endptr;	char c1,c2,cbuffer[6]; /* should be able to contain -end plus 1 char */	int i1,i2;	char *str;	int start_specified,end_specified,blksize_specified;	int devfile_specified,devno_specified,range_specified;	/******************* initialization ********************/	endptr=NULL;	/* set default values */	format_params.start_unit=0;	format_params.stop_unit=-1;	format_params.blksize=4096;	testmode=0;	verbosity=0;	withoutprompt=0;	start_specified=end_specified=blksize_specified=0;	devfile_specified=devno_specified=range_specified=0;	/*************** parse parameters **********************/	/* avoid error message generated by getopt */	opterr=0;	while ( (oc=getopt(argc,argv,"r:s:e:b:n:f:hty?vV")) !=EOF) {		switch (oc) {		case 'y':			withoutprompt=1;			break;		case 't':			testmode=1;			break;		case 'v':			verbosity++;			break;		case '?': /* fall-through */		case ':':			exit_usage(EXIT_MISUSE);		case 'h':			exit_usage(0);		case 'V':			printf("%s version 0.99\n",prog_name);			exit(0);		case 's' :			start_param_str=optarg;			start_specified++;			break;					case 'e' :			end_param_str=optarg;			end_specified++;			break;		case 'b' :			blksize_param_str=optarg;			blksize_specified++;			break;					case 'n' :			devno_param_str=optarg;			devno_specified++;			break;				case 'f' :			dev_filename=optarg;			devfile_specified++;			break;		case 'r' :			range_param_str=optarg;			range_specified++;			break;		}	}	/******************** checking of parameters **************/	/* convert range into -s and -e */	CHECK_SPEC_MAX_ONCE(range_specified,"formatting range");	while (range_specified) {		start_specified++;		end_specified++;		/* scan for 1 or 2 integers, separated by a dash */		rc=sscanf(range_param_str,"%d%c%d%c",&i1,&c1,&i2,&c2);		if ((rc==3)&&(c1=='-')) {			format_params.start_unit=i1;			format_params.stop_unit=i2;			break;		}		if (rc==1) {			format_params.start_unit=i1;			break;		}		/* scan for integer and -END */		rc=sscanf(range_param_str,"%d%s",&i1,cbuffer);		if ((rc==2)&&(!strcasecmp(cbuffer,"-END"))) {			format_params.start_unit=i1;			format_params.stop_unit=-1;			break;		}		ERRMSG_EXIT(EXIT_MISUSE,"%s: specified range " \			"is in invalid format\n",prog_name);	}	if ((!devfile_specified)&&(!devno_specified))		ERRMSG_EXIT(EXIT_MISUSE,"%s: device to format " \			"not specified\n",prog_name);	if ((devfile_specified+devno_specified)>1)		ERRMSG_EXIT(EXIT_MISUSE,"%s: device to format " \			"can only be specified once\n",prog_name);	if ((!start_specified)&&(!end_specified)&&(!range_specified)&&		(!blksize_specified)) {		format_params=ask_user_for_data(format_params);	}	CHECK_SPEC_MAX_ONCE(start_specified,"start track");	CHECK_SPEC_MAX_ONCE(end_specified,"end track");	CHECK_SPEC_MAX_ONCE(blksize_specified,"blocksize");	if (devno_specified)		PARSE_PARAM_INTO(devno,devno_param_str,16,"device number");	if (start_specified&&!range_specified)		PARSE_PARAM_INTO(format_params.start_unit,start_param_str,10,			"start track");	if (end_specified&&!range_specified)		PARSE_PARAM_INTO(format_params.stop_unit,end_param_str,10,			"end track");	if (blksize_specified)		PARSE_PARAM_INTO(format_params.blksize,blksize_param_str,10,			"blocksize");	/***********get dev_name *********************/	dev_name=(devno_specified)?		get_devname_from_devno(devno,verbosity):		dev_filename;	/*** range checking *********/	str=check_param(CHECK_ALL,format_params);	if (str!=NULL) ERRMSG_EXIT(EXIT_MISUSE,"%s: %s\n",prog_name,str);	/*************** issue the real command *****************/	do_format_dasd(dev_name,format_params,testmode,verbosity,		withoutprompt);	/*************** cleanup ********************************/	if (strncmp(dev_name,TEMPFILENAME,TEMPFILENAMECHARS)==0) {		rc=unlink(dev_name);		if ((rc)&&(verbosity>=1))			ERRMSG("%s: temporary device node %s could not be " \				"removed: %s\n",prog_name,dev_name,				strerror(errno));	} else {		if (devno_specified) {			/* so we have allocated space for the filename */			free(dev_name);		}	}	return 0;}

⌨️ 快捷键说明

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