2 * cfdisk.c - Display or manipulate a disk partition table.
4 * Copyright (C) 2014-2015 Karel Zak <kzak@redhat.com>
5 * Copyright (C) 1994 Kevin E. Martin (martin@cs.unc.edu)
7 * The original cfdisk was inspired by the fdisk program
8 * by A. V. Le Blanc (leblanc@mcc.ac.uk.
10 * cfdisk is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
22 #include <libsmartcols.h>
23 #include <sys/ioctl.h>
27 # include <libmount.h> /* keep it optional for non-linux systems */
32 #elif defined(HAVE_SLANG_SLANG_H)
33 # include <slang/slang.h>
36 #ifdef HAVE_SLCURSES_H
37 # include <slcurses.h>
38 #elif defined(HAVE_SLANG_SLCURSES_H)
39 # include <slang/slcurses.h>
40 #elif defined(HAVE_NCURSESW_NCURSES_H) && defined(HAVE_WIDECHAR)
41 # include <ncursesw/ncurses.h>
42 #elif defined(HAVE_NCURSES_H)
44 #elif defined(HAVE_NCURSES_NCURSES_H)
45 # include <ncurses/ncurses.h>
54 #include "closestream.h"
64 static const char *default_disks
[] = {
68 #elif defined(__FreeBSD__)
78 #define ARROW_CURSOR_STRING ">> "
79 #define ARROW_CURSOR_DUMMY " "
80 #define ARROW_CURSOR_WIDTH (sizeof(ARROW_CURSOR_STRING) - 1)
83 #define MENU_V_SPADDING 1 /* space around menu item string */
86 #define MENU_H_SPADDING 0 /* space around menu item string */
87 #define MENU_H_BETWEEN 2 /* space between menu items */
88 #define MENU_H_PRESTR "["
89 #define MENU_H_POSTSTR "]"
91 #define MENU_TITLE_PADDING 3
93 #define MENU_H_PRESTR_SZ (sizeof(MENU_H_PRESTR) - 1)
94 #define MENU_H_POSTSTR_SZ (sizeof(MENU_H_POSTSTR) - 1)
96 #define TABLE_START_LINE 4
97 #define MENU_START_LINE (ui_lines - 4) /* The menu maybe use two lines */
98 #define INFO_LINE (ui_lines - 2)
99 #define WARN_LINE INFO_LINE
100 #define HINT_LINE (ui_lines - 1)
102 #define CFDISK_ERR_ESC 5000
105 # define KEY_ESC '\033'
108 # define KEY_DELETE '\177'
122 static const int color_pairs
[][2] = {
123 /* color foreground, background */
124 [CFDISK_CL_WARNING
] = { COLOR_RED
, -1 },
125 [CFDISK_CL_FREESPACE
] = { COLOR_GREEN
, -1 },
126 [CFDISK_CL_INFO
] = { COLOR_BLUE
, -1 }
131 static struct cfdisk_menuitem
*menu_get_menuitem(struct cfdisk
*cf
, size_t idx
);
132 static struct cfdisk_menuitem
*menu_get_menuitem_by_key(struct cfdisk
*cf
, int key
, size_t *idx
);
133 static struct cfdisk_menu
*menu_push(struct cfdisk
*cf
, struct cfdisk_menuitem
*item
);
134 static struct cfdisk_menu
*menu_pop(struct cfdisk
*cf
);
135 static void menu_refresh_size(struct cfdisk
*cf
);
137 static int ui_end(void);
138 static int ui_refresh(struct cfdisk
*cf
);
139 static void ui_warnx(const char *fmt
, ...);
140 static void ui_warn(const char *fmt
, ...);
141 static void ui_info(const char *fmt
, ...);
142 static void ui_draw_menu(struct cfdisk
*cf
);
143 static int ui_menu_move(struct cfdisk
*cf
, int key
);
144 static void ui_menu_resize(struct cfdisk
*cf
);
146 static int ui_get_size(struct cfdisk
*cf
, const char *prompt
, uintmax_t *res
,
147 uintmax_t low
, uintmax_t up
, int *expsize
);
149 static int ui_enabled
;
150 static volatile sig_atomic_t sig_resize
;
151 static volatile sig_atomic_t sig_die
;
153 /* ncurses LINES and COLS may be actual variables or *macros*, but we need
154 * something portable and writable */
155 static size_t ui_lines
;
156 static size_t ui_cols
;
159 struct cfdisk_menuitem
{
160 int key
; /* keyboard shortcut */
161 const char *name
; /* item name */
162 const char *desc
; /* item description (hint) */
168 char *title
; /* optional menu title */
169 struct cfdisk_menuitem
*items
; /* array with menu items */
170 char *ignore
;/* string with keys to ignore */
171 size_t width
; /* maximal width of the menu item */
172 size_t nitems
; /* number of the active menu items */
173 size_t page_sz
;/* when menu longer than screen */
174 size_t idx
; /* the current menu item */
175 int prefkey
;/* preferred menu item */
176 struct cfdisk_menu
*prev
;
178 /* @ignore keys generator */
179 int (*ignore_cb
) (struct cfdisk
*, char *, size_t);
181 unsigned int vertical
: 1; /* enable vertical mode */
185 static struct cfdisk_menuitem main_menuitems
[] = {
186 { 'b', N_("Bootable"), N_("Toggle bootable flag of the current partition") },
187 { 'd', N_("Delete"), N_("Delete the current partition") },
188 { 'r', N_("Resize"), N_("Reduce or enlarge the current partition") },
189 { 'n', N_("New"), N_("Create new partition from free space") },
190 { 'q', N_("Quit"), N_("Quit program without writing changes") },
191 { 't', N_("Type"), N_("Change the partition type") },
192 { 'h', N_("Help"), N_("Print help screen") },
193 { 's', N_("Sort"), N_("Fix partitions order") },
194 { 'W', N_("Write"), N_("Write partition table to disk (this might destroy data)") },
195 { 'u', N_("Dump"), N_("Dump partition table to sfdisk compatible script file") },
199 /* extra partinfo in name:value pairs */
200 struct cfdisk_extra
{
204 struct list_head exs
;
207 /* line and extra partinfo list_head */
209 char *data
; /* line data */
210 struct libscols_table
*extra
; /* extra info ('X') */
211 WINDOW
*w
; /* window with extra info */
214 /* top level control struct */
216 struct fdisk_context
*cxt
; /* libfdisk context */
217 struct fdisk_table
*table
; /* partition table */
218 struct fdisk_table
*original_layout
; /* original on-disk PT */
220 struct cfdisk_menu
*menu
; /* the current menu */
222 int *fields
; /* output columns IDs */
223 size_t nfields
; /* number of columns IDs */
225 char *linesbuf
; /* table as string */
226 size_t linesbufsz
; /* size of the tb_buf */
228 struct cfdisk_line
*lines
; /* list of lines */
230 size_t nlines
; /* number of lines */
231 size_t lines_idx
; /* current line <0..N>, exclude header */
234 unsigned int nwrites
; /* fdisk_write_disklabel() counter */
236 WINDOW
*act_win
; /* the window currently on the screen */
239 struct libmnt_table
*mtab
;
240 struct libmnt_table
*fstab
;
241 struct libmnt_cache
*mntcache
;
243 unsigned int wrong_order
:1, /* PT not in right order */
244 zero_start
:1, /* ignore existing partition table */
245 device_is_used
: 1, /* don't use re-read ioctl */
246 show_extra
:1; /* show extra partinfo */
251 * let's use include/debug.h stuff for cfdisk too
253 static UL_DEBUG_DEFINE_MASK(cfdisk
);
254 UL_DEBUG_DEFINE_MASKNAMES(cfdisk
) = UL_DEBUG_EMPTY_MASKNAMES
;
256 #define CFDISK_DEBUG_INIT (1 << 1)
257 #define CFDISK_DEBUG_UI (1 << 2)
258 #define CFDISK_DEBUG_MENU (1 << 3)
259 #define CFDISK_DEBUG_MISC (1 << 4)
260 #define CFDISK_DEBUG_TABLE (1 << 5)
261 #define CFDISK_DEBUG_ALL 0xFFFF
263 #define DBG(m, x) __UL_DBG(cfdisk, CFDISK_DEBUG_, m, x)
265 static void cfdisk_init_debug(void)
267 __UL_INIT_DEBUG(cfdisk
, CFDISK_DEBUG_
, 0, CFDISK_DEBUG
);
270 /* Initialize output columns -- we follow libfdisk fields (usually specific
273 static int cols_init(struct cfdisk
*cf
)
281 return fdisk_label_get_fields_ids(NULL
, cf
->cxt
, &cf
->fields
, &cf
->nfields
);
284 static void die_on_signal(void)
286 DBG(MISC
, ul_debug("die on signal."));
291 static void resize(void)
295 if (ioctl(fileno(stdout
), TIOCGWINSZ
, &ws
) != -1
296 && ws
.ws_row
&& ws
.ws_col
) {
297 ui_lines
= ws
.ws_row
;
300 resizeterm(ws
.ws_row
, ws
.ws_col
);
302 clearok(stdscr
, TRUE
);
306 DBG(UI
, ul_debug("ui: resize refresh ui_cols=%zu, ui_lines=%zu",
311 /* Reads partition in tree-like order from scols
313 static int partition_from_scols(struct fdisk_table
*tb
,
314 struct libscols_line
*ln
)
316 struct fdisk_partition
*pa
= scols_line_get_userdata(ln
);
318 fdisk_table_add_partition(tb
, pa
);
319 fdisk_unref_partition(pa
);
321 if (scols_line_has_children(ln
)) {
322 struct libscols_line
*chln
;
323 struct libscols_iter
*itr
= scols_new_iter(SCOLS_ITER_FORWARD
);
327 while (scols_line_next_child(ln
, itr
, &chln
) == 0)
328 partition_from_scols(tb
, chln
);
329 scols_free_iter(itr
);
334 static char *table_to_string(struct cfdisk
*cf
, struct fdisk_table
*tb
)
336 struct fdisk_partition
*pa
;
337 struct fdisk_label
*lb
;
338 struct fdisk_iter
*itr
;
339 struct libscols_table
*table
= NULL
;
340 struct libscols_iter
*s_itr
= NULL
;
344 struct libscols_line
*ln
, *ln_cont
= NULL
;
346 DBG(TABLE
, ul_debug("convert to string"));
353 lb
= fdisk_get_label(cf
->cxt
, NULL
);
356 itr
= fdisk_new_iter(FDISK_ITER_FORWARD
);
360 /* get container (e.g. extended partition) */
361 while (fdisk_table_next_partition(tb
, itr
, &pa
) == 0) {
362 if (fdisk_partition_is_nested(pa
)) {
363 DBG(TABLE
, ul_debug("nested detected, using tree"));
364 tree
= SCOLS_FL_TREE
;
369 table
= scols_new_table();
372 scols_table_enable_maxout(table
, 1);
373 scols_table_enable_nowrap(table
, 1);
375 #if !defined(HAVE_LIBNCURSESW) || !defined(HAVE_WIDECHAR)
376 scols_table_enable_ascii(table
, 1);
380 for (i
= 0; i
< cf
->nfields
; i
++) {
382 const struct fdisk_field
*field
=
383 fdisk_label_get_field(lb
, cf
->fields
[i
]);
387 if (fdisk_field_is_number(field
))
388 fl
|= SCOLS_FL_RIGHT
;
389 if (fdisk_field_get_id(field
) == FDISK_FIELD_TYPE
)
390 fl
|= SCOLS_FL_TRUNC
;
391 if (tree
&& fdisk_field_get_id(field
) == FDISK_FIELD_DEVICE
)
394 if (!scols_table_new_column(table
,
395 _(fdisk_field_get_name(field
)),
396 fdisk_field_get_width(field
), fl
))
401 fdisk_reset_iter(itr
, FDISK_ITER_FORWARD
);
403 while (fdisk_table_next_partition(tb
, itr
, &pa
) == 0) {
404 struct libscols_line
*parent
= fdisk_partition_is_nested(pa
) ? ln_cont
: NULL
;
406 ln
= scols_table_new_line(table
, parent
);
409 for (i
= 0; i
< cf
->nfields
; i
++) {
412 if (fdisk_partition_to_string(pa
, cf
->cxt
,
413 cf
->fields
[i
], &cdata
))
415 scols_line_refer_data(ln
, i
, cdata
);
417 if (tree
&& fdisk_partition_is_container(pa
))
420 scols_line_set_userdata(ln
, (void *) pa
);
421 fdisk_ref_partition(pa
);
424 if (scols_table_is_empty(table
))
427 scols_table_reduce_termwidth(table
, ARROW_CURSOR_WIDTH
);
428 scols_print_table_to_string(table
, &res
);
430 /* scols_* code might reorder lines, let's reorder @tb according to the
431 * final output (it's no problem because partitions are addressed by
432 * parno stored within struct fdisk_partition) */
435 fdisk_reset_iter(itr
, FDISK_ITER_FORWARD
);
436 while (fdisk_table_next_partition(tb
, itr
, &pa
) == 0)
437 fdisk_table_remove_partition(tb
, pa
);
439 s_itr
= scols_new_iter(SCOLS_ITER_FORWARD
);
443 /* add all in the right order (don't forget the output is tree) */
444 while (scols_table_next_line(table
, s_itr
, &ln
) == 0) {
445 if (scols_line_get_parent(ln
))
447 if (partition_from_scols(tb
, ln
))
451 scols_unref_table(table
);
452 scols_free_iter(s_itr
);
453 fdisk_free_iter(itr
);
458 static void cfdisk_free_lines(struct cfdisk
*cf
)
461 while(i
< cf
->nlines
) {
462 scols_unref_table(cf
->lines
[i
].extra
);
464 DBG(UI
, ul_debug("delete window: %p",
468 delwin(cf
->lines
[i
].w
);
469 cf
->lines
[i
].w
= NULL
;
477 * Read data about partitions from libfdisk and prepare output lines.
479 static int lines_refresh(struct cfdisk
*cf
)
487 DBG(TABLE
, ul_debug("refreshing buffer"));
490 cfdisk_free_lines(cf
);
496 fdisk_unref_table(cf
->table
);
499 /* read partitions and free spaces into cf->table */
500 rc
= fdisk_get_partitions(cf
->cxt
, &cf
->table
);
502 rc
= fdisk_get_freespaces(cf
->cxt
, &cf
->table
);
506 cf
->linesbuf
= table_to_string(cf
, cf
->table
);
510 cf
->linesbufsz
= strlen(cf
->linesbuf
);
511 cf
->nlines
= fdisk_table_get_nents(cf
->table
) + 1; /* 1 for header line */
513 cf
->wrong_order
= fdisk_table_wrong_order(cf
->table
) ? 1 : 0;
515 if (MENU_START_LINE
- TABLE_START_LINE
< cf
->nlines
)
516 cf
->page_sz
= MENU_START_LINE
- TABLE_START_LINE
- 1;
518 cf
->lines
= xcalloc(cf
->nlines
, sizeof(struct cfdisk_line
));
520 for (p
= cf
->linesbuf
, i
= 0; p
&& i
< cf
->nlines
; i
++) {
521 cf
->lines
[i
].data
= p
;
527 cf
->lines
[i
].extra
= scols_new_table();
528 scols_table_enable_noheadings(cf
->lines
[i
].extra
, 1);
529 scols_table_new_column(cf
->lines
[i
].extra
, NULL
, 0, SCOLS_FL_RIGHT
);
530 scols_table_new_column(cf
->lines
[i
].extra
, NULL
, 0, SCOLS_FL_TRUNC
);
536 static struct fdisk_partition
*get_current_partition(struct cfdisk
*cf
)
541 return fdisk_table_get_partition(cf
->table
, cf
->lines_idx
);
544 static int is_freespace(struct cfdisk
*cf
, size_t i
)
546 struct fdisk_partition
*pa
;
551 pa
= fdisk_table_get_partition(cf
->table
, i
);
552 return fdisk_partition_is_freespace(pa
);
555 /* converts libfdisk FDISK_ASKTYPE_MENU to cfdisk menu and returns user's
556 * response back to libfdisk
558 static int ask_menu(struct fdisk_ask
*ask
, struct cfdisk
*cf
)
560 struct cfdisk_menuitem
*d
, *cm
;
562 size_t i
= 0, nitems
;
563 const char *name
, *desc
;
568 /* create cfdisk menu according to libfdisk ask-menu, note that the
569 * last cm[] item has to be empty -- so nitems + 1 */
570 nitems
= fdisk_ask_menu_get_nitems(ask
);
571 cm
= xcalloc(nitems
+ 1, sizeof(struct cfdisk_menuitem
));
573 for (i
= 0; i
< nitems
; i
++) {
574 if (fdisk_ask_menu_get_item(ask
, i
, &key
, &name
, &desc
))
581 /* make the new menu active */
594 if (ui_menu_move(cf
, key
) == 0)
601 d
= menu_get_menuitem(cf
, cf
->menu
->idx
);
603 fdisk_ask_menu_set_result(ask
, d
->key
);
620 static int ask_callback(struct fdisk_context
*cxt
__attribute__((__unused__
)),
621 struct fdisk_ask
*ask
,
622 void *data
__attribute__((__unused__
)))
628 switch(fdisk_ask_get_type(ask
)) {
629 case FDISK_ASKTYPE_INFO
:
630 ui_info(fdisk_ask_print_get_mesg(ask
));
632 case FDISK_ASKTYPE_WARNX
:
633 ui_warnx(fdisk_ask_print_get_mesg(ask
));
635 case FDISK_ASKTYPE_WARN
:
636 ui_warn(fdisk_ask_print_get_mesg(ask
));
638 case FDISK_ASKTYPE_MENU
:
639 ask_menu(ask
, (struct cfdisk
*) data
);
642 ui_warnx(_("internal error: unsupported dialog type %d"),
643 fdisk_ask_get_type(ask
));
649 static int ui_end(void)
654 #if defined(HAVE_SLCURSES_H) || defined(HAVE_SLANG_SLCURSES_H)
655 SLsmg_gotorc(ui_lines
- 1, 0);
658 mvcur(0, ui_cols
- 1, ui_lines
-1, 0);
668 static void ui_vprint_center(size_t line
, int attrs
, const char *fmt
, va_list ap
)
676 xvasprintf(&buf
, fmt
, ap
);
678 width
= mbs_safe_width(buf
);
679 if (width
> (size_t) ui_cols
) {
680 char *p
= strrchr(buf
+ ui_cols
, ' ');
684 if (line
+ 1 >= ui_lines
)
687 mvaddstr(line
, 0, buf
);
688 mvaddstr(line
+ 1, 0, p
+1);
692 mvaddstr(line
, (ui_cols
- width
) / 2, buf
);
698 static void ui_center(size_t line
, const char *fmt
, ...)
702 ui_vprint_center(line
, 0, fmt
, ap
);
706 static void ui_warnx(const char *fmt
, ...)
711 ui_vprint_center(WARN_LINE
,
712 colors_wanted() ? COLOR_PAIR(CFDISK_CL_WARNING
) : 0,
715 vfprintf(stderr
, fmt
, ap
);
721 static void ui_warn(const char *fmt
, ...)
726 xasprintf(&fmt_m
, "%s: %m", fmt
);
730 ui_vprint_center(WARN_LINE
,
731 colors_wanted() ? COLOR_PAIR(CFDISK_CL_WARNING
) : 0,
734 vfprintf(stderr
, fmt_m
, ap
);
741 static void ui_clean_warn(void)
747 static int __attribute__((__noreturn__
)) ui_errx(int rc
, const char *fmt
, ...)
753 fprintf(stderr
, "%s: ", program_invocation_short_name
);
754 vfprintf(stderr
, fmt
, ap
);
761 static void ui_info(const char *fmt
, ...)
766 ui_vprint_center(INFO_LINE
,
767 colors_wanted() ? COLOR_PAIR(CFDISK_CL_INFO
) : 0,
770 vfprintf(stdout
, fmt
, ap
);
776 static void ui_clean_info(void)
782 static void ui_hint(const char *fmt
, ...)
787 ui_vprint_center(HINT_LINE
, A_BOLD
, fmt
, ap
);
789 vfprintf(stdout
, fmt
, ap
);
795 static void ui_clean_hint(void)
802 static void sig_handler_die(int dummy
__attribute__((__unused__
)))
807 static void sig_handler_resize(int dummy
__attribute__((__unused__
)))
812 static void menu_refresh_size(struct cfdisk
*cf
)
814 if (cf
->menu
&& cf
->menu
->nitems
)
815 cf
->menu
->page_sz
= (cf
->menu
->nitems
/ (ui_lines
- 4)) ? ui_lines
- 4 : 0;
818 static void menu_update_ignore(struct cfdisk
*cf
)
820 char ignore
[128] = { 0 };
822 struct cfdisk_menu
*m
;
823 struct cfdisk_menuitem
*d
, *org
= NULL
;
828 assert(cf
->menu
->ignore_cb
);
831 DBG(MENU
, ul_debug("update menu ignored keys"));
833 i
= m
->ignore_cb(cf
, ignore
, sizeof(ignore
));
836 /* return if no change */
837 if ((!m
->ignore
&& !*ignore
)
838 || (m
->ignore
&& *ignore
&& strcmp(m
->ignore
, ignore
) == 0)) {
843 org
= menu_get_menuitem(cf
, m
->idx
);
846 m
->ignore
= xstrdup(ignore
);
849 for (d
= m
->items
; d
->name
; d
++) {
850 if (m
->ignore
&& strchr(m
->ignore
, d
->key
))
855 DBG(MENU
, ul_debug("update menu preferred keys"));
857 /* refresh menu index to be at the same menuitem or go to the first */
858 if (org
&& menu_get_menuitem_by_key(cf
, org
->key
, &idx
))
860 else if (m
->prefkey
&& menu_get_menuitem_by_key(cf
, m
->prefkey
, &idx
))
865 menu_refresh_size(cf
);
868 static struct cfdisk_menu
*menu_push(
870 struct cfdisk_menuitem
*items
)
872 struct cfdisk_menu
*m
= xcalloc(1, sizeof(*m
));
873 struct cfdisk_menuitem
*d
;
877 DBG(MENU
, ul_debug("new menu"));
882 for (d
= m
->items
; d
->name
; d
++) {
883 const char *name
= _(d
->name
);
884 size_t len
= mbs_safe_width(name
);
892 menu_refresh_size(cf
);
896 static struct cfdisk_menu
*menu_pop(struct cfdisk
*cf
)
898 struct cfdisk_menu
*m
= NULL
;
902 DBG(MENU
, ul_debug("pop menu"));
906 free(cf
->menu
->ignore
);
907 free(cf
->menu
->title
);
914 static void menu_set_title(struct cfdisk_menu
*m
, const char *title
)
919 size_t len
= mbs_safe_width(title
);
920 if (len
+ MENU_TITLE_PADDING
> m
->width
)
921 m
->width
= len
+ MENU_TITLE_PADDING
;
922 str
= xstrdup(title
);
928 static int ui_init(struct cfdisk
*cf
__attribute__((__unused__
)))
932 DBG(UI
, ul_debug("init"));
934 /* setup SIGCHLD handler */
935 sigemptyset(&sa
.sa_mask
);
937 sa
.sa_handler
= sig_handler_die
;
938 sigaction(SIGINT
, &sa
, NULL
);
939 sigaction(SIGTERM
, &sa
, NULL
);
941 sa
.sa_handler
= sig_handler_resize
;
942 sigaction(SIGWINCH
, &sa
, NULL
);
947 #ifdef HAVE_USE_DEFAULT_COLORS
948 if (colors_wanted() && has_colors()) {
952 use_default_colors();
953 for (i
= 1; i
< ARRAY_SIZE(color_pairs
); i
++) /* yeah, start from 1! */
954 init_pair(i
, color_pairs
[i
][0], color_pairs
[i
][1]);
964 keypad(stdscr
, TRUE
);
970 #define MENU_H_ITEMWIDTH(m) ( MENU_H_PRESTR_SZ \
976 #define MENU_V_ITEMWIDTH(m) (MENU_V_SPADDING + (m)->width + MENU_V_SPADDING)
979 static size_t menuitem_get_line(struct cfdisk
*cf
, size_t idx
)
981 struct cfdisk_menu
*m
= cf
->menu
;
984 if (!m
->page_sz
) /* small menu */
985 return (ui_lines
- (cf
->menu
->nitems
+ 1)) / 2 + idx
;
986 return (idx
% m
->page_sz
) + 1;
988 size_t len
= MENU_H_ITEMWIDTH(m
) + MENU_H_BETWEEN
; /** item width */
989 size_t items
= ui_cols
/ len
; /* items per line */
993 return MENU_START_LINE
+ ((idx
/ items
));
997 static int menuitem_get_column(struct cfdisk
*cf
, size_t idx
)
999 if (cf
->menu
->vertical
) {
1000 size_t nc
= MENU_V_ITEMWIDTH(cf
->menu
);
1001 if ((size_t) ui_cols
<= nc
)
1003 return (ui_cols
- nc
) / 2;
1005 size_t len
= MENU_H_ITEMWIDTH(cf
->menu
) + MENU_H_BETWEEN
; /* item width */
1006 size_t items
= ui_cols
/ len
; /* items per line */
1007 size_t extra
= items
< cf
->menu
->nitems
? /* extra space on line */
1008 ui_cols
% len
: /* - multi-line menu */
1009 ui_cols
- (cf
->menu
->nitems
* len
); /* - one line menu */
1012 return 0; /* hmm... no space */
1014 extra
+= MENU_H_BETWEEN
; /* add padding after last item to extra */
1017 return (idx
* len
) + (extra
/ 2);
1018 return ((idx
% items
) * len
) + (extra
/ 2);
1022 static int menuitem_on_page(struct cfdisk
*cf
, size_t idx
)
1024 struct cfdisk_menu
*m
= cf
->menu
;
1026 if (m
->page_sz
== 0 ||
1027 m
->idx
/ m
->page_sz
== idx
/ m
->page_sz
)
1032 static struct cfdisk_menuitem
*menu_get_menuitem(struct cfdisk
*cf
, size_t idx
)
1034 struct cfdisk_menuitem
*d
;
1037 for (i
= 0, d
= cf
->menu
->items
; d
->name
; d
++) {
1038 if (cf
->menu
->ignore
&& strchr(cf
->menu
->ignore
, d
->key
))
1047 static struct cfdisk_menuitem
*menu_get_menuitem_by_key(struct cfdisk
*cf
,
1048 int key
, size_t *idx
)
1050 struct cfdisk_menuitem
*d
;
1052 for (*idx
= 0, d
= cf
->menu
->items
; d
->name
; d
++) {
1053 if (cf
->menu
->ignore
&& strchr(cf
->menu
->ignore
, d
->key
))
1063 static void ui_draw_menuitem(struct cfdisk
*cf
,
1064 struct cfdisk_menuitem
*d
,
1070 const size_t buf_sz
= 80 * MB_CUR_MAX
;
1071 int ln
, cl
, vert
= cf
->menu
->vertical
;
1073 if (!menuitem_on_page(cf
, idx
))
1074 return; /* no visible item */
1075 ln
= menuitem_get_line(cf
, idx
);
1076 cl
= menuitem_get_column(cf
, idx
);
1078 ptr
= buf
= xmalloc(buf_sz
);
1081 width
= cf
->menu
->width
+ MENU_V_SPADDING
;
1082 memset(ptr
, ' ', MENU_V_SPADDING
);
1083 ptr
+= MENU_V_SPADDING
;
1085 width
= MENU_H_SPADDING
+ cf
->menu
->width
+ MENU_H_SPADDING
;
1088 mbsalign(name
, ptr
, buf_sz
, &width
,
1089 vert
? MBS_ALIGN_LEFT
: MBS_ALIGN_CENTER
,
1092 DBG(MENU
, ul_debug("menuitem: cl=%d, ln=%d, item='%s'",
1096 mvaddch(ln
, cl
- 1, ACS_VLINE
);
1097 mvaddch(ln
, cl
+ MENU_V_ITEMWIDTH(cf
->menu
), ACS_VLINE
);
1100 if (cf
->menu
->idx
== idx
)
1104 mvprintw(ln
, cl
, "%s", buf
);
1106 mvprintw(ln
, cl
, "%s%s%s", MENU_H_PRESTR
, buf
, MENU_H_POSTSTR
);
1109 if (cf
->menu
->idx
== idx
) {
1112 ui_hint(_(d
->desc
));
1116 static void ui_clean_menu(struct cfdisk
*cf
)
1120 struct cfdisk_menu
*m
= cf
->menu
;
1121 size_t ln
= menuitem_get_line(cf
, 0);
1124 lastline
= ln
+ (m
->page_sz
? m
->page_sz
: m
->nitems
);
1126 lastline
= menuitem_get_line(cf
, m
->nitems
);
1128 for (i
= ln
; i
<= lastline
; i
++) {
1131 DBG(MENU
, ul_debug("clean_menu: line %zu", i
));
1140 static void ui_draw_menu(struct cfdisk
*cf
)
1142 struct cfdisk_menuitem
*d
;
1143 struct cfdisk_menu
*m
;
1145 size_t ln
= menuitem_get_line(cf
, 0);
1151 DBG(MENU
, ul_debug("draw start"));
1157 nlines
= m
->page_sz
? m
->page_sz
: m
->nitems
;
1159 nlines
= menuitem_get_line(cf
, m
->nitems
);
1162 menu_update_ignore(cf
);
1164 while ((d
= menu_get_menuitem(cf
, i
)))
1165 ui_draw_menuitem(cf
, d
, i
++);
1168 size_t cl
= menuitem_get_column(cf
, 0);
1169 size_t curpg
= m
->page_sz
? m
->idx
/ m
->page_sz
: 0;
1171 /* corners and horizontal lines */
1172 mvaddch(ln
- 1, cl
- 1, ACS_ULCORNER
);
1173 mvaddch(ln
+ nlines
, cl
- 1, ACS_LLCORNER
);
1175 for (i
= 0; i
< MENU_V_ITEMWIDTH(m
); i
++) {
1176 mvaddch(ln
- 1, cl
+ i
, ACS_HLINE
);
1177 mvaddch(ln
+ nlines
, cl
+ i
, ACS_HLINE
);
1180 mvaddch(ln
- 1, cl
+ i
, ACS_URCORNER
);
1181 mvaddch(ln
+ nlines
, cl
+ i
, ACS_LRCORNER
);
1183 /* draw also lines around empty lines on last page */
1185 m
->nitems
/ m
->page_sz
== m
->idx
/ m
->page_sz
) {
1186 for (i
= m
->nitems
% m
->page_sz
+ 1; i
<= m
->page_sz
; i
++) {
1187 mvaddch(i
, cl
- 1, ACS_VLINE
);
1188 mvaddch(i
, cl
+ MENU_V_ITEMWIDTH(m
), ACS_VLINE
);
1193 mvprintw(ln
- 1, cl
, " %s ", m
->title
);
1197 mvaddch(ln
- 1, cl
+ MENU_V_ITEMWIDTH(m
) - 2, ACS_UARROW
);
1198 if (m
->page_sz
&& curpg
< m
->nitems
/ m
->page_sz
)
1199 mvaddch(ln
+ nlines
, cl
+ MENU_V_ITEMWIDTH(m
) - 2, ACS_DARROW
);
1202 DBG(MENU
, ul_debug("draw end."));
1205 inline static int extra_insert_pair(struct cfdisk_line
*l
, const char *name
, const char *data
)
1207 struct libscols_line
*lsl
;
1213 if (!data
|| !*data
)
1216 lsl
= scols_table_new_line(l
->extra
, NULL
);
1220 rc
= scols_line_set_data(lsl
, 0, name
);
1222 rc
= scols_line_set_data(lsl
, 1, data
);
1227 #ifndef HAVE_LIBMOUNT
1228 static char *get_mountpoint( struct cfdisk
*cf
__attribute__((unused
)),
1229 const char *tagname
__attribute__((unused
)),
1230 const char *tagdata
__attribute__((unused
)))
1235 static char *get_mountpoint(struct cfdisk
*cf
, const char *tagname
, const char *tagdata
)
1237 struct libmnt_fs
*fs
= NULL
;
1238 char *target
= NULL
;
1244 DBG(UI
, ul_debug("asking for mountpoint [%s=%s]", tagname
, tagdata
));
1247 cf
->mntcache
= mnt_new_cache();
1249 /* 1st try between mounted filesystems */
1251 cf
->mtab
= mnt_new_table();
1253 mnt_table_set_cache(cf
->mtab
, cf
->mntcache
);
1254 mnt_table_parse_mtab(cf
->mtab
, NULL
);
1259 fs
= mnt_table_find_tag(cf
->mtab
, tagname
, tagdata
, MNT_ITER_FORWARD
);
1264 cf
->fstab
= mnt_new_table();
1266 mnt_table_set_cache(cf
->fstab
, cf
->mntcache
);
1267 mnt_table_parse_fstab(cf
->fstab
, NULL
);
1271 fs
= mnt_table_find_tag(cf
->fstab
, tagname
, tagdata
, MNT_ITER_FORWARD
);
1277 xasprintf(&target
, _("%s (mounted)"), mnt_fs_get_target(fs
));
1279 target
= xstrdup(mnt_fs_get_target(fs
));
1284 #endif /* HAVE_LIBMOUNT */
1286 static void extra_prepare_data(struct cfdisk
*cf
)
1288 struct fdisk_partition
*pa
= get_current_partition(cf
);
1289 struct cfdisk_line
*l
= &cf
->lines
[cf
->lines_idx
];
1291 char *mountpoint
= NULL
;
1293 DBG(UI
, ul_debug("preparing extra data"));
1295 /* string data should not equal an empty string */
1296 if (!fdisk_partition_to_string(pa
, cf
->cxt
, FDISK_FIELD_NAME
, &data
) && data
) {
1297 extra_insert_pair(l
, _("Partition name:"), data
);
1299 mountpoint
= get_mountpoint(cf
, "PARTLABEL", data
);
1303 if (!fdisk_partition_to_string(pa
, cf
->cxt
, FDISK_FIELD_UUID
, &data
) && data
) {
1304 extra_insert_pair(l
, _("Partition UUID:"), data
);
1306 mountpoint
= get_mountpoint(cf
, "PARTUUID", data
);
1310 if (!fdisk_partition_to_string(pa
, cf
->cxt
, FDISK_FIELD_TYPE
, &data
) && data
) {
1311 char *code
= NULL
, *type
= NULL
;
1313 fdisk_partition_to_string(pa
, cf
->cxt
, FDISK_FIELD_TYPEID
, &code
);
1314 xasprintf(&type
, "%s (%s)", data
, code
);
1316 extra_insert_pair(l
, _("Partition type:"), type
);
1322 if (!fdisk_partition_to_string(pa
, cf
->cxt
, FDISK_FIELD_ATTR
, &data
) && data
) {
1323 extra_insert_pair(l
, _("Attributes:"), data
);
1327 /* for numeric data, only show non-zero rows */
1328 if (!fdisk_partition_to_string(pa
, cf
->cxt
, FDISK_FIELD_BSIZE
, &data
) && data
) {
1330 extra_insert_pair(l
, "BSIZE:", data
);
1334 if (!fdisk_partition_to_string(pa
, cf
->cxt
, FDISK_FIELD_CPG
, &data
) && data
) {
1336 extra_insert_pair(l
, "CPG:", data
);
1340 if (!fdisk_partition_to_string(pa
, cf
->cxt
, FDISK_FIELD_FSIZE
, &data
) && data
) {
1342 extra_insert_pair(l
, "FSIZE:", data
);
1346 if (!fdisk_partition_to_string(pa
, cf
->cxt
, FDISK_FIELD_FSUUID
, &data
) && data
) {
1347 extra_insert_pair(l
, _("Filesystem UUID:"), data
);
1349 mountpoint
= get_mountpoint(cf
, "UUID", data
);
1353 if (!fdisk_partition_to_string(pa
, cf
->cxt
, FDISK_FIELD_FSLABEL
, &data
) && data
) {
1354 extra_insert_pair(l
, _("Filesystem LABEL:"), data
);
1356 mountpoint
= get_mountpoint(cf
, "LABEL", data
);
1359 if (!fdisk_partition_to_string(pa
, cf
->cxt
, FDISK_FIELD_FSTYPE
, &data
) && data
) {
1360 extra_insert_pair(l
, _("Filesystem:"), data
);
1365 extra_insert_pair(l
, _("Mountpoint:"), mountpoint
);
1370 static int ui_draw_extra(struct cfdisk
*cf
)
1374 struct cfdisk_line
*ln
= &cf
->lines
[cf
->lines_idx
];
1375 char *tbstr
= NULL
, *end
;
1376 int win_ex_start_line
, win_height
, tblen
;
1379 if (!cf
->show_extra
)
1382 DBG(UI
, ul_debug("draw extra"));
1387 wclear(cf
->act_win
);
1391 if (scols_table_is_empty(ln
->extra
)) {
1392 extra_prepare_data(cf
);
1393 if (scols_table_is_empty(ln
->extra
))
1397 ndatalines
= fdisk_table_get_nents(cf
->table
) + 1;
1399 /* nents + header + one free line */
1400 win_ex_start_line
= TABLE_START_LINE
+ ndatalines
;
1401 win_height
= MENU_START_LINE
- win_ex_start_line
;
1402 tblen
= scols_table_get_nlines(ln
->extra
);
1404 /* we can't get a single line of data under the partlist*/
1408 /* number of data lines + 2 for top/bottom lines */
1409 win_height
= win_height
< tblen
+ 2 ? win_height
: tblen
+ 2;
1411 if ((size_t) win_ex_start_line
+ win_height
+ 1 < MENU_START_LINE
)
1412 win_ex_start_line
= MENU_START_LINE
- win_height
;
1414 win_ex
= subwin(stdscr
, win_height
, ui_cols
- 2, win_ex_start_line
, 1);
1416 scols_table_reduce_termwidth(ln
->extra
, 4);
1417 scols_print_table_to_string(ln
->extra
, &tbstr
);
1420 while ((end
= strchr(end
, '\n')))
1426 while (--win_height
> 1) {
1427 mvwaddstr(win_ex
, wline
++, 1 /* window column*/, tbstr
);
1428 tbstr
+= strlen(tbstr
) + 1;
1435 DBG(UI
, ul_debug("draw window: %p", win_ex
));
1439 cf
->act_win
= ln
->w
= win_ex
;
1443 static void ui_menu_goto(struct cfdisk
*cf
, int where
)
1445 struct cfdisk_menuitem
*d
;
1448 /* stop and begin/end for vertical menus */
1449 if (cf
->menu
->vertical
) {
1452 else if (where
> (int) cf
->menu
->nitems
- 1)
1453 where
= cf
->menu
->nitems
- 1;
1455 /* continue from begin/end */
1457 where
= cf
->menu
->nitems
- 1;
1458 else if ((size_t) where
> cf
->menu
->nitems
- 1)
1461 if ((size_t) where
== cf
->menu
->idx
)
1466 old
= cf
->menu
->idx
;
1467 cf
->menu
->idx
= where
;
1469 if (!menuitem_on_page(cf
, old
)) {
1474 d
= menu_get_menuitem(cf
, old
);
1475 ui_draw_menuitem(cf
, d
, old
);
1477 d
= menu_get_menuitem(cf
, where
);
1478 ui_draw_menuitem(cf
, d
, where
);
1482 static int ui_menu_move(struct cfdisk
*cf
, int key
)
1484 struct cfdisk_menu
*m
;
1489 if (key
== (int) ERR
)
1490 return 0; /* ignore errors */
1494 DBG(MENU
, ul_debug("menu move key >%c<.", key
));
1500 case '\016': /* ^N */
1501 case 'j': /* Vi-like alternative */
1502 ui_menu_goto(cf
, m
->idx
+ 1);
1505 case '\020': /* ^P */
1506 case 'k': /* Vi-like alternative */
1507 ui_menu_goto(cf
, (int) m
->idx
- 1);
1511 ui_menu_goto(cf
, (int) m
->idx
- m
->page_sz
);
1516 ui_menu_goto(cf
, 0);
1520 ui_menu_goto(cf
, m
->idx
+ m
->page_sz
);
1525 ui_menu_goto(cf
, m
->nitems
);
1532 ui_menu_goto(cf
, m
->idx
+ 1);
1538 ui_menu_goto(cf
, (int) m
->idx
- 1);
1543 if (key
== '\014') { /* ^L refresh */
1548 DBG(MENU
, ul_debug(" no menu move key"));
1552 /* but don't call me from ui_run(), this is for pop-up menus only */
1553 static void ui_menu_resize(struct cfdisk
*cf
)
1555 DBG(MENU
, ul_debug("menu resize/refresh"));
1558 menu_refresh_size(cf
);
1563 static int partition_on_page(struct cfdisk
*cf
, size_t i
)
1565 if (cf
->page_sz
== 0 ||
1566 cf
->lines_idx
/ cf
->page_sz
== i
/ cf
->page_sz
)
1571 static void ui_draw_partition(struct cfdisk
*cf
, size_t i
)
1573 int ln
= TABLE_START_LINE
+ 1 + i
; /* skip table header */
1574 int cl
= ARROW_CURSOR_WIDTH
; /* we need extra space for cursor */
1575 int cur
= cf
->lines_idx
== i
;
1579 if (!partition_on_page(cf
, i
))
1581 ln
= TABLE_START_LINE
+ (i
% cf
->page_sz
) + 1;
1582 curpg
= cf
->lines_idx
/ cf
->page_sz
;
1586 "draw partition %zu [page_sz=%zu, "
1587 "line=%d, idx=%zu]",
1588 i
, cf
->page_sz
, ln
, cf
->lines_idx
));
1592 mvaddstr(ln
, 0, ARROW_CURSOR_STRING
);
1593 mvaddstr(ln
, cl
, cf
->lines
[i
+ 1].data
);
1598 if (colors_wanted() && is_freespace(cf
, i
)) {
1599 attron(COLOR_PAIR(CFDISK_CL_FREESPACE
));
1602 mvaddstr(ln
, 0, ARROW_CURSOR_DUMMY
);
1603 mvaddstr(ln
, cl
, cf
->lines
[i
+ 1].data
);
1605 attroff(COLOR_PAIR(CFDISK_CL_FREESPACE
));
1608 if ((size_t) ln
== MENU_START_LINE
- 1 &&
1609 cf
->page_sz
&& curpg
< cf
->nlines
/ cf
->page_sz
) {
1612 mvaddch(ln
, ui_cols
- 1, ACS_DARROW
);
1613 mvaddch(ln
, 0, ACS_DARROW
);
1620 static int ui_draw_table(struct cfdisk
*cf
)
1622 int cl
= ARROW_CURSOR_WIDTH
;
1623 size_t i
, nparts
= fdisk_table_get_nents(cf
->table
);
1624 size_t curpg
= cf
->page_sz
? cf
->lines_idx
/ cf
->page_sz
: 0;
1626 DBG(UI
, ul_debug("draw table"));
1628 for (i
= TABLE_START_LINE
; i
<= TABLE_START_LINE
+ cf
->page_sz
; i
++) {
1633 if (nparts
== 0 || (size_t) cf
->lines_idx
> nparts
- 1)
1634 cf
->lines_idx
= nparts
? nparts
- 1 : 0;
1638 mvaddstr(TABLE_START_LINE
, cl
, cf
->lines
[0].data
);
1641 /* print partitions */
1642 for (i
= 0; i
< nparts
; i
++)
1643 ui_draw_partition(cf
, i
);
1646 mvaddch(TABLE_START_LINE
, ui_cols
- 1, ACS_UARROW
);
1647 mvaddch(TABLE_START_LINE
, 0, ACS_UARROW
);
1649 if (cf
->page_sz
&& curpg
< cf
->nlines
/ cf
->page_sz
) {
1650 mvaddch(MENU_START_LINE
- 1, ui_cols
- 1, ACS_DARROW
);
1651 mvaddch(MENU_START_LINE
- 1, 0, ACS_DARROW
);
1656 static int ui_table_goto(struct cfdisk
*cf
, int where
)
1659 size_t nparts
= fdisk_table_get_nents(cf
->table
);
1661 DBG(UI
, ul_debug("goto table %d", where
));
1665 else if ((size_t) where
> nparts
- 1)
1668 if ((size_t) where
== cf
->lines_idx
)
1671 old
= cf
->lines_idx
;
1672 cf
->lines_idx
= where
;
1674 if (!partition_on_page(cf
, old
) ||!partition_on_page(cf
, where
))
1677 ui_draw_partition(cf
, old
); /* cleanup old */
1678 ui_draw_partition(cf
, where
); /* draw new */
1688 static int ui_refresh(struct cfdisk
*cf
)
1690 struct fdisk_label
*lb
;
1692 uint64_t bytes
= fdisk_get_nsectors(cf
->cxt
) * fdisk_get_sector_size(cf
->cxt
);
1698 strsz
= size_to_human_string(SIZE_SUFFIX_SPACE
1699 | SIZE_SUFFIX_3LETTER
, bytes
);
1701 lb
= fdisk_get_label(cf
->cxt
, NULL
);
1708 ui_center(0, _("Disk: %s"), fdisk_get_devname(cf
->cxt
));
1710 ui_center(1, _("Size: %s, %ju bytes, %ju sectors"),
1711 strsz
, bytes
, (uintmax_t) fdisk_get_nsectors(cf
->cxt
));
1712 if (fdisk_get_disklabel_id(cf
->cxt
, &id
) == 0 && id
)
1713 ui_center(2, _("Label: %s, identifier: %s"),
1714 fdisk_label_get_name(lb
), id
);
1716 ui_center(2, _("Label: %s"), fdisk_label_get_name(lb
));
1725 static ssize_t
ui_get_string(const char *prompt
,
1726 const char *hint
, char *buf
, size_t len
)
1728 int ln
= MENU_START_LINE
, cl
= 1;
1730 struct mbs_editor
*edit
;
1732 DBG(UI
, ul_debug("ui get string"));
1744 mvaddstr(ln
, cl
, (char *) prompt
);
1745 cl
+= mbs_safe_width(prompt
);
1748 edit
= mbs_new_edit(buf
, len
, ui_cols
- cl
);
1752 mbs_edit_goto(edit
, MBS_EDIT_END
);
1762 wint_t c
; /* we have fallback in widechar.h */
1766 mvaddstr(ln
, cl
, edit
->buf
);
1767 move(ln
, cl
+ edit
->cursor_cells
);
1770 #if !defined(HAVE_SLCURSES_H) && !defined(HAVE_SLANG_SLCURSES_H) && \
1771 defined(HAVE_LIBNCURSESW) && defined(HAVE_WIDECHAR)
1772 if (get_wch(&c
) == ERR
) {
1774 if ((c
= getch()) == (wint_t) ERR
) {
1782 if (!isatty(STDIN_FILENO
))
1788 DBG(UI
, ul_debug("ui get string: key=%lc", c
));
1790 if (c
== '\r' || c
== '\n' || c
== KEY_ENTER
)
1797 rc
= -CFDISK_ERR_ESC
;
1800 rc
= mbs_edit_goto(edit
, MBS_EDIT_LEFT
);
1803 rc
= mbs_edit_goto(edit
, MBS_EDIT_RIGHT
);
1806 rc
= mbs_edit_goto(edit
, MBS_EDIT_END
);
1809 rc
= mbs_edit_goto(edit
, MBS_EDIT_HOME
);
1815 rc
= mbs_edit_delete(edit
);
1820 rc
= mbs_edit_backspace(edit
);
1823 rc
= mbs_edit_insert(edit
, c
);
1833 rc
= strlen(edit
->buf
); /* success */
1839 mbs_free_edit(edit
);
1844 static int ui_get_size(struct cfdisk
*cf
, /* context */
1845 const char *prompt
, /* UI dialog string */
1846 uintmax_t *res
, /* result in bytes */
1847 uintmax_t low
, /* minimal size */
1848 uintmax_t up
, /* maximal size */
1849 int *expsize
) /* explicitly specified size */
1854 char *dflt
= size_to_human_string(0, *res
);
1856 DBG(UI
, ul_debug("get_size (default=%ju)", *res
));
1860 snprintf(buf
, sizeof(buf
), "%s", dflt
);
1863 int pwr
= 0, insec
= 0;
1865 rc
= ui_get_string(prompt
,
1866 _("May be followed by M for MiB, G for GiB, "
1867 "T for TiB, or S for sectors."),
1872 ui_warnx(_("Please, specify size."));
1873 continue; /* nothing specified */
1874 } else if (rc
== -CFDISK_ERR_ESC
)
1875 break; /* cancel dialog */
1877 if (strcmp(buf
, dflt
) == 0)
1878 user
= *res
, rc
= 0; /* no change, use default */
1880 size_t len
= strlen(buf
);
1881 if (buf
[len
- 1] == 'S' || buf
[len
- 1] == 's') {
1883 buf
[len
- 1] = '\0';
1885 rc
= parse_size(buf
, &user
, &pwr
); /* parse */
1889 DBG(UI
, ul_debug("get_size user=%ju, power=%d, in-sectors=%s",
1890 user
, pwr
, insec
? "yes" : "no"));
1892 user
*= fdisk_get_sector_size(cf
->cxt
);
1894 ui_warnx(_("Minimum size is %ju bytes."), low
);
1897 if (user
> up
&& pwr
&& user
< up
+ (1ULL << pwr
* 10))
1898 /* ignore when the user specified size overflow
1899 * with in range specified by suffix (e.g. MiB) */
1903 ui_warnx(_("Maximum size is %ju bytes."), up
);
1906 if (rc
== 0 && insec
&& expsize
)
1910 ui_warnx(_("Failed to parse size."));
1917 DBG(UI
, ul_debug("get_size (result=%ju, rc=%zd)", *res
, rc
));
1921 static struct fdisk_parttype
*ui_get_parttype(struct cfdisk
*cf
,
1922 struct fdisk_parttype
*cur
)
1924 struct cfdisk_menuitem
*d
, *cm
;
1925 size_t i
= 0, nitems
, idx
= 0;
1926 struct fdisk_parttype
*t
= NULL
;
1927 struct fdisk_label
*lb
;
1930 DBG(UI
, ul_debug("asking for parttype."));
1932 lb
= fdisk_get_label(cf
->cxt
, NULL
);
1934 /* create cfdisk menu according to label types, note that the
1935 * last cm[] item has to be empty -- so nitems + 1 */
1936 nitems
= fdisk_label_get_nparttypes(lb
);
1940 cm
= xcalloc(nitems
+ 1, sizeof(struct cfdisk_menuitem
));
1944 codetypes
= fdisk_label_has_code_parttypes(lb
);
1946 for (i
= 0; i
< nitems
; i
++) {
1947 const struct fdisk_parttype
*x
= fdisk_label_get_parttype(lb
, i
);
1950 cm
[i
].userdata
= (void *) x
;
1952 xasprintf(&name
, "%2x %s",
1953 fdisk_parttype_get_code(x
),
1954 _(fdisk_parttype_get_name(x
)));
1956 name
= (char *) _(fdisk_parttype_get_name(x
));
1957 cm
[i
].desc
= fdisk_parttype_get_string(x
);
1964 /* make the new menu active */
1966 cf
->menu
->vertical
= 1;
1967 cf
->menu
->idx
= idx
;
1968 menu_set_title(cf
->menu
, _("Select partition type"));
1979 if (ui_menu_move(cf
, key
) == 0)
1986 d
= menu_get_menuitem(cf
, cf
->menu
->idx
);
1988 t
= (struct fdisk_parttype
*) d
->userdata
;
2002 for (i
= 0; i
< nitems
; i
++)
2003 free((char *) cm
[i
].name
);
2006 DBG(UI
, ul_debug("get parrtype done [type=%s] ", t
?
2007 fdisk_parttype_get_name(t
) : NULL
));
2011 static int ui_script_read(struct cfdisk
*cf
)
2013 struct fdisk_script
*sc
= NULL
;
2014 char buf
[PATH_MAX
] = { 0 };
2018 rc
= ui_get_string( _("Enter script file name: "),
2019 _("The script file will be applied to in-memory partition table."),
2026 sc
= fdisk_new_script_from_file(cf
->cxt
, buf
);
2028 ui_warn(_("Cannot open %s"), buf
);
2030 ui_warnx(_("Failed to parse script file %s"), buf
);
2031 else if (fdisk_apply_script(cf
->cxt
, sc
) != 0)
2032 ui_warnx(_("Failed to apply script %s"), buf
);
2037 fdisk_unref_script(sc
);
2041 static int ui_script_write(struct cfdisk
*cf
)
2043 struct fdisk_script
*sc
= NULL
;
2044 char buf
[PATH_MAX
] = { 0 };
2048 rc
= ui_get_string( _("Enter script file name: "),
2049 _("The current in-memory partition table will be dumped to the file."),
2055 sc
= fdisk_new_script(cf
->cxt
);
2057 ui_warn(_("Failed to allocate script handler"));
2061 rc
= fdisk_script_read_context(sc
, NULL
);
2063 ui_warnx(_("Failed to read disk layout into script."));
2067 DBG(UI
, ul_debug("writing dump into: '%s'", buf
));
2068 f
= fopen(buf
, "w");
2070 ui_warn(_("Cannot open %s"), buf
);
2075 rc
= fdisk_script_write_file(sc
, f
);
2077 ui_info(_("Disk layout successfully dumped."));
2080 ui_warn(_("Failed to write script %s"), buf
);
2083 fdisk_unref_script(sc
);
2087 /* prints menu with libfdisk labels and waits for users response */
2088 static int ui_create_label(struct cfdisk
*cf
)
2090 struct cfdisk_menuitem
*d
, *cm
;
2091 int rc
= 1, refresh_menu
= 1;
2092 size_t i
= 0, nitems
;
2093 struct fdisk_label
*lb
= NULL
;
2097 DBG(UI
, ul_debug("asking for new disklabe."));
2099 /* create cfdisk menu according to libfdisk labels, note that the
2100 * last cm[] item has to be empty -- so nitems + 1 */
2101 nitems
= fdisk_get_nlabels(cf
->cxt
);
2102 cm
= xcalloc(nitems
+ 1, sizeof(struct cfdisk_menuitem
));
2104 while (fdisk_next_label(cf
->cxt
, &lb
) == 0) {
2105 if (fdisk_label_is_disabled(lb
) ||
2106 fdisk_label_get_type(lb
) == FDISK_DISKLABEL_BSD
)
2108 cm
[i
++].name
= fdisk_label_get_name(lb
);
2113 /* make the new menu active */
2115 cf
->menu
->vertical
= 1;
2116 menu_set_title(cf
->menu
, _("Select label type"));
2118 if (!cf
->zero_start
)
2119 ui_info(_("Device does not contain a recognized partition table."));
2127 ui_hint(_("Select a type to create a new label or press 'L' to load script file."));
2138 if (ui_menu_move(cf
, key
) == 0)
2144 d
= menu_get_menuitem(cf
, cf
->menu
->idx
);
2146 rc
= fdisk_create_disklabel(cf
->cxt
, d
->name
);
2154 rc
= ui_script_read(cf
);
2167 DBG(UI
, ul_debug("create label done [rc=%d] ", rc
));
2172 static int ui_help(void)
2175 static const char *help
[] = {
2176 N_("This is cfdisk, a curses-based disk partitioning program."),
2177 N_("It lets you create, delete, and modify partitions on a block device."),
2179 N_("Command Meaning"),
2180 N_("------- -------"),
2181 N_(" b Toggle bootable flag of the current partition"),
2182 N_(" d Delete the current partition"),
2183 N_(" h Print this screen"),
2184 N_(" n Create new partition from free space"),
2185 N_(" q Quit program without writing partition table"),
2186 N_(" s Fix partitions order (only when in disarray)"),
2187 N_(" t Change the partition type"),
2188 N_(" u Dump disk layout to sfdisk compatible script file"),
2189 N_(" W Write partition table to disk (you must enter uppercase W);"),
2190 N_(" since this might destroy data on the disk, you must either"),
2191 N_(" confirm or deny the write by entering 'yes' or 'no'"),
2192 N_(" x Display/hide extra information about a partition"),
2193 N_("Up Arrow Move cursor to the previous partition"),
2194 N_("Down Arrow Move cursor to the next partition"),
2195 N_("Left Arrow Move cursor to the previous menu item"),
2196 N_("Right Arrow Move cursor to the next menu item"),
2198 N_("Note: All of the commands can be entered with either upper or lower"),
2199 N_("case letters (except for Write)."),
2201 N_("Use lsblk(8) or partx(8) to see more details about the device."),
2204 "Copyright (C) 2014-2017 Karel Zak <kzak@redhat.com>"
2208 for (i
= 0; i
< ARRAY_SIZE(help
); i
++)
2209 mvaddstr(i
, 1, _(help
[i
]));
2211 ui_info(_("Press a key to continue."));
2220 /* TODO: use @sz, now 128bytes */
2221 static int main_menu_ignore_keys(struct cfdisk
*cf
, char *ignore
,
2222 size_t sz
__attribute__((__unused__
)))
2224 struct fdisk_partition
*pa
= get_current_partition(cf
);
2229 if (fdisk_partition_is_freespace(pa
)) {
2230 ignore
[i
++] = 'd'; /* delete */
2231 ignore
[i
++] = 't'; /* set type */
2232 ignore
[i
++] = 'b'; /* set bootable */
2233 ignore
[i
++] = 'r'; /* resize */
2234 cf
->menu
->prefkey
= 'n';
2236 cf
->menu
->prefkey
= 'q';
2238 if (!fdisk_is_label(cf
->cxt
, DOS
) &&
2239 !fdisk_is_label(cf
->cxt
, SGI
))
2243 if (!cf
->wrong_order
)
2246 if (fdisk_is_readonly(cf
->cxt
))
2253 /* returns: error: < 0, success: 0, quit: 1 */
2254 static int main_menu_action(struct cfdisk
*cf
, int key
)
2257 int ref
= 0, rc
, org_order
= cf
->wrong_order
;
2258 const char *info
= NULL
, *warn
= NULL
;
2259 struct fdisk_partition
*pa
;
2266 struct cfdisk_menuitem
*d
= menu_get_menuitem(cf
, cf
->menu
->idx
);
2271 } else if (key
!= 'w' && key
!= 'W')
2272 key
= tolower(key
); /* case insensitive except 'W'rite */
2274 DBG(MENU
, ul_debug("main menu action: key=%c", key
));
2276 if (cf
->menu
->ignore
&& strchr(cf
->menu
->ignore
, key
)) {
2277 DBG(MENU
, ul_debug(" ignore '%c'", key
));
2281 pa
= get_current_partition(cf
);
2284 n
= fdisk_partition_get_partno(pa
);
2286 DBG(MENU
, ul_debug("menu action on %p", pa
));
2291 case 'b': /* Bootable flag */
2293 int fl
= fdisk_is_label(cf
->cxt
, DOS
) ? DOS_FLAG_ACTIVE
:
2294 fdisk_is_label(cf
->cxt
, SGI
) ? SGI_FLAG_BOOT
: 0;
2296 if (fl
&& fdisk_toggle_partition_flag(cf
->cxt
, n
, fl
))
2297 warn
= _("Could not toggle the flag.");
2305 case 'd': /* Delete */
2306 if (fdisk_delete_partition(cf
->cxt
, n
) != 0)
2307 warn
= _("Could not delete partition %zu.");
2309 info
= _("Partition %zu has been deleted.");
2312 case 'h': /* Help */
2319 uint64_t start
, size
, dflt_size
, secs
, max_size
;
2320 struct fdisk_partition
*npa
; /* the new partition */
2321 int expsize
= 0; /* size specified explicitly in sectors */
2323 if (!fdisk_partition_is_freespace(pa
) || !fdisk_partition_has_start(pa
))
2326 /* free space range */
2327 start
= fdisk_partition_get_start(pa
);
2328 size
= max_size
= dflt_size
= fdisk_partition_get_size(pa
) * fdisk_get_sector_size(cf
->cxt
);
2330 if (ui_get_size(cf
, _("Partition size: "), &size
,
2331 fdisk_get_sector_size(cf
->cxt
),
2332 max_size
, &expsize
) == -CFDISK_ERR_ESC
)
2335 secs
= size
/ fdisk_get_sector_size(cf
->cxt
);
2337 npa
= fdisk_new_partition();
2341 if (dflt_size
== size
) /* default is to fillin all free space */
2342 fdisk_partition_end_follow_default(npa
, 1);
2344 fdisk_partition_set_size(npa
, secs
);
2347 fdisk_partition_size_explicit(pa
, 1);
2349 fdisk_partition_set_start(npa
, start
);
2350 fdisk_partition_partno_follow_default(npa
, 1);
2351 /* add to disk label -- libfdisk will ask for missing details */
2352 rc
= fdisk_add_partition(cf
->cxt
, npa
, NULL
);
2353 fdisk_unref_partition(npa
);
2358 case 'q': /* Quit */
2360 case 't': /* Type */
2362 struct fdisk_parttype
*t
;
2364 if (fdisk_partition_is_freespace(pa
))
2366 t
= (struct fdisk_parttype
*) fdisk_partition_get_type(pa
);
2367 t
= ui_get_parttype(cf
, t
);
2370 if (t
&& fdisk_set_partition_type(cf
->cxt
, n
, t
) == 0)
2371 info
= _("Changed type of partition %zu.");
2373 info
= _("The type of partition %zu is unchanged.");
2376 case 'r': /* resize */
2378 struct fdisk_partition
*npa
, *next
;
2379 uint64_t size
, max_size
, secs
;
2381 if (fdisk_partition_is_freespace(pa
) || !fdisk_partition_has_start(pa
))
2384 size
= fdisk_partition_get_size(pa
);
2386 /* is the next freespace? */
2387 next
= fdisk_table_get_partition(cf
->table
, cf
->lines_idx
+ 1);
2388 if (next
&& fdisk_partition_is_freespace(next
))
2389 size
+= fdisk_partition_get_size(next
);
2391 size
*= fdisk_get_sector_size(cf
->cxt
);
2394 if (ui_get_size(cf
, _("New size: "), &size
,
2395 fdisk_get_sector_size(cf
->cxt
),
2396 max_size
, NULL
) == -CFDISK_ERR_ESC
)
2398 secs
= size
/ fdisk_get_sector_size(cf
->cxt
);
2399 npa
= fdisk_new_partition();
2403 fdisk_partition_set_size(npa
, secs
);
2405 rc
= fdisk_set_partition(cf
->cxt
, n
, npa
);
2406 fdisk_unref_partition(npa
);
2409 info
= _("Partition %zu resized.");
2413 case 's': /* Sort */
2414 if (cf
->wrong_order
) {
2415 fdisk_reorder_partitions(cf
->cxt
);
2419 case 'u': /* dUmp */
2420 ui_script_write(cf
);
2422 case 'W': /* Write */
2424 char buf
[64] = { 0 };
2426 if (fdisk_is_readonly(cf
->cxt
)) {
2427 warn
= _("Device is open in read-only mode.");
2432 _("Are you sure you want to write the partition "
2434 _("Type \"yes\" or \"no\", or press ESC to leave this dialog."),
2438 if (rc
<= 0 || (strcasecmp(buf
, "yes") != 0 &&
2439 strcasecmp(buf
, _("yes")) != 0)) {
2440 info
= _("Did not write partition table to disk.");
2443 rc
= fdisk_write_disklabel(cf
->cxt
);
2445 warn
= _("Failed to write disklabel.");
2447 if (cf
->device_is_used
)
2448 fdisk_reread_changes(cf
->cxt
, cf
->original_layout
);
2450 fdisk_reread_partition_table(cf
->cxt
);
2451 info
= _("The partition table has been altered.");
2470 ui_warnx(warn
, n
+ 1);
2472 ui_info(info
, n
+ 1);
2473 else if (key
== 'n' && cf
->wrong_order
&& org_order
== 0)
2474 ui_info(_("Note that partition table entries are not in disk order now."));
2479 static void ui_resize_refresh(struct cfdisk
*cf
)
2481 DBG(UI
, ul_debug("ui resize/refresh"));
2483 menu_refresh_size(cf
);
2489 static void toggle_show_extra(struct cfdisk
*cf
)
2491 if (cf
->show_extra
&& cf
->act_win
) {
2492 wclear(cf
->act_win
);
2495 cf
->show_extra
= cf
->show_extra
? 0 : 1;
2499 DBG(MENU
, ul_debug("extra: %s", cf
->show_extra
? "ENABLED" : "DISABLED" ));
2502 static int ui_run(struct cfdisk
*cf
)
2508 DBG(UI
, ul_debug("start cols=%zu, lines=%zu", ui_cols
, ui_lines
));
2510 if (fdisk_get_collision(cf
->cxt
)) {
2511 ui_warnx(_("Device already contains a %s signature; it will be removed by a write command."),
2512 fdisk_get_collision(cf
->cxt
));
2513 fdisk_enable_wipe(cf
->cxt
, 1);
2514 ui_hint(_("Press a key to continue."));
2518 if (!fdisk_has_label(cf
->cxt
) || cf
->zero_start
) {
2519 rc
= ui_create_label(cf
);
2521 ui_errx(EXIT_FAILURE
,
2522 _("failed to create a new disklabel"));
2528 rc
= lines_refresh(cf
);
2530 ui_errx(EXIT_FAILURE
, _("failed to read partitions"));
2532 menu_push(cf
, main_menuitems
);
2533 cf
->menu
->ignore_cb
= main_menu_ignore_keys
;
2535 rc
= ui_refresh(cf
);
2542 if (fdisk_is_readonly(cf
->cxt
))
2543 ui_warnx(_("Device is open in read-only mode."));
2544 else if (cf
->wrong_order
)
2545 ui_info(_("Note that partition table entries are not in disk order now."));
2555 /* Note that ncurses getch() returns ERR when interrupted
2556 * by signal, but SLang does not interrupt at all. */
2557 ui_resize_refresh(cf
);
2560 if (key
== '\014') { /* ^L refresh */
2561 ui_resize_refresh(cf
);
2564 if (ui_menu_move(cf
, key
) == 0)
2567 DBG(UI
, ul_debug("main action key >%1$c< [\\0%1$o].", key
));
2571 case '\016': /* ^N */
2572 case 'j': /* Vi-like alternative */
2573 ui_table_goto(cf
, cf
->lines_idx
+ 1);
2576 case '\020': /* ^P */
2577 case 'k': /* Vi-like alternative */
2578 ui_table_goto(cf
, (int) cf
->lines_idx
- 1);
2582 ui_table_goto(cf
, (int) cf
->lines_idx
- cf
->page_sz
);
2587 ui_table_goto(cf
, 0);
2591 ui_table_goto(cf
, cf
->lines_idx
+ cf
->page_sz
);
2596 ui_table_goto(cf
, (int) cf
->nlines
- 1);
2601 rc
= main_menu_action(cf
, 0);
2604 case 'x': /* Extra */
2605 toggle_show_extra(cf
);
2608 rc
= main_menu_action(cf
, key
);
2620 DBG(UI
, ul_debug("end"));
2624 static void __attribute__((__noreturn__
)) usage(void)
2627 fputs(USAGE_HEADER
, out
);
2629 _(" %1$s [options] <disk>\n"), program_invocation_short_name
);
2631 fputs(USAGE_SEPARATOR
, out
);
2632 fputs(_("Display or manipulate a disk partition table.\n"), out
);
2634 fputs(USAGE_OPTIONS
, out
);
2635 fputs(_(" -L, --color[=<when>] colorize output (auto, always or never)\n"), out
);
2637 " %s\n", USAGE_COLORS_DEFAULT
);
2638 fputs(_(" -z, --zero start with zeroed partition table\n"), out
);
2640 fputs(USAGE_SEPARATOR
, out
);
2641 printf(USAGE_HELP_OPTIONS(26));
2643 printf(USAGE_MAN_TAIL("cfdisk(8)"));
2647 int main(int argc
, char *argv
[])
2649 const char *diskpath
= NULL
;
2650 int rc
, c
, colormode
= UL_COLORMODE_UNDEF
;
2651 struct cfdisk _cf
= { .lines_idx
= 0 },
2654 static const struct option longopts
[] = {
2655 { "color", optional_argument
, NULL
, 'L' },
2656 { "help", no_argument
, NULL
, 'h' },
2657 { "version", no_argument
, NULL
, 'V' },
2658 { "zero", no_argument
, NULL
, 'z' },
2659 { NULL
, 0, NULL
, 0 },
2662 setlocale(LC_ALL
, "");
2663 bindtextdomain(PACKAGE
, LOCALEDIR
);
2664 textdomain(PACKAGE
);
2665 atexit(close_stdout
);
2667 while((c
= getopt_long(argc
, argv
, "L::hVz", longopts
, NULL
)) != -1) {
2673 colormode
= UL_COLORMODE_AUTO
;
2675 colormode
= colormode_or_err(optarg
,
2676 _("unsupported color mode"));
2679 printf(UTIL_LINUX_VERSION
);
2680 return EXIT_SUCCESS
;
2685 errtryhelp(EXIT_FAILURE
);
2689 colors_init(colormode
, "cfdisk");
2691 fdisk_init_debug(0);
2692 scols_init_debug(0);
2693 cfdisk_init_debug();
2694 cf
->cxt
= fdisk_new_context();
2696 err(EXIT_FAILURE
, _("failed to allocate libfdisk context"));
2698 fdisk_set_ask(cf
->cxt
, ask_callback
, (void *) cf
);
2700 if (optind
== argc
) {
2703 for (i
= 0; i
< ARRAY_SIZE(default_disks
); i
++) {
2704 if (access(default_disks
[i
], F_OK
) == 0) {
2705 diskpath
= default_disks
[i
];
2710 diskpath
= default_disks
[0]; /* default, used for "cannot open" */
2712 diskpath
= argv
[optind
];
2714 rc
= fdisk_assign_device(cf
->cxt
, diskpath
, 0);
2716 rc
= fdisk_assign_device(cf
->cxt
, diskpath
, 1);
2718 err(EXIT_FAILURE
, _("cannot open %s"), diskpath
);
2720 if (!fdisk_is_readonly(cf
->cxt
)) {
2721 cf
->device_is_used
= fdisk_device_is_used(cf
->cxt
);
2722 fdisk_get_partitions(cf
->cxt
, &cf
->original_layout
);
2725 /* Don't use err(), warn() from this point */
2730 cfdisk_free_lines(cf
);
2733 fdisk_unref_table(cf
->table
);
2734 #ifdef HAVE_LIBMOUNT
2735 mnt_unref_table(cf
->fstab
);
2736 mnt_unref_table(cf
->mtab
);
2737 mnt_unref_cache(cf
->mntcache
);
2739 rc
= fdisk_deassign_device(cf
->cxt
, cf
->nwrites
== 0);
2740 fdisk_unref_context(cf
->cxt
);
2741 DBG(MISC
, ul_debug("bye! [rc=%d]", rc
));
2742 return rc
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
;