Home | History | Annotate | Line # | Download | only in sysinst
label.c revision 1.10.2.6
      1  1.10.2.6    bouyer /*	$NetBSD: label.c,v 1.10.2.6 2020/10/15 19:36:51 bouyer Exp $	*/
      2       1.1  dholland 
      3       1.1  dholland /*
      4       1.1  dholland  * Copyright 1997 Jonathan Stone
      5       1.1  dholland  * All rights reserved.
      6       1.1  dholland  *
      7       1.1  dholland  * Redistribution and use in source and binary forms, with or without
      8       1.1  dholland  * modification, are permitted provided that the following conditions
      9       1.1  dholland  * are met:
     10       1.1  dholland  * 1. Redistributions of source code must retain the above copyright
     11       1.1  dholland  *    notice, this list of conditions and the following disclaimer.
     12       1.1  dholland  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.1  dholland  *    notice, this list of conditions and the following disclaimer in the
     14       1.1  dholland  *    documentation and/or other materials provided with the distribution.
     15       1.1  dholland  * 3. All advertising materials mentioning features or use of this software
     16       1.1  dholland  *    must display the following acknowledgement:
     17       1.1  dholland  *      This product includes software developed for the NetBSD Project by
     18       1.1  dholland  *      Jonathan Stone.
     19       1.1  dholland  * 4. The name of Jonathan Stone may not be used to endorse
     20       1.1  dholland  *    or promote products derived from this software without specific prior
     21       1.1  dholland  *    written permission.
     22       1.1  dholland  *
     23       1.1  dholland  * THIS SOFTWARE IS PROVIDED BY JONATHAN STONE ``AS IS''
     24       1.1  dholland  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25       1.1  dholland  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26       1.1  dholland  * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
     27       1.1  dholland  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     28       1.1  dholland  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     29       1.1  dholland  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     30       1.1  dholland  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     31       1.1  dholland  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     32       1.1  dholland  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     33       1.1  dholland  * THE POSSIBILITY OF SUCH DAMAGE.
     34       1.1  dholland  *
     35       1.1  dholland  */
     36       1.1  dholland 
     37       1.1  dholland #include <sys/cdefs.h>
     38       1.1  dholland #if defined(LIBC_SCCS) && !defined(lint)
     39  1.10.2.6    bouyer __RCSID("$NetBSD: label.c,v 1.10.2.6 2020/10/15 19:36:51 bouyer Exp $");
     40       1.1  dholland #endif
     41       1.1  dholland 
     42       1.1  dholland #include <sys/types.h>
     43       1.1  dholland #include <stddef.h>
     44       1.7    martin #include <assert.h>
     45       1.1  dholland #include <errno.h>
     46       1.1  dholland #include <stdio.h>
     47       1.1  dholland #include <fcntl.h>
     48       1.1  dholland #include <util.h>
     49       1.1  dholland #include <unistd.h>
     50       1.1  dholland #include <sys/dkio.h>
     51       1.1  dholland #include <sys/param.h>
     52       1.7    martin #include <sys/bootblock.h>
     53  1.10.2.6    bouyer #include <sys/bitops.h>
     54       1.1  dholland #include <ufs/ffs/fs.h>
     55       1.1  dholland 
     56       1.1  dholland #include "defs.h"
     57       1.1  dholland #include "msg_defs.h"
     58       1.1  dholland #include "menu_defs.h"
     59       1.1  dholland 
     60       1.1  dholland /*
     61       1.1  dholland  * local prototypes
     62       1.1  dholland  */
     63       1.7    martin static bool boringpart(const struct disk_part_info *info);
     64  1.10.2.4   msaitoh static bool checklabel(struct disk_partitions*, char *, char *);
     65       1.7    martin static void show_partition_adder(menudesc *, struct partition_usage_set*);
     66       1.1  dholland 
     67       1.1  dholland /*
     68       1.7    martin  * Return 1 if a partition should be ignored when checking
     69       1.1  dholland  * for overlapping partitions.
     70       1.1  dholland  */
     71       1.7    martin static bool
     72       1.7    martin boringpart(const struct disk_part_info *info)
     73       1.1  dholland {
     74       1.1  dholland 
     75       1.7    martin 	if (info->size == 0)
     76       1.7    martin 		return true;
     77       1.7    martin 	if (info->flags &
     78       1.7    martin 	     (PTI_PSCHEME_INTERNAL|PTI_WHOLE_DISK|PTI_SEC_CONTAINER|
     79       1.7    martin 	     PTI_RAW_PART))
     80       1.7    martin 		return true;
     81       1.7    martin 
     82       1.7    martin 	return false;
     83       1.1  dholland }
     84       1.1  dholland 
     85       1.7    martin /*
     86       1.7    martin  * We have some partitions in our "wanted" list that we may not edit,
     87       1.7    martin  * like the RAW_PART in disklabel, some that just represent external
     88       1.7    martin  * mount entries for the final fstab or similar.
     89       1.7    martin  * We have previously sorted pset->parts and pset->infos to be in sync,
     90       1.7    martin  * but the former "array" may be shorter.
     91       1.7    martin  * Here are a few quick predicates to check for them.
     92       1.7    martin  */
     93       1.7    martin static bool
     94       1.7    martin real_partition(const struct partition_usage_set *pset, int index)
     95       1.7    martin {
     96       1.7    martin 	if (index < 0 || (size_t)index >= pset->num)
     97       1.7    martin 		return false;
     98       1.1  dholland 
     99       1.7    martin 	return pset->infos[index].cur_part_id != NO_PART;
    100       1.7    martin }
    101       1.1  dholland 
    102       1.1  dholland /*
    103       1.7    martin  * Check partitioning for overlapping partitions.
    104       1.1  dholland  * Returns 0 if no overlapping partition found, nonzero otherwise.
    105       1.1  dholland  * Sets reference arguments ovly1 and ovly2 to the indices of
    106       1.1  dholland  * overlapping partitions if any are found.
    107       1.1  dholland  */
    108       1.7    martin static bool
    109       1.7    martin checklabel(struct disk_partitions *parts,
    110  1.10.2.4   msaitoh     char *ovl1, char *ovl2)
    111       1.7    martin {
    112       1.7    martin 	part_id i, j;
    113       1.7    martin 	struct disk_part_info info;
    114       1.7    martin 	daddr_t istart, iend, jstart, jend;
    115       1.7    martin 	unsigned int fs_type, fs_sub_type;
    116       1.1  dholland 
    117       1.7    martin 	for (i = 0; i < parts->num_part - 1; i ++ ) {
    118       1.7    martin 		if (!parts->pscheme->get_part_info(parts, i, &info))
    119       1.7    martin 			continue;
    120       1.1  dholland 
    121       1.1  dholland 		/* skip unused or reserved partitions */
    122       1.7    martin 		if (boringpart(&info))
    123       1.1  dholland 			continue;
    124       1.1  dholland 
    125       1.1  dholland 		/*
    126       1.1  dholland 		 * check succeeding partitions for overlap.
    127  1.10.2.4   msaitoh 		 * O(n^2), but n is small.
    128       1.1  dholland 		 */
    129       1.7    martin 		istart = info.start;
    130       1.7    martin 		iend = istart + info.size;
    131       1.7    martin 		fs_type = info.fs_type;
    132       1.7    martin 		fs_sub_type = info.fs_sub_type;
    133       1.1  dholland 
    134       1.7    martin 		for (j = i+1; j < parts->num_part; j++) {
    135       1.7    martin 
    136       1.7    martin 			if (!parts->pscheme->get_part_info(parts, j, &info))
    137       1.7    martin 				continue;
    138       1.1  dholland 
    139       1.1  dholland 			/* skip unused or reserved partitions */
    140       1.7    martin 			if (boringpart(&info))
    141       1.1  dholland 				continue;
    142       1.1  dholland 
    143       1.7    martin 			jstart = info.start;
    144       1.7    martin 			jend = jstart + info.size;
    145       1.1  dholland 
    146       1.1  dholland 			/* overlap? */
    147       1.7    martin 			if ((istart <= jstart && jstart < iend) ||
    148       1.7    martin 			    (jstart <= istart && istart < jend)) {
    149  1.10.2.4   msaitoh 				snprintf(ovl1, MENUSTRSIZE,
    150       1.7    martin 				    "%" PRIu64 " - %" PRIu64 " %s, %s",
    151       1.7    martin 				    istart / sizemult, iend / sizemult,
    152       1.7    martin 				    multname,
    153       1.7    martin 				    getfslabelname(fs_type, fs_sub_type));
    154  1.10.2.4   msaitoh 				snprintf(ovl2, MENUSTRSIZE,
    155       1.7    martin 				    "%" PRIu64 " - %" PRIu64 " %s, %s",
    156       1.7    martin 				    jstart / sizemult, jend / sizemult,
    157       1.7    martin 				    multname,
    158       1.7    martin 				    getfslabelname(info.fs_type,
    159       1.7    martin 				        info.fs_sub_type));
    160       1.7    martin 				return false;
    161       1.1  dholland 			}
    162       1.1  dholland 		}
    163       1.1  dholland 	}
    164       1.1  dholland 
    165       1.7    martin 	return true;
    166       1.1  dholland }
    167       1.1  dholland 
    168       1.2    martin int
    169       1.7    martin checkoverlap(struct disk_partitions *parts)
    170       1.2    martin {
    171       1.7    martin 	char desc1[MENUSTRSIZE], desc2[MENUSTRSIZE];
    172       1.7    martin 	if (!checklabel(parts, desc1, desc2)) {
    173       1.7    martin 		msg_display_subst(MSG_partitions_overlap, 2, desc1, desc2);
    174       1.2    martin 		return 1;
    175       1.2    martin 	}
    176       1.2    martin 	return 0;
    177       1.2    martin }
    178       1.2    martin 
    179       1.7    martin /*
    180       1.7    martin  * return (see post_edit_verify):
    181       1.7    martin  *  0 -> abort
    182       1.7    martin  *  1 -> re-edit
    183       1.7    martin  *  2 -> continue installation
    184       1.7    martin  */
    185       1.1  dholland static int
    186  1.10.2.5   msaitoh verify_parts(struct partition_usage_set *pset, bool install)
    187       1.1  dholland {
    188       1.7    martin 	struct part_usage_info *wanted;
    189       1.7    martin 	struct disk_partitions *parts;
    190       1.7    martin 	size_t i, num_root;
    191  1.10.2.6    bouyer 	daddr_t first_bsdstart, inst_start;
    192       1.7    martin 	int rv;
    193       1.7    martin 
    194  1.10.2.6    bouyer 	first_bsdstart = inst_start = -1;
    195       1.7    martin 	num_root = 0;
    196       1.7    martin 	parts = pset->parts;
    197       1.7    martin 	for (i = 0; i < pset->num; i++) {
    198       1.7    martin 		wanted = &pset->infos[i];
    199       1.1  dholland 
    200       1.7    martin 		if (wanted->flags & PUIFLG_JUST_MOUNTPOINT)
    201       1.1  dholland 			continue;
    202       1.7    martin 		if (wanted->cur_part_id == NO_PART)
    203       1.7    martin 			continue;
    204       1.7    martin 		if (!(wanted->instflags & PUIINST_MOUNT))
    205       1.1  dholland 			continue;
    206       1.7    martin 		if (strcmp(wanted->mount, "/") != 0)
    207       1.1  dholland 			continue;
    208       1.7    martin 		num_root++;
    209       1.7    martin 
    210  1.10.2.6    bouyer 		if (first_bsdstart <= 0) {
    211       1.7    martin 			first_bsdstart = wanted->cur_start;
    212       1.7    martin 		}
    213  1.10.2.6    bouyer 		if (inst_start < 0 &&
    214  1.10.2.6    bouyer 		    (wanted->cur_flags & PTI_INSTALL_TARGET)) {
    215       1.7    martin 			inst_start = wanted->cur_start;
    216       1.7    martin 		}
    217       1.7    martin 	}
    218       1.7    martin 
    219  1.10.2.5   msaitoh 	if ((num_root == 0 && install) ||
    220  1.10.2.6    bouyer 	    (num_root > 1 && inst_start < 0)) {
    221  1.10.2.5   msaitoh 		if (num_root == 0 && install)
    222       1.7    martin 			msg_display_subst(MSG_must_be_one_root, 2,
    223       1.7    martin 			    msg_string(parts->pscheme->name),
    224       1.7    martin 			    msg_string(parts->pscheme->short_name));
    225       1.7    martin 		else
    226       1.7    martin 			msg_display_subst(MSG_multbsdpart, 2,
    227       1.7    martin 			    msg_string(parts->pscheme->name),
    228       1.7    martin 			    msg_string(parts->pscheme->short_name));
    229       1.7    martin 		rv = ask_reedit(parts);
    230       1.7    martin 		if (rv != 2)
    231       1.7    martin 			return rv;
    232       1.7    martin 	}
    233       1.7    martin 
    234       1.7    martin 	/* Check for overlaps */
    235       1.7    martin 	if (checkoverlap(parts) != 0) {
    236       1.7    martin 		rv = ask_reedit(parts);
    237       1.7    martin 		if (rv != 2)
    238       1.7    martin 			return rv;
    239       1.1  dholland 	}
    240       1.7    martin 
    241       1.7    martin 	/*
    242       1.7    martin 	 * post_edit_verify returns:
    243       1.7    martin 	 *  0 -> abort
    244       1.7    martin 	 *  1 -> re-edit
    245       1.7    martin 	 *  2 -> continue installation
    246       1.7    martin 	 */
    247       1.7    martin 	if (parts->pscheme->post_edit_verify)
    248       1.7    martin 		return parts->pscheme->post_edit_verify(parts, false);
    249       1.7    martin 
    250       1.7    martin 	return 2;
    251       1.1  dholland }
    252       1.1  dholland 
    253       1.1  dholland static int
    254       1.1  dholland edit_fs_start(menudesc *m, void *arg)
    255       1.1  dholland {
    256       1.7    martin 	struct single_part_fs_edit *edit = arg;
    257       1.7    martin 	daddr_t start, end;
    258       1.1  dholland 
    259       1.7    martin 	start = getpartoff(edit->pset->parts, edit->info.start);
    260       1.7    martin 	if (edit->info.size != 0) {
    261       1.1  dholland 		/* Try to keep end in the same place */
    262       1.7    martin 		end = edit->info.start + edit->info.size;
    263       1.1  dholland 		if (end < start)
    264       1.7    martin 			edit->info.size = edit->pset->parts->pscheme->
    265       1.7    martin 			    max_free_space_at(edit->pset->parts,
    266       1.7    martin 			    edit->info.start);
    267       1.1  dholland 		else
    268       1.7    martin 			edit->info.size = end - start;
    269       1.1  dholland 	}
    270       1.7    martin 	edit->info.start = start;
    271       1.1  dholland 	return 0;
    272       1.1  dholland }
    273       1.1  dholland 
    274       1.1  dholland static int
    275       1.1  dholland edit_fs_size(menudesc *m, void *arg)
    276       1.1  dholland {
    277       1.7    martin 	struct single_part_fs_edit *edit = arg;
    278  1.10.2.6    bouyer 	struct disk_part_info pinfo;
    279       1.7    martin 	daddr_t size;
    280       1.1  dholland 
    281  1.10.2.6    bouyer 	/* get original partition data, in case start moved already */
    282  1.10.2.6    bouyer 	edit->pset->parts->pscheme->get_part_info(edit->pset->parts,
    283  1.10.2.6    bouyer 	    edit->id, &pinfo);
    284  1.10.2.6    bouyer 	/* ask for new size with old start and current values */
    285  1.10.2.6    bouyer 	size = getpartsize(edit->pset->parts, pinfo.start,
    286  1.10.2.6    bouyer 	    edit->info.start, edit->info.size);
    287       1.7    martin 	if (size < 0)
    288       1.7    martin 		return 0;
    289       1.7    martin 	if (size > edit->pset->parts->disk_size)
    290       1.7    martin 		size = edit->pset->parts->disk_size - edit->info.start;
    291       1.7    martin 	edit->info.size = size;
    292       1.1  dholland 	return 0;
    293       1.1  dholland }
    294       1.1  dholland 
    295       1.7    martin static int
    296  1.10.2.6    bouyer set_ffs_opt_pow2(menudesc *m, void *arg)
    297  1.10.2.6    bouyer {
    298  1.10.2.6    bouyer 	struct single_part_fs_edit *edit = arg;
    299  1.10.2.6    bouyer 	size_t val = 1 << (edit->offset+m->cursel);
    300  1.10.2.6    bouyer 
    301  1.10.2.6    bouyer 	if (edit->mode == 1) {
    302  1.10.2.6    bouyer 		edit->info.fs_opt1 = val;
    303  1.10.2.6    bouyer 		edit->wanted->fs_opt1 = val;
    304  1.10.2.6    bouyer 	} else if (edit->mode == 2) {
    305  1.10.2.6    bouyer 		edit->info.fs_opt2 = val;
    306  1.10.2.6    bouyer 		edit->wanted->fs_opt2 = val;
    307  1.10.2.6    bouyer 	}
    308  1.10.2.6    bouyer 	return 0;
    309  1.10.2.6    bouyer }
    310  1.10.2.6    bouyer 
    311  1.10.2.6    bouyer static int
    312  1.10.2.6    bouyer edit_fs_ffs_opt(menudesc *m, void *arg, msg head,
    313  1.10.2.6    bouyer     size_t min_val, size_t max_val)
    314  1.10.2.6    bouyer {
    315  1.10.2.6    bouyer 	struct single_part_fs_edit *edit = arg;
    316  1.10.2.6    bouyer 	menu_ent opts[min(MAXPHYS/4096, 8)];
    317  1.10.2.6    bouyer 	char names[min(MAXPHYS/4096, 8)][20];
    318  1.10.2.6    bouyer 	size_t i, val;
    319  1.10.2.6    bouyer 	int menu;
    320  1.10.2.6    bouyer 
    321  1.10.2.6    bouyer 	edit->offset = ilog2(min_val);
    322  1.10.2.6    bouyer 	memset(opts, 0, sizeof opts);
    323  1.10.2.6    bouyer 	for (i = 0, val = min_val; val <= max_val; i++, val <<= 1) {
    324  1.10.2.6    bouyer 		snprintf(names[i], sizeof names[i], "%zu", val);
    325  1.10.2.6    bouyer 		opts[i].opt_name = names[i];
    326  1.10.2.6    bouyer 		opts[i].opt_action = set_ffs_opt_pow2;
    327  1.10.2.6    bouyer 		opts[i].opt_flags = OPT_EXIT;
    328  1.10.2.6    bouyer 	}
    329  1.10.2.6    bouyer 	menu = new_menu(head, opts, i, 40, 6, 0, 0, MC_NOEXITOPT,
    330  1.10.2.6    bouyer 	    NULL, NULL, NULL, NULL, NULL);
    331  1.10.2.6    bouyer 	if (menu < 0)
    332  1.10.2.6    bouyer 		return 1;
    333  1.10.2.6    bouyer 	process_menu(menu, arg);
    334  1.10.2.6    bouyer 	free_menu(menu);
    335  1.10.2.6    bouyer 	return 0;
    336  1.10.2.6    bouyer }
    337  1.10.2.6    bouyer 
    338  1.10.2.6    bouyer static int
    339  1.10.2.6    bouyer edit_fs_ffs_block(menudesc *m, void *arg)
    340  1.10.2.6    bouyer {
    341  1.10.2.6    bouyer 	struct single_part_fs_edit *edit = arg;
    342  1.10.2.6    bouyer 
    343  1.10.2.6    bouyer 	edit->mode = 1;		/* edit fs_opt1 */
    344  1.10.2.6    bouyer 	return edit_fs_ffs_opt(m, arg, MSG_Select_file_system_block_size,
    345  1.10.2.6    bouyer 	    4096, MAXPHYS);
    346  1.10.2.6    bouyer }
    347  1.10.2.6    bouyer 
    348  1.10.2.6    bouyer static int
    349  1.10.2.6    bouyer edit_fs_ffs_frag(menudesc *m, void *arg)
    350  1.10.2.6    bouyer {
    351  1.10.2.6    bouyer 	struct single_part_fs_edit *edit = arg;
    352  1.10.2.6    bouyer 	size_t bsize, sec_size;
    353  1.10.2.6    bouyer 
    354  1.10.2.6    bouyer 	edit->mode = 2;		/* edit fs_opt2 */
    355  1.10.2.6    bouyer 	bsize = edit->info.fs_opt1;
    356  1.10.2.6    bouyer 	if (bsize == 0) {
    357  1.10.2.6    bouyer 		sec_size = edit->wanted->parts->bytes_per_sector;
    358  1.10.2.6    bouyer 		if (edit->wanted->size >= (daddr_t)(128L*(GIG/sec_size)))
    359  1.10.2.6    bouyer 			bsize = 32*1024;
    360  1.10.2.6    bouyer 		else if (edit->wanted->size >= (daddr_t)(1000L*(MEG/sec_size)))
    361  1.10.2.6    bouyer 			bsize = 16*1024;
    362  1.10.2.6    bouyer 		else if (edit->wanted->size >= (daddr_t)(20L*(MEG/sec_size)))
    363  1.10.2.6    bouyer 			bsize = 8*1024;
    364  1.10.2.6    bouyer 		else
    365  1.10.2.6    bouyer 			bsize = 4+1024;
    366  1.10.2.6    bouyer 	}
    367  1.10.2.6    bouyer 	return edit_fs_ffs_opt(m, arg, MSG_Select_file_system_fragment_size,
    368  1.10.2.6    bouyer 		bsize / 8, bsize);
    369  1.10.2.6    bouyer }
    370  1.10.2.6    bouyer 
    371  1.10.2.6    bouyer static int
    372  1.10.2.6    bouyer edit_fs_ffs_avg_size(menudesc *m, void *arg)
    373  1.10.2.6    bouyer {
    374  1.10.2.6    bouyer 	struct single_part_fs_edit *edit = arg;
    375  1.10.2.6    bouyer 	char answer[12];
    376  1.10.2.6    bouyer 
    377  1.10.2.6    bouyer 	snprintf(answer, sizeof answer, "%u", edit->info.fs_opt3);
    378  1.10.2.6    bouyer 	msg_prompt_win(MSG_ptn_isize_prompt, -1, 18, 0, 0,
    379  1.10.2.6    bouyer 		answer, answer, sizeof answer);
    380  1.10.2.6    bouyer 	edit->info.fs_opt3 = atol(answer);
    381  1.10.2.6    bouyer 	edit->wanted->fs_opt3 = edit->info.fs_opt3;
    382  1.10.2.6    bouyer 
    383  1.10.2.6    bouyer 	return 0;
    384  1.10.2.6    bouyer }
    385  1.10.2.6    bouyer 
    386  1.10.2.6    bouyer static int
    387       1.7    martin edit_fs_preserve(menudesc *m, void *arg)
    388       1.1  dholland {
    389       1.7    martin 	struct single_part_fs_edit *edit = arg;
    390       1.1  dholland 
    391       1.7    martin 	edit->wanted->instflags ^= PUIINST_NEWFS;
    392       1.7    martin 	return 0;
    393       1.1  dholland }
    394       1.1  dholland 
    395       1.7    martin static int
    396       1.7    martin edit_install(menudesc *m, void *arg)
    397       1.1  dholland {
    398       1.7    martin 	struct single_part_fs_edit *edit = arg;
    399       1.1  dholland 
    400  1.10.2.6    bouyer 	edit->info.flags ^= PTI_INSTALL_TARGET;
    401       1.7    martin 	return 0;
    402       1.1  dholland }
    403       1.1  dholland 
    404       1.7    martin static int
    405       1.7    martin edit_fs_mount(menudesc *m, void *arg)
    406       1.1  dholland {
    407       1.7    martin 	struct single_part_fs_edit *edit = arg;
    408       1.1  dholland 
    409       1.7    martin 	edit->wanted->instflags ^= PUIINST_MOUNT;
    410       1.7    martin 	return 0;
    411       1.1  dholland }
    412       1.1  dholland 
    413       1.1  dholland static int
    414       1.7    martin edit_fs_mountpt(menudesc *m, void *arg)
    415       1.1  dholland {
    416       1.7    martin 	struct single_part_fs_edit *edit = arg;
    417       1.7    martin 	char *p, *first, *last, buf[MOUNTLEN];
    418       1.7    martin 
    419       1.7    martin 	strlcpy(buf, edit->wanted->mount, sizeof buf);
    420       1.7    martin 	msg_prompt_win(MSG_mountpoint, -1, 18, 0, 0,
    421       1.7    martin 		buf, buf, MOUNTLEN);
    422       1.7    martin 
    423       1.7    martin 	/*
    424       1.7    martin 	 * Trim all leading and trailing whitespace
    425       1.7    martin 	 */
    426       1.7    martin 	for (first = NULL, last = NULL, p = buf; *p; p++) {
    427       1.7    martin 		if (isspace((unsigned char)*p))
    428       1.7    martin 			continue;
    429       1.7    martin 		if (first == NULL)
    430       1.7    martin 			first = p;
    431       1.7    martin 		last = p;
    432       1.7    martin 	}
    433       1.7    martin 	if (last != NULL)
    434       1.7    martin 		last[1] = 0;
    435       1.7    martin 
    436  1.10.2.6    bouyer 	if (first == NULL || *first == 0 || strcmp(first, "none") == 0) {
    437       1.7    martin 		edit->wanted->mount[0] = 0;
    438       1.7    martin 		edit->wanted->instflags &= ~PUIINST_MOUNT;
    439       1.7    martin 		return 0;
    440       1.7    martin 	}
    441       1.7    martin 
    442       1.7    martin 	if (*first != '/') {
    443       1.7    martin 		edit->wanted->mount[0] = '/';
    444       1.7    martin 		strlcpy(&edit->wanted->mount[1], first,
    445       1.7    martin 		    sizeof(edit->wanted->mount)-1);
    446       1.7    martin 	} else {
    447       1.7    martin 		strlcpy(edit->wanted->mount, first, sizeof edit->wanted->mount);
    448       1.7    martin 	}
    449       1.1  dholland 
    450       1.1  dholland 	return 0;
    451       1.7    martin }
    452       1.1  dholland 
    453       1.1  dholland static int
    454       1.7    martin edit_restore(menudesc *m, void *arg)
    455       1.1  dholland {
    456       1.7    martin 	struct single_part_fs_edit *edit = arg;
    457       1.1  dholland 
    458       1.7    martin 	edit->info = edit->old_info;
    459       1.7    martin 	*edit->wanted = edit->old_usage;
    460       1.1  dholland 	return 0;
    461       1.1  dholland }
    462       1.1  dholland 
    463       1.1  dholland static int
    464       1.7    martin edit_cancel(menudesc *m, void *arg)
    465       1.1  dholland {
    466       1.7    martin 	struct single_part_fs_edit *edit = arg;
    467       1.1  dholland 
    468       1.7    martin 	edit->rv = -1;
    469       1.7    martin 	return 1;
    470       1.1  dholland }
    471       1.1  dholland 
    472       1.1  dholland static int
    473       1.7    martin edit_delete_ptn(menudesc *m, void *arg)
    474       1.1  dholland {
    475       1.7    martin 	struct single_part_fs_edit *edit = arg;
    476       1.1  dholland 
    477       1.7    martin 	edit->rv = -2;
    478       1.7    martin 	return 1;
    479       1.7    martin }
    480       1.7    martin 
    481       1.7    martin /*
    482       1.7    martin  * We have added/removed partitions, all cur_part_id values are
    483       1.7    martin  * out of sync. Re-fetch and reorder partitions accordingly.
    484       1.7    martin  */
    485       1.7    martin static void
    486       1.7    martin renumber_partitions(struct partition_usage_set *pset)
    487       1.7    martin {
    488       1.7    martin 	struct part_usage_info *ninfos;
    489       1.7    martin 	struct disk_part_info info;
    490       1.7    martin 	size_t i;
    491       1.7    martin 	part_id pno;
    492       1.7    martin 
    493       1.7    martin 	ninfos = calloc(pset->parts->num_part, sizeof(*ninfos));
    494       1.7    martin 	if (ninfos == NULL) {
    495       1.7    martin 		err_msg_win(err_outofmem);
    496       1.7    martin 		return;
    497       1.7    martin 	}
    498       1.1  dholland 
    499       1.7    martin 	for (pno = 0; pno < pset->parts->num_part; pno++) {
    500       1.7    martin 		if (!pset->parts->pscheme->get_part_info(pset->parts, pno,
    501       1.7    martin 		    &info))
    502       1.7    martin 			continue;
    503       1.7    martin 		for (i = 0; i < pset->parts->num_part; i++) {
    504       1.7    martin 			if (pset->infos[i].cur_start != info.start)
    505       1.7    martin 				continue;
    506  1.10.2.5   msaitoh 			if (pset->infos[i].cur_flags != info.flags)
    507  1.10.2.5   msaitoh 				continue;
    508  1.10.2.6    bouyer 			if ((info.fs_type != FS_UNUSED &&
    509  1.10.2.6    bouyer 			    info.fs_type == pset->infos[i].fs_type) ||
    510  1.10.2.6    bouyer 			    (pset->infos[i].type ==
    511  1.10.2.6    bouyer 			    info.nat_type->generic_ptype)) {
    512  1.10.2.6    bouyer 				memcpy(&ninfos[pno], &pset->infos[i],
    513  1.10.2.6    bouyer 				    sizeof(ninfos[pno]));
    514  1.10.2.6    bouyer 				ninfos[pno].cur_part_id = pno;
    515  1.10.2.6    bouyer 				break;
    516  1.10.2.6    bouyer 			}
    517       1.7    martin 		}
    518       1.1  dholland 	}
    519       1.1  dholland 
    520       1.7    martin 	memcpy(pset->infos, ninfos, sizeof(*pset->infos)*pset->parts->num_part);
    521       1.7    martin 	free(ninfos);
    522       1.7    martin }
    523       1.7    martin 
    524       1.7    martin /*
    525       1.7    martin  * Most often used file system types, we offer them in a first level menu.
    526       1.7    martin  */
    527       1.7    martin static const uint edit_fs_common_types[] =
    528       1.7    martin     { FS_BSDFFS, FS_SWAP, FS_MSDOS, FS_BSDLFS, FS_EX2FS };
    529       1.7    martin 
    530       1.7    martin /*
    531       1.7    martin  * Functions for uncommon file system types - we offer the full list,
    532       1.7    martin  * but put FFSv2 and FFSv1 at the front.
    533       1.7    martin  */
    534       1.7    martin static void
    535       1.7    martin init_fs_type_ext(menudesc *menu, void *arg)
    536       1.7    martin {
    537       1.7    martin 	struct single_part_fs_edit *edit = arg;
    538       1.7    martin 	uint t = edit->info.fs_type;
    539       1.7    martin 	size_t i, ndx, max = menu->numopts;
    540       1.7    martin 
    541       1.7    martin 	if (t == FS_BSDFFS) {
    542       1.7    martin 		if (edit->info.fs_sub_type == 2)
    543       1.7    martin 			menu->cursel = 0;
    544       1.7    martin 		else
    545       1.7    martin 			menu->cursel = 1;
    546       1.7    martin 		return;
    547  1.10.2.4   msaitoh 	} else if (t == FS_EX2FS && edit->info.fs_sub_type == 1) {
    548  1.10.2.4   msaitoh 		menu->cursel = FSMAXTYPES;
    549  1.10.2.4   msaitoh 		return;
    550       1.1  dholland 	}
    551       1.7    martin 	/* skip the two FFS entries, and do not add FFS later again */
    552       1.7    martin 	for (ndx = 2, i = 0; i < FSMAXTYPES && ndx < max; i++) {
    553       1.7    martin 		if (i == FS_UNUSED)
    554       1.7    martin 			continue;
    555       1.7    martin 		if (i == FS_BSDFFS)
    556       1.7    martin 			continue;
    557       1.7    martin 		if (fstypenames[i] == NULL)
    558       1.7    martin 			continue;
    559       1.1  dholland 
    560       1.7    martin 		if (i == t) {
    561       1.7    martin 			menu->cursel = ndx;
    562       1.7    martin 			break;
    563       1.7    martin 		}
    564       1.7    martin 		ndx++;
    565       1.7    martin 	}
    566       1.1  dholland }
    567       1.1  dholland 
    568       1.1  dholland static int
    569       1.7    martin set_fstype_ext(menudesc *menu, void *arg)
    570       1.1  dholland {
    571       1.7    martin 	struct single_part_fs_edit *edit = arg;
    572       1.7    martin 	size_t i, ndx, max = menu->numopts;
    573  1.10.2.4   msaitoh 	enum part_type pt;
    574       1.7    martin 
    575       1.7    martin 	if (menu->cursel == 0 || menu->cursel == 1) {
    576       1.7    martin 		edit->info.fs_type = FS_BSDFFS;
    577       1.7    martin 		edit->info.fs_sub_type = menu->cursel == 0 ? 2 : 1;
    578  1.10.2.4   msaitoh 		goto found_type;
    579  1.10.2.4   msaitoh 	} else if (menu->cursel == FSMAXTYPES) {
    580  1.10.2.4   msaitoh 		edit->info.fs_type = FS_EX2FS;
    581  1.10.2.4   msaitoh 		edit->info.fs_sub_type = 1;
    582  1.10.2.4   msaitoh 		goto found_type;
    583       1.7    martin 	}
    584       1.1  dholland 
    585       1.7    martin 	for (ndx = 2, i = 0; i < FSMAXTYPES && ndx < max; i++) {
    586       1.7    martin 		if (i == FS_UNUSED)
    587       1.7    martin 			continue;
    588       1.7    martin 		if (i == FS_BSDFFS)
    589       1.7    martin 			continue;
    590       1.7    martin 		if (fstypenames[i] == NULL)
    591       1.7    martin 			continue;
    592       1.7    martin 
    593       1.7    martin 		if (ndx == (size_t)menu->cursel) {
    594       1.7    martin 			edit->info.fs_type = i;
    595       1.7    martin 			edit->info.fs_sub_type = 0;
    596  1.10.2.4   msaitoh 			goto found_type;
    597       1.7    martin 		}
    598       1.7    martin 		ndx++;
    599       1.7    martin 	}
    600       1.1  dholland 	return 1;
    601  1.10.2.4   msaitoh 
    602  1.10.2.4   msaitoh found_type:
    603  1.10.2.4   msaitoh 	pt = edit->info.nat_type ? edit->info.nat_type->generic_ptype : PT_root;
    604  1.10.2.4   msaitoh 	edit->info.nat_type = edit->pset->parts->pscheme->
    605  1.10.2.4   msaitoh 	    get_fs_part_type(pt, edit->info.fs_type, edit->info.fs_sub_type);
    606  1.10.2.4   msaitoh 	if (edit->info.nat_type == NULL)
    607  1.10.2.4   msaitoh 		edit->info.nat_type = edit->pset->parts->pscheme->
    608  1.10.2.4   msaitoh 		    get_generic_part_type(PT_root);
    609  1.10.2.4   msaitoh 	edit->wanted->type = edit->info.nat_type->generic_ptype;
    610  1.10.2.4   msaitoh 	edit->wanted->fs_type = edit->info.fs_type;
    611  1.10.2.4   msaitoh 	edit->wanted->fs_version = edit->info.fs_sub_type;
    612  1.10.2.4   msaitoh 	return 1;
    613       1.1  dholland }
    614       1.1  dholland 
    615       1.7    martin /*
    616       1.7    martin  * Offer a menu with "exotic" file system types, start with FFSv2 and FFSv1,
    617       1.7    martin  * skip later FFS entry in the generic list.
    618       1.7    martin  */
    619       1.1  dholland static int
    620       1.7    martin edit_fs_type_ext(menudesc *menu, void *arg)
    621       1.1  dholland {
    622       1.7    martin 	menu_ent *opts;
    623       1.7    martin 	int m;
    624       1.7    martin 	size_t i, ndx, cnt;
    625       1.7    martin 
    626  1.10.2.4   msaitoh 	cnt = __arraycount(fstypenames);
    627       1.7    martin 	opts = calloc(cnt, sizeof(*opts));
    628       1.7    martin 	if (opts == NULL)
    629       1.7    martin 		return 1;
    630       1.7    martin 
    631       1.7    martin 	ndx = 0;
    632       1.7    martin 	opts[ndx].opt_name = msg_string(MSG_fs_type_ffsv2);
    633       1.7    martin 	opts[ndx].opt_action = set_fstype_ext;
    634       1.7    martin 	ndx++;
    635       1.7    martin 	opts[ndx].opt_name = msg_string(MSG_fs_type_ffs);
    636       1.7    martin 	opts[ndx].opt_action = set_fstype_ext;
    637       1.7    martin 	ndx++;
    638       1.7    martin 	for (i = 0; i < FSMAXTYPES && ndx < cnt; i++) {
    639       1.7    martin 		if (i == FS_UNUSED)
    640       1.7    martin 			continue;
    641       1.7    martin 		if (i == FS_BSDFFS)
    642       1.7    martin 			continue;
    643       1.7    martin 		if (fstypenames[i] == NULL)
    644       1.7    martin 			continue;
    645       1.7    martin 		opts[ndx].opt_name = fstypenames[i];
    646       1.7    martin 		opts[ndx].opt_action = set_fstype_ext;
    647       1.7    martin 		ndx++;
    648       1.7    martin 	}
    649  1.10.2.4   msaitoh 	opts[ndx].opt_name = msg_string(MSG_fs_type_ext2old);
    650  1.10.2.4   msaitoh 	opts[ndx].opt_action = set_fstype_ext;
    651  1.10.2.4   msaitoh 	ndx++;
    652       1.7    martin 	assert(ndx == cnt);
    653       1.7    martin 	m = new_menu(MSG_Select_the_type, opts, ndx,
    654       1.7    martin 		30, 6, 10, 0, MC_SUBMENU | MC_SCROLL,
    655       1.7    martin 		init_fs_type_ext, NULL, NULL, NULL, MSG_unchanged);
    656       1.7    martin 
    657       1.7    martin 	if (m < 0)
    658       1.7    martin 		return 1;
    659       1.7    martin 	process_menu(m, arg);
    660       1.7    martin 	free_menu(m);
    661       1.7    martin 	free(opts);
    662       1.1  dholland 
    663       1.1  dholland 	return 1;
    664       1.1  dholland }
    665       1.1  dholland 
    666       1.1  dholland static void
    667       1.7    martin init_fs_type(menudesc *menu, void *arg)
    668       1.7    martin {
    669       1.7    martin 	struct single_part_fs_edit *edit = arg;
    670       1.7    martin 	size_t i;
    671       1.7    martin 
    672       1.7    martin 	/* init menu->cursel from fs type in arg */
    673       1.7    martin 	if (edit->info.fs_type == FS_BSDFFS) {
    674       1.7    martin 		if (edit->info.fs_sub_type == 2)
    675       1.7    martin 			menu->cursel = 0;
    676       1.7    martin 		else
    677       1.7    martin 			menu->cursel = 1;
    678       1.7    martin 	}
    679       1.7    martin 	for (i = 1; i < __arraycount(edit_fs_common_types); i++) {
    680       1.7    martin 		if (edit->info.fs_type == edit_fs_common_types[i]) {
    681       1.7    martin 			menu->cursel = i+1;
    682       1.7    martin 			break;
    683       1.7    martin 		}
    684       1.7    martin 	}
    685       1.7    martin }
    686       1.7    martin 
    687       1.7    martin static int
    688       1.7    martin set_fstype(menudesc *menu, void *arg)
    689       1.7    martin {
    690       1.7    martin 	struct single_part_fs_edit *edit = arg;
    691  1.10.2.4   msaitoh 	enum part_type pt;
    692       1.7    martin 	int ndx;
    693       1.7    martin 
    694  1.10.2.4   msaitoh 	pt = edit->info.nat_type ? edit->info.nat_type->generic_ptype : PT_root;
    695       1.7    martin 	if (menu->cursel < 2) {
    696       1.7    martin 		edit->info.fs_type = FS_BSDFFS;
    697       1.7    martin 		edit->info.fs_sub_type = menu->cursel == 0 ? 2 : 1;
    698       1.7    martin 		edit->info.nat_type = edit->pset->parts->pscheme->
    699  1.10.2.4   msaitoh 		    get_fs_part_type(pt, FS_BSDFFS, 2);
    700       1.7    martin 		if (edit->info.nat_type == NULL)
    701       1.7    martin 			edit->info.nat_type = edit->pset->parts->
    702       1.7    martin 			    pscheme->get_generic_part_type(PT_root);
    703       1.7    martin 		edit->wanted->type = edit->info.nat_type->generic_ptype;
    704       1.7    martin 		edit->wanted->fs_type = edit->info.fs_type;
    705       1.7    martin 		edit->wanted->fs_version = edit->info.fs_sub_type;
    706       1.7    martin 		return 1;
    707       1.7    martin 	}
    708       1.7    martin 	ndx = menu->cursel-1;
    709       1.7    martin 
    710       1.7    martin 	if (ndx < 0 ||
    711       1.7    martin 	    (size_t)ndx >= __arraycount(edit_fs_common_types))
    712       1.7    martin 		return 1;
    713       1.7    martin 
    714       1.7    martin 	edit->info.fs_type = edit_fs_common_types[ndx];
    715       1.7    martin 	edit->info.fs_sub_type = 0;
    716       1.7    martin 	edit->info.nat_type = edit->pset->parts->pscheme->
    717  1.10.2.4   msaitoh 	    get_fs_part_type(pt, edit->info.fs_type, 0);
    718       1.7    martin 	if (edit->info.nat_type == NULL)
    719       1.7    martin 		edit->info.nat_type = edit->pset->parts->
    720       1.7    martin 		    pscheme->get_generic_part_type(PT_root);
    721       1.7    martin 	edit->wanted->type = edit->info.nat_type->generic_ptype;
    722       1.7    martin 	edit->wanted->fs_type = edit->info.fs_type;
    723       1.7    martin 	edit->wanted->fs_version = edit->info.fs_sub_type;
    724       1.7    martin 	return 1;
    725       1.7    martin }
    726       1.7    martin 
    727       1.7    martin /*
    728       1.7    martin  * Offer a menu selecting the common file system types
    729       1.7    martin  */
    730       1.7    martin static int
    731       1.7    martin edit_fs_type(menudesc *menu, void *arg)
    732       1.1  dholland {
    733       1.7    martin 	struct single_part_fs_edit *edit = arg;
    734       1.7    martin 	menu_ent *opts;
    735       1.7    martin 	int m, cnt;
    736       1.7    martin 	size_t i;
    737       1.7    martin 
    738       1.7    martin 	/*
    739       1.7    martin 	 * Shortcut to full menu if we have an exotic value
    740       1.7    martin 	 */
    741  1.10.2.4   msaitoh 	if (edit->info.fs_type == FS_EX2FS && edit->info.fs_sub_type == 1) {
    742  1.10.2.4   msaitoh 		edit_fs_type_ext(menu, arg);
    743  1.10.2.4   msaitoh 		return 0;
    744  1.10.2.4   msaitoh 	}
    745       1.7    martin 	for (i = 0; i < __arraycount(edit_fs_common_types); i++)
    746       1.7    martin 		if (edit->info.fs_type == edit_fs_common_types[i])
    747       1.7    martin 			break;
    748       1.7    martin 	if (i >= __arraycount(edit_fs_common_types)) {
    749       1.7    martin 		edit_fs_type_ext(menu, arg);
    750       1.7    martin 		return 0;
    751       1.7    martin 	}
    752       1.1  dholland 
    753       1.7    martin 	/*
    754       1.7    martin 	 * Starting with a common type, show short menu first
    755       1.7    martin 	 */
    756       1.7    martin 	cnt = __arraycount(edit_fs_common_types) + 2;
    757       1.7    martin 	opts = calloc(cnt, sizeof(*opts));
    758       1.7    martin 	if (opts == NULL)
    759       1.7    martin 		return 0;
    760       1.7    martin 
    761       1.7    martin 	/* special case entry 0: two FFS entries */
    762       1.7    martin 	for (i = 0; i < __arraycount(edit_fs_common_types); i++) {
    763       1.7    martin 		opts[i+1].opt_name = getfslabelname(edit_fs_common_types[i], 0);
    764       1.7    martin 		opts[i+1].opt_action = set_fstype;
    765       1.7    martin 	}
    766       1.7    martin 	/* duplicate FFS (at offset 1) into first entry */
    767       1.7    martin 	opts[0] = opts[1];
    768       1.7    martin 	opts[0].opt_name = msg_string(MSG_fs_type_ffsv2);
    769       1.7    martin 	opts[1].opt_name = msg_string(MSG_fs_type_ffs);
    770       1.7    martin 	/* add secondary sub-menu */
    771       1.7    martin 	assert(i+1 < (size_t)cnt);
    772       1.7    martin 	opts[i+1].opt_name = msg_string(MSG_other_fs_type);
    773       1.7    martin 	opts[i+1].opt_action = edit_fs_type_ext;
    774       1.7    martin 
    775       1.7    martin 	m = new_menu(MSG_Select_the_type, opts, cnt,
    776       1.7    martin 		30, 6, 0, 0, MC_SUBMENU | MC_SCROLL,
    777       1.7    martin 		init_fs_type, NULL, NULL, NULL, MSG_unchanged);
    778       1.7    martin 
    779       1.7    martin 	if (m < 0)
    780       1.7    martin 		return 0;
    781       1.7    martin 	process_menu(m, arg);
    782       1.7    martin 	free_menu(m);
    783       1.7    martin 	free(opts);
    784       1.7    martin 
    785       1.7    martin 	return 0;
    786       1.1  dholland }
    787       1.1  dholland 
    788       1.7    martin 
    789       1.7    martin static void update_edit_ptn_menu(menudesc *m, void *arg);
    790       1.7    martin static void draw_edit_ptn_line(menudesc *m, int opt, void *arg);
    791       1.7    martin static int edit_ptn_custom_type(menudesc *m, void *arg);
    792       1.1  dholland 
    793  1.10.2.6    bouyer static void
    794  1.10.2.6    bouyer remember_deleted(struct partition_usage_set *pset,
    795  1.10.2.6    bouyer     struct disk_partitions *parts)
    796  1.10.2.6    bouyer {
    797  1.10.2.6    bouyer 	size_t i, num;
    798  1.10.2.6    bouyer 	struct disk_partitions **tab;
    799  1.10.2.6    bouyer 
    800  1.10.2.6    bouyer 	/* do we have parts on record already? */
    801  1.10.2.6    bouyer 	for (i = 0; i < pset->num_write_back; i++)
    802  1.10.2.6    bouyer 		if (pset->write_back[i] == parts)
    803  1.10.2.6    bouyer 			return;
    804  1.10.2.6    bouyer 	/*
    805  1.10.2.6    bouyer 	 * Need to record this partition table for write back
    806  1.10.2.6    bouyer 	 */
    807  1.10.2.6    bouyer 	num = pset->num_write_back + 1;
    808  1.10.2.6    bouyer 	tab = realloc(pset->write_back, num*sizeof(*pset->write_back));
    809  1.10.2.6    bouyer 	if (!tab)
    810  1.10.2.6    bouyer 		return;
    811  1.10.2.6    bouyer 	tab[pset->num_write_back] = parts;
    812  1.10.2.6    bouyer 	pset->write_back = tab;
    813  1.10.2.6    bouyer 	pset->num_write_back = num;
    814  1.10.2.6    bouyer }
    815  1.10.2.6    bouyer 
    816       1.2    martin int
    817       1.1  dholland edit_ptn(menudesc *menu, void *arg)
    818       1.1  dholland {
    819       1.7    martin 	struct partition_usage_set *pset = arg;
    820       1.7    martin 	struct single_part_fs_edit edit;
    821       1.7    martin 	int fspart_menu, num_opts;
    822       1.7    martin 	const char *err;
    823       1.7    martin 	menu_ent *mopts, *popt;
    824       1.7    martin 	bool is_new_part, with_inst_opt = pset->parts->parent == NULL;
    825       1.7    martin 
    826       1.7    martin 	static const menu_ent edit_ptn_fields_head[] = {
    827       1.8  christos 		{ .opt_action=edit_fs_type },
    828       1.8  christos 		{ .opt_action=edit_fs_start },
    829       1.8  christos 		{ .opt_action=edit_fs_size },
    830       1.8  christos 		{ .opt_flags=OPT_IGNORE },
    831       1.7    martin 	};
    832       1.7    martin 
    833       1.7    martin 	static const menu_ent edit_ptn_fields_head_add[] = {
    834       1.8  christos 		{ .opt_action=edit_install },
    835       1.7    martin 	};
    836       1.7    martin 
    837       1.7    martin 	static const menu_ent edit_ptn_fields_head2[] = {
    838       1.8  christos 		{ .opt_action=edit_fs_preserve },
    839       1.8  christos 		{ .opt_action=edit_fs_mount },
    840       1.7    martin 		{ .opt_menu=MENU_mountoptions, .opt_flags=OPT_SUB },
    841       1.8  christos 		{ .opt_action=edit_fs_mountpt },
    842       1.7    martin 	};
    843  1.10.2.6    bouyer 
    844  1.10.2.6    bouyer 	static const menu_ent edit_ptn_fields_ffs[] = {
    845  1.10.2.6    bouyer 		{ .opt_action=edit_fs_ffs_avg_size },
    846  1.10.2.6    bouyer 		{ .opt_action=edit_fs_ffs_block },
    847  1.10.2.6    bouyer 		{ .opt_action=edit_fs_ffs_frag },
    848  1.10.2.6    bouyer 	};
    849  1.10.2.6    bouyer 
    850       1.7    martin 	static const menu_ent edit_ptn_fields_tail[] = {
    851       1.7    martin 		{ .opt_name=MSG_askunits, .opt_menu=MENU_sizechoice,
    852       1.7    martin 		  .opt_flags=OPT_SUB },
    853       1.8  christos 		{ .opt_name=MSG_restore,
    854       1.7    martin 		  .opt_action=edit_restore},
    855       1.8  christos 		{ .opt_name=MSG_Delete_partition,
    856       1.7    martin 		  .opt_action=edit_delete_ptn},
    857       1.8  christos 		{ .opt_name=MSG_cancel,
    858       1.7    martin 		  .opt_action=edit_cancel},
    859       1.1  dholland 	};
    860       1.7    martin 
    861       1.7    martin 	memset(&edit, 0, sizeof edit);
    862       1.7    martin 	edit.pset = pset;
    863       1.7    martin 	edit.index = menu->cursel;
    864       1.7    martin 	edit.wanted = &pset->infos[edit.index];
    865       1.7    martin 
    866       1.7    martin 	if (menu->cursel < 0 || (size_t)menu->cursel > pset->parts->num_part)
    867       1.7    martin 		return 0;
    868       1.7    martin 	is_new_part = (size_t)menu->cursel == pset->parts->num_part;
    869       1.7    martin 
    870       1.7    martin 	num_opts = __arraycount(edit_ptn_fields_head) +
    871       1.7    martin 	    __arraycount(edit_ptn_fields_head2) +
    872       1.7    martin 	    __arraycount(edit_ptn_fields_tail);
    873  1.10.2.6    bouyer 	if (edit.wanted->fs_type == FS_BSDFFS ||
    874  1.10.2.6    bouyer 	    edit.wanted->fs_type == FS_BSDLFS)
    875  1.10.2.6    bouyer 		num_opts += __arraycount(edit_ptn_fields_ffs);
    876       1.7    martin 	if (with_inst_opt)
    877       1.7    martin 		num_opts += __arraycount(edit_ptn_fields_head_add);
    878       1.7    martin 	if (is_new_part)
    879       1.7    martin 		num_opts--;
    880       1.7    martin 	else
    881       1.7    martin 		num_opts += pset->parts->pscheme->custom_attribute_count;
    882       1.7    martin 
    883       1.7    martin 	mopts = calloc(num_opts, sizeof(*mopts));
    884       1.7    martin 	if (mopts == NULL) {
    885       1.7    martin 		err_msg_win(err_outofmem);
    886       1.7    martin 		return 0;
    887       1.7    martin 	}
    888       1.7    martin 	memcpy(mopts, edit_ptn_fields_head, sizeof(edit_ptn_fields_head));
    889       1.7    martin 	popt = mopts + __arraycount(edit_ptn_fields_head);
    890       1.7    martin 	if (with_inst_opt) {
    891       1.7    martin 		memcpy(popt, edit_ptn_fields_head_add,
    892       1.7    martin 		    sizeof(edit_ptn_fields_head_add));
    893       1.7    martin 		popt +=  __arraycount(edit_ptn_fields_head_add);
    894       1.7    martin 	}
    895       1.7    martin 	memcpy(popt, edit_ptn_fields_head2, sizeof(edit_ptn_fields_head2));
    896       1.7    martin 	popt +=  __arraycount(edit_ptn_fields_head2);
    897  1.10.2.6    bouyer 	if (edit.wanted->fs_type == FS_BSDFFS ||
    898  1.10.2.6    bouyer 	    edit.wanted->fs_type == FS_BSDLFS) {
    899  1.10.2.6    bouyer 		memcpy(popt, edit_ptn_fields_ffs, sizeof(edit_ptn_fields_ffs));
    900  1.10.2.6    bouyer 		popt +=  __arraycount(edit_ptn_fields_ffs);
    901  1.10.2.6    bouyer 	}
    902       1.7    martin 	edit.first_custom_attr = popt - mopts;
    903       1.7    martin 	if (!is_new_part) {
    904       1.7    martin 		for (size_t i = 0;
    905       1.7    martin 		    i < pset->parts->pscheme->custom_attribute_count;
    906       1.7    martin 		    i++, popt++) {
    907       1.7    martin 			popt->opt_action = edit_ptn_custom_type;
    908       1.7    martin 		}
    909       1.7    martin 	}
    910       1.7    martin 	memcpy(popt, edit_ptn_fields_tail, sizeof(edit_ptn_fields_tail));
    911       1.7    martin 	popt += __arraycount(edit_ptn_fields_tail) - 1;
    912       1.7    martin 	if (is_new_part)
    913       1.7    martin 		memcpy(popt-1, popt, sizeof(*popt));
    914       1.7    martin 
    915       1.7    martin 	if (is_new_part) {
    916       1.7    martin 		struct disk_part_free_space space;
    917       1.7    martin 		daddr_t align = pset->parts->pscheme->get_part_alignment(
    918       1.7    martin 		    pset->parts);
    919       1.7    martin 
    920       1.7    martin 		edit.id = NO_PART;
    921       1.7    martin 		if (pset->parts->pscheme->get_free_spaces(pset->parts,
    922       1.7    martin 		    &space, 1, align, align, -1, -1) == 1) {
    923       1.7    martin 			edit.info.start = space.start;
    924       1.7    martin 			edit.info.size = space.size;
    925       1.7    martin 			edit.info.fs_type = FS_BSDFFS;
    926       1.7    martin 			edit.info.fs_sub_type = 2;
    927       1.7    martin 			edit.info.nat_type = pset->parts->pscheme->
    928  1.10.2.4   msaitoh 			    get_fs_part_type(PT_root, edit.info.fs_type,
    929       1.7    martin 			    edit.info.fs_sub_type);
    930       1.7    martin 			edit.wanted->instflags = PUIINST_NEWFS;
    931       1.7    martin 		}
    932       1.7    martin 	} else {
    933       1.7    martin 		edit.id = pset->infos[edit.index].cur_part_id;
    934       1.7    martin 		if (!pset->parts->pscheme->get_part_info(pset->parts, edit.id,
    935       1.7    martin 		    &edit.info)) {
    936       1.7    martin 			free(mopts);
    937       1.7    martin 			return 0;
    938       1.7    martin 		}
    939       1.7    martin 	}
    940       1.7    martin 
    941       1.7    martin 	edit.old_usage = *edit.wanted;
    942       1.7    martin 	edit.old_info = edit.info;
    943       1.7    martin 
    944       1.7    martin 	fspart_menu = new_menu(MSG_edfspart, mopts, num_opts,
    945       1.7    martin 		15, 2, 0, 55, MC_NOCLEAR | MC_SCROLL,
    946       1.7    martin 		update_edit_ptn_menu, draw_edit_ptn_line, NULL,
    947       1.7    martin 		NULL, MSG_OK);
    948       1.7    martin 
    949       1.7    martin 	process_menu(fspart_menu, &edit);
    950       1.7    martin 	free(mopts);
    951       1.7    martin 	free_menu(fspart_menu);
    952       1.7    martin 
    953       1.7    martin 	if (edit.rv == 0) {	/* OK, set new data */
    954       1.7    martin 		edit.info.last_mounted = edit.wanted->mount;
    955       1.7    martin 		if (is_new_part) {
    956  1.10.2.6    bouyer 			edit.wanted->parts = pset->parts;
    957       1.7    martin 			edit.wanted->cur_part_id = pset->parts->pscheme->
    958       1.7    martin 			    add_partition(pset->parts, &edit.info, &err);
    959       1.7    martin 			if (edit.wanted->cur_part_id == NO_PART)
    960       1.7    martin 				err_msg_win(err);
    961       1.7    martin 			else {
    962       1.7    martin 				pset->parts->pscheme->get_part_info(
    963       1.7    martin 				    pset->parts, edit.wanted->cur_part_id,
    964       1.7    martin 				    &edit.info);
    965       1.7    martin 				edit.wanted->cur_start = edit.info.start;
    966       1.7    martin 				edit.wanted->size = edit.info.size;
    967       1.7    martin 				edit.wanted->type =
    968       1.7    martin 				    edit.info.nat_type->generic_ptype;
    969       1.7    martin 				edit.wanted->fs_type = edit.info.fs_type;
    970       1.7    martin 				edit.wanted->fs_version = edit.info.fs_sub_type;
    971       1.7    martin 				/* things have changed, re-sort */
    972       1.7    martin 				renumber_partitions(pset);
    973       1.7    martin 			}
    974       1.7    martin 		} else {
    975       1.7    martin 			if (!pset->parts->pscheme->set_part_info(pset->parts,
    976       1.7    martin 			    edit.id, &edit.info, &err))
    977       1.7    martin 				err_msg_win(err);
    978       1.7    martin 		}
    979       1.7    martin 
    980       1.7    martin 		/*
    981       1.7    martin 		 * if size has changed, we may need to add or remove
    982       1.7    martin 		 * the option to add partitions
    983       1.7    martin 		 */
    984       1.7    martin 		show_partition_adder(menu, pset);
    985       1.7    martin 	} else if (edit.rv == -1) {	/* cancel edit */
    986       1.7    martin 		if (is_new_part) {
    987       1.7    martin 			memmove(pset->infos+edit.index,
    988       1.7    martin 			    pset->infos+edit.index+1,
    989       1.7    martin 			    sizeof(*pset->infos)*(pset->num-edit.index));
    990       1.7    martin 			memmove(menu->opts+edit.index,
    991       1.7    martin 			    menu->opts+edit.index+1,
    992       1.7    martin 			    sizeof(*menu->opts)*(menu->numopts-edit.index));
    993       1.7    martin 			menu->numopts--;
    994       1.7    martin 			menu->cursel = 0;
    995       1.7    martin 			pset->num--;
    996       1.7    martin 			return -1;
    997       1.7    martin 		}
    998       1.7    martin 		pset->infos[edit.index] = edit.old_usage;
    999       1.7    martin 	} else if (!is_new_part && edit.rv == -2) {	/* delete partition */
   1000       1.7    martin 		if (!pset->parts->pscheme->delete_partition(pset->parts,
   1001       1.7    martin 		    edit.id, &err)) {
   1002       1.7    martin 			err_msg_win(err);
   1003       1.7    martin 			return 0;
   1004       1.7    martin 		}
   1005  1.10.2.6    bouyer 		remember_deleted(pset,
   1006  1.10.2.6    bouyer 		    pset->infos[edit.index].parts);
   1007  1.10.2.6    bouyer 		pset->cur_free_space += pset->infos[edit.index].size;
   1008       1.7    martin 		memmove(pset->infos+edit.index,
   1009       1.7    martin 		    pset->infos+edit.index+1,
   1010       1.7    martin 		    sizeof(*pset->infos)*(pset->num-edit.index));
   1011       1.7    martin 		memmove(menu->opts+edit.index,
   1012       1.7    martin 		    menu->opts+edit.index+1,
   1013       1.7    martin 		    sizeof(*menu->opts)*(menu->numopts-edit.index));
   1014       1.7    martin 		menu->numopts--;
   1015       1.7    martin 		menu->cursel = 0;
   1016  1.10.2.6    bouyer 		if (pset->parts->num_part == 0)
   1017  1.10.2.6    bouyer 			menu->cursel = 1;	/* skip sentinel line */
   1018       1.7    martin 
   1019       1.7    martin 		/* things have changed, re-sort */
   1020       1.7    martin 		pset->num--;
   1021       1.7    martin 		renumber_partitions(pset);
   1022       1.7    martin 
   1023       1.7    martin 		/* we can likely add new partitions now */
   1024       1.7    martin 		show_partition_adder(menu, pset);
   1025       1.7    martin 
   1026       1.7    martin 		return -1;
   1027       1.1  dholland 	}
   1028       1.1  dholland 
   1029       1.1  dholland 	return 0;
   1030       1.1  dholland }
   1031       1.1  dholland 
   1032       1.1  dholland static void
   1033       1.7    martin update_edit_ptn_menu(menudesc *m, void *arg)
   1034       1.1  dholland {
   1035       1.7    martin 	struct single_part_fs_edit *edit = arg;
   1036       1.1  dholland 	int i;
   1037       1.7    martin 	uint t = edit->info.fs_type;
   1038       1.7    martin 	size_t attr_no;
   1039       1.1  dholland 
   1040       1.7    martin 	/* Determine which of the properties can be changed */
   1041       1.7    martin 	for (i = 0; i < m->numopts; i++) {
   1042       1.7    martin 		if (m->opts[i].opt_action == NULL &&
   1043       1.7    martin 		    m->opts[i].opt_menu != MENU_mountoptions)
   1044       1.7    martin 			continue;
   1045       1.1  dholland 
   1046       1.1  dholland 		/* Default to disabled... */
   1047       1.1  dholland 		m->opts[i].opt_flags |= OPT_IGNORE;
   1048       1.7    martin 		if ((t == FS_UNUSED || t == FS_SWAP) &&
   1049       1.7    martin 		    (m->opts[i].opt_action == edit_fs_preserve ||
   1050       1.7    martin 		     m->opts[i].opt_action == edit_fs_mount ||
   1051       1.7    martin 		     m->opts[i].opt_action == edit_fs_mountpt ||
   1052       1.7    martin 		     m->opts[i].opt_menu == MENU_mountoptions))
   1053       1.1  dholland 			continue;
   1054       1.7    martin 		if (m->opts[i].opt_action == edit_install &&
   1055       1.7    martin 		    edit->info.nat_type &&
   1056       1.7    martin 		    edit->info.nat_type->generic_ptype != PT_root)
   1057       1.7    martin 			/* can only install onto PT_root partitions */
   1058       1.7    martin 			continue;
   1059       1.7    martin 		if (m->opts[i].opt_action == edit_fs_preserve &&
   1060       1.9    martin 		    t != FS_BSDFFS && t != FS_BSDLFS && t != FS_APPLEUFS &&
   1061       1.9    martin 		    t != FS_MSDOS && t != FS_EX2FS) {
   1062       1.9    martin 			/* Can not newfs this filesystem */
   1063       1.7    martin 			edit->wanted->instflags &= ~PUIINST_NEWFS;
   1064       1.1  dholland 			continue;
   1065       1.1  dholland 		}
   1066       1.7    martin 		if (m->opts[i].opt_action == edit_ptn_custom_type) {
   1067       1.7    martin 			attr_no = (size_t)i - edit->first_custom_attr;
   1068       1.7    martin 			if (!edit->pset->parts->pscheme->
   1069       1.7    martin 			    custom_attribute_writable(
   1070       1.7    martin 			    edit->pset->parts, edit->id, attr_no))
   1071       1.1  dholland 				continue;
   1072       1.1  dholland 		}
   1073       1.1  dholland 		/* Ok: we want this one */
   1074       1.1  dholland 		m->opts[i].opt_flags &= ~OPT_IGNORE;
   1075       1.1  dholland 	}
   1076       1.1  dholland }
   1077       1.1  dholland 
   1078       1.1  dholland static void
   1079       1.7    martin draw_edit_ptn_line(menudesc *m, int opt, void *arg)
   1080       1.1  dholland {
   1081       1.7    martin 	struct single_part_fs_edit *edit = arg;
   1082       1.7    martin 	static int col_width;
   1083       1.7    martin 	static const char *ptn_type, *ptn_start, *ptn_size, *ptn_end,
   1084       1.7    martin 	     *ptn_newfs, *ptn_mount, *ptn_mount_options, *ptn_mountpt,
   1085  1.10.2.6    bouyer 	     *ptn_install, *ptn_bsize, *ptn_fsize, *ptn_isize;
   1086       1.7    martin 	const char *c;
   1087       1.7    martin 	char val[MENUSTRSIZE];
   1088       1.7    martin 	const char *attrname;
   1089       1.7    martin 	size_t attr_no;
   1090       1.7    martin 
   1091       1.7    martin 	if (col_width == 0) {
   1092       1.7    martin 		int l;
   1093       1.7    martin 
   1094       1.7    martin #define	LOAD(STR)	STR = msg_string(MSG_##STR); l = strlen(STR); \
   1095       1.7    martin 			if (l > col_width) col_width = l
   1096       1.7    martin 
   1097       1.7    martin 		LOAD(ptn_type);
   1098       1.7    martin 		LOAD(ptn_start);
   1099       1.7    martin 		LOAD(ptn_size);
   1100       1.7    martin 		LOAD(ptn_end);
   1101       1.7    martin 		LOAD(ptn_install);
   1102       1.7    martin 		LOAD(ptn_newfs);
   1103       1.7    martin 		LOAD(ptn_mount);
   1104       1.7    martin 		LOAD(ptn_mount_options);
   1105       1.7    martin 		LOAD(ptn_mountpt);
   1106  1.10.2.6    bouyer 		LOAD(ptn_bsize);
   1107  1.10.2.6    bouyer 		LOAD(ptn_fsize);
   1108  1.10.2.6    bouyer 		LOAD(ptn_isize);
   1109       1.7    martin #undef LOAD
   1110       1.7    martin 
   1111       1.7    martin 		for (size_t i = 0;
   1112       1.7    martin 		    i < edit->pset->parts->pscheme->custom_attribute_count;
   1113       1.7    martin 		    i++) {
   1114       1.7    martin 			attrname = msg_string(
   1115       1.7    martin 			    edit->pset->parts->pscheme->custom_attributes[i]
   1116       1.7    martin 			    .label);
   1117       1.7    martin 			l = strlen(attrname);
   1118       1.7    martin 			if (l > col_width) col_width = l;
   1119       1.7    martin 		}
   1120       1.1  dholland 
   1121       1.7    martin 		col_width += 3;
   1122       1.7    martin 	}
   1123       1.1  dholland 
   1124       1.1  dholland 	if (m->opts[opt].opt_flags & OPT_IGNORE
   1125       1.7    martin 	    && (opt != 3 || edit->info.fs_type == FS_UNUSED)
   1126       1.7    martin 	    && m->opts[opt].opt_action != edit_ptn_custom_type) {
   1127       1.7    martin 		wprintw(m->mw, "%*s -", col_width, "");
   1128       1.1  dholland 		return;
   1129       1.1  dholland 	}
   1130       1.1  dholland 
   1131       1.7    martin 	if (opt < 4) {
   1132       1.7    martin 		switch (opt) {
   1133       1.7    martin 		case 0:
   1134       1.7    martin 			if (edit->info.fs_type == FS_BSDFFS)
   1135       1.7    martin 				if (edit->info.fs_sub_type == 2)
   1136       1.7    martin 					c = msg_string(MSG_fs_type_ffsv2);
   1137       1.7    martin 				else
   1138       1.7    martin 					c = msg_string(MSG_fs_type_ffs);
   1139       1.1  dholland 			else
   1140       1.7    martin 				c = getfslabelname(edit->info.fs_type,
   1141       1.7    martin 				    edit->info.fs_sub_type);
   1142       1.7    martin 			wprintw(m->mw, "%*s : %s", col_width, ptn_type, c);
   1143       1.7    martin 			return;
   1144       1.7    martin 		case 1:
   1145       1.7    martin 			wprintw(m->mw, "%*s : %" PRIu64 " %s", col_width,
   1146       1.7    martin 			    ptn_start, edit->info.start / sizemult, multname);
   1147       1.7    martin 			return;
   1148       1.7    martin 		case 2:
   1149       1.7    martin 			wprintw(m->mw, "%*s : %" PRIu64 " %s", col_width,
   1150       1.7    martin 			    ptn_size, edit->info.size / sizemult, multname);
   1151       1.7    martin 			return;
   1152       1.7    martin 		case 3:
   1153       1.7    martin 			wprintw(m->mw, "%*s : %" PRIu64 " %s", col_width,
   1154       1.7    martin 			    ptn_end, (edit->info.start + edit->info.size)
   1155       1.7    martin 			    / sizemult, multname);
   1156       1.7    martin 			return;
   1157       1.7    martin 		 }
   1158       1.7    martin 	}
   1159       1.7    martin 	if (m->opts[opt].opt_action == edit_install) {
   1160       1.7    martin 		wprintw(m->mw, "%*s : %s", col_width, ptn_install,
   1161  1.10.2.6    bouyer 			msg_string((edit->info.flags & PTI_INSTALL_TARGET)
   1162       1.7    martin 			    ? MSG_Yes : MSG_No));
   1163       1.7    martin 		return;
   1164       1.7    martin 	}
   1165       1.7    martin 	if (m->opts[opt].opt_action == edit_fs_preserve) {
   1166       1.7    martin 		wprintw(m->mw, "%*s : %s", col_width, ptn_newfs,
   1167       1.7    martin 			msg_string(edit->wanted->instflags & PUIINST_NEWFS
   1168       1.7    martin 			    ? MSG_Yes : MSG_No));
   1169       1.7    martin 		return;
   1170       1.7    martin 	}
   1171       1.7    martin 	if (m->opts[opt].opt_action == edit_fs_mount) {
   1172       1.7    martin 		wprintw(m->mw, "%*s : %s", col_width, ptn_mount,
   1173       1.7    martin 			msg_string(edit->wanted->instflags & PUIINST_MOUNT
   1174       1.7    martin 			    ? MSG_Yes : MSG_No));
   1175       1.7    martin 		return;
   1176       1.7    martin 	}
   1177  1.10.2.6    bouyer 	if (m->opts[opt].opt_action == edit_fs_ffs_block) {
   1178  1.10.2.6    bouyer 		wprintw(m->mw, "%*s : %u", col_width, ptn_bsize,
   1179  1.10.2.6    bouyer 			edit->wanted->fs_opt1);
   1180  1.10.2.6    bouyer 		return;
   1181  1.10.2.6    bouyer 	}
   1182  1.10.2.6    bouyer 	if (m->opts[opt].opt_action == edit_fs_ffs_frag) {
   1183  1.10.2.6    bouyer 		wprintw(m->mw, "%*s : %u", col_width, ptn_fsize,
   1184  1.10.2.6    bouyer 			edit->wanted->fs_opt2);
   1185  1.10.2.6    bouyer 		return;
   1186  1.10.2.6    bouyer 	}
   1187  1.10.2.6    bouyer 	if (m->opts[opt].opt_action == edit_fs_ffs_avg_size) {
   1188  1.10.2.6    bouyer 		if (edit->wanted->fs_opt3 == 0)
   1189  1.10.2.6    bouyer 			wprintw(m->mw, "%*s : %s", col_width, ptn_isize,
   1190  1.10.2.6    bouyer 				msg_string(MSG_ptn_isize_dflt));
   1191  1.10.2.6    bouyer 		else {
   1192  1.10.2.6    bouyer         	        char buf[24], *line;
   1193  1.10.2.6    bouyer 			const char *t = buf;
   1194  1.10.2.6    bouyer 
   1195  1.10.2.6    bouyer 			snprintf(buf, sizeof buf, "%u", edit->wanted->fs_opt3);
   1196  1.10.2.6    bouyer 			line = str_arg_subst(msg_string(MSG_ptn_isize_bytes),
   1197  1.10.2.6    bouyer 			    1, &t);
   1198  1.10.2.6    bouyer 			wprintw(m->mw, "%*s : %s", col_width, ptn_isize,
   1199  1.10.2.6    bouyer 				line);
   1200  1.10.2.6    bouyer 			free(line);
   1201  1.10.2.6    bouyer 		}
   1202  1.10.2.6    bouyer 		return;
   1203  1.10.2.6    bouyer 	}
   1204       1.7    martin 	if (m->opts[opt].opt_menu == MENU_mountoptions) {
   1205       1.7    martin 		wprintw(m->mw, "%*s : ", col_width, ptn_mount_options);
   1206       1.7    martin 		if (edit->wanted->mountflags & PUIMNT_ASYNC)
   1207       1.1  dholland 			wprintw(m->mw, "async ");
   1208       1.7    martin 		if (edit->wanted->mountflags & PUIMNT_NOATIME)
   1209       1.1  dholland 			wprintw(m->mw, "noatime ");
   1210       1.7    martin 		if (edit->wanted->mountflags & PUIMNT_NODEV)
   1211       1.1  dholland 			wprintw(m->mw, "nodev ");
   1212       1.7    martin 		if (edit->wanted->mountflags & PUIMNT_NODEVMTIME)
   1213       1.1  dholland 			wprintw(m->mw, "nodevmtime ");
   1214       1.7    martin 		if (edit->wanted->mountflags & PUIMNT_NOEXEC)
   1215       1.1  dholland 			wprintw(m->mw, "noexec ");
   1216       1.7    martin 		if (edit->wanted->mountflags & PUIMNT_NOSUID)
   1217       1.1  dholland 			wprintw(m->mw, "nosuid ");
   1218       1.7    martin 		if (edit->wanted->mountflags & PUIMNT_LOG)
   1219       1.1  dholland 			wprintw(m->mw, "log ");
   1220       1.7    martin 		if (edit->wanted->mountflags & PUIMNT_NOAUTO)
   1221       1.7    martin 			wprintw(m->mw, "noauto  ");
   1222       1.7    martin 		return;
   1223       1.7    martin 	}
   1224       1.7    martin 	if (m->opts[opt].opt_action == edit_fs_mountpt) {
   1225       1.7    martin 		wprintw(m->mw, "%*s : %s", col_width, ptn_mountpt,
   1226       1.7    martin 		    edit->wanted->mount);
   1227       1.7    martin 		return;
   1228       1.7    martin 	}
   1229       1.7    martin 
   1230       1.7    martin 	attr_no = opt - edit->first_custom_attr;
   1231       1.7    martin 	edit->pset->parts->pscheme->format_custom_attribute(
   1232       1.7    martin 	    edit->pset->parts, edit->id, attr_no, &edit->info,
   1233       1.7    martin 	    val, sizeof val);
   1234       1.7    martin 	attrname = msg_string(edit->pset->parts->pscheme->
   1235       1.7    martin 	    custom_attributes[attr_no].label);
   1236       1.7    martin 	wprintw(m->mw, "%*s : %s", col_width, attrname, val);
   1237       1.7    martin }
   1238       1.7    martin 
   1239       1.7    martin static int
   1240       1.7    martin edit_ptn_custom_type(menudesc *m, void *arg)
   1241       1.7    martin {
   1242       1.7    martin 	struct single_part_fs_edit *edit = arg;
   1243       1.7    martin 	size_t attr_no = m->cursel - edit->first_custom_attr;
   1244       1.7    martin 	char line[STRSIZE];
   1245       1.7    martin 
   1246       1.7    martin 	switch (edit->pset->parts->pscheme->custom_attributes[attr_no].type) {
   1247       1.7    martin 	case pet_bool:
   1248       1.7    martin 		edit->pset->parts->pscheme->custom_attribute_toggle(
   1249       1.7    martin 		    edit->pset->parts, edit->id, attr_no);
   1250       1.1  dholland 		break;
   1251       1.7    martin 	case pet_cardinal:
   1252       1.7    martin 	case pet_str:
   1253       1.7    martin 		edit->pset->parts->pscheme->format_custom_attribute(
   1254       1.7    martin 		    edit->pset->parts, edit->id, attr_no, &edit->info,
   1255       1.7    martin 		    line, sizeof(line));
   1256       1.7    martin 		msg_prompt_win(
   1257       1.7    martin 		    edit->pset->parts->pscheme->custom_attributes[attr_no].
   1258       1.7    martin 		    label, -1, 18, 0, 0, line, line, sizeof(line));
   1259       1.7    martin 		edit->pset->parts->pscheme->custom_attribute_set_str(
   1260       1.7    martin 		    edit->pset->parts, edit->id, attr_no, line);
   1261       1.1  dholland 		break;
   1262       1.1  dholland 	}
   1263       1.7    martin 
   1264       1.7    martin 	return 0;
   1265       1.7    martin }
   1266       1.7    martin 
   1267       1.7    martin 
   1268       1.7    martin /*
   1269       1.7    martin  * Some column width depend on translation, we will set these in
   1270       1.7    martin  * fmt_fspart_header and later use it when formatting single entries
   1271       1.7    martin  * in fmt_fspart_row.
   1272       1.7    martin  * The table consist of 3 "size like" columns, all fixed width, then
   1273       1.7    martin  * ptnheaders_fstype, part_header_col_flag, and finally the mount point
   1274       1.7    martin  * (which is variable width).
   1275       1.7    martin  */
   1276       1.7    martin static int fstype_width, flags_width;
   1277       1.7    martin static char fspart_separator[MENUSTRSIZE];
   1278       1.7    martin static char fspart_title[2*MENUSTRSIZE];
   1279       1.7    martin 
   1280       1.7    martin /*
   1281       1.7    martin  * Format the header of the main partition editor menu.
   1282       1.7    martin  */
   1283       1.7    martin static void
   1284       1.7    martin fmt_fspart_header(menudesc *menu, void *arg)
   1285       1.7    martin {
   1286       1.7    martin 	struct partition_usage_set *pset = arg;
   1287       1.7    martin 	char total[6], free_space[6], scol[13], ecol[13], szcol[13],
   1288  1.10.2.3   msaitoh 	    sepline[MENUSTRSIZE], *p, desc[MENUSTRSIZE];
   1289       1.7    martin 	const char *fstype, *flags;
   1290       1.7    martin 	int i;
   1291  1.10.2.3   msaitoh 	size_t ptn;
   1292  1.10.2.3   msaitoh 	bool with_clone, with_inst_flag = pset->parts->parent == NULL;
   1293       1.7    martin 
   1294  1.10.2.3   msaitoh 	with_clone = false;
   1295  1.10.2.3   msaitoh 	for (ptn = 0; ptn < pset->num && !with_clone; ptn++)
   1296  1.10.2.3   msaitoh 		if (pset->infos[ptn].flags & PUIFLG_CLONE_PARTS)
   1297  1.10.2.3   msaitoh 			with_clone = true;
   1298       1.7    martin 	humanize_number(total, sizeof total,
   1299  1.10.2.5   msaitoh 	    pset->parts->disk_size * pset->parts->bytes_per_sector,
   1300       1.7    martin 	    "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
   1301       1.7    martin 	humanize_number(free_space, sizeof free_space,
   1302  1.10.2.5   msaitoh 	    pset->cur_free_space * pset->parts->bytes_per_sector,
   1303       1.7    martin 	    "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
   1304       1.7    martin 
   1305  1.10.2.3   msaitoh 	if (with_clone)
   1306  1.10.2.3   msaitoh 		strlcpy(desc, msg_string(MSG_clone_flag_desc), sizeof desc);
   1307  1.10.2.3   msaitoh 	else
   1308  1.10.2.3   msaitoh 		desc[0] = 0;
   1309  1.10.2.3   msaitoh 	if (pset->parts->pscheme->part_flag_desc)
   1310  1.10.2.3   msaitoh 		strlcat(desc, msg_string(pset->parts->pscheme->part_flag_desc),
   1311  1.10.2.3   msaitoh 		sizeof desc);
   1312  1.10.2.3   msaitoh 
   1313       1.7    martin 	msg_display_subst(MSG_fspart, 7, pset->parts->disk,
   1314       1.7    martin 	    msg_string(pset->parts->pscheme->name),
   1315       1.7    martin 	    msg_string(pset->parts->pscheme->short_name),
   1316       1.7    martin 	    with_inst_flag ? msg_string(MSG_ptn_instflag_desc) : "",
   1317  1.10.2.3   msaitoh 	    desc, total, free_space);
   1318       1.7    martin 
   1319       1.7    martin 	snprintf(scol, sizeof scol, "%s (%s)",
   1320       1.7    martin 	    msg_string(MSG_ptnheaders_start), multname);
   1321       1.7    martin 	snprintf(ecol, sizeof ecol, "%s (%s)",
   1322       1.7    martin 	    msg_string(MSG_ptnheaders_end), multname);
   1323       1.7    martin 	snprintf(szcol, sizeof szcol, "%s (%s)",
   1324       1.7    martin 	    msg_string(MSG_ptnheaders_size), multname);
   1325       1.7    martin 
   1326       1.7    martin 	fstype = msg_string(MSG_ptnheaders_fstype);
   1327       1.7    martin 	flags = msg_string(MSG_part_header_col_flag);
   1328       1.7    martin 	fstype_width = max(strlen(fstype), 8);
   1329       1.7    martin 	flags_width = strlen(flags);
   1330       1.7    martin 	for (i = 0, p = sepline; i < fstype_width; i++)
   1331       1.7    martin 		*p++ = '-';
   1332       1.7    martin 	for (i = 0, *p++ = ' '; i < flags_width; i++)
   1333       1.7    martin 		*p++ = '-';
   1334       1.7    martin 	*p = 0;
   1335       1.7    martin 
   1336       1.7    martin 	snprintf(fspart_separator, sizeof(fspart_separator),
   1337       1.7    martin 	    "------------ ------------ ------------ %s ----------------",
   1338       1.7    martin 	    sepline);
   1339       1.7    martin 
   1340       1.7    martin 	snprintf(fspart_title, sizeof(fspart_title),
   1341       1.7    martin 	    "   %12.12s %12.12s %12.12s %*s %*s %s\n"
   1342       1.7    martin 	    "   %s", scol, ecol, szcol, fstype_width, fstype,
   1343       1.7    martin 	    flags_width, flags, msg_string(MSG_ptnheaders_filesystem),
   1344       1.7    martin 	    fspart_separator);
   1345       1.7    martin 
   1346       1.7    martin 	msg_table_add("\n\n");
   1347       1.7    martin }
   1348       1.7    martin 
   1349       1.7    martin /*
   1350       1.7    martin  * Format one partition entry in the main partition editor.
   1351       1.7    martin  */
   1352       1.7    martin static void
   1353       1.7    martin fmt_fspart_row(menudesc *m, int ptn, void *arg)
   1354       1.7    martin {
   1355       1.7    martin 	struct partition_usage_set *pset = arg;
   1356       1.7    martin 	struct disk_part_info info;
   1357       1.7    martin 	daddr_t poffset, psize, pend;
   1358       1.7    martin 	const char *desc;
   1359       1.7    martin 	static const char *Yes;
   1360       1.7    martin 	char flag_str[MENUSTRSIZE], *fp;
   1361       1.7    martin 	unsigned inst_flags;
   1362  1.10.2.3   msaitoh #ifndef NO_CLONES
   1363  1.10.2.3   msaitoh 	size_t clone_cnt;
   1364  1.10.2.3   msaitoh #endif
   1365       1.7    martin 	bool with_inst_flag = pset->parts->parent == NULL;
   1366       1.7    martin 
   1367       1.7    martin 	if (Yes == NULL)
   1368       1.7    martin 		Yes = msg_string(MSG_Yes);
   1369       1.7    martin 
   1370  1.10.2.3   msaitoh #ifndef NO_CLONES
   1371  1.10.2.3   msaitoh 	if ((pset->infos[ptn].flags & PUIFLG_CLONE_PARTS) &&
   1372  1.10.2.3   msaitoh 	   pset->infos[ptn].cur_part_id == NO_PART) {
   1373  1.10.2.3   msaitoh 		psize = pset->infos[ptn].size / sizemult;
   1374  1.10.2.3   msaitoh 		if (pset->infos[ptn].clone_ndx <
   1375  1.10.2.3   msaitoh 		    pset->infos[ptn].clone_src->num_sel)
   1376  1.10.2.3   msaitoh 			clone_cnt = 1;
   1377  1.10.2.3   msaitoh 		else
   1378  1.10.2.3   msaitoh 			clone_cnt = pset->infos[ptn].clone_src->num_sel;
   1379  1.10.2.3   msaitoh 		if (pset->infos[ptn].cur_part_id == NO_PART)
   1380  1.10.2.3   msaitoh 			wprintw(m->mw, "                          %12" PRIu64
   1381  1.10.2.3   msaitoh 			    " [%zu %s]", psize, clone_cnt,
   1382  1.10.2.3   msaitoh 			    msg_string(MSG_clone_target_disp));
   1383  1.10.2.3   msaitoh 		else {
   1384  1.10.2.3   msaitoh 			poffset = pset->infos[ptn].cur_start / sizemult;
   1385  1.10.2.3   msaitoh 			pend = (pset->infos[ptn].cur_start +
   1386  1.10.2.3   msaitoh 			    pset->infos[ptn].size) / sizemult - 1;
   1387  1.10.2.3   msaitoh 			wprintw(m->mw, "%12" PRIu64 " %12" PRIu64 " %12" PRIu64
   1388  1.10.2.3   msaitoh 			    " [%zu %s]",
   1389  1.10.2.3   msaitoh 			    poffset, pend, psize, clone_cnt,
   1390  1.10.2.3   msaitoh 			    msg_string(MSG_clone_target_disp));
   1391  1.10.2.3   msaitoh 		}
   1392  1.10.2.3   msaitoh 		if (m->title == fspart_title)
   1393  1.10.2.3   msaitoh 			m->opts[ptn].opt_flags |= OPT_IGNORE;
   1394  1.10.2.3   msaitoh 		else
   1395  1.10.2.3   msaitoh 			m->opts[ptn].opt_flags &= ~OPT_IGNORE;
   1396  1.10.2.3   msaitoh 		return;
   1397  1.10.2.3   msaitoh 	}
   1398  1.10.2.3   msaitoh #endif
   1399  1.10.2.3   msaitoh 
   1400       1.7    martin 	if (!real_partition(pset, ptn))
   1401       1.7    martin 		return;
   1402       1.7    martin 
   1403       1.7    martin 	if (!pset->parts->pscheme->get_part_info(pset->parts,
   1404       1.7    martin 	    pset->infos[ptn].cur_part_id, &info))
   1405       1.7    martin 		return;
   1406       1.7    martin 
   1407  1.10.2.3   msaitoh 	/*
   1408  1.10.2.3   msaitoh 	 * We use this function in multiple menus, but only want it
   1409  1.10.2.3   msaitoh 	 * to play with enable/disable in a single one:
   1410  1.10.2.3   msaitoh 	 */
   1411  1.10.2.3   msaitoh 	if (m->title == fspart_title) {
   1412  1.10.2.3   msaitoh 		/*
   1413  1.10.2.3   msaitoh 		 * Enable / disable this line if it is something
   1414  1.10.2.3   msaitoh 		 * like RAW_PART
   1415  1.10.2.3   msaitoh 		 */
   1416  1.10.2.3   msaitoh 		if ((info.flags &
   1417  1.10.2.3   msaitoh 		    (PTI_WHOLE_DISK|PTI_PSCHEME_INTERNAL|PTI_RAW_PART))
   1418  1.10.2.3   msaitoh 		    || (pset->infos[ptn].flags & PUIFLG_CLONE_PARTS))
   1419  1.10.2.3   msaitoh 			m->opts[ptn].opt_flags |= OPT_IGNORE;
   1420  1.10.2.3   msaitoh 		else
   1421  1.10.2.3   msaitoh 			m->opts[ptn].opt_flags &= ~OPT_IGNORE;
   1422  1.10.2.3   msaitoh 	}
   1423       1.7    martin 
   1424       1.7    martin 	poffset = info.start / sizemult;
   1425       1.7    martin 	psize = info.size / sizemult;
   1426       1.7    martin 	if (psize == 0)
   1427       1.7    martin 		pend = 0;
   1428       1.7    martin 	else
   1429       1.7    martin 		pend = (info.start + info.size) / sizemult - 1;
   1430       1.7    martin 
   1431       1.7    martin 	if (info.flags & PTI_WHOLE_DISK)
   1432       1.7    martin 		desc = msg_string(MSG_NetBSD_partition_cant_change);
   1433       1.7    martin 	else if (info.flags & PTI_RAW_PART)
   1434       1.7    martin 		desc = msg_string(MSG_Whole_disk_cant_change);
   1435       1.7    martin 	else if (info.flags & PTI_BOOT)
   1436       1.7    martin 		desc = msg_string(MSG_Boot_partition_cant_change);
   1437       1.7    martin 	else
   1438       1.7    martin 		desc = getfslabelname(info.fs_type, info.fs_sub_type);
   1439       1.7    martin 
   1440       1.7    martin 	fp = flag_str;
   1441       1.7    martin 	inst_flags = pset->infos[ptn].instflags;
   1442  1.10.2.6    bouyer 	if (with_inst_flag && (info.flags & PTI_INSTALL_TARGET) &&
   1443       1.7    martin 	    info.nat_type->generic_ptype == PT_root) {
   1444       1.7    martin 		static char inst_flag;
   1445       1.7    martin 
   1446       1.7    martin 		if (inst_flag == 0)
   1447       1.7    martin 			inst_flag = msg_string(MSG_install_flag)[0];
   1448       1.7    martin 		*fp++ = inst_flag;
   1449       1.7    martin 	}
   1450       1.7    martin 	if (inst_flags & PUIINST_NEWFS)
   1451       1.7    martin 		*fp++ = msg_string(MSG_newfs_flag)[0];
   1452  1.10.2.3   msaitoh 	if (pset->infos[ptn].flags & PUIFLG_CLONE_PARTS)
   1453  1.10.2.3   msaitoh 		*fp++ = msg_string(MSG_clone_flag)[0];
   1454       1.7    martin 	*fp = 0;
   1455       1.7    martin 	if (pset->parts->pscheme->get_part_attr_str != NULL)
   1456       1.7    martin 		pset->parts->pscheme->get_part_attr_str(pset->parts,
   1457       1.7    martin 		    pset->infos[ptn].cur_part_id, fp, sizeof(flag_str)-1);
   1458       1.7    martin 
   1459       1.7    martin 	/* if the fstype description does not fit, check if we can overrun */
   1460       1.7    martin 	if (strlen(desc) > (size_t)fstype_width &&
   1461       1.7    martin 	     flag_str[0] == 0 && (info.last_mounted == NULL ||
   1462       1.7    martin 	     info.last_mounted[0] == 0))
   1463       1.7    martin 		wprintw(m->mw, "%12" PRIu64 " %12" PRIu64 " %12" PRIu64
   1464       1.7    martin 		    " %s",
   1465       1.7    martin 		    poffset, pend, psize, desc);
   1466       1.7    martin 	else
   1467       1.7    martin 		wprintw(m->mw, "%12" PRIu64 " %12" PRIu64 " %12" PRIu64
   1468       1.7    martin 		    " %*.*s %*s %s",
   1469       1.7    martin 		    poffset, pend, psize, fstype_width, fstype_width, desc,
   1470       1.7    martin 		    -flags_width, flag_str,
   1471       1.7    martin 		    (inst_flags & PUIINST_MOUNT) && info.last_mounted &&
   1472       1.7    martin 		     info.last_mounted[0] ? info.last_mounted : "");
   1473       1.1  dholland }
   1474       1.1  dholland 
   1475  1.10.2.3   msaitoh #ifndef NO_CLONES
   1476  1.10.2.3   msaitoh static int
   1477  1.10.2.3   msaitoh part_ext_clone(menudesc *m, void *arg)
   1478  1.10.2.3   msaitoh {
   1479  1.10.2.3   msaitoh 	struct selected_partitions selected, *clone_src;
   1480  1.10.2.3   msaitoh 	struct clone_target_menu_data data;
   1481  1.10.2.3   msaitoh 	struct partition_usage_set *pset = arg;
   1482  1.10.2.3   msaitoh 	struct part_usage_info *p;
   1483  1.10.2.3   msaitoh 	struct disk_part_info sinfo, cinfo;
   1484  1.10.2.3   msaitoh 	struct disk_partitions *csrc;
   1485  1.10.2.3   msaitoh 	struct disk_part_free_space space;
   1486  1.10.2.3   msaitoh 	menu_ent *men;
   1487  1.10.2.3   msaitoh 	daddr_t clone_size, free_size, offset, align;
   1488  1.10.2.3   msaitoh 	int num_men, i;
   1489  1.10.2.3   msaitoh 	size_t s, clone_cnt;
   1490  1.10.2.3   msaitoh 	part_id cid;
   1491  1.10.2.3   msaitoh 	struct clone_data {
   1492  1.10.2.3   msaitoh 		struct disk_part_info info;
   1493  1.10.2.3   msaitoh 		part_id new_id;
   1494  1.10.2.3   msaitoh 		size_t ndx;
   1495  1.10.2.3   msaitoh 	};
   1496  1.10.2.3   msaitoh 	struct clone_data *clones = NULL;
   1497  1.10.2.3   msaitoh 
   1498  1.10.2.3   msaitoh 	if (!select_partitions(&selected, pm->parts))
   1499  1.10.2.3   msaitoh 		return 0;
   1500  1.10.2.3   msaitoh 
   1501  1.10.2.3   msaitoh 	clone_size = selected_parts_size(&selected);
   1502  1.10.2.3   msaitoh 	num_men = pset->num+1;
   1503  1.10.2.3   msaitoh 	men = calloc(num_men, sizeof *men);
   1504  1.10.2.3   msaitoh 	if (men == NULL)
   1505  1.10.2.3   msaitoh 		return 0;
   1506  1.10.2.3   msaitoh 	for (i = 0; i < num_men; i++) {
   1507  1.10.2.3   msaitoh 		men[i].opt_action = clone_target_select;
   1508  1.10.2.3   msaitoh 		if (i == 0)
   1509  1.10.2.3   msaitoh 			free_size = pset->infos[i].cur_start;
   1510  1.10.2.3   msaitoh 		else if (i > 0 && (size_t)i < pset->num)
   1511  1.10.2.3   msaitoh 			free_size = pset->infos[i].cur_start -
   1512  1.10.2.3   msaitoh 			    pset->infos[i-1].cur_start - pset->infos[i-1].size;
   1513  1.10.2.3   msaitoh 		else
   1514  1.10.2.3   msaitoh 			free_size = pset->parts->free_space;
   1515  1.10.2.3   msaitoh 		if (free_size < clone_size)
   1516  1.10.2.3   msaitoh 			men[i].opt_flags = OPT_IGNORE;
   1517  1.10.2.3   msaitoh 	}
   1518  1.10.2.3   msaitoh 	men[num_men-1].opt_name = MSG_clone_target_end;
   1519  1.10.2.3   msaitoh 
   1520  1.10.2.3   msaitoh 	memset(&data, 0, sizeof data);
   1521  1.10.2.3   msaitoh 	data.usage = *pset;
   1522  1.10.2.3   msaitoh 	data.res = -1;
   1523  1.10.2.3   msaitoh 
   1524  1.10.2.3   msaitoh 	data.usage.menu = new_menu(MSG_clone_target_hdr,
   1525  1.10.2.3   msaitoh 	    men, num_men, 3, 2, 0, 65, MC_SCROLL,
   1526  1.10.2.3   msaitoh 	    NULL, fmt_fspart_row, NULL, NULL, MSG_cancel);
   1527  1.10.2.3   msaitoh 	process_menu(data.usage.menu, &data);
   1528  1.10.2.3   msaitoh 	free_menu(data.usage.menu);
   1529  1.10.2.3   msaitoh 	free(men);
   1530  1.10.2.3   msaitoh 
   1531  1.10.2.3   msaitoh 	if (data.res < 0)
   1532  1.10.2.3   msaitoh 		goto err;
   1533  1.10.2.3   msaitoh 
   1534  1.10.2.3   msaitoh 	/* create temporary infos for all clones that work out */
   1535  1.10.2.3   msaitoh 	clone_cnt = 0;
   1536  1.10.2.3   msaitoh 	clones = calloc(selected.num_sel, sizeof(*clones));
   1537  1.10.2.3   msaitoh 	if (clones == NULL)
   1538  1.10.2.3   msaitoh 		goto err;
   1539  1.10.2.3   msaitoh 
   1540  1.10.2.3   msaitoh 	clone_src = malloc(sizeof(selected));
   1541  1.10.2.3   msaitoh 	if (clone_src == NULL)
   1542  1.10.2.3   msaitoh 		goto err;
   1543  1.10.2.3   msaitoh 	*clone_src = selected;
   1544  1.10.2.3   msaitoh 
   1545  1.10.2.3   msaitoh 	/* find selected offset from data.res and insert clones there */
   1546  1.10.2.3   msaitoh 	align = pset->parts->pscheme->get_part_alignment(pset->parts);
   1547  1.10.2.3   msaitoh 	offset = -1;
   1548  1.10.2.3   msaitoh 	if (data.res > 0)
   1549  1.10.2.3   msaitoh 		offset = pset->infos[data.res-1].cur_start
   1550  1.10.2.3   msaitoh 		    + pset->infos[data.res-1].size;
   1551  1.10.2.3   msaitoh 	else
   1552  1.10.2.3   msaitoh 		offset = 0;
   1553  1.10.2.3   msaitoh 	for (s = 0; s < selected.num_sel; s++) {
   1554  1.10.2.3   msaitoh 		csrc = selected.selection[s].parts;
   1555  1.10.2.3   msaitoh 		cid = selected.selection[s].id;
   1556  1.10.2.3   msaitoh 		csrc->pscheme->get_part_info(csrc, cid, &sinfo);
   1557  1.10.2.3   msaitoh 		if (!pset->parts->pscheme->adapt_foreign_part_info(
   1558  1.10.2.3   msaitoh 		    pset->parts, &cinfo, csrc->pscheme, &sinfo))
   1559  1.10.2.3   msaitoh 			continue;
   1560  1.10.2.3   msaitoh 		size_t cnt = pset->parts->pscheme->get_free_spaces(
   1561  1.10.2.3   msaitoh 		    pset->parts, &space, 1, cinfo.size-align, align,
   1562  1.10.2.3   msaitoh 		    offset, -1);
   1563  1.10.2.3   msaitoh 		if (cnt == 0)
   1564  1.10.2.3   msaitoh 			continue;
   1565  1.10.2.3   msaitoh 		cinfo.start = space.start;
   1566  1.10.2.3   msaitoh 		cid = pset->parts->pscheme->add_partition(
   1567  1.10.2.3   msaitoh 		    pset->parts, &cinfo, NULL);
   1568  1.10.2.3   msaitoh 		if (cid == NO_PART)
   1569  1.10.2.3   msaitoh 			continue;
   1570  1.10.2.3   msaitoh 		pset->parts->pscheme->get_part_info(pset->parts, cid, &cinfo);
   1571  1.10.2.3   msaitoh 		clones[clone_cnt].info = cinfo;
   1572  1.10.2.3   msaitoh 		clones[clone_cnt].new_id = cid;
   1573  1.10.2.3   msaitoh 		clones[clone_cnt].ndx = s;
   1574  1.10.2.3   msaitoh 		clone_cnt++;
   1575  1.10.2.6    bouyer 		offset = roundup(cinfo.start+cinfo.size, align);
   1576  1.10.2.3   msaitoh 	}
   1577  1.10.2.3   msaitoh 
   1578  1.10.2.3   msaitoh 	/* insert new clone records at offset data.res */
   1579  1.10.2.3   msaitoh 	men = realloc(m->opts, (m->numopts+clone_cnt)*sizeof(*m->opts));
   1580  1.10.2.3   msaitoh 	if (men == NULL)
   1581  1.10.2.3   msaitoh 		goto err;
   1582  1.10.2.3   msaitoh 	pset->menu_opts = men;
   1583  1.10.2.3   msaitoh 	m->opts = men;
   1584  1.10.2.3   msaitoh 	m->numopts += clone_cnt;
   1585  1.10.2.3   msaitoh 
   1586  1.10.2.3   msaitoh 	p = realloc(pset->infos, (pset->num+clone_cnt)*sizeof(*pset->infos));
   1587  1.10.2.3   msaitoh 	if (p == NULL)
   1588  1.10.2.3   msaitoh 		goto err;
   1589  1.10.2.3   msaitoh 	pset->infos = p;
   1590  1.10.2.3   msaitoh 
   1591  1.10.2.3   msaitoh 	men += data.res;
   1592  1.10.2.3   msaitoh 	p += data.res;
   1593  1.10.2.3   msaitoh 	memmove(men+clone_cnt, men,
   1594  1.10.2.3   msaitoh 	    sizeof(*men)*(m->numopts-data.res-clone_cnt));
   1595  1.10.2.3   msaitoh 	if (pset->num > (size_t)data.res)
   1596  1.10.2.3   msaitoh 		memmove(p+clone_cnt, p, sizeof(*p)*(pset->num-data.res));
   1597  1.10.2.3   msaitoh 	memset(men, 0, sizeof(*men)*clone_cnt);
   1598  1.10.2.3   msaitoh 	memset(p, 0, sizeof(*p)*clone_cnt);
   1599  1.10.2.3   msaitoh 	for (s = 0; s < clone_cnt; s++) {
   1600  1.10.2.3   msaitoh 		p[s].cur_part_id = clones[s].new_id;
   1601  1.10.2.3   msaitoh 		p[s].cur_start = clones[s].info.start;
   1602  1.10.2.3   msaitoh 		p[s].size = clones[s].info.size;
   1603  1.10.2.3   msaitoh 		p[s].cur_flags = clones[s].info.flags;
   1604  1.10.2.3   msaitoh 		p[s].flags = PUIFLG_CLONE_PARTS;
   1605  1.10.2.3   msaitoh 		p[s].parts = pset->parts;
   1606  1.10.2.3   msaitoh 		p[s].clone_src = clone_src;
   1607  1.10.2.3   msaitoh 		p[s].clone_ndx = s;
   1608  1.10.2.3   msaitoh 	}
   1609  1.10.2.3   msaitoh 	free(clones);
   1610  1.10.2.3   msaitoh 	m->cursel = ((size_t)data.res >= pset->num) ? 0 : data.res+clone_cnt;
   1611  1.10.2.3   msaitoh 	pset->num += clone_cnt;
   1612  1.10.2.3   msaitoh 	m->h = 0;
   1613  1.10.2.3   msaitoh 	resize_menu_height(m);
   1614  1.10.2.3   msaitoh 
   1615  1.10.2.3   msaitoh 	return -1;
   1616  1.10.2.3   msaitoh 
   1617  1.10.2.3   msaitoh err:
   1618  1.10.2.3   msaitoh 	free(clones);
   1619  1.10.2.3   msaitoh 	free_selected_partitions(&selected);
   1620  1.10.2.3   msaitoh 	return 0;
   1621  1.10.2.3   msaitoh }
   1622  1.10.2.3   msaitoh #endif
   1623  1.10.2.3   msaitoh 
   1624       1.1  dholland static int
   1625       1.7    martin edit_fspart_pack(menudesc *m, void *arg)
   1626       1.1  dholland {
   1627       1.7    martin 	struct partition_usage_set *pset = arg;
   1628       1.7    martin 	char buf[STRSIZE];
   1629       1.1  dholland 
   1630       1.7    martin 	if (!pset->parts->pscheme->get_disk_pack_name(pset->parts,
   1631       1.7    martin 	    buf, sizeof buf))
   1632       1.7    martin 		return 0;
   1633       1.7    martin 
   1634       1.7    martin 	msg_prompt_win(MSG_edit_disk_pack_hdr,
   1635       1.7    martin 	    -1, 18, 0, -1, buf, buf, sizeof(buf));
   1636       1.7    martin 
   1637       1.7    martin 	pset->parts->pscheme->set_disk_pack_name(pset->parts, buf);
   1638       1.1  dholland 	return 0;
   1639       1.1  dholland }
   1640       1.1  dholland 
   1641       1.7    martin static int
   1642       1.7    martin edit_fspart_add(menudesc *m, void *arg)
   1643       1.7    martin {
   1644       1.7    martin 	struct partition_usage_set *pset = arg;
   1645       1.7    martin 	struct part_usage_info *ninfo;
   1646       1.7    martin 	menu_ent *nmenopts;
   1647       1.7    martin 	size_t cnt, off;
   1648       1.7    martin 
   1649       1.7    martin 	ninfo = realloc(pset->infos, (pset->num+1)*sizeof(*pset->infos));
   1650       1.7    martin 	if (ninfo == NULL)
   1651       1.7    martin 		return 0;
   1652       1.7    martin 	pset->infos = ninfo;
   1653       1.7    martin 	off = pset->parts->num_part;
   1654       1.7    martin 	cnt = pset->num-pset->parts->num_part;
   1655       1.7    martin 	if (cnt > 0)
   1656       1.7    martin 		memmove(pset->infos+off+1,pset->infos+off,
   1657       1.7    martin 		    cnt*sizeof(*pset->infos));
   1658       1.7    martin 	memset(pset->infos+off, 0, sizeof(*pset->infos));
   1659       1.7    martin 
   1660       1.7    martin 	nmenopts = realloc(m->opts, (m->numopts+1)*sizeof(*m->opts));
   1661       1.7    martin 	if (nmenopts == NULL)
   1662       1.7    martin 		return 0;
   1663       1.7    martin 	memmove(nmenopts+off+1, nmenopts+off,
   1664       1.7    martin 	    (m->numopts-off)*sizeof(*nmenopts));
   1665       1.7    martin 	memset(&nmenopts[off], 0, sizeof(nmenopts[off]));
   1666       1.7    martin 	nmenopts[off].opt_action = edit_ptn;
   1667       1.7    martin 	pset->menu_opts = m->opts = nmenopts;
   1668       1.7    martin 	m->numopts++;
   1669       1.7    martin 	m->cursel = off;
   1670       1.7    martin 	pset->num++;
   1671       1.7    martin 
   1672       1.7    martin 	/* now update edit menu to fit */
   1673       1.7    martin 	m->h = 0;
   1674       1.7    martin 	resize_menu_height(m);
   1675       1.7    martin 
   1676       1.7    martin 	/* and directly invoke the partition editor for the new part */
   1677       1.7    martin 	edit_ptn(m, arg);
   1678       1.7    martin 
   1679       1.7    martin 	show_partition_adder(m, pset);
   1680       1.7    martin 
   1681       1.7    martin 	return -1;
   1682       1.7    martin }
   1683       1.7    martin 
   1684       1.1  dholland static void
   1685       1.7    martin add_partition_adder(menudesc *m, struct partition_usage_set *pset)
   1686       1.1  dholland {
   1687       1.7    martin 	struct part_usage_info *ninfo;
   1688       1.7    martin 	menu_ent *nmenopts;
   1689       1.7    martin 	size_t off;
   1690       1.7    martin 
   1691       1.7    martin 	ninfo = realloc(pset->infos, (pset->num+1)*sizeof(*pset->infos));
   1692       1.7    martin 	if (ninfo == NULL)
   1693       1.7    martin 		return;
   1694       1.7    martin 	pset->infos = ninfo;
   1695       1.7    martin 	off = pset->parts->num_part+1;
   1696       1.1  dholland 
   1697       1.7    martin 	nmenopts = realloc(m->opts, (m->numopts+1)*sizeof(*m->opts));
   1698       1.7    martin 	if (nmenopts == NULL)
   1699       1.7    martin 		return;
   1700       1.7    martin 	memmove(nmenopts+off+1, nmenopts+off,
   1701       1.7    martin 	    (m->numopts-off)*sizeof(*nmenopts));
   1702       1.7    martin 	memset(&nmenopts[off], 0, sizeof(nmenopts[off]));
   1703       1.7    martin 
   1704       1.7    martin 	nmenopts[off].opt_name = MSG_addpart;
   1705       1.7    martin 	nmenopts[off].opt_flags = OPT_SUB;
   1706       1.7    martin 	nmenopts[off].opt_action = edit_fspart_add;
   1707       1.7    martin 
   1708       1.7    martin 	m->opts = nmenopts;
   1709       1.7    martin 	m->numopts++;
   1710       1.7    martin }
   1711       1.1  dholland 
   1712       1.7    martin static void
   1713       1.7    martin remove_partition_adder(menudesc *m, struct partition_usage_set *pset)
   1714       1.7    martin {
   1715       1.7    martin 	size_t off;
   1716       1.1  dholland 
   1717       1.7    martin 	off = pset->parts->num_part+1;
   1718       1.7    martin 	memmove(m->opts+off, m->opts+off+1,
   1719       1.7    martin 	    (m->numopts-off-1)*sizeof(*m->opts));
   1720       1.7    martin 	m->numopts--;
   1721       1.1  dholland }
   1722       1.1  dholland 
   1723       1.1  dholland /*
   1724       1.7    martin  * Called whenever the "add a partition" option may need to be removed
   1725       1.7    martin  * or added
   1726       1.1  dholland  */
   1727       1.7    martin static void
   1728       1.7    martin show_partition_adder(menudesc *m, struct partition_usage_set *pset)
   1729       1.1  dholland {
   1730  1.10.2.5   msaitoh 	if (m->opts == NULL)
   1731  1.10.2.5   msaitoh 		return;
   1732  1.10.2.5   msaitoh 
   1733       1.7    martin 	bool can_add_partition = pset->parts->pscheme->can_add_partition(
   1734       1.7    martin 	    pset->parts);
   1735       1.7    martin 	bool part_adder_present =
   1736       1.7    martin 	    (m->opts[pset->parts->num_part].opt_flags & OPT_IGNORE) &&
   1737       1.7    martin 	    (m->opts[pset->parts->num_part+1].opt_action == edit_fspart_add);
   1738       1.1  dholland 
   1739       1.7    martin 	if (can_add_partition == part_adder_present)
   1740       1.7    martin 		return;
   1741       1.1  dholland 
   1742       1.7    martin 	if (can_add_partition)
   1743       1.7    martin 		add_partition_adder(m, pset);
   1744       1.7    martin 	else
   1745       1.7    martin 		remove_partition_adder(m, pset);
   1746       1.1  dholland 
   1747       1.7    martin 	/* now update edit menu to fit */
   1748       1.7    martin 	m->h = 0;
   1749       1.7    martin 	resize_menu_height(m);
   1750       1.7    martin }
   1751       1.1  dholland 
   1752       1.7    martin static int
   1753       1.7    martin edit_fspart_abort(menudesc *m, void *arg)
   1754       1.7    martin {
   1755       1.7    martin 	struct partition_usage_set *pset = arg;
   1756       1.1  dholland 
   1757       1.7    martin 	pset->ok = false;
   1758       1.7    martin 	return 1;
   1759       1.1  dholland }
   1760       1.1  dholland 
   1761       1.1  dholland /*
   1762       1.7    martin  * Check a disklabel.
   1763       1.7    martin  * If there are overlapping active partitions,
   1764       1.7    martin  * Ask the user if they want to edit the partition or give up.
   1765       1.1  dholland  */
   1766       1.1  dholland int
   1767  1.10.2.5   msaitoh edit_and_check_label(struct pm_devs *p, struct partition_usage_set *pset,
   1768  1.10.2.5   msaitoh     bool install)
   1769       1.1  dholland {
   1770       1.7    martin 	menu_ent *op;
   1771       1.7    martin 	size_t cnt, i;
   1772       1.7    martin 	bool may_add = pset->parts->pscheme->can_add_partition(pset->parts);
   1773       1.7    martin 	bool may_edit_pack =
   1774       1.7    martin 	    pset->parts->pscheme->get_disk_pack_name != NULL &&
   1775       1.7    martin 	    pset->parts->pscheme->set_disk_pack_name != NULL;
   1776       1.7    martin 
   1777  1.10.2.3   msaitoh #ifdef NO_CLONES
   1778  1.10.2.3   msaitoh #define	C_M_ITEMS	0
   1779  1.10.2.3   msaitoh #else
   1780  1.10.2.3   msaitoh #define	C_M_ITEMS	1
   1781  1.10.2.3   msaitoh #endif
   1782       1.7    martin 	pset->menu_opts = calloc(pset->parts->num_part
   1783  1.10.2.3   msaitoh 	     +3+C_M_ITEMS+may_add+may_edit_pack,
   1784       1.7    martin 	     sizeof *pset->menu_opts);
   1785       1.7    martin 	if (pset->menu_opts == NULL)
   1786       1.7    martin 		return 0;
   1787       1.1  dholland 
   1788       1.7    martin 	op = pset->menu_opts;
   1789       1.7    martin 	for (i = 0; i < pset->parts->num_part; i++) {
   1790       1.7    martin 		op->opt_action = edit_ptn;
   1791       1.7    martin 		op++;
   1792       1.7    martin 	}
   1793       1.7    martin 	/* separator line between partitions and actions */
   1794       1.7    martin 	op->opt_name = fspart_separator;
   1795       1.7    martin 	op->opt_flags = OPT_IGNORE|OPT_NOSHORT;
   1796       1.7    martin 	op++;
   1797       1.7    martin 
   1798       1.7    martin 	/* followed by new partition adder */
   1799       1.7    martin 	if (may_add) {
   1800       1.7    martin 		op->opt_name = MSG_addpart;
   1801       1.7    martin 		op->opt_flags = OPT_SUB;
   1802       1.7    martin 		op->opt_action = edit_fspart_add;
   1803       1.7    martin 		op++;
   1804       1.7    martin 	}
   1805       1.7    martin 
   1806       1.7    martin 	/* and unit changer */
   1807       1.7    martin 	op->opt_name = MSG_askunits;
   1808       1.7    martin 	op->opt_menu = MENU_sizechoice;
   1809       1.7    martin 	op->opt_flags = OPT_SUB;
   1810       1.7    martin 	op->opt_action = NULL;
   1811       1.7    martin 	op++;
   1812       1.7    martin 
   1813       1.7    martin 	if (may_edit_pack) {
   1814       1.7    martin 		op->opt_name = MSG_editpack;
   1815       1.7    martin 		op->opt_flags = OPT_SUB;
   1816       1.7    martin 		op->opt_action = edit_fspart_pack;
   1817       1.7    martin 		op++;
   1818       1.7    martin 	}
   1819  1.10.2.3   msaitoh 
   1820  1.10.2.3   msaitoh #ifndef NO_CLONES
   1821  1.10.2.3   msaitoh 	/* add a clone-from-elsewhere option */
   1822  1.10.2.3   msaitoh 	op->opt_name = MSG_clone_from_elsewhere;
   1823  1.10.2.3   msaitoh 	op->opt_action = part_ext_clone;
   1824  1.10.2.3   msaitoh 	op++;
   1825  1.10.2.3   msaitoh #endif
   1826       1.7    martin 
   1827       1.7    martin 	/* and abort option */
   1828       1.7    martin 	op->opt_name = MSG_cancel;
   1829       1.7    martin 	op->opt_flags = OPT_EXIT;
   1830       1.7    martin 	op->opt_action = edit_fspart_abort;
   1831       1.7    martin 	op++;
   1832       1.7    martin 	cnt = op - pset->menu_opts;
   1833  1.10.2.3   msaitoh 	assert(cnt == pset->parts->num_part+3+C_M_ITEMS+may_add+may_edit_pack);
   1834       1.7    martin 
   1835       1.7    martin 	pset->menu = new_menu(fspart_title, pset->menu_opts, cnt,
   1836       1.7    martin 			0, -1, 0, 74,
   1837       1.7    martin 			MC_ALWAYS_SCROLL|MC_NOBOX|MC_DFLTEXIT|
   1838       1.7    martin 			MC_NOCLEAR|MC_CONTINUOUS,
   1839       1.7    martin 			fmt_fspart_header, fmt_fspart_row, NULL, NULL,
   1840       1.7    martin 			MSG_partition_sizes_ok);
   1841       1.7    martin 
   1842       1.7    martin 	if (pset->menu < 0) {
   1843       1.7    martin 		free(pset->menu_opts);
   1844       1.7    martin 		pset->menu_opts = NULL;
   1845       1.7    martin 		return 0;
   1846       1.7    martin 	}
   1847       1.1  dholland 
   1848       1.7    martin 	p->current_cylsize = p->dlcylsize;
   1849       1.1  dholland 
   1850       1.7    martin 	for (;;) {
   1851       1.7    martin 		/* first give the user the option to edit the label... */
   1852       1.7    martin 		pset->ok = true;
   1853       1.7    martin 		process_menu(pset->menu, pset);
   1854       1.7    martin 		if (!pset->ok) {
   1855       1.7    martin 			i = 0;
   1856       1.7    martin 			break;
   1857       1.7    martin 		}
   1858       1.1  dholland 
   1859       1.7    martin 		/* User thinks the label is OK. */
   1860  1.10.2.5   msaitoh 		i = verify_parts(pset, install);
   1861       1.7    martin 		if (i == 1)
   1862       1.7    martin 			continue;
   1863       1.7    martin 		break;
   1864       1.1  dholland 	}
   1865       1.7    martin 	free(pset->menu_opts);
   1866       1.7    martin 	pset->menu_opts = NULL;
   1867       1.7    martin 	free_menu(pset->menu);
   1868       1.7    martin 	pset->menu = -1;
   1869       1.1  dholland 
   1870       1.7    martin 	return i != 0;
   1871       1.1  dholland }
   1872       1.1  dholland 
   1873       1.1  dholland /*
   1874      1.10    martin  * strip trailing / to avoid confusion in path comparisions later
   1875      1.10    martin  */
   1876      1.10    martin void
   1877      1.10    martin canonicalize_last_mounted(char *path)
   1878      1.10    martin {
   1879      1.10    martin 	char *p;
   1880      1.10    martin 
   1881  1.10.2.1   msaitoh 	if (path == NULL)
   1882  1.10.2.1   msaitoh 		return;
   1883  1.10.2.1   msaitoh 
   1884  1.10.2.1   msaitoh 	if (strcmp(path, "/") == 0)
   1885  1.10.2.1   msaitoh 		return;	/* in this case a "trailing" slash is allowed */
   1886  1.10.2.1   msaitoh 
   1887      1.10    martin 	for (;;) {
   1888      1.10    martin 		p = strrchr(path, '/');
   1889      1.10    martin 		if (p == NULL)
   1890      1.10    martin 			return;
   1891      1.10    martin 		if (p[1] != 0)
   1892      1.10    martin 			return;
   1893      1.10    martin 		p[0] = 0;
   1894      1.10    martin 	}
   1895      1.10    martin }
   1896      1.10    martin 
   1897      1.10    martin /*
   1898       1.1  dholland  * Try to get 'last mounted on' (or equiv) from fs superblock.
   1899       1.1  dholland  */
   1900       1.1  dholland const char *
   1901       1.7    martin get_last_mounted(int fd, daddr_t partstart, uint *fs_type, uint *fs_sub_type,
   1902       1.7    martin     uint flags)
   1903       1.1  dholland {
   1904       1.1  dholland 	static char sblk[SBLOCKSIZE];		/* is this enough? */
   1905       1.1  dholland 	struct fs *SB = (struct fs *)sblk;
   1906       1.7    martin 	static const off_t sblocks[] = SBLOCKSEARCH;
   1907       1.7    martin 	const off_t *sbp;
   1908       1.1  dholland 	const char *mnt = NULL;
   1909       1.1  dholland 	int len;
   1910       1.1  dholland 
   1911       1.1  dholland 	if (fd == -1)
   1912       1.1  dholland 		return "";
   1913       1.1  dholland 
   1914       1.7    martin 	if (fs_type)
   1915       1.7    martin 		*fs_type = 0;
   1916       1.7    martin 	if (fs_sub_type)
   1917       1.7    martin 		*fs_sub_type = 0;
   1918       1.7    martin 
   1919       1.1  dholland 	/* Check UFS1/2 (and hence LFS) superblock */
   1920       1.1  dholland 	for (sbp = sblocks; mnt == NULL && *sbp != -1; sbp++) {
   1921       1.1  dholland 		if (pread(fd, sblk, sizeof sblk,
   1922       1.7    martin 		    (off_t)partstart * (off_t)512 + *sbp) != sizeof sblk)
   1923       1.7    martin 			continue;
   1924       1.7    martin 
   1925       1.7    martin 		/*
   1926       1.7    martin 		 * If start of partition and allowed by flags check
   1927       1.7    martin 		 * for other fs types
   1928       1.7    martin 		 */
   1929       1.7    martin 		if (*sbp == 0 && (flags & GLM_MAYBE_FAT32) &&
   1930       1.7    martin 		    sblk[0x42] == 0x29 && memcmp(sblk + 0x52, "FAT", 3) == 0) {
   1931       1.7    martin 			/* Probably a FAT filesystem, report volume name */
   1932       1.7    martin 			size_t i;
   1933       1.7    martin 			for (i = 0x51; i >= 0x47; i--) {
   1934       1.7    martin 				if (sblk[i] != ' ')
   1935       1.7    martin 					break;
   1936       1.7    martin 				sblk[i] = 0;
   1937       1.7    martin 			}
   1938       1.7    martin 			sblk[0x52] = 0;
   1939       1.7    martin 			if (fs_type)
   1940       1.7    martin 				*fs_type = FS_MSDOS;
   1941       1.7    martin 			if (fs_sub_type)
   1942       1.7    martin 				*fs_sub_type = sblk[0x53];
   1943       1.7    martin 			return sblk + 0x47;
   1944       1.7    martin 		} else if (*sbp == 0 && (flags & GLM_MAYBE_NTFS) &&
   1945       1.7    martin 		    memcmp(sblk+3, "NTFS    ", 8) == 0) {
   1946       1.7    martin 			if (fs_type)
   1947       1.7    martin 				*fs_type = FS_NTFS;
   1948       1.7    martin 			if (fs_sub_type)
   1949       1.7    martin 				*fs_sub_type = MBR_PTYPE_NTFS;
   1950       1.7    martin 			/* XXX dig for volume name attribute ? */
   1951       1.7    martin 			return "";
   1952       1.7    martin 		}
   1953       1.7    martin 
   1954       1.7    martin 		if (!(flags & GLM_LIKELY_FFS))
   1955       1.1  dholland 			continue;
   1956       1.7    martin 
   1957       1.1  dholland 		/* Maybe we should validate the checksum??? */
   1958       1.1  dholland 		switch (SB->fs_magic) {
   1959       1.1  dholland 		case FS_UFS1_MAGIC:
   1960       1.1  dholland 		case FS_UFS1_MAGIC_SWAPPED:
   1961       1.1  dholland 			if (!(SB->fs_old_flags & FS_FLAGS_UPDATED)) {
   1962       1.1  dholland 				if (*sbp == SBLOCK_UFS1)
   1963       1.1  dholland 					mnt = (const char *)SB->fs_fsmnt;
   1964       1.1  dholland 			} else {
   1965       1.1  dholland 				/* Check we have the main superblock */
   1966       1.1  dholland 				if (SB->fs_sblockloc == *sbp)
   1967       1.1  dholland 					mnt = (const char *)SB->fs_fsmnt;
   1968       1.1  dholland 			}
   1969       1.7    martin 			if (fs_type)
   1970       1.7    martin 				*fs_type = FS_BSDFFS;
   1971       1.7    martin 			if (fs_sub_type)
   1972  1.10.2.2   msaitoh 				*fs_sub_type = 1;
   1973       1.1  dholland 			continue;
   1974       1.1  dholland 		case FS_UFS2_MAGIC:
   1975       1.1  dholland 		case FS_UFS2_MAGIC_SWAPPED:
   1976       1.1  dholland 			/* Check we have the main superblock */
   1977       1.1  dholland 			if (SB->fs_sblockloc == *sbp) {
   1978       1.1  dholland 				mnt = (const char *)SB->fs_fsmnt;
   1979       1.7    martin 				if (fs_type)
   1980       1.7    martin 					*fs_type = FS_BSDFFS;
   1981       1.7    martin 				if (fs_sub_type)
   1982       1.7    martin 					*fs_sub_type = 2;
   1983       1.1  dholland 			}
   1984       1.1  dholland 			continue;
   1985       1.1  dholland 		}
   1986       1.1  dholland 	}
   1987       1.1  dholland 
   1988       1.1  dholland 	if (mnt == NULL)
   1989       1.1  dholland 		return "";
   1990       1.1  dholland 
   1991       1.1  dholland 	/* If sysinst mounted this last then strip prefix */
   1992       1.1  dholland 	len = strlen(targetroot_mnt);
   1993       1.1  dholland 	if (memcmp(mnt, targetroot_mnt, len) == 0) {
   1994       1.1  dholland 		if (mnt[len] == 0)
   1995       1.1  dholland 			return "/";
   1996       1.1  dholland 		if (mnt[len] == '/')
   1997       1.1  dholland 			return mnt + len;
   1998       1.1  dholland 	}
   1999       1.1  dholland 	return mnt;
   2000       1.1  dholland 	#undef SB
   2001       1.1  dholland }
   2002       1.1  dholland 
   2003       1.1  dholland /* Ask for a partition offset, check bounds and do the needed roundups */
   2004       1.7    martin daddr_t
   2005       1.7    martin getpartoff(struct disk_partitions *parts, daddr_t defpartstart)
   2006       1.1  dholland {
   2007       1.7    martin 	char defstart[24], isize[24], maxpart, minspace, maxspace,
   2008       1.7    martin 	    *prompt, *label_msg, valid_parts[4], valid_spaces[4],
   2009       1.7    martin 	    space_prompt[1024], *head, *hint_part, *hint_space, *tail;
   2010       1.7    martin 	size_t num_freespace, spaces, ndx;
   2011       1.7    martin 	struct disk_part_free_space *freespace;
   2012       1.7    martin 	daddr_t i, localsizemult, ptn_alignment, min, max;
   2013       1.7    martin 	part_id partn;
   2014       1.7    martin 	struct disk_part_info info;
   2015       1.7    martin 	const char *errmsg = NULL;
   2016       1.7    martin 
   2017       1.7    martin 	min = parts->disk_start;
   2018       1.7    martin 	max = min + parts->disk_size;
   2019       1.7    martin 
   2020       1.7    martin 	/* upper bound on the number of free spaces, plus some slope */
   2021       1.7    martin 	num_freespace = parts->num_part * 2 + 5;
   2022       1.7    martin 	freespace = calloc(num_freespace, sizeof(*freespace));
   2023       1.7    martin 	if (freespace == NULL)
   2024       1.7    martin 		return -1;
   2025       1.7    martin 
   2026       1.7    martin 	ptn_alignment = parts->pscheme->get_part_alignment(parts);
   2027       1.7    martin 	spaces = parts->pscheme->get_free_spaces(parts, freespace,
   2028       1.7    martin 	    num_freespace, max(sizemult, ptn_alignment), ptn_alignment, -1,
   2029       1.7    martin 	    defpartstart);
   2030       1.7    martin 
   2031       1.7    martin 	maxpart = 'a' + parts->num_part -1;
   2032       1.7    martin 	if (parts->num_part > 1) {
   2033       1.7    martin 		snprintf(valid_parts, sizeof valid_parts, "a-%c", maxpart);
   2034       1.7    martin 	} else if (parts->num_part == 1) {
   2035       1.7    martin 		snprintf(valid_parts, sizeof valid_parts, "  %c", maxpart);
   2036       1.7    martin 	} else {
   2037       1.7    martin 		strcpy(valid_parts, "---");
   2038       1.7    martin 	}
   2039       1.7    martin 	if (spaces > 1) {
   2040       1.7    martin 		minspace = maxpart + 1;
   2041       1.7    martin 		maxspace = minspace + spaces -1;
   2042       1.7    martin 		snprintf(valid_spaces, sizeof valid_spaces, "%c-%c", minspace,
   2043       1.7    martin 		    maxspace);
   2044       1.7    martin 	} else if (spaces == 1) {
   2045       1.7    martin 		maxspace = minspace = maxpart + 1;
   2046       1.7    martin 		snprintf(valid_spaces, sizeof valid_spaces, "  %c", minspace);
   2047       1.7    martin 	} else {
   2048       1.7    martin 		minspace = 0;
   2049       1.7    martin 		maxspace = 0;
   2050       1.7    martin 		strcpy(valid_spaces, "---");
   2051       1.7    martin 	}
   2052       1.7    martin 
   2053       1.7    martin 	/* Add description of start/size to user prompt */
   2054       1.7    martin 	const char *mstr = msg_string(MSG_free_space_line);
   2055       1.7    martin         space_prompt[0] = 0;
   2056       1.7    martin         for (ndx = 0; ndx < spaces; ndx++) {
   2057       1.7    martin                 char str_start[40], str_end[40], str_size[40], str_tag[4];
   2058       1.7    martin 
   2059       1.7    martin 		sprintf(str_tag, "%c: ", minspace+(int)ndx);
   2060       1.7    martin                 sprintf(str_start, "%" PRIu64, freespace[ndx].start / sizemult);
   2061       1.7    martin                 sprintf(str_end, "%" PRIu64,
   2062       1.7    martin                     (freespace[ndx].start + freespace[ndx].size) / sizemult);
   2063       1.7    martin                 sprintf(str_size, "%" PRIu64, freespace[ndx].size / sizemult);
   2064       1.7    martin                 const char *args[4] = { str_start, str_end, str_size,
   2065       1.7    martin                     multname };
   2066       1.7    martin                 char *line = str_arg_subst(mstr, 4, args);
   2067       1.7    martin 		strlcat(space_prompt, str_tag, sizeof(space_prompt));
   2068       1.7    martin                 size_t len = strlcat(space_prompt, line, sizeof(space_prompt));
   2069       1.7    martin                 free (line);
   2070       1.7    martin                 if (len >= sizeof space_prompt)
   2071       1.7    martin                         break;
   2072       1.7    martin         }
   2073       1.7    martin 
   2074       1.7    martin 	const char *args[] = { valid_parts, valid_spaces, multname };
   2075       1.7    martin 	hint_part = NULL;
   2076       1.7    martin 	hint_space = NULL;
   2077       1.7    martin 	head = str_arg_subst(msg_string(MSG_label_offset_head),
   2078       1.7    martin 	    __arraycount(args), args);
   2079       1.7    martin 	if (parts->num_part)
   2080       1.7    martin 		hint_part = str_arg_subst(msg_string(
   2081       1.7    martin 		    MSG_label_offset_part_hint), __arraycount(args), args);
   2082       1.7    martin 	if (spaces)
   2083       1.7    martin 		hint_space = str_arg_subst(msg_string(
   2084       1.7    martin 		    MSG_label_offset_space_hint), __arraycount(args), args);
   2085       1.7    martin 	tail = str_arg_subst(msg_string(MSG_label_offset_tail),
   2086       1.7    martin 	    __arraycount(args), args);
   2087       1.7    martin 
   2088       1.7    martin 	if (hint_part && hint_space)
   2089       1.7    martin 		asprintf(&label_msg, "%s\n%s\n%s\n\n%s\n%s",
   2090       1.7    martin 		    head, hint_part, hint_space, space_prompt, tail);
   2091       1.7    martin 	else if (hint_part)
   2092       1.7    martin 		asprintf(&label_msg, "%s\n%s\n\n%s",
   2093       1.7    martin 		    head, hint_part, tail);
   2094       1.7    martin 	else if (hint_space)
   2095       1.7    martin 		asprintf(&label_msg, "%s\n%s\n\n%s\n%s",
   2096       1.7    martin 		    head, hint_space, space_prompt, tail);
   2097       1.7    martin 	else
   2098       1.7    martin 		asprintf(&label_msg, "%s\n\n%s",
   2099       1.7    martin 		    head, tail);
   2100       1.7    martin 	free(head); free(hint_part); free(hint_space); free(tail);
   2101       1.1  dholland 
   2102       1.7    martin 	localsizemult = sizemult;
   2103       1.7    martin 	errmsg = NULL;
   2104       1.1  dholland 	for (;;) {
   2105       1.7    martin 		snprintf(defstart, sizeof defstart, "%" PRIu64,
   2106       1.7    martin 		    defpartstart/sizemult);
   2107       1.7    martin 		if (errmsg != NULL && errmsg[0] != 0)
   2108       1.7    martin 			asprintf(&prompt, "%s\n\n%s", errmsg, label_msg);
   2109       1.7    martin 		else
   2110       1.7    martin 			prompt = label_msg;
   2111       1.7    martin 		msg_prompt_win(prompt, -1, 13, 70, -1,
   2112       1.7    martin 		    (defpartstart > 0) ? defstart : NULL, isize, sizeof isize);
   2113       1.7    martin 		if (label_msg != prompt)
   2114       1.7    martin 			free(prompt);
   2115       1.7    martin 		if (strcmp(defstart, isize) == 0) {
   2116       1.1  dholland 			/* Don't do rounding if default accepted */
   2117       1.7    martin 			i = defpartstart;
   2118       1.7    martin 			break;
   2119       1.7    martin 		}
   2120       1.1  dholland 		if (isize[1] == '\0' && isize[0] >= 'a' &&
   2121       1.7    martin 		    isize[0] <= maxpart) {
   2122       1.1  dholland 			partn = isize[0] - 'a';
   2123       1.7    martin 			if (parts->pscheme->get_part_info(parts, partn,
   2124       1.7    martin 			    &info)) {
   2125       1.7    martin 				i = info.start + info.size;
   2126       1.7    martin 				localsizemult = 1;
   2127       1.7    martin 			} else {
   2128       1.7    martin 				errmsg = msg_string(MSG_invalid_sector_number);
   2129       1.7    martin 				continue;
   2130       1.7    martin 			}
   2131       1.7    martin 		} else if (isize[1] == '\0' && isize[0] >= minspace &&
   2132       1.7    martin 		    isize[0] <= maxspace) {
   2133       1.7    martin 			ndx = isize[0] - minspace;
   2134       1.7    martin 			i = freespace[ndx].start;
   2135       1.1  dholland 			localsizemult = 1;
   2136       1.1  dholland 		} else if (atoi(isize) == -1) {
   2137       1.7    martin 			i = min;
   2138       1.1  dholland 			localsizemult = 1;
   2139       1.1  dholland 		} else {
   2140  1.10.2.5   msaitoh 			i = parse_disk_pos(isize, &localsizemult,
   2141  1.10.2.5   msaitoh 			    parts->bytes_per_sector,
   2142  1.10.2.5   msaitoh 			    parts->pscheme->get_cylinder_size(parts), NULL);
   2143       1.7    martin 			if (i < 0) {
   2144       1.1  dholland 				errmsg = msg_string(MSG_invalid_sector_number);
   2145       1.1  dholland 				continue;
   2146       1.1  dholland 			}
   2147       1.1  dholland 		}
   2148       1.1  dholland 		/* round to cylinder size if localsizemult != 1 */
   2149  1.10.2.5   msaitoh 		int cylsize = parts->pscheme->get_cylinder_size(parts);
   2150  1.10.2.5   msaitoh 		i = NUMSEC(i, localsizemult, cylsize);
   2151       1.1  dholland 		/* Adjust to start of slice if needed */
   2152       1.7    martin 		if ((i < min && (min - i) < localsizemult) ||
   2153       1.7    martin 		    (i > min && (i - min) < localsizemult)) {
   2154       1.7    martin 			i = min;
   2155       1.1  dholland 		}
   2156       1.7    martin 		if (max == 0 || i <= max)
   2157       1.1  dholland 			break;
   2158       1.1  dholland 		errmsg = msg_string(MSG_startoutsidedisk);
   2159       1.1  dholland 	}
   2160       1.7    martin 	free(label_msg);
   2161       1.7    martin 	free(freespace);
   2162       1.7    martin 
   2163       1.1  dholland 	return i;
   2164       1.1  dholland }
   2165       1.1  dholland 
   2166       1.1  dholland 
   2167       1.1  dholland /* Ask for a partition size, check bounds and do the needed roundups */
   2168       1.7    martin daddr_t
   2169  1.10.2.6    bouyer getpartsize(struct disk_partitions *parts, daddr_t orig_start,
   2170  1.10.2.6    bouyer     daddr_t partstart, daddr_t dflt)
   2171       1.1  dholland {
   2172       1.7    martin 	char dsize[24], isize[24], max_size[24], maxpartc, valid_parts[4],
   2173       1.7    martin 	    *label_msg, *prompt, *head, *hint, *tail;
   2174       1.7    martin 	const char *errmsg = NULL;
   2175  1.10.2.5   msaitoh 	daddr_t i, partend, diskend, localsizemult, max, max_r, dflt_r;
   2176       1.7    martin 	struct disk_part_info info;
   2177       1.7    martin 	part_id partn;
   2178       1.7    martin 
   2179  1.10.2.5   msaitoh 	diskend = parts->disk_start + parts->disk_size;
   2180  1.10.2.6    bouyer 	max = parts->pscheme->max_free_space_at(parts, orig_start);
   2181  1.10.2.6    bouyer 	max += orig_start - partstart;
   2182  1.10.2.6    bouyer 	if (sizemult == 1)
   2183  1.10.2.6    bouyer 		max--;	/* with hugher scale proper rounding later will be ok */
   2184       1.7    martin 
   2185       1.7    martin 	/* We need to keep both the unrounded and rounded (_r) max and dflt */
   2186       1.7    martin 	dflt_r = (partstart + dflt) / sizemult - partstart / sizemult;
   2187       1.7    martin 	if (max == dflt)
   2188       1.7    martin 		max_r = dflt_r;
   2189       1.7    martin 	else
   2190       1.7    martin 		max_r = max / sizemult;
   2191       1.7    martin 	/* the partition may have been moved and now not fit any longer */
   2192       1.7    martin 	if (dflt > max)
   2193       1.7    martin 		dflt = max;
   2194       1.7    martin 	if (dflt_r > max_r)
   2195       1.7    martin 		dflt_r = max_r;
   2196       1.7    martin 
   2197       1.7    martin 	snprintf(max_size, sizeof max_size, "%" PRIu64, max_r);
   2198       1.7    martin 
   2199       1.7    martin 	maxpartc = 'a' + parts->num_part -1;
   2200       1.7    martin 	if (parts->num_part > 1) {
   2201       1.7    martin 		snprintf(valid_parts, sizeof valid_parts, "a-%c", maxpartc);
   2202       1.7    martin 	} else if (parts->num_part == 1) {
   2203       1.7    martin 		snprintf(valid_parts, sizeof valid_parts, "  %c", maxpartc);
   2204       1.7    martin 	} else {
   2205       1.7    martin 		strcpy(valid_parts, "---");
   2206       1.7    martin 	}
   2207       1.7    martin 
   2208       1.7    martin 	const char *args[] = { valid_parts, max_size, multname };
   2209       1.7    martin 	hint = NULL;
   2210       1.7    martin 	head = str_arg_subst(msg_string(MSG_label_size_head),
   2211       1.7    martin 	    __arraycount(args), args);
   2212       1.7    martin 	if (parts->num_part)
   2213       1.7    martin 		hint  = str_arg_subst(msg_string(MSG_label_size_part_hint),
   2214       1.7    martin 		    __arraycount(args), args);
   2215       1.7    martin 	tail = str_arg_subst(msg_string(MSG_label_size_tail),
   2216       1.7    martin 	    __arraycount(args), args);
   2217       1.7    martin 
   2218       1.7    martin 	if (hint != NULL)
   2219       1.7    martin 		asprintf(&label_msg, "%s\n%s\n\n%s", head, hint, tail);
   2220       1.7    martin 	else
   2221       1.7    martin 		asprintf(&label_msg, "%s\n\n%s", head, tail);
   2222       1.7    martin 	free(head); free(hint); free(tail);
   2223       1.1  dholland 
   2224       1.7    martin 	localsizemult = sizemult;
   2225       1.7    martin 	i = -1;
   2226       1.1  dholland 	for (;;) {
   2227       1.7    martin 		snprintf(dsize, sizeof dsize, "%" PRIu64, dflt_r);
   2228       1.7    martin 
   2229       1.7    martin 		if (errmsg != NULL && errmsg[0] != 0)
   2230       1.7    martin 			asprintf(&prompt, "%s\n\n%s", errmsg, label_msg);
   2231       1.7    martin 		else
   2232       1.7    martin 			prompt = label_msg;
   2233       1.7    martin 		msg_prompt_win(prompt, -1, 12, 70, -1,
   2234       1.7    martin 		    (dflt != 0) ? dsize : 0, isize, sizeof isize);
   2235       1.7    martin 		if (prompt != label_msg)
   2236       1.7    martin 			free(prompt);
   2237       1.7    martin 
   2238       1.7    martin 		if (strcmp(isize, dsize) == 0) {
   2239       1.7    martin 			free(label_msg);
   2240       1.7    martin 			return dflt;
   2241       1.7    martin 		}
   2242       1.7    martin 		if (parts->num_part && isize[1] == '\0' && isize[0] >= 'a' &&
   2243       1.1  dholland 		    isize[0] <= maxpartc) {
   2244       1.1  dholland 			partn = isize[0] - 'a';
   2245       1.7    martin 			if (parts->pscheme->get_part_info(parts, partn,
   2246       1.7    martin 			    &info)) {
   2247       1.7    martin 				i = info.start - partstart -1;
   2248       1.7    martin 				localsizemult = 1;
   2249       1.7    martin 				max_r = max;
   2250       1.7    martin 			}
   2251       1.1  dholland 		} else if (atoi(isize) == -1) {
   2252       1.7    martin 			i = max;
   2253       1.1  dholland 			localsizemult = 1;
   2254       1.7    martin 			max_r = max;
   2255       1.1  dholland 		} else {
   2256       1.7    martin 			i = parse_disk_pos(isize, &localsizemult,
   2257  1.10.2.5   msaitoh 			    parts->bytes_per_sector,
   2258  1.10.2.5   msaitoh 			    parts->pscheme->get_cylinder_size(parts), NULL);
   2259       1.7    martin 			if (localsizemult != sizemult)
   2260       1.7    martin 				max_r = max;
   2261       1.7    martin 		}
   2262       1.7    martin 		if (i < 0) {
   2263       1.7    martin 			errmsg = msg_string(MSG_Invalid_numeric);
   2264       1.7    martin 			continue;
   2265       1.7    martin 		} else if (i > max_r) {
   2266       1.7    martin 			errmsg = msg_string(MSG_Too_large);
   2267       1.7    martin 			continue;
   2268       1.1  dholland 		}
   2269       1.1  dholland 		/*
   2270       1.1  dholland 		 * partend is aligned to a cylinder if localsizemult
   2271       1.1  dholland 		 * is not 1 sector
   2272       1.1  dholland 		 */
   2273  1.10.2.5   msaitoh 		int cylsize = parts->pscheme->get_cylinder_size(parts);
   2274       1.7    martin 		partend = NUMSEC((partstart + i*localsizemult) / localsizemult,
   2275  1.10.2.5   msaitoh 		    localsizemult, cylsize);
   2276       1.1  dholland 		/* Align to end-of-disk or end-of-slice if close enough */
   2277  1.10.2.5   msaitoh 		if (partend > (diskend - sizemult)
   2278  1.10.2.5   msaitoh 		    && partend < (diskend + sizemult))
   2279  1.10.2.5   msaitoh 			partend = diskend;
   2280       1.7    martin 		if (partend > (partstart + max - sizemult)
   2281       1.7    martin 		    && partend < (partstart + max + sizemult))
   2282       1.7    martin 			partend = partstart + max;
   2283       1.1  dholland 		/* sanity checks */
   2284  1.10.2.5   msaitoh 		if (partend > diskend) {
   2285  1.10.2.5   msaitoh 			partend = diskend;
   2286       1.7    martin 			errmsg = msg_string(MSG_endoutsidedisk);
   2287       1.7    martin 			continue;
   2288       1.1  dholland 		}
   2289       1.7    martin 		free(label_msg);
   2290       1.1  dholland 		if (partend < partstart)
   2291       1.1  dholland 			return 0;
   2292       1.1  dholland 		return (partend - partstart);
   2293       1.1  dholland 	}
   2294       1.1  dholland 	/* NOTREACHED */
   2295       1.1  dholland }
   2296       1.1  dholland 
   2297       1.1  dholland /*
   2298       1.1  dholland  * convert a string to a number of sectors, with a possible unit
   2299       1.1  dholland  * 150M = 150 Megabytes
   2300       1.1  dholland  * 2000c = 2000 cylinders
   2301       1.1  dholland  * 150256s = 150256 sectors
   2302       1.7    martin  * Without units, use the default (sizemult).
   2303       1.7    martin  * returns the raw input value, and the unit used. Caller needs to multiply!
   2304       1.7    martin  * On invalid inputs, returns -1.
   2305       1.1  dholland  */
   2306       1.7    martin daddr_t
   2307       1.7    martin parse_disk_pos(
   2308       1.7    martin 	const char *str,
   2309       1.7    martin 	daddr_t *localsizemult,
   2310  1.10.2.5   msaitoh 	daddr_t bps,
   2311       1.7    martin 	daddr_t cyl_size,
   2312       1.7    martin 	bool *extend_this)
   2313       1.1  dholland {
   2314       1.7    martin 	daddr_t val;
   2315       1.7    martin 	char *cp;
   2316       1.7    martin 	bool mult_found;
   2317       1.1  dholland 
   2318       1.1  dholland 	if (str[0] == '\0') {
   2319       1.7    martin 		return -1;
   2320       1.1  dholland 	}
   2321       1.7    martin 	val = strtoull(str, &cp, 10);
   2322       1.7    martin 	mult_found = false;
   2323       1.7    martin 	if (extend_this)
   2324       1.7    martin 		*extend_this = false;
   2325       1.7    martin 	while (*cp != 0) {
   2326       1.7    martin 		if (*cp == 'G' || *cp == 'g') {
   2327       1.7    martin 			if (mult_found)
   2328       1.7    martin 				return -1;
   2329  1.10.2.5   msaitoh 			*localsizemult = GIG / bps;
   2330       1.7    martin 			goto next;
   2331       1.1  dholland 		}
   2332       1.7    martin 		if (*cp == 'M' || *cp == 'm') {
   2333       1.7    martin 			if (mult_found)
   2334       1.7    martin 				return -1;
   2335  1.10.2.5   msaitoh 			*localsizemult = MEG / bps;
   2336       1.7    martin 			goto next;
   2337       1.1  dholland 		}
   2338       1.7    martin 		if (*cp == 'c' || *cp == 'C') {
   2339       1.7    martin 			if (mult_found)
   2340       1.7    martin 				return -1;
   2341  1.10.2.5   msaitoh 			*localsizemult = cyl_size;
   2342       1.7    martin 			goto next;
   2343       1.1  dholland 		}
   2344       1.7    martin 		if (*cp == 's' || *cp == 'S') {
   2345       1.7    martin 			if (mult_found)
   2346       1.7    martin 				return -1;
   2347       1.1  dholland 			*localsizemult = 1;
   2348       1.7    martin 			goto next;
   2349       1.7    martin 		}
   2350       1.7    martin 		if (*cp == '+' && extend_this) {
   2351       1.7    martin 			*extend_this = true;
   2352       1.7    martin 			cp++;
   2353       1.1  dholland 			break;
   2354       1.1  dholland 		}
   2355       1.7    martin 
   2356       1.1  dholland 		/* not a known unit */
   2357       1.7    martin 		return -1;
   2358       1.7    martin 
   2359       1.7    martin next:
   2360       1.7    martin 		mult_found = true;
   2361       1.7    martin 		cp++;
   2362       1.7    martin 		continue;
   2363       1.1  dholland 	}
   2364       1.7    martin 	if (*cp != 0)
   2365       1.7    martin 		return -1;
   2366       1.7    martin 
   2367       1.7    martin 	return val;
   2368       1.1  dholland }
   2369