clearerr(stdin);
char	*getstring __P((char *, char *, char *));u_int32_t getuint __P((struct disklabel *, int, char *, char *, u_int32_t, u_int32_t, u_int32_t, int));int	has_overlap __P((struct disklabel *, u_int32_t *, int));void	make_contiguous __P((struct disklabel *));u_int32_t next_offset __P((struct disklabel *, u_int32_t *));int	partition_cmp __P((const void *, const void *));struct partition **sort_partitions __P((struct disklabel *, u_int16_t *));void	getdisktype __P((struct disklabel *, char *, char *));void	find_bounds __P((struct disklabel *, struct disklabel *));void	set_bounds __P((struct disklabel *, u_int32_t *));struct diskchunk *free_chunks __P((struct disklabel *));char **	mpcopy __P((char **, char **));int	micmp __P((const void *, const void *));int	mpequal __P((char **, char **));int	mpsave __P((struct disklabel *, char **, char *, char *));int	get_bsize __P((struct disklabel *, int));int	get_cpg __P((struct disklabel *, int));int	get_fsize __P((struct disklabel *, int));int	get_fstype __P((struct disklabel *, int));int	get_mp __P((struct disklabel *, char **, int));int	get_offset __P((struct disklabel *, int));int	get_size __P((struct disklabel *, int, u_int32_t *, int));void	get_geometry __P((int, struct disklabel **, struct disklabel **));void	set_geometry __P((struct disklabel *, struct disklabel *, struct disklabel *, struct disklabel *, char *));void	zero_partitions __P((struct disklabel *, u_int32_t *));static u_int32_t starting_sector;static u_int32_t ending_sector;static int expert;/* from disklabel.c */int	checklabel __P((struct disklabel *));void	display __P((FILE *, struct disklabel *));void	display_partition __P((FILE *, struct disklabel *, char **, int, char, int));int	width_partition __P((struct disklabel *, int));struct disklabel *readlabel __P((int));struct disklabel *makebootarea __P((char *, struct disklabel *, int));int	writelabel __P((int, char *, struct disklabel *));extern	char *bootarea, *specname;extern	int donothing;#ifdef DOSLABELextern	struct dos_partition *dosdp;	/* DOS partition, if found */#endif/* * Simple partition editor.  Primarily intended for new labels. */inteditor(lp, f, dev, fstabfile)	struct disklabel *lp;	int f;	char *dev;	char *fstabfile;{	struct disklabel lastlabel, tmplabel, label = *lp;	struct disklabel *disk_geop, *bios_geop;	struct partition *pp;	u_int32_t freesectors;	FILE *fp;	char buf[BUFSIZ], *cmd, *arg;	char **mountpoints = NULL, **omountpoints, **tmpmountpoints;	/* Alloc and init mount point info */	if (fstabfile) {		if (!(mountpoints = calloc(MAXPARTITIONS, sizeof(char *))) ||		    !(omountpoints = calloc(MAXPARTITIONS, sizeof(char *))) ||		    !(tmpmountpoints = calloc(MAXPARTITIONS, sizeof(char *))))			errx(4, "out of memory");	}	/* Don't allow disk type of "unknown" */	getdisktype(&label, "You need to specify a type for this disk.", dev);	/* Get the on-disk and BIOS geometries if possible */	get_geometry(f, &disk_geop, &bios_geop);	/* How big is the OpenBSD portion of the disk?  */	find_bounds(&label, bios_geop);	/* Set freesectors based on bounds and initial label */	editor_countfree(&label, &freesectors);	/* Make sure there is no partition overlap. */	if (has_overlap(&label, &freesectors, 1))		errx(1, "can't run when there is partition overlap.");	/* If we don't have a 'c' partition, create one. */	pp = &label.d_partitions[RAW_PART];	if (label.d_npartitions < 3 || pp->p_size == 0) {		puts("No 'c' partition found, adding one that spans the disk.");		if (label.d_npartitions < 3)			label.d_npartitions = 3;		pp->p_offset = 0;		pp->p_size = label.d_secperunit;		pp->p_fstype = FS_UNUSED;		pp->p_fsize = pp->p_frag = pp->p_cpg = 0;	}#ifdef CYLCHECK	puts("This platform requires that partition offsets/sizes be on cylinder boundaries.\nPartition offsets/sizes will be rounded to the nearest cylinder automatically.");#endif	/* Set d_bbsize and d_sbsize as necessary */	if (label.d_bbsize == 0)		label.d_bbsize = BBSIZE;	if (label.d_sbsize == 0)		label.d_sbsize = SBSIZE;	/* Interleave must be >= 1 */	if (label.d_interleave == 0)		label.d_interleave = 1;	puts("\nInitial label editor (enter '?' for help at any prompt)");	lastlabel = label;	for (;;) {		fputs("> ", stdout);		if (fgets(buf, sizeof(buf), stdin) == NULL) {			putchar('\n');			buf[0] = 'q';			buf[1] = '\0';		}		if ((cmd = strtok(buf, " \t\r\n")) == NULL)			continue;		arg = strtok(NULL, " \t\r\n");		switch (*cmd) {		case '?':		case 'h':			editor_help(arg ? arg : "");			break;		case 'a':			tmplabel = lastlabel;			lastlabel = label;			if (mountpoints != NULL) {				mpcopy(tmpmountpoints, omountpoints);				mpcopy(omountpoints, mountpoints);			}			editor_add(&label, mountpoints, &freesectors, arg);			if (memcmp(&label, &lastlabel, sizeof(label)) == 0)				lastlabel = tmplabel;			if (mountpoints != NULL && mpequal(omountpoints, tmpmountpoints))				mpcopy(omountpoints, tmpmountpoints);			break;		case 'b':			tmplabel = lastlabel;			lastlabel = label;			set_bounds(&label, &freesectors);			if (memcmp(&label, &lastlabel, sizeof(label)) == 0)				lastlabel = tmplabel;			break;		case 'c':			tmplabel = lastlabel;			lastlabel = label;			editor_change(&label, &freesectors, arg);			if (memcmp(&label, &lastlabel, sizeof(label)) == 0)				lastlabel = tmplabel;			break;		case 'D':			tmplabel = lastlabel;			lastlabel = label;			if (ioctl(f, DIOCGPDINFO, &label) == 0)				editor_countfree(&label, &freesectors);			else {				warn("unable to get default partition table");				lastlabel = tmplabel;			}			break;		case 'd':			tmplabel = lastlabel;			lastlabel = label;			if (mountpoints != NULL) {				mpcopy(tmpmountpoints, omountpoints);				mpcopy(omountpoints, mountpoints);			}			editor_delete(&label, mountpoints, &freesectors, arg);			if (memcmp(&label, &lastlabel, sizeof(label)) == 0)				lastlabel = tmplabel;			if (mountpoints != NULL && mpequal(omountpoints, tmpmountpoints))				mpcopy(omountpoints, tmpmountpoints);			break;		case 'e':			tmplabel = lastlabel;			lastlabel = label;			edit_parms(&label, &freesectors);			if (memcmp(&label, &lastlabel, sizeof(label)) == 0)				lastlabel = tmplabel;			break;		case 'g':			tmplabel = lastlabel;			lastlabel = label;			set_geometry(&label, disk_geop, bios_geop, lp, arg);			if (memcmp(&label, &lastlabel, sizeof(label)) == 0)				lastlabel = tmplabel;			break;		case 'm':			tmplabel = lastlabel;			lastlabel = label;			if (mountpoints != NULL) {				mpcopy(tmpmountpoints, omountpoints);				mpcopy(omountpoints, mountpoints);			}			editor_modify(&label, mountpoints, &freesectors, arg);			if (memcmp(&label, &lastlabel, sizeof(label)) == 0)				lastlabel = tmplabel;			if (mountpoints != NULL && mpequal(omountpoints, tmpmountpoints))				mpcopy(omountpoints, tmpmountpoints);			break;		case 'n':			if (mountpoints == NULL) {				fputs("This option is not valid when run "				    "without the -F flag.\n", stderr);				break;			}			mpcopy(tmpmountpoints, omountpoints);			mpcopy(omountpoints, mountpoints);			editor_name(&label, mountpoints, arg);			if (mpequal(omountpoints, tmpmountpoints))				mpcopy(omountpoints, tmpmountpoints);			break;		case 'p':			editor_display(&label, mountpoints, &freesectors,			    arg ? *arg : 0);			break;		case 'M': {			sig_t opipe = signal(SIGPIPE, SIG_IGN);			char *pager;			extern char manpage[];			if ((pager = getenv("PAGER")) == NULL || *pager == '\0')				pager = _PATH_LESS;			if ((fp = popen(pager, "w")) != NULL) {				(void) fwrite(manpage, strlen(manpage), 1, fp);				pclose(fp);			} else				warn("unable to execute %s", pager);			(void)signal(SIGPIPE, opipe);			break;		}		case 'q':			if (donothing) {				puts("In no change mode, not writing label.");				return(1);			}			/* Save mountpoint info if there is any. */			if (mountpoints != NULL)				mpsave(&label, mountpoints, dev, fstabfile);			if (memcmp(lp, &label, sizeof(label)) == 0) {				puts("No label changes.");				return(1);			}			do {				arg = getstring("Write new label?",				    "Write the modified label to disk?",				    "y");			} while (arg && tolower(*arg) != 'y' && tolower(*arg) != 'n');			if (arg && tolower(*arg) == 'y') {				if (writelabel(f, bootarea, &label) == 0) {					*lp = label;					return(0);				}				warnx("unable to write label");			}			return(1);			/* NOTREACHED */			break;		case 'r':		    /* Recalculate free space */		    editor_countfree(&label, &freesectors);		    puts("Recalculated free space.");		    break;		case 's':			if (arg == NULL) {				arg = getstring("Filename",				    "Name of the file to save label into.",				    NULL);				if (arg == NULL && *arg == '\0')					break;			}			if ((fp = fopen(arg, "w")) == NULL) {				warn("cannot open %s", arg);			} else {				display(fp, &label);				(void)fclose(fp);			}			break;		case 'u':			if (memcmp(&label, &lastlabel, sizeof(label)) == 0 &&			    mountpoints != NULL &&			    mpequal(mountpoints, omountpoints)) {				puts("Nothing to undo!");			} else {				tmplabel = label;				label = lastlabel;				lastlabel = tmplabel;				/* Recalculate free space */				editor_countfree(&label, &freesectors);				/* Restore mountpoints */				if (mountpoints != NULL)					mpcopy(mountpoints, omountpoints);				puts("Last change undone.");			}			break;		case 'w':			if (donothing)  {				puts("In no change mode, not writing label.");				break;			}			/* Save mountpoint info if there is any. */			if (mountpoints != NULL)				mpsave(&label, mountpoints, dev, fstabfile);			/* Save label if it has changed. */			if (memcmp(lp, &label, sizeof(label)) == 0)				puts("No label changes.");			else if (writelabel(f, bootarea, &label) != 0)				warnx("unable to write label");			else				*lp = label;			break;		case 'X':			expert = !expert;			printf("%s expert mode\n", expert ? "Entering" :			    "Exiting");			break;		case 'x':			return(1);			break;		case 'z':			tmplabel = lastlabel;			lastlabel = label;			zero_partitions(&label, &freesectors);			break;		case '\n':			break;		default:			printf("Unknown option: %c ('?' for help)\n", *cmd);			break;		}	}}/* * Add a new partition. */voideditor_add(lp, mp, freep, p)	struct disklabel *lp;	char **mp;	u_int32_t *freep;	char *p;{	struct partition *pp;	struct diskchunk *chunks;	char buf[BUFSIZ];	int i, partno;	u_int32_t ui, old_offset, old_size;	/* XXX - prompt user to steal space from another partition instead */	if (*freep == 0) {		fputs("No space left, you need to shrink a partition\n",		    stderr);		return;	}	/* XXX - make more like other editor_* */	if (p != NULL) {		partno = p[0] - 'a';		if (partno < 0 || partno == RAW_PART ||		    partno >= MAXPARTITIONS) {			fprintf(stderr,			    "Partition must be between 'a' and '%c' "			    "(excluding 'c').\n", 'a' + MAXPARTITIONS - 1);			return;		} else if (lp->d_partitions[partno].p_fstype != FS_UNUSED &&		    lp->d_partitions[partno].p_size != 0) {			fprintf(stderr,			    "Partition '%c' exists.  Delete it first.\n",			    p[0]);			return;		}	} else {		/* Find first unused partition that is not 'c' */		for (partno = 0; partno < MAXPARTITIONS; partno++, p++) {			if (lp->d_partitions[partno].p_size == 0 &&			    partno != RAW_PART)				break;		}		if (partno < MAXPARTITIONS) {			buf[0] = partno + 'a';			buf[1] = '\0';			p = &buf[0];		} else			p = NULL;		for (;;) {			p = getstring("partition",			    "The letter of the new partition, a - p.", p);			if (p == NULL)				return;			partno = p[0] - 'a';			if (lp->d_partitions[partno].p_fstype != FS_UNUSED &&			    lp->d_partitions[partno].p_size != 0) {				fprintf(stderr,				    "Partition '%c' already exists.\n", p[0]);			} else if (partno >= 0 && partno < MAXPARTITIONS)				break;			fprintf(stderr,			    "Partition must be between 'a' and '%c'.\n",			    'a' + MAXPARTITIONS - 1);		}	}	/* Increase d_npartitions if necesary */	if (partno >= lp->d_npartitions)		lp->d_npartitions = partno + 1;	/* Set defaults */	pp = &lp->d_partitions[partno];	if (partno >= lp->d_npartitions)		lp->d_npartitions = partno + 1;	memset(pp, 0, sizeof(*pp));	pp->p_size = *freep;	pp->p_offset = next_offset(lp, &pp->p_size);	pp->p_fstype = partno == 1 ? FS_SWAP : FS_BSDFFS;	pp->p_fsize = 1024;	pp->p_frag = 8;	pp->p_cpg = 16;	old_offset = pp->p_offset;	old_size = pp->p_size;getoff1:	/* Get offset */	if (get_offset(lp, partno) != 0) {		pp->p_size = 0;			/* effective delete */		return;	}	/* Recompute recommended size based on new offset */	ui = pp->p_fstype;	pp->p_fstype = FS_UNUSED;	chunks = free_chunks(lp);	for (i = 0; chunks[i].start != 0 || chunks[i].stop != 0; i++) {		if (pp->p_offset >= chunks[i].start &&		    pp->p_offset < chunks[i].stop) {			pp->p_size = chunks[i].stop - pp->p_offset;			break;		}	}	pp->p_fstype = ui;		/* Get size */	if (get_size(lp, partno, freep, 1) != 0 || pp->p_size == 0) {		pp->p_size = 0;			/* effective delete */		return;	}	/* Check for overlap */	if (has_overlap(lp, freep, 0)) {		printf("\nPlease re-enter an offset and size for partition "		    "%c.\n", 'a' + partno);		pp->p_offset = old_offset;		pp->p_size = old_size;		goto getoff1;		/* Yeah, I know... */	}	/* Get filesystem type and mountpoint */	if (get_fstype(lp, partno) != 0 || get_mp(lp, mp, partno) != 0) {		pp->p_size = 0;			/* effective delete */		return;	}	if (expert && pp->p_fstype == FS_BSDFFS) {		/* Get fsize, bsize, and cpg */		if (get_fsize(lp, partno) != 0 || get_bsize(lp, partno) != 0 ||		    get_cpg(lp, partno) != 0) {			pp->p_size = 0;			/* effective delete */			return;		}	}	/* Update free sector count and make sure things stay contiguous. */	*freep -= pp->p_size;	if (pp->p_size + pp->p_offset > ending_sector ||	    has_overlap(lp, freep, -1))		make_contiguous(lp);}/* * Set the mountpoint of an existing partition ('name'). */voideditor_name(lp, mp, p)	struct disklabel *lp;	char **mp;	char *p;{	struct partition *pp;	int partno;	/* Change which partition? */	if (p == NULL) {		p = getstring("partition to name",		    "The letter of the partition to name, a - p.", NULL);	}	if (p == NULL) {		fputs("Command aborted\n", stderr);		return;	}	partno = p[0] - 'a';	pp = &lp->d_partitions[partno];	if (partno < 0 || partno >= lp->d_npartitions) {		fprintf(stderr, "Partition must be between 'a' and '%c'.\n",		    'a' + lp->d_npartitions - 1);		return;	} else if (partno >= lp->d_npartitions ||	    (pp->p_fstype == FS_UNUSED && pp->p_size == 0)) {		fprintf(stderr, "Partition '%c' is not in use.\n", 'a' + partno);		return;	}		/* Not all fstypes can be named */	if (pp->p_fstype == FS_UNUSED || pp->p_fstype == FS_SWAP ||	    pp->p_fstype == FS_BOOT || pp->p_fstype == FS_OTHER) {		fprintf(stderr, "You cannot name a filesystem of type %s.\n",		    fstypenames[lp->d_partitions[partno].p_fstype]);		return;	}	get_mp(lp, mp, partno);}/* * Change an existing partition. */voideditor_modify(lp, mp, freep, p)	struct disklabel *lp;	char **mp;	u_int32_t *freep;	char *p;{	struct partition origpart, *pp;	int partno;	/* Change which partition? */	if (p == NULL) {		p = getstring("partition to modify",		    "The letter of the partition to modify, a - p.", NULL);	}	if (p == NULL) {		fputs("Command aborted\n", stderr);		return;	}	partno = p[0] - 'a';	pp = &lp->d_partitions[partno];	origpart = lp->d_partitions[partno];	if (partno < 0 || partno >= lp->d_npartitions) {		fprintf(stderr, "Partition must be between 'a' and '%c'.\n",		    'a' + lp->d_npartitions - 1);		return;	} else if (partno >= lp->d_npartitions ||	    (pp->p_fstype == FS_UNUSED && pp->p_size == 0)) {		fprintf(stderr, "Partition '%c' is not in use.\n", 'a' + partno);		return;	}		/* Get filesystem type */	if (get_fstype(lp, partno) != 0) {		*pp = origpart;			/* undo changes */		return;	}	/* Did they disable/enable the partition? */	if ((pp->p_fstype == FS_UNUSED || pp->p_fstype == FS_BOOT) &&	    origpart.p_fstype != FS_UNUSED && origpart.p_fstype != FS_BOOT)		*freep += origpart.p_size;	else if (pp->p_fstype != FS_UNUSED && pp->p_fstype != FS_BOOT &&	    (origpart.p_fstype == FS_UNUSED || origpart.p_fstype == FS_BOOT)) {		if (pp->p_size > *freep) {			fprintf(stderr,			    "Warning, need %u sectors but there are only %u "			    "free.  Setting size to %u.\n", pp->p_size, *freep,			    *freep);			pp->p_fstype = *freep;			*freep = 0;		} else			*freep -= pp->p_size;		/* have enough space */	}getoff2:	/* Get offset */	if (get_offset(lp, partno) != 0) {		*pp = origpart;			/* undo changes */		return;	}	/* Get size */	if (get_size(lp, partno, freep, 0) != 0 || pp->p_size == 0) {		pp->p_size = 0;			/* effective delete */		return;	}	/* Check for overlap and restore if not resolved */	if (has_overlap(lp, freep, 0)) {		puts("\nPlease re-enter an offset and size");		pp->p_offset = origpart.p_offset;		pp->p_size = origpart.p_size;		goto getoff2;		/* Yeah, I know... */	}	/* get mount point */	if (get_mp(lp, mp, partno) != 0) {		*pp = origpart;			/* undo changes */		return;	}	if (expert && (pp->p_fstype == FS_BSDFFS || pp->p_fstype == FS_UNUSED)){		/* get fsize */		if (get_fsize(lp, partno) != 0) {			*pp = origpart;		/* undo changes */			return;		}		/* get bsize */		if (get_bsize(lp, partno) != 0) {			*pp = origpart;		/* undo changes */			return;		}		if (pp->p_fstype == FS_BSDFFS) {			/* get cpg */			if (get_cpg(lp, partno) != 0) {				*pp = origpart;	/* undo changes */				return;			}		}	}	/* Make sure things stay contiguous. */	if (pp->p_size + pp->p_offset > ending_sector ||	    has_overlap(lp, freep, -1))		make_contiguous(lp);}/* * Delete an existing partition. */voideditor_delete(lp, mp, freep, p)	struct disklabel *lp;	char **mp;	u_int32_t *freep;	char *p;{	int c;	if (p == NULL) {		p = getstring("partition to delete",		    "The letter of the partition to delete, a - p, or '*'.",		    NULL);	}	if (p == NULL) {		fputs("Command aborted\n", stderr);		return;	}	if (p[0] == '*') {		for (c = 0; c < lp->d_npartitions; c++) {			if (c == RAW_PART)				continue;			/* Update free sector count. */			if (lp->d_partitions[c].p_fstype != FS_UNUSED &&			    lp->d_partitions[c].p_fstype != FS_BOOT &&			    lp->d_partitions[c].p_size != 0)				*freep += lp->d_partitions[c].p_size;			(void)memset(&lp->d_partitions[c], 0,			    sizeof(lp->d_partitions[c]));		}		return;	}	c = p[0] - 'a';	if (c < 0 || c >= lp->d_npartitions)		fprintf(stderr, "Partition must be between 'a' and '%c'.\n",		    'a' + lp->d_npartitions - 1);	else if (c >= lp->d_npartitions || (lp->d_partitions[c].p_fstype ==	    FS_UNUSED && lp->d_partitions[c].p_size == 0))		fprintf(stderr, "Partition '%c' is not in use.\n", 'a' + c);	else if (c == RAW_PART)		fputs("You may not delete the 'c' partition.  The 'c' partition must exist and\n""should span the entire disk.  By default it is of type 'unused' and so\n""does not take up any space.\n", stderr);	else {		/* Update free sector count. */		if (lp->d_partitions[c].p_offset < ending_sector &&		    lp->d_partitions[c].p_offset >= starting_sector &&		    lp->d_partitions[c].p_fstype != FS_UNUSED &&		    lp->d_partitions[c].p_fstype != FS_BOOT &&		    lp->d_partitions[c].p_size != 0)			*freep += lp->d_partitions[c].p_size;		/* Really delete it (as opposed to just setting to "unused") */		(void)memset(&lp->d_partitions[c], 0,		    sizeof(lp->d_partitions[c]));	}	if (mp != NULL && mp[c] != NULL) {		free(mp[c]);		mp[c] = NULL;	}}/* * Simplified display() for use with the builtin editor. */voideditor_display(lp, mp, freep, unit)	struct disklabel *lp;	char **mp;	u_int32_t *freep;	char unit;{	int i;	int width;	printf("device: %s\n", specname);	printf("type: %s\n", dktypenames[lp->d_type]);	printf("disk: %.*s\n", (int)sizeof(lp->d_typename), lp->d_typename);	printf("label: %.*s\n", (int)sizeof(lp->d_packname), lp->d_packname);	printf("bytes/sector: %ld\n", (long)lp->d_secsize);	printf("sectors/track: %ld\n", (long)lp->d_nsectors);	printf("tracks/cylinder: %ld\n", (long)lp->d_ntracks);	printf("sectors/cylinder: %ld\n", (long)lp->d_secpercyl);	printf("cylinders: %ld\n", (long)lp->d_ncylinders);	printf("total sectors: %ld\n", (long)lp->d_secperunit);	printf("free sectors: %u\n", *freep);	printf("rpm: %ld\n", (long)lp->d_rpm);	printf("\n%d partitions:\n", lp->d_npartitions);	width = width_partition(lp, unit);	printf("#    %*.*s %*.*s    fstype   [fsize bsize   cpg]\n",		width, width, "size", width, width, "offset");	for (i = 0; i < lp->d_npartitions; i++)		display_partition(stdout, lp, mp, i, unit, width);}/* * Find the next reasonable starting offset and returns it. * Assumes there is a least one free sector left (returns 0 if not). */u_int32_tnext_offset(lp, sizep)	struct disklabel *lp;	u_int32_t *sizep;{	struct partition **spp;	struct diskchunk *chunks;	u_int16_t npartitions;	u_int32_t new_offset, new_size;	int i, good_offset;	/* Get a sorted list of the partitions */	if ((spp = sort_partitions(lp, &npartitions)) == NULL)		return(starting_sector);		new_offset = starting_sector;	for (i = 0; i < npartitions; i++ ) {		/*		 * Is new_offset inside this partition?  If so,		 * make it the next sector after the partition ends.		 */		if (spp[i]->p_offset + spp[i]->p_size < ending_sector &&		    ((new_offset >= spp[i]->p_offset &&		    new_offset < spp[i]->p_offset + spp[i]->p_size) ||		    (new_offset + *sizep >= spp[i]->p_offset && new_offset		    + *sizep <= spp[i]->p_offset + spp[i]->p_size)))			new_offset = spp[i]->p_offset + spp[i]->p_size;	}	/* Did we find a suitable offset? */	for (good_offset = 1, i = 0; i < npartitions; i++ ) {		if (new_offset + *sizep >= spp[i]->p_offset &&		    new_offset + *sizep <= spp[i]->p_offset + spp[i]->p_size) {			/* Nope */			good_offset = 0;			break;		}	}	/* Specified size is too big, find something that fits */	if (!good_offset) {		chunks = free_chunks(lp);		new_size = 0;		for (i = 0; chunks[i].start != 0 || chunks[i].stop != 0; i++) {			if (chunks[i].stop - chunks[i].start > new_size) {			    new_size = chunks[i].stop - chunks[i].start;			    new_offset = chunks[i].start;			}		}		/* XXX - should do something intelligent if new_size == 0 */		*sizep = new_size;	}	(void)free(spp);	return(new_offset);}/* * Change the size of an existing partition. */voideditor_change(lp, freep, p)	struct disklabel *lp;	u_int32_t *freep;	char *p;{	int partno;	u_int32_t newsize;	struct partition *pp;	if (p == NULL) {		p = getstring("partition to change size",		    "The letter of the partition to change size, a - p.", NULL);	}	if (p == NULL) {		fputs("Command aborted\n", stderr);		return;	}	partno = p[0] - 'a';	if (partno < 0 || partno >= lp->d_npartitions) {		fprintf(stderr, "Partition must be between 'a' and '%c'.\n",		    'a' + lp->d_npartitions - 1);		return;	} else if (partno >= lp->d_npartitions ||	    lp->d_partitions[partno].p_size == 0) {		fprintf(stderr, "Partition '%c' is not in use.\n", 'a' + partno);		return;	}	pp = &lp->d_partitions[partno];	printf("Partition %c is currently %u sectors in size (%u free).\n",	    partno + 'a', pp->p_size, *freep);	/* XXX - make maxsize lp->d_secperunit if FS_UNUSED/FS_BOOT? */	newsize = getuint(lp, partno, "new size", "Size of the partition.  "	    "You may also say +/- amount for a relative change.",	    pp->p_size, pp->p_size + *freep, pp->p_offset, DO_CONVERSIONS |	    (pp->p_fstype == FS_BSDFFS ? DO_ROUNDING : 0));	if (newsize == UINT_MAX - 1) {		fputs("Command aborted\n", stderr);		return;	} else if (newsize == UINT_MAX) {		fputs("Invalid entry\n", stderr);		return;	} else if (newsize == pp->p_size)		return;	if (pp->p_fstype != FS_UNUSED && pp->p_fstype != FS_BOOT) {		if (newsize > pp->p_size) {			if (newsize - pp->p_size > *freep) {				fprintf(stderr,				    "Only %u sectors free, you asked for %u\n",				    *freep, newsize - pp->p_size);				return;			}			*freep -= newsize - pp->p_size;		} else if (newsize < pp->p_size) {			*freep += pp->p_size - newsize;		}	} else {		if (partno == RAW_PART && newsize +		    pp->p_offset > lp->d_secperunit) {			fputs("'c' partition may not be larger than the disk\n",			    stderr);			return;		}	}	pp->p_size = newsize;	if (newsize + pp->p_offset > ending_sector ||	    has_overlap(lp, freep, -1))		make_contiguous(lp);}voidmake_contiguous(lp)	struct disklabel *lp;{	struct partition **spp;	u_int16_t npartitions;	int i;	/* Get a sorted list of the partitions */	if ((spp = sort_partitions(lp, &npartitions)) == NULL)		return;	/*	 * Make everything contiguous but don't muck with start of the first one	 * or partitions not in the BSD part of the label.	 */	for (i = 1; i < npartitions; i++) {		if (spp[i]->p_offset >= starting_sector ||		    spp[i]->p_offset < ending_sector)			spp[i]->p_offset =			    spp[i - 1]->p_offset + spp[i - 1]->p_size;	}	(void)free(spp);}/* * Sort the partitions based on starting offset. * This assumes there can be no overlap. */intpartition_cmp(e1, e2)	const void *e1, *e2;{	struct partition *p1 = *(struct partition **)e1;	struct partition *p2 = *(struct partition **)e2;	return((int)(p1->p_offset - p2->p_offset));}char *getstring(prompt, helpstring, oval)	char *prompt;	char *helpstring;	char *oval;{	static char buf[BUFSIZ];	int n;	buf[0] = '\0';	do {		printf("%s: [%s] ", prompt, oval ? oval : "");		if (fgets(buf, sizeof(buf), stdin) == NULL) {			buf[0] = '\0';			if (feof(stdin)) {				putchar('\n');				return(NULL);			}		}		n = strlen(buf);		if (n > 0 && buf[n-1] == '\n')			buf[--n] = '\0';		if (buf[0] == '?')			puts(helpstring);		else if (oval != NULL && buf[0] == '\0') {			(void)strncpy(buf, oval, sizeof(buf) - 1);			buf[sizeof(buf) - 1] = '\0';		}	} while (buf[0] == '?');	return(&buf[0]);}