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_refresh(struct cfdisk
*cf
);
138 static void ui_warnx(const char *fmt
, ...);
139 static void ui_warn(const char *fmt
, ...);
140 static void ui_info(const char *fmt
, ...);
141 static void ui_draw_menu(struct cfdisk
*cf
);
142 static int ui_menu_move(struct cfdisk
*cf
, int key
);
143 static void ui_menu_resize(struct cfdisk
*cf
);
145 static int ui_get_size(struct cfdisk
*cf
, const char *prompt
, uintmax_t *res
,
146 uintmax_t low
, uintmax_t up
, int *expsize
);
148 static int ui_enabled
;
149 static int ui_resize
;
151 /* ncurses LINES and COLS may be actual variables or *macros*, but we need
152 * something portable and writable */
153 static size_t ui_lines
;
154 static size_t ui_cols
;
157 struct cfdisk_menuitem
{
158 int key
; /* keyboard shortcut */
159 const char *name
; /* item name */
160 const char *desc
; /* item description (hint) */
166 char *title
; /* optional menu title */
167 struct cfdisk_menuitem
*items
; /* array with menu items */
168 char *ignore
;/* string with keys to ignore */
169 size_t width
; /* maximal width of the menu item */
170 size_t nitems
; /* number of the active menu items */
171 size_t page_sz
;/* when menu longer than screen */
172 size_t idx
; /* the current menu item */
173 int prefkey
;/* preferred menu item */
174 struct cfdisk_menu
*prev
;
176 /* @ignore keys generator */
177 int (*ignore_cb
) (struct cfdisk
*, char *, size_t);
179 unsigned int vertical
: 1; /* enable vertical mode */
183 static struct cfdisk_menuitem main_menuitems
[] = {
184 { 'b', N_("Bootable"), N_("Toggle bootable flag of the current partition") },
185 { 'd', N_("Delete"), N_("Delete the current partition") },
186 { 'n', N_("New"), N_("Create new partition from free space") },
187 { 'q', N_("Quit"), N_("Quit program without writing changes") },
188 { 't', N_("Type"), N_("Change the partition type") },
189 { 'h', N_("Help"), N_("Print help screen") },
190 { 's', N_("Sort"), N_("Fix partitions order") },
191 { 'W', N_("Write"), N_("Write partition table to disk (this might destroy data)") },
192 { 'u', N_("Dump"), N_("Dump partition table to sfdisk compatible script file") },
196 /* extra partinfo in name:value pairs */
197 struct cfdisk_extra
{
201 struct list_head exs
;
204 /* line and extra partinfo list_head */
206 char *data
; /* line data */
207 struct libscols_table
*extra
; /* extra info ('X') */
208 WINDOW
*w
; /* window with extra info */
211 /* top level control struct */
213 struct fdisk_context
*cxt
; /* libfdisk context */
214 struct fdisk_table
*table
; /* partition table */
216 struct cfdisk_menu
*menu
; /* the current menu */
218 int *fields
; /* output columns IDs */
219 size_t nfields
; /* number of columns IDs */
221 char *linesbuf
; /* table as string */
222 size_t linesbufsz
; /* size of the tb_buf */
224 struct cfdisk_line
*lines
; /* list of lines */
226 size_t nlines
; /* number of lines */
227 size_t lines_idx
; /* current line <0..N>, exclude header */
230 unsigned int nwrites
; /* fdisk_write_disklabel() counter */
232 WINDOW
*act_win
; /* the window currently on the screen */
235 struct libmnt_table
*mtab
;
236 struct libmnt_table
*fstab
;
237 struct libmnt_cache
*mntcache
;
239 unsigned int wrong_order
:1, /* PT not in right order */
240 zero_start
:1, /* ignore existing partition table */
241 show_extra
:1; /* show extra partinfo */
246 * let's use include/debug.h stuff for cfdisk too
248 static UL_DEBUG_DEFINE_MASK(cfdisk
);
249 UL_DEBUG_DEFINE_MASKNAMES(cfdisk
) = UL_DEBUG_EMPTY_MASKNAMES
;
251 #define CFDISK_DEBUG_INIT (1 << 1)
252 #define CFDISK_DEBUG_UI (1 << 2)
253 #define CFDISK_DEBUG_MENU (1 << 3)
254 #define CFDISK_DEBUG_MISC (1 << 4)
255 #define CFDISK_DEBUG_TABLE (1 << 5)
256 #define CFDISK_DEBUG_ALL 0xFFFF
258 #define DBG(m, x) __UL_DBG(cfdisk, CFDISK_DEBUG_, m, x)
260 static void cfdisk_init_debug(void)
262 __UL_INIT_DEBUG(cfdisk
, CFDISK_DEBUG_
, 0, CFDISK_DEBUG
);
265 /* Initialize output columns -- we follow libfdisk fields (usually specific
268 static int cols_init(struct cfdisk
*cf
)
276 return fdisk_label_get_fields_ids(NULL
, cf
->cxt
, &cf
->fields
, &cf
->nfields
);
279 static void resize(void)
283 if (ioctl(fileno(stdout
), TIOCGWINSZ
, &ws
) != -1
284 && ws
.ws_row
&& ws
.ws_col
) {
285 ui_lines
= ws
.ws_row
;
288 resizeterm(ws
.ws_row
, ws
.ws_col
);
290 clearok(stdscr
, TRUE
);
294 DBG(UI
, ul_debug("ui: resize refresh ui_cols=%zu, ui_lines=%zu",
299 /* Reads partition in tree-like order from scols
301 static int partition_from_scols(struct fdisk_table
*tb
,
302 struct libscols_line
*ln
)
304 struct fdisk_partition
*pa
= scols_line_get_userdata(ln
);
306 fdisk_table_add_partition(tb
, pa
);
307 fdisk_unref_partition(pa
);
309 if (scols_line_has_children(ln
)) {
310 struct libscols_line
*chln
;
311 struct libscols_iter
*itr
= scols_new_iter(SCOLS_ITER_FORWARD
);
315 while (scols_line_next_child(ln
, itr
, &chln
) == 0)
316 partition_from_scols(tb
, chln
);
317 scols_free_iter(itr
);
322 static char *table_to_string(struct cfdisk
*cf
, struct fdisk_table
*tb
)
324 struct fdisk_partition
*pa
;
325 struct fdisk_label
*lb
;
326 struct fdisk_iter
*itr
;
327 struct libscols_table
*table
= NULL
;
328 struct libscols_iter
*s_itr
= NULL
;
332 struct libscols_line
*ln
, *ln_cont
= NULL
;
334 DBG(TABLE
, ul_debug("convert to string"));
341 lb
= fdisk_get_label(cf
->cxt
, NULL
);
344 itr
= fdisk_new_iter(FDISK_ITER_FORWARD
);
348 /* get container (e.g. extended partition) */
349 while (fdisk_table_next_partition(tb
, itr
, &pa
) == 0) {
350 if (fdisk_partition_is_nested(pa
)) {
351 DBG(TABLE
, ul_debug("nested detected, using tree"));
352 tree
= SCOLS_FL_TREE
;
357 table
= scols_new_table();
360 scols_table_enable_maxout(table
, 1);
361 scols_table_enable_nowrap(table
, 1);
363 #if !defined(HAVE_LIBNCURSESW) || !defined(HAVE_WIDECHAR)
364 scols_table_enable_ascii(table
, 1);
368 for (i
= 0; i
< cf
->nfields
; i
++) {
370 const struct fdisk_field
*field
=
371 fdisk_label_get_field(lb
, cf
->fields
[i
]);
375 if (fdisk_field_is_number(field
))
376 fl
|= SCOLS_FL_RIGHT
;
377 if (fdisk_field_get_id(field
) == FDISK_FIELD_TYPE
)
378 fl
|= SCOLS_FL_TRUNC
;
379 if (tree
&& fdisk_field_get_id(field
) == FDISK_FIELD_DEVICE
)
382 if (!scols_table_new_column(table
,
383 _(fdisk_field_get_name(field
)),
384 fdisk_field_get_width(field
), fl
))
389 fdisk_reset_iter(itr
, FDISK_ITER_FORWARD
);
391 while (fdisk_table_next_partition(tb
, itr
, &pa
) == 0) {
392 struct libscols_line
*parent
= fdisk_partition_is_nested(pa
) ? ln_cont
: NULL
;
394 ln
= scols_table_new_line(table
, parent
);
397 for (i
= 0; i
< cf
->nfields
; i
++) {
400 if (fdisk_partition_to_string(pa
, cf
->cxt
,
401 cf
->fields
[i
], &cdata
))
403 scols_line_refer_data(ln
, i
, cdata
);
405 if (tree
&& fdisk_partition_is_container(pa
))
408 scols_line_set_userdata(ln
, (void *) pa
);
409 fdisk_ref_partition(pa
);
412 if (scols_table_is_empty(table
))
415 scols_table_reduce_termwidth(table
, ARROW_CURSOR_WIDTH
);
416 scols_print_table_to_string(table
, &res
);
418 /* scols_* code might reorder lines, let's reorder @tb according to the
419 * final output (it's no problem because partitions are addressed by
420 * parno stored within struct fdisk_partition) */
423 fdisk_reset_iter(itr
, FDISK_ITER_FORWARD
);
424 while (fdisk_table_next_partition(tb
, itr
, &pa
) == 0)
425 fdisk_table_remove_partition(tb
, pa
);
427 s_itr
= scols_new_iter(SCOLS_ITER_FORWARD
);
431 /* add all in the right order (don't forget the output is tree) */
432 while (scols_table_next_line(table
, s_itr
, &ln
) == 0) {
433 if (scols_line_get_parent(ln
))
435 if (partition_from_scols(tb
, ln
))
439 scols_unref_table(table
);
440 scols_free_iter(s_itr
);
441 fdisk_free_iter(itr
);
446 static void cfdisk_free_lines(struct cfdisk
*cf
)
449 while(i
< cf
->nlines
) {
450 scols_unref_table(cf
->lines
[i
].extra
);
452 DBG(UI
, ul_debug("delete window: %p",
456 delwin(cf
->lines
[i
].w
);
457 cf
->lines
[i
].w
= NULL
;
465 * Read data about partitions from libfdisk and prepare output lines.
467 static int lines_refresh(struct cfdisk
*cf
)
475 DBG(TABLE
, ul_debug("refreshing buffer"));
478 cfdisk_free_lines(cf
);
484 fdisk_unref_table(cf
->table
);
487 /* read partitions and free spaces into cf->table */
488 rc
= fdisk_get_partitions(cf
->cxt
, &cf
->table
);
490 rc
= fdisk_get_freespaces(cf
->cxt
, &cf
->table
);
494 cf
->linesbuf
= table_to_string(cf
, cf
->table
);
498 cf
->linesbufsz
= strlen(cf
->linesbuf
);
499 cf
->nlines
= fdisk_table_get_nents(cf
->table
) + 1; /* 1 for header line */
501 cf
->wrong_order
= fdisk_table_wrong_order(cf
->table
) ? 1 : 0;
503 if (MENU_START_LINE
- TABLE_START_LINE
< cf
->nlines
)
504 cf
->page_sz
= MENU_START_LINE
- TABLE_START_LINE
- 1;
506 cf
->lines
= xcalloc(cf
->nlines
, sizeof(struct cfdisk_line
));
508 for (p
= cf
->linesbuf
, i
= 0; p
&& i
< cf
->nlines
; i
++) {
509 cf
->lines
[i
].data
= p
;
515 cf
->lines
[i
].extra
= scols_new_table();
516 scols_table_enable_noheadings(cf
->lines
[i
].extra
, 1);
517 scols_table_new_column(cf
->lines
[i
].extra
, NULL
, 0, SCOLS_FL_RIGHT
);
518 scols_table_new_column(cf
->lines
[i
].extra
, NULL
, 0, SCOLS_FL_TRUNC
);
524 static struct fdisk_partition
*get_current_partition(struct cfdisk
*cf
)
529 return fdisk_table_get_partition(cf
->table
, cf
->lines_idx
);
532 static int is_freespace(struct cfdisk
*cf
, size_t i
)
534 struct fdisk_partition
*pa
;
539 pa
= fdisk_table_get_partition(cf
->table
, i
);
540 return fdisk_partition_is_freespace(pa
);
543 /* converts libfdisk FDISK_ASKTYPE_MENU to cfdisk menu and returns user's
544 * response back to libfdisk
546 static int ask_menu(struct fdisk_ask
*ask
, struct cfdisk
*cf
)
548 struct cfdisk_menuitem
*d
, *cm
;
550 size_t i
= 0, nitems
;
551 const char *name
, *desc
;
556 /* create cfdisk menu according to libfdisk ask-menu, note that the
557 * last cm[] item has to be empty -- so nitems + 1 */
558 nitems
= fdisk_ask_menu_get_nitems(ask
);
559 cm
= xcalloc(nitems
+ 1, sizeof(struct cfdisk_menuitem
));
561 for (i
= 0; i
< nitems
; i
++) {
562 if (fdisk_ask_menu_get_item(ask
, i
, &key
, &name
, &desc
))
569 /* make the new menu active */
580 if (ui_menu_move(cf
, key
) == 0)
587 d
= menu_get_menuitem(cf
, cf
->menu
->idx
);
589 fdisk_ask_menu_set_result(ask
, d
->key
);
603 static int ask_callback(struct fdisk_context
*cxt
__attribute__((__unused__
)),
604 struct fdisk_ask
*ask
,
605 void *data
__attribute__((__unused__
)))
611 switch(fdisk_ask_get_type(ask
)) {
612 case FDISK_ASKTYPE_INFO
:
613 ui_info(fdisk_ask_print_get_mesg(ask
));
615 case FDISK_ASKTYPE_WARNX
:
616 ui_warnx(fdisk_ask_print_get_mesg(ask
));
618 case FDISK_ASKTYPE_WARN
:
619 ui_warn(fdisk_ask_print_get_mesg(ask
));
621 case FDISK_ASKTYPE_MENU
:
622 ask_menu(ask
, (struct cfdisk
*) data
);
625 ui_warnx(_("internal error: unsupported dialog type %d"),
626 fdisk_ask_get_type(ask
));
632 static int ui_end(void)
637 #if defined(HAVE_SLCURSES_H) || defined(HAVE_SLANG_SLCURSES_H)
638 SLsmg_gotorc(ui_lines
- 1, 0);
641 mvcur(0, ui_cols
- 1, ui_lines
-1, 0);
651 static void ui_vprint_center(size_t line
, int attrs
, const char *fmt
, va_list ap
)
659 xvasprintf(&buf
, fmt
, ap
);
661 width
= mbs_safe_width(buf
);
662 if (width
> (size_t) ui_cols
) {
663 char *p
= strrchr(buf
+ ui_cols
, ' ');
667 if (line
+ 1 >= ui_lines
)
670 mvaddstr(line
, 0, buf
);
671 mvaddstr(line
+ 1, 0, p
+1);
675 mvaddstr(line
, (ui_cols
- width
) / 2, buf
);
681 static void ui_center(size_t line
, const char *fmt
, ...)
685 ui_vprint_center(line
, 0, fmt
, ap
);
689 static void ui_warnx(const char *fmt
, ...)
694 ui_vprint_center(WARN_LINE
,
695 colors_wanted() ? COLOR_PAIR(CFDISK_CL_WARNING
) : 0,
698 vfprintf(stderr
, fmt
, ap
);
704 static void ui_warn(const char *fmt
, ...)
709 xasprintf(&fmt_m
, "%s: %m", fmt
);
713 ui_vprint_center(WARN_LINE
,
714 colors_wanted() ? COLOR_PAIR(CFDISK_CL_WARNING
) : 0,
717 vfprintf(stderr
, fmt_m
, ap
);
724 static void ui_clean_warn(void)
730 static int __attribute__((__noreturn__
)) ui_errx(int rc
, const char *fmt
, ...)
736 fprintf(stderr
, "%s: ", program_invocation_short_name
);
737 vfprintf(stderr
, fmt
, ap
);
744 static void ui_info(const char *fmt
, ...)
749 ui_vprint_center(INFO_LINE
,
750 colors_wanted() ? COLOR_PAIR(CFDISK_CL_INFO
) : 0,
753 vfprintf(stdout
, fmt
, ap
);
759 static void ui_clean_info(void)
765 static void ui_hint(const char *fmt
, ...)
770 ui_vprint_center(HINT_LINE
, A_BOLD
, fmt
, ap
);
772 vfprintf(stdout
, fmt
, ap
);
778 static void ui_clean_hint(void)
784 static void die_on_signal(int dummy
__attribute__((__unused__
)))
786 DBG(MISC
, ul_debug("die on signal."));
791 static void resize_on_signal(int dummy
__attribute__((__unused__
)))
793 DBG(MISC
, ul_debug("resize on signal."));
797 static void menu_refresh_size(struct cfdisk
*cf
)
799 if (cf
->menu
&& cf
->menu
->nitems
)
800 cf
->menu
->page_sz
= (cf
->menu
->nitems
/ (ui_lines
- 4)) ? ui_lines
- 4 : 0;
803 static void menu_update_ignore(struct cfdisk
*cf
)
805 char ignore
[128] = { 0 };
807 struct cfdisk_menu
*m
;
808 struct cfdisk_menuitem
*d
, *org
= NULL
;
813 assert(cf
->menu
->ignore_cb
);
816 DBG(MENU
, ul_debug("update menu ignored keys"));
818 i
= m
->ignore_cb(cf
, ignore
, sizeof(ignore
));
821 /* return if no change */
822 if ((!m
->ignore
&& !*ignore
)
823 || (m
->ignore
&& *ignore
&& strcmp(m
->ignore
, ignore
) == 0)) {
828 org
= menu_get_menuitem(cf
, m
->idx
);
831 m
->ignore
= xstrdup(ignore
);
834 for (d
= m
->items
; d
->name
; d
++) {
835 if (m
->ignore
&& strchr(m
->ignore
, d
->key
))
840 DBG(MENU
, ul_debug("update menu preferred keys"));
842 /* refresh menu index to be at the same menuitem or go to the first */
843 if (org
&& menu_get_menuitem_by_key(cf
, org
->key
, &idx
))
845 else if (m
->prefkey
&& menu_get_menuitem_by_key(cf
, m
->prefkey
, &idx
))
850 menu_refresh_size(cf
);
853 static struct cfdisk_menu
*menu_push(
855 struct cfdisk_menuitem
*items
)
857 struct cfdisk_menu
*m
= xcalloc(1, sizeof(*m
));
858 struct cfdisk_menuitem
*d
;
862 DBG(MENU
, ul_debug("new menu"));
867 for (d
= m
->items
; d
->name
; d
++) {
868 const char *name
= _(d
->name
);
869 size_t len
= mbs_safe_width(name
);
877 menu_refresh_size(cf
);
881 static struct cfdisk_menu
*menu_pop(struct cfdisk
*cf
)
883 struct cfdisk_menu
*m
= NULL
;
887 DBG(MENU
, ul_debug("pop menu"));
891 free(cf
->menu
->ignore
);
892 free(cf
->menu
->title
);
899 static void menu_set_title(struct cfdisk_menu
*m
, const char *title
)
904 size_t len
= mbs_safe_width(title
);
905 if (len
+ MENU_TITLE_PADDING
> m
->width
)
906 m
->width
= len
+ MENU_TITLE_PADDING
;
907 str
= xstrdup(title
);
913 static int ui_init(struct cfdisk
*cf
__attribute__((__unused__
)))
917 DBG(UI
, ul_debug("init"));
919 /* setup SIGCHLD handler */
920 sigemptyset(&sa
.sa_mask
);
922 sa
.sa_handler
= die_on_signal
;
923 sigaction(SIGINT
, &sa
, NULL
);
924 sigaction(SIGTERM
, &sa
, NULL
);
926 sa
.sa_handler
= resize_on_signal
;
927 sigaction(SIGWINCH
, &sa
, NULL
);
932 #ifdef HAVE_USE_DEFAULT_COLORS
933 if (colors_wanted() && has_colors()) {
937 use_default_colors();
938 for (i
= 1; i
< ARRAY_SIZE(color_pairs
); i
++) /* yeah, start from 1! */
939 init_pair(i
, color_pairs
[i
][0], color_pairs
[i
][1]);
949 keypad(stdscr
, TRUE
);
955 #define MENU_H_ITEMWIDTH(m) ( MENU_H_PRESTR_SZ \
961 #define MENU_V_ITEMWIDTH(m) (MENU_V_SPADDING + (m)->width + MENU_V_SPADDING)
964 static size_t menuitem_get_line(struct cfdisk
*cf
, size_t idx
)
966 struct cfdisk_menu
*m
= cf
->menu
;
969 if (!m
->page_sz
) /* small menu */
970 return (ui_lines
- (cf
->menu
->nitems
+ 1)) / 2 + idx
;
971 return (idx
% m
->page_sz
) + 1;
973 size_t len
= MENU_H_ITEMWIDTH(m
) + MENU_H_BETWEEN
; /** item width */
974 size_t items
= ui_cols
/ len
; /* items per line */
978 return MENU_START_LINE
+ ((idx
/ items
));
982 static int menuitem_get_column(struct cfdisk
*cf
, size_t idx
)
984 if (cf
->menu
->vertical
) {
985 size_t nc
= MENU_V_ITEMWIDTH(cf
->menu
);
986 if ((size_t) ui_cols
<= nc
)
988 return (ui_cols
- nc
) / 2;
990 size_t len
= MENU_H_ITEMWIDTH(cf
->menu
) + MENU_H_BETWEEN
; /* item width */
991 size_t items
= ui_cols
/ len
; /* items per line */
992 size_t extra
= items
< cf
->menu
->nitems
? /* extra space on line */
993 ui_cols
% len
: /* - multi-line menu */
994 ui_cols
- (cf
->menu
->nitems
* len
); /* - one line menu */
997 return 0; /* hmm... no space */
999 extra
+= MENU_H_BETWEEN
; /* add padding after last item to extra */
1002 return (idx
* len
) + (extra
/ 2);
1003 return ((idx
% items
) * len
) + (extra
/ 2);
1007 static int menuitem_on_page(struct cfdisk
*cf
, size_t idx
)
1009 struct cfdisk_menu
*m
= cf
->menu
;
1011 if (m
->page_sz
== 0 ||
1012 m
->idx
/ m
->page_sz
== idx
/ m
->page_sz
)
1017 static struct cfdisk_menuitem
*menu_get_menuitem(struct cfdisk
*cf
, size_t idx
)
1019 struct cfdisk_menuitem
*d
;
1022 for (i
= 0, d
= cf
->menu
->items
; d
->name
; d
++) {
1023 if (cf
->menu
->ignore
&& strchr(cf
->menu
->ignore
, d
->key
))
1032 static struct cfdisk_menuitem
*menu_get_menuitem_by_key(struct cfdisk
*cf
,
1033 int key
, size_t *idx
)
1035 struct cfdisk_menuitem
*d
;
1037 for (*idx
= 0, d
= cf
->menu
->items
; d
->name
; d
++) {
1038 if (cf
->menu
->ignore
&& strchr(cf
->menu
->ignore
, d
->key
))
1048 static void ui_draw_menuitem(struct cfdisk
*cf
,
1049 struct cfdisk_menuitem
*d
,
1055 const size_t buf_sz
= 80 * MB_CUR_MAX
;
1056 int ln
, cl
, vert
= cf
->menu
->vertical
;
1058 if (!menuitem_on_page(cf
, idx
))
1059 return; /* no visible item */
1060 ln
= menuitem_get_line(cf
, idx
);
1061 cl
= menuitem_get_column(cf
, idx
);
1063 ptr
= buf
= xmalloc(buf_sz
);
1066 width
= cf
->menu
->width
+ MENU_V_SPADDING
;
1067 memset(ptr
, ' ', MENU_V_SPADDING
);
1068 ptr
+= MENU_V_SPADDING
;
1070 width
= MENU_H_SPADDING
+ cf
->menu
->width
+ MENU_H_SPADDING
;
1073 mbsalign(name
, ptr
, buf_sz
, &width
,
1074 vert
? MBS_ALIGN_LEFT
: MBS_ALIGN_CENTER
,
1077 DBG(MENU
, ul_debug("menuitem: cl=%d, ln=%d, item='%s'",
1081 mvaddch(ln
, cl
- 1, ACS_VLINE
);
1082 mvaddch(ln
, cl
+ MENU_V_ITEMWIDTH(cf
->menu
), ACS_VLINE
);
1085 if (cf
->menu
->idx
== idx
)
1089 mvprintw(ln
, cl
, "%s", buf
);
1091 mvprintw(ln
, cl
, "%s%s%s", MENU_H_PRESTR
, buf
, MENU_H_POSTSTR
);
1094 if (cf
->menu
->idx
== idx
) {
1097 ui_hint(_(d
->desc
));
1101 static void ui_clean_menu(struct cfdisk
*cf
)
1105 struct cfdisk_menu
*m
= cf
->menu
;
1106 size_t ln
= menuitem_get_line(cf
, 0);
1109 lastline
= ln
+ (m
->page_sz
? m
->page_sz
: m
->nitems
);
1111 lastline
= menuitem_get_line(cf
, m
->nitems
);
1113 for (i
= ln
; i
<= lastline
; i
++) {
1116 DBG(MENU
, ul_debug("clean_menu: line %zu", i
));
1125 static void ui_draw_menu(struct cfdisk
*cf
)
1127 struct cfdisk_menuitem
*d
;
1128 struct cfdisk_menu
*m
;
1130 size_t ln
= menuitem_get_line(cf
, 0);
1136 DBG(MENU
, ul_debug("draw start"));
1142 nlines
= m
->page_sz
? m
->page_sz
: m
->nitems
;
1144 nlines
= menuitem_get_line(cf
, m
->nitems
);
1147 menu_update_ignore(cf
);
1149 while ((d
= menu_get_menuitem(cf
, i
)))
1150 ui_draw_menuitem(cf
, d
, i
++);
1153 size_t cl
= menuitem_get_column(cf
, 0);
1154 size_t curpg
= m
->page_sz
? m
->idx
/ m
->page_sz
: 0;
1156 /* corners and horizontal lines */
1157 mvaddch(ln
- 1, cl
- 1, ACS_ULCORNER
);
1158 mvaddch(ln
+ nlines
, cl
- 1, ACS_LLCORNER
);
1160 for (i
= 0; i
< MENU_V_ITEMWIDTH(m
); i
++) {
1161 mvaddch(ln
- 1, cl
+ i
, ACS_HLINE
);
1162 mvaddch(ln
+ nlines
, cl
+ i
, ACS_HLINE
);
1165 mvaddch(ln
- 1, cl
+ i
, ACS_URCORNER
);
1166 mvaddch(ln
+ nlines
, cl
+ i
, ACS_LRCORNER
);
1168 /* draw also lines around empty lines on last page */
1170 m
->nitems
/ m
->page_sz
== m
->idx
/ m
->page_sz
) {
1171 for (i
= m
->nitems
% m
->page_sz
+ 1; i
<= m
->page_sz
; i
++) {
1172 mvaddch(i
, cl
- 1, ACS_VLINE
);
1173 mvaddch(i
, cl
+ MENU_V_ITEMWIDTH(m
), ACS_VLINE
);
1178 mvprintw(ln
- 1, cl
, " %s ", m
->title
);
1182 mvaddch(ln
- 1, cl
+ MENU_V_ITEMWIDTH(m
) - 2, ACS_UARROW
);
1183 if (m
->page_sz
&& curpg
< m
->nitems
/ m
->page_sz
)
1184 mvaddch(ln
+ nlines
, cl
+ MENU_V_ITEMWIDTH(m
) - 2, ACS_DARROW
);
1187 DBG(MENU
, ul_debug("draw end."));
1190 inline static int extra_insert_pair(struct cfdisk_line
*l
, const char *name
, const char *data
)
1192 struct libscols_line
*lsl
;
1198 if (!data
|| !*data
)
1201 lsl
= scols_table_new_line(l
->extra
, NULL
);
1205 rc
= scols_line_set_data(lsl
, 0, name
);
1207 rc
= scols_line_set_data(lsl
, 1, data
);
1212 #ifndef HAVE_LIBMOUNT
1213 static char *get_mountpoint( struct cfdisk
*cf
__attribute__((unused
)),
1214 const char *tagname
__attribute__((unused
)),
1215 const char *tagdata
__attribute__((unused
)))
1220 static char *get_mountpoint(struct cfdisk
*cf
, const char *tagname
, const char *tagdata
)
1222 struct libmnt_fs
*fs
= NULL
;
1223 char *target
= NULL
;
1229 DBG(UI
, ul_debug("asking for mountpoint [%s=%s]", tagname
, tagdata
));
1232 cf
->mntcache
= mnt_new_cache();
1234 /* 1st try between mounted filesystems */
1236 cf
->mtab
= mnt_new_table();
1238 mnt_table_set_cache(cf
->mtab
, cf
->mntcache
);
1239 mnt_table_parse_mtab(cf
->mtab
, NULL
);
1244 fs
= mnt_table_find_tag(cf
->mtab
, tagname
, tagdata
, MNT_ITER_FORWARD
);
1249 cf
->fstab
= mnt_new_table();
1251 mnt_table_set_cache(cf
->fstab
, cf
->mntcache
);
1252 mnt_table_parse_fstab(cf
->fstab
, NULL
);
1256 fs
= mnt_table_find_tag(cf
->fstab
, tagname
, tagdata
, MNT_ITER_FORWARD
);
1262 xasprintf(&target
, _("%s (mounted)"), mnt_fs_get_target(fs
));
1264 target
= xstrdup(mnt_fs_get_target(fs
));
1269 #endif /* HAVE_LIBMOUNT */
1271 static void extra_prepare_data(struct cfdisk
*cf
)
1273 struct fdisk_partition
*pa
= get_current_partition(cf
);
1274 struct cfdisk_line
*l
= &cf
->lines
[cf
->lines_idx
];
1276 char *mountpoint
= NULL
;
1278 DBG(UI
, ul_debug("preparing extra data"));
1280 /* string data should not equal an empty string */
1281 if (!fdisk_partition_to_string(pa
, cf
->cxt
, FDISK_FIELD_NAME
, &data
) && data
) {
1282 extra_insert_pair(l
, _("Partition name:"), data
);
1284 mountpoint
= get_mountpoint(cf
, "PARTLABEL", data
);
1288 if (!fdisk_partition_to_string(pa
, cf
->cxt
, FDISK_FIELD_UUID
, &data
) && data
) {
1289 extra_insert_pair(l
, _("Partition UUID:"), data
);
1291 mountpoint
= get_mountpoint(cf
, "PARTUUID", data
);
1295 if (!fdisk_partition_to_string(pa
, cf
->cxt
, FDISK_FIELD_TYPE
, &data
) && data
) {
1296 char *code
= NULL
, *type
= NULL
;
1298 fdisk_partition_to_string(pa
, cf
->cxt
, FDISK_FIELD_TYPEID
, &code
);
1299 xasprintf(&type
, "%s (%s)", data
, code
);
1301 extra_insert_pair(l
, _("Partition type:"), type
);
1307 if (!fdisk_partition_to_string(pa
, cf
->cxt
, FDISK_FIELD_ATTR
, &data
) && data
) {
1308 extra_insert_pair(l
, _("Attributes:"), data
);
1312 /* for numeric data, only show non-zero rows */
1313 if (!fdisk_partition_to_string(pa
, cf
->cxt
, FDISK_FIELD_BSIZE
, &data
) && data
) {
1315 extra_insert_pair(l
, "BSIZE:", data
);
1319 if (!fdisk_partition_to_string(pa
, cf
->cxt
, FDISK_FIELD_CPG
, &data
) && data
) {
1321 extra_insert_pair(l
, "CPG:", data
);
1325 if (!fdisk_partition_to_string(pa
, cf
->cxt
, FDISK_FIELD_FSIZE
, &data
) && data
) {
1327 extra_insert_pair(l
, "FSIZE:", data
);
1331 if (!fdisk_partition_to_string(pa
, cf
->cxt
, FDISK_FIELD_FSUUID
, &data
) && data
) {
1332 extra_insert_pair(l
, _("Filesystem UUID:"), data
);
1334 mountpoint
= get_mountpoint(cf
, "UUID", data
);
1338 if (!fdisk_partition_to_string(pa
, cf
->cxt
, FDISK_FIELD_FSLABEL
, &data
) && data
) {
1339 extra_insert_pair(l
, _("Filesystem LABEL:"), data
);
1341 mountpoint
= get_mountpoint(cf
, "LABEL", data
);
1344 if (!fdisk_partition_to_string(pa
, cf
->cxt
, FDISK_FIELD_FSTYPE
, &data
) && data
) {
1345 extra_insert_pair(l
, _("Filesystem:"), data
);
1350 extra_insert_pair(l
, _("Mountpoint:"), mountpoint
);
1355 static int ui_draw_extra(struct cfdisk
*cf
)
1359 struct cfdisk_line
*ln
= &cf
->lines
[cf
->lines_idx
];
1360 char *tbstr
= NULL
, *end
;
1361 int win_ex_start_line
, win_height
, tblen
;
1364 if (!cf
->show_extra
)
1367 DBG(UI
, ul_debug("draw extra"));
1372 wclear(cf
->act_win
);
1376 if (scols_table_is_empty(ln
->extra
)) {
1377 extra_prepare_data(cf
);
1378 if (scols_table_is_empty(ln
->extra
))
1382 ndatalines
= fdisk_table_get_nents(cf
->table
) + 1;
1384 /* nents + header + one free line */
1385 win_ex_start_line
= TABLE_START_LINE
+ ndatalines
;
1386 win_height
= MENU_START_LINE
- win_ex_start_line
;
1387 tblen
= scols_table_get_nlines(ln
->extra
);
1389 /* we can't get a single line of data under the partlist*/
1393 /* number of data lines + 2 for top/bottom lines */
1394 win_height
= win_height
< tblen
+ 2 ? win_height
: tblen
+ 2;
1396 if ((size_t) win_ex_start_line
+ win_height
+ 1 < MENU_START_LINE
)
1397 win_ex_start_line
= MENU_START_LINE
- win_height
;
1399 win_ex
= subwin(stdscr
, win_height
, ui_cols
- 2, win_ex_start_line
, 1);
1401 scols_table_reduce_termwidth(ln
->extra
, 4);
1402 scols_print_table_to_string(ln
->extra
, &tbstr
);
1405 while ((end
= strchr(end
, '\n')))
1411 while (--win_height
> 1) {
1412 mvwaddstr(win_ex
, wline
++, 1 /* window column*/, tbstr
);
1413 tbstr
+= strlen(tbstr
) + 1;
1420 DBG(UI
, ul_debug("draw window: %p", win_ex
));
1424 cf
->act_win
= ln
->w
= win_ex
;
1428 static void ui_menu_goto(struct cfdisk
*cf
, int where
)
1430 struct cfdisk_menuitem
*d
;
1433 /* stop and begin/end for vertical menus */
1434 if (cf
->menu
->vertical
) {
1437 else if (where
> (int) cf
->menu
->nitems
- 1)
1438 where
= cf
->menu
->nitems
- 1;
1440 /* continue from begin/end */
1442 where
= cf
->menu
->nitems
- 1;
1443 else if ((size_t) where
> cf
->menu
->nitems
- 1)
1446 if ((size_t) where
== cf
->menu
->idx
)
1451 old
= cf
->menu
->idx
;
1452 cf
->menu
->idx
= where
;
1454 if (!menuitem_on_page(cf
, old
)) {
1459 d
= menu_get_menuitem(cf
, old
);
1460 ui_draw_menuitem(cf
, d
, old
);
1462 d
= menu_get_menuitem(cf
, where
);
1463 ui_draw_menuitem(cf
, d
, where
);
1467 static int ui_menu_move(struct cfdisk
*cf
, int key
)
1469 struct cfdisk_menu
*m
;
1474 if (key
== (int) ERR
)
1475 return 0; /* ignore errors */
1479 DBG(MENU
, ul_debug("menu move key >%c<.", key
));
1485 case '\016': /* ^N */
1486 case 'j': /* Vi-like alternative */
1487 ui_menu_goto(cf
, m
->idx
+ 1);
1490 case '\020': /* ^P */
1491 case 'k': /* Vi-like alternative */
1492 ui_menu_goto(cf
, (int) m
->idx
- 1);
1496 ui_menu_goto(cf
, (int) m
->idx
- m
->page_sz
);
1501 ui_menu_goto(cf
, 0);
1505 ui_menu_goto(cf
, m
->idx
+ m
->page_sz
);
1510 ui_menu_goto(cf
, m
->nitems
);
1517 ui_menu_goto(cf
, m
->idx
+ 1);
1523 ui_menu_goto(cf
, (int) m
->idx
- 1);
1528 if (key
== '\014') { /* ^L refresh */
1533 DBG(MENU
, ul_debug(" no menu move key"));
1537 /* but don't call me from ui_run(), this is for pop-up menus only */
1538 static void ui_menu_resize(struct cfdisk
*cf
)
1540 DBG(MENU
, ul_debug("menu resize/refresh"));
1543 menu_refresh_size(cf
);
1548 static int partition_on_page(struct cfdisk
*cf
, size_t i
)
1550 if (cf
->page_sz
== 0 ||
1551 cf
->lines_idx
/ cf
->page_sz
== i
/ cf
->page_sz
)
1556 static void ui_draw_partition(struct cfdisk
*cf
, size_t i
)
1558 int ln
= TABLE_START_LINE
+ 1 + i
; /* skip table header */
1559 int cl
= ARROW_CURSOR_WIDTH
; /* we need extra space for cursor */
1560 int cur
= cf
->lines_idx
== i
;
1564 if (!partition_on_page(cf
, i
))
1566 ln
= TABLE_START_LINE
+ (i
% cf
->page_sz
) + 1;
1567 curpg
= cf
->lines_idx
/ cf
->page_sz
;
1571 "draw partition %zu [page_sz=%zu, "
1572 "line=%d, idx=%zu]",
1573 i
, cf
->page_sz
, ln
, cf
->lines_idx
));
1577 mvaddstr(ln
, 0, ARROW_CURSOR_STRING
);
1578 mvaddstr(ln
, cl
, cf
->lines
[i
+ 1].data
);
1583 if (colors_wanted() && is_freespace(cf
, i
)) {
1584 attron(COLOR_PAIR(CFDISK_CL_FREESPACE
));
1587 mvaddstr(ln
, 0, ARROW_CURSOR_DUMMY
);
1588 mvaddstr(ln
, cl
, cf
->lines
[i
+ 1].data
);
1590 attroff(COLOR_PAIR(CFDISK_CL_FREESPACE
));
1593 if ((size_t) ln
== MENU_START_LINE
- 1 &&
1594 cf
->page_sz
&& curpg
< cf
->nlines
/ cf
->page_sz
) {
1597 mvaddch(ln
, ui_cols
- 1, ACS_DARROW
);
1598 mvaddch(ln
, 0, ACS_DARROW
);
1605 static int ui_draw_table(struct cfdisk
*cf
)
1607 int cl
= ARROW_CURSOR_WIDTH
;
1608 size_t i
, nparts
= fdisk_table_get_nents(cf
->table
);
1609 size_t curpg
= cf
->page_sz
? cf
->lines_idx
/ cf
->page_sz
: 0;
1611 DBG(UI
, ul_debug("draw table"));
1613 for (i
= TABLE_START_LINE
; i
<= TABLE_START_LINE
+ cf
->page_sz
; i
++) {
1618 if (nparts
== 0 || (size_t) cf
->lines_idx
> nparts
- 1)
1619 cf
->lines_idx
= nparts
? nparts
- 1 : 0;
1623 mvaddstr(TABLE_START_LINE
, cl
, cf
->lines
[0].data
);
1626 /* print partitions */
1627 for (i
= 0; i
< nparts
; i
++)
1628 ui_draw_partition(cf
, i
);
1631 mvaddch(TABLE_START_LINE
, ui_cols
- 1, ACS_UARROW
);
1632 mvaddch(TABLE_START_LINE
, 0, ACS_UARROW
);
1634 if (cf
->page_sz
&& curpg
< cf
->nlines
/ cf
->page_sz
) {
1635 mvaddch(MENU_START_LINE
- 1, ui_cols
- 1, ACS_DARROW
);
1636 mvaddch(MENU_START_LINE
- 1, 0, ACS_DARROW
);
1641 static int ui_table_goto(struct cfdisk
*cf
, int where
)
1644 size_t nparts
= fdisk_table_get_nents(cf
->table
);
1646 DBG(UI
, ul_debug("goto table %d", where
));
1650 else if ((size_t) where
> nparts
- 1)
1653 if ((size_t) where
== cf
->lines_idx
)
1656 old
= cf
->lines_idx
;
1657 cf
->lines_idx
= where
;
1659 if (!partition_on_page(cf
, old
) ||!partition_on_page(cf
, where
))
1662 ui_draw_partition(cf
, old
); /* cleanup old */
1663 ui_draw_partition(cf
, where
); /* draw new */
1673 static int ui_refresh(struct cfdisk
*cf
)
1675 struct fdisk_label
*lb
;
1677 uint64_t bytes
= fdisk_get_nsectors(cf
->cxt
) * fdisk_get_sector_size(cf
->cxt
);
1683 strsz
= size_to_human_string(SIZE_SUFFIX_SPACE
1684 | SIZE_SUFFIX_3LETTER
, bytes
);
1686 lb
= fdisk_get_label(cf
->cxt
, NULL
);
1693 ui_center(0, _("Disk: %s"), fdisk_get_devname(cf
->cxt
));
1695 ui_center(1, _("Size: %s, %ju bytes, %ju sectors"),
1696 strsz
, bytes
, (uintmax_t) fdisk_get_nsectors(cf
->cxt
));
1697 if (fdisk_get_disklabel_id(cf
->cxt
, &id
) == 0 && id
)
1698 ui_center(2, _("Label: %s, identifier: %s"),
1699 fdisk_label_get_name(lb
), id
);
1701 ui_center(2, _("Label: %s"), fdisk_label_get_name(lb
));
1710 static ssize_t
ui_get_string(const char *prompt
,
1711 const char *hint
, char *buf
, size_t len
)
1713 int ln
= MENU_START_LINE
, cl
= 1;
1715 struct mbs_editor
*edit
;
1717 DBG(UI
, ul_debug("ui get string"));
1729 mvaddstr(ln
, cl
, (char *) prompt
);
1730 cl
+= mbs_safe_width(prompt
);
1733 edit
= mbs_new_edit(buf
, len
, ui_cols
- cl
);
1737 mbs_edit_goto(edit
, MBS_EDIT_END
);
1747 wint_t c
; /* we have fallback in widechar.h */
1751 mvaddstr(ln
, cl
, edit
->buf
);
1752 move(ln
, cl
+ edit
->cursor_cells
);
1755 #if !defined(HAVE_SLCURSES_H) && !defined(HAVE_SLANG_SLCURSES_H) && \
1756 defined(HAVE_LIBNCURSESW) && defined(HAVE_WIDECHAR)
1757 if (get_wch(&c
) == ERR
) {
1759 if ((c
= getch()) == (wint_t) ERR
) {
1765 if (!isatty(STDIN_FILENO
))
1771 DBG(UI
, ul_debug("ui get string: key=%lc", c
));
1773 if (c
== '\r' || c
== '\n' || c
== KEY_ENTER
)
1780 rc
= -CFDISK_ERR_ESC
;
1783 rc
= mbs_edit_goto(edit
, MBS_EDIT_LEFT
);
1786 rc
= mbs_edit_goto(edit
, MBS_EDIT_RIGHT
);
1789 rc
= mbs_edit_goto(edit
, MBS_EDIT_END
);
1792 rc
= mbs_edit_goto(edit
, MBS_EDIT_HOME
);
1798 rc
= mbs_edit_delete(edit
);
1802 rc
= mbs_edit_backspace(edit
);
1805 rc
= mbs_edit_insert(edit
, c
);
1812 rc
= strlen(edit
->buf
); /* success */
1818 mbs_free_edit(edit
);
1823 /* @res is default value as well as result in bytes */
1824 static int ui_get_size(struct cfdisk
*cf
, const char *prompt
, uintmax_t *res
,
1825 uintmax_t low
, uintmax_t up
, int *expsize
)
1830 char *dflt
= size_to_human_string(0, *res
);
1832 DBG(UI
, ul_debug("get_size (default=%ju)", *res
));
1836 snprintf(buf
, sizeof(buf
), "%s", dflt
);
1839 int pwr
= 0, insec
= 0;
1841 rc
= ui_get_string(prompt
,
1842 _("May be followed by M for MiB, G for GiB, "
1843 "T for TiB, or S for sectors."),
1848 ui_warnx(_("Please, specify size."));
1849 continue; /* nothing specified */
1850 } else if (rc
== -CFDISK_ERR_ESC
)
1851 break; /* cancel dialog */
1853 if (strcmp(buf
, dflt
) == 0)
1854 user
= *res
, rc
= 0; /* no change, use default */
1856 size_t len
= strlen(buf
);
1857 if (buf
[len
- 1] == 'S' || buf
[len
- 1] == 's') {
1859 buf
[len
- 1] = '\0';
1861 rc
= parse_size(buf
, &user
, &pwr
); /* parse */
1865 DBG(UI
, ul_debug("get_size user=%ju, power=%d, sectors=%s",
1866 user
, pwr
, insec
? "yes" : "no"));
1868 user
*= fdisk_get_sector_size(cf
->cxt
);
1870 ui_warnx(_("Minimum size is %ju bytes."), low
);
1873 if (user
> up
&& pwr
&& user
< up
+ (1ULL << pwr
* 10))
1874 /* ignore when the user specified size overflow
1875 * with in range specified by suffix (e.g. MiB) */
1879 ui_warnx(_("Maximum size is %ju bytes."), up
);
1882 if (rc
== 0 && insec
&& expsize
)
1886 ui_warnx(_("Failed to parse size."));
1893 DBG(UI
, ul_debug("get_size (result=%ju, rc=%zd)", *res
, rc
));
1897 static struct fdisk_parttype
*ui_get_parttype(struct cfdisk
*cf
,
1898 struct fdisk_parttype
*cur
)
1900 struct cfdisk_menuitem
*d
, *cm
;
1901 size_t i
= 0, nitems
, idx
= 0;
1902 struct fdisk_parttype
*t
= NULL
;
1903 struct fdisk_label
*lb
;
1906 DBG(UI
, ul_debug("asking for parttype."));
1908 lb
= fdisk_get_label(cf
->cxt
, NULL
);
1910 /* create cfdisk menu according to label types, note that the
1911 * last cm[] item has to be empty -- so nitems + 1 */
1912 nitems
= fdisk_label_get_nparttypes(lb
);
1916 cm
= xcalloc(nitems
+ 1, sizeof(struct cfdisk_menuitem
));
1920 codetypes
= fdisk_label_has_code_parttypes(lb
);
1922 for (i
= 0; i
< nitems
; i
++) {
1923 const struct fdisk_parttype
*x
= fdisk_label_get_parttype(lb
, i
);
1926 cm
[i
].userdata
= (void *) x
;
1928 xasprintf(&name
, "%2x %s",
1929 fdisk_parttype_get_code(x
),
1930 _(fdisk_parttype_get_name(x
)));
1932 name
= (char *) _(fdisk_parttype_get_name(x
));
1933 cm
[i
].desc
= fdisk_parttype_get_string(x
);
1940 /* make the new menu active */
1942 cf
->menu
->vertical
= 1;
1943 cf
->menu
->idx
= idx
;
1944 menu_set_title(cf
->menu
, _("Select partition type"));
1953 if (ui_menu_move(cf
, key
) == 0)
1960 d
= menu_get_menuitem(cf
, cf
->menu
->idx
);
1962 t
= (struct fdisk_parttype
*) d
->userdata
;
1974 for (i
= 0; i
< nitems
; i
++)
1975 free((char *) cm
[i
].name
);
1978 DBG(UI
, ul_debug("get parrtype done [type=%s] ", t
?
1979 fdisk_parttype_get_name(t
) : NULL
));
1983 static int ui_script_read(struct cfdisk
*cf
)
1985 struct fdisk_script
*sc
= NULL
;
1986 char buf
[PATH_MAX
] = { 0 };
1990 rc
= ui_get_string( _("Enter script file name: "),
1991 _("The script file will be applied to in-memory partition table."),
1998 sc
= fdisk_new_script_from_file(cf
->cxt
, buf
);
2000 ui_warn(_("Cannot open %s"), buf
);
2002 ui_warnx(_("Failed to parse script file %s"), buf
);
2003 else if (fdisk_apply_script(cf
->cxt
, sc
) != 0)
2004 ui_warnx(_("Failed to apply script %s"), buf
);
2009 fdisk_unref_script(sc
);
2013 static int ui_script_write(struct cfdisk
*cf
)
2015 struct fdisk_script
*sc
= NULL
;
2016 char buf
[PATH_MAX
] = { 0 };
2020 rc
= ui_get_string( _("Enter script file name: "),
2021 _("The current in-memory partition table will be dumped to the file."),
2027 sc
= fdisk_new_script(cf
->cxt
);
2029 ui_warn(_("Failed to allocate script handler"));
2033 rc
= fdisk_script_read_context(sc
, NULL
);
2035 ui_warnx(_("Failed to read disk layout into script."));
2039 DBG(UI
, ul_debug("writing dump into: '%s'", buf
));
2040 f
= fopen(buf
, "w");
2042 ui_warn(_("Cannot open %s"), buf
);
2047 rc
= fdisk_script_write_file(sc
, f
);
2049 ui_info(_("Disk layout successfully dumped."));
2052 ui_warn(_("Failed to write script %s"), buf
);
2055 fdisk_unref_script(sc
);
2059 /* prints menu with libfdisk labels and waits for users response */
2060 static int ui_create_label(struct cfdisk
*cf
)
2062 struct cfdisk_menuitem
*d
, *cm
;
2063 int rc
= 1, refresh_menu
= 1;
2064 size_t i
= 0, nitems
;
2065 struct fdisk_label
*lb
= NULL
;
2069 DBG(UI
, ul_debug("asking for new disklabe."));
2071 /* create cfdisk menu according to libfdisk labels, note that the
2072 * last cm[] item has to be empty -- so nitems + 1 */
2073 nitems
= fdisk_get_nlabels(cf
->cxt
);
2074 cm
= xcalloc(nitems
+ 1, sizeof(struct cfdisk_menuitem
));
2076 while (fdisk_next_label(cf
->cxt
, &lb
) == 0) {
2077 if (fdisk_label_is_disabled(lb
) ||
2078 fdisk_label_get_type(lb
) == FDISK_DISKLABEL_BSD
)
2080 cm
[i
++].name
= fdisk_label_get_name(lb
);
2085 /* make the new menu active */
2087 cf
->menu
->vertical
= 1;
2088 menu_set_title(cf
->menu
, _("Select label type"));
2090 if (!cf
->zero_start
)
2091 ui_info(_("Device does not contain a recognized partition table."));
2099 ui_hint(_("Select a type to create a new label or press 'L' to load script file."));
2108 if (ui_menu_move(cf
, key
) == 0)
2114 d
= menu_get_menuitem(cf
, cf
->menu
->idx
);
2116 rc
= fdisk_create_disklabel(cf
->cxt
, d
->name
);
2124 rc
= ui_script_read(cf
);
2135 DBG(UI
, ul_debug("create label done [rc=%d] ", rc
));
2140 static int ui_help(void)
2143 static const char *help
[] = {
2144 N_("This is cfdisk, a curses-based disk partitioning program."),
2145 N_("It lets you create, delete, and modify partitions on a block device."),
2147 N_("Command Meaning"),
2148 N_("------- -------"),
2149 N_(" b Toggle bootable flag of the current partition"),
2150 N_(" d Delete the current partition"),
2151 N_(" h Print this screen"),
2152 N_(" n Create new partition from free space"),
2153 N_(" q Quit program without writing partition table"),
2154 N_(" s Fix partitions order (only when in disarray)"),
2155 N_(" t Change the partition type"),
2156 N_(" u Dump disk layout to sfdisk compatible script file"),
2157 N_(" W Write partition table to disk (you must enter uppercase W);"),
2158 N_(" since this might destroy data on the disk, you must either"),
2159 N_(" confirm or deny the write by entering 'yes' or 'no'"),
2160 N_(" x Display/hide extra information about a partition"),
2161 N_("Up Arrow Move cursor to the previous partition"),
2162 N_("Down Arrow Move cursor to the next partition"),
2163 N_("Left Arrow Move cursor to the previous menu item"),
2164 N_("Right Arrow Move cursor to the next menu item"),
2166 N_("Note: All of the commands can be entered with either upper or lower"),
2167 N_("case letters (except for Write)."),
2169 N_("Use lsblk(8) or partx(8) to see more details about the device."),
2172 "Copyright (C) 2014-2017 Karel Zak <kzak@redhat.com>"
2176 for (i
= 0; i
< ARRAY_SIZE(help
); i
++)
2177 mvaddstr(i
, 1, _(help
[i
]));
2179 ui_info(_("Press a key to continue."));
2185 /* TODO: use @sz, now 128bytes */
2186 static int main_menu_ignore_keys(struct cfdisk
*cf
, char *ignore
,
2187 size_t sz
__attribute__((__unused__
)))
2189 struct fdisk_partition
*pa
= get_current_partition(cf
);
2194 if (fdisk_partition_is_freespace(pa
)) {
2195 ignore
[i
++] = 'd'; /* delete */
2196 ignore
[i
++] = 't'; /* set type */
2197 ignore
[i
++] = 'b'; /* set bootable */
2198 cf
->menu
->prefkey
= 'n';
2200 cf
->menu
->prefkey
= 'q';
2202 if (!fdisk_is_label(cf
->cxt
, DOS
) &&
2203 !fdisk_is_label(cf
->cxt
, SGI
))
2207 if (!cf
->wrong_order
)
2210 if (fdisk_is_readonly(cf
->cxt
))
2217 /* returns: error: < 0, success: 0, quit: 1 */
2218 static int main_menu_action(struct cfdisk
*cf
, int key
)
2221 int ref
= 0, rc
, org_order
= cf
->wrong_order
;
2222 const char *info
= NULL
, *warn
= NULL
;
2223 struct fdisk_partition
*pa
;
2230 struct cfdisk_menuitem
*d
= menu_get_menuitem(cf
, cf
->menu
->idx
);
2235 } else if (key
!= 'w' && key
!= 'W')
2236 key
= tolower(key
); /* case insensitive except 'W'rite */
2238 DBG(MENU
, ul_debug("main menu action: key=%c", key
));
2240 if (cf
->menu
->ignore
&& strchr(cf
->menu
->ignore
, key
)) {
2241 DBG(MENU
, ul_debug(" ignore '%c'", key
));
2245 pa
= get_current_partition(cf
);
2248 n
= fdisk_partition_get_partno(pa
);
2250 DBG(MENU
, ul_debug("menu action on %p", pa
));
2255 case 'b': /* Bootable flag */
2257 int fl
= fdisk_is_label(cf
->cxt
, DOS
) ? DOS_FLAG_ACTIVE
:
2258 fdisk_is_label(cf
->cxt
, SGI
) ? SGI_FLAG_BOOT
: 0;
2260 if (fl
&& fdisk_toggle_partition_flag(cf
->cxt
, n
, fl
))
2261 warn
= _("Could not toggle the flag.");
2269 case 'd': /* Delete */
2270 if (fdisk_delete_partition(cf
->cxt
, n
) != 0)
2271 warn
= _("Could not delete partition %zu.");
2273 info
= _("Partition %zu has been deleted.");
2276 case 'h': /* Help */
2283 uint64_t start
, size
, dflt_size
, secs
;
2284 struct fdisk_partition
*npa
; /* the new partition */
2285 int expsize
= 0; /* size specified explicitly in sectors */
2287 if (!fdisk_partition_is_freespace(pa
) || !fdisk_partition_has_start(pa
))
2290 /* free space range */
2291 start
= fdisk_partition_get_start(pa
);
2292 size
= dflt_size
= fdisk_partition_get_size(pa
) * fdisk_get_sector_size(cf
->cxt
);
2294 if (ui_get_size(cf
, _("Partition size: "), &size
,
2295 fdisk_get_sector_size(cf
->cxt
),
2296 size
, &expsize
) == -CFDISK_ERR_ESC
)
2299 secs
= size
/ fdisk_get_sector_size(cf
->cxt
);
2301 npa
= fdisk_new_partition();
2305 if (dflt_size
== size
) /* default is to fillin all free space */
2306 fdisk_partition_end_follow_default(npa
, 1);
2308 fdisk_partition_set_size(npa
, secs
);
2311 fdisk_partition_size_explicit(pa
, 1);
2313 fdisk_partition_set_start(npa
, start
);
2314 fdisk_partition_partno_follow_default(npa
, 1);
2315 /* add to disk label -- libfdisk will ask for missing details */
2316 rc
= fdisk_add_partition(cf
->cxt
, npa
, NULL
);
2317 fdisk_unref_partition(npa
);
2322 case 'q': /* Quit */
2324 case 't': /* Type */
2326 struct fdisk_parttype
*t
;
2328 if (fdisk_partition_is_freespace(pa
))
2330 t
= (struct fdisk_parttype
*) fdisk_partition_get_type(pa
);
2331 t
= ui_get_parttype(cf
, t
);
2334 if (t
&& fdisk_set_partition_type(cf
->cxt
, n
, t
) == 0)
2335 info
= _("Changed type of partition %zu.");
2337 info
= _("The type of partition %zu is unchanged.");
2340 case 's': /* Sort */
2341 if (cf
->wrong_order
) {
2342 fdisk_reorder_partitions(cf
->cxt
);
2346 case 'u': /* dUmp */
2347 ui_script_write(cf
);
2349 case 'W': /* Write */
2351 char buf
[64] = { 0 };
2353 if (fdisk_is_readonly(cf
->cxt
)) {
2354 warn
= _("Device is open in read-only mode.");
2359 _("Are you sure you want to write the partition "
2361 _("Type \"yes\" or \"no\", or press ESC to leave this dialog."),
2365 if (rc
<= 0 || (strcasecmp(buf
, "yes") != 0 &&
2366 strcasecmp(buf
, _("yes")) != 0)) {
2367 info
= _("Did not write partition table to disk.");
2370 rc
= fdisk_write_disklabel(cf
->cxt
);
2372 warn
= _("Failed to write disklabel.");
2374 fdisk_reread_partition_table(cf
->cxt
);
2375 info
= _("The partition table has been altered.");
2394 ui_warnx(warn
, n
+ 1);
2396 ui_info(info
, n
+ 1);
2397 else if (key
== 'n' && cf
->wrong_order
&& org_order
== 0)
2398 ui_info(_("Note that partition table entries are not in disk order now."));
2403 static void ui_resize_refresh(struct cfdisk
*cf
)
2405 DBG(UI
, ul_debug("ui resize/refresh"));
2407 menu_refresh_size(cf
);
2413 static void toggle_show_extra(struct cfdisk
*cf
)
2415 if (cf
->show_extra
&& cf
->act_win
) {
2416 wclear(cf
->act_win
);
2419 cf
->show_extra
= cf
->show_extra
? 0 : 1;
2423 DBG(MENU
, ul_debug("extra: %s", cf
->show_extra
? "ENABLED" : "DISABLED" ));
2426 static int ui_run(struct cfdisk
*cf
)
2432 DBG(UI
, ul_debug("start cols=%zu, lines=%zu", ui_cols
, ui_lines
));
2434 if (fdisk_get_collision(cf
->cxt
)) {
2435 ui_warnx(_("Device already contains a %s signature; it will be removed by a write command."),
2436 fdisk_get_collision(cf
->cxt
));
2437 fdisk_enable_wipe(cf
->cxt
, 1);
2438 ui_hint(_("Press a key to continue."));
2442 if (!fdisk_has_label(cf
->cxt
) || cf
->zero_start
) {
2443 rc
= ui_create_label(cf
);
2445 ui_errx(EXIT_FAILURE
,
2446 _("failed to create a new disklabel"));
2452 rc
= lines_refresh(cf
);
2454 ui_errx(EXIT_FAILURE
, _("failed to read partitions"));
2456 menu_push(cf
, main_menuitems
);
2457 cf
->menu
->ignore_cb
= main_menu_ignore_keys
;
2459 rc
= ui_refresh(cf
);
2466 if (fdisk_is_readonly(cf
->cxt
))
2467 ui_warnx(_("Device is open in read-only mode."));
2468 else if (cf
->wrong_order
)
2469 ui_info(_("Note that partition table entries are not in disk order now."));
2476 /* Note that ncurses getch() returns ERR when interrupted
2477 * by signal, but SLang does not interrupt at all. */
2478 ui_resize_refresh(cf
);
2481 if (key
== '\014') { /* ^L refresh */
2482 ui_resize_refresh(cf
);
2485 if (ui_menu_move(cf
, key
) == 0)
2488 DBG(UI
, ul_debug("main action key >%1$c< [\\0%1$o].", key
));
2492 case '\016': /* ^N */
2493 case 'j': /* Vi-like alternative */
2494 ui_table_goto(cf
, cf
->lines_idx
+ 1);
2497 case '\020': /* ^P */
2498 case 'k': /* Vi-like alternative */
2499 ui_table_goto(cf
, (int) cf
->lines_idx
- 1);
2503 ui_table_goto(cf
, (int) cf
->lines_idx
- cf
->page_sz
);
2508 ui_table_goto(cf
, 0);
2512 ui_table_goto(cf
, cf
->lines_idx
+ cf
->page_sz
);
2517 ui_table_goto(cf
, (int) cf
->nlines
- 1);
2522 rc
= main_menu_action(cf
, 0);
2525 case 'x': /* Extra */
2526 toggle_show_extra(cf
);
2529 rc
= main_menu_action(cf
, key
);
2541 DBG(UI
, ul_debug("end"));
2545 static void __attribute__((__noreturn__
)) usage(void)
2548 fputs(USAGE_HEADER
, out
);
2550 _(" %1$s [options] <disk>\n"), program_invocation_short_name
);
2552 fputs(USAGE_SEPARATOR
, out
);
2553 fputs(_("Display or manipulate a disk partition table.\n"), out
);
2555 fputs(USAGE_OPTIONS
, out
);
2556 fputs(_(" -L, --color[=<when>] colorize output (auto, always or never)\n"), out
);
2558 " %s\n", USAGE_COLORS_DEFAULT
);
2559 fputs(_(" -z, --zero start with zeroed partition table\n"), out
);
2561 fputs(USAGE_SEPARATOR
, out
);
2562 fputs(USAGE_HELP
, out
);
2563 fputs(USAGE_VERSION
, out
);
2565 fprintf(out
, USAGE_MAN_TAIL("cfdisk(8)"));
2569 int main(int argc
, char *argv
[])
2571 const char *diskpath
= NULL
;
2572 int rc
, c
, colormode
= UL_COLORMODE_UNDEF
;
2573 struct cfdisk _cf
= { .lines_idx
= 0 },
2576 static const struct option longopts
[] = {
2577 { "color", optional_argument
, NULL
, 'L' },
2578 { "help", no_argument
, NULL
, 'h' },
2579 { "version", no_argument
, NULL
, 'V' },
2580 { "zero", no_argument
, NULL
, 'z' },
2581 { NULL
, 0, NULL
, 0 },
2584 setlocale(LC_ALL
, "");
2585 bindtextdomain(PACKAGE
, LOCALEDIR
);
2586 textdomain(PACKAGE
);
2587 atexit(close_stdout
);
2589 while((c
= getopt_long(argc
, argv
, "L::hVz", longopts
, NULL
)) != -1) {
2595 colormode
= UL_COLORMODE_AUTO
;
2597 colormode
= colormode_or_err(optarg
,
2598 _("unsupported color mode"));
2601 printf(UTIL_LINUX_VERSION
);
2602 return EXIT_SUCCESS
;
2607 errtryhelp(EXIT_FAILURE
);
2611 colors_init(colormode
, "cfdisk");
2613 fdisk_init_debug(0);
2614 scols_init_debug(0);
2615 cfdisk_init_debug();
2616 cf
->cxt
= fdisk_new_context();
2618 err(EXIT_FAILURE
, _("failed to allocate libfdisk context"));
2620 fdisk_set_ask(cf
->cxt
, ask_callback
, (void *) cf
);
2622 if (optind
== argc
) {
2625 for (i
= 0; i
< ARRAY_SIZE(default_disks
); i
++) {
2626 if (access(default_disks
[i
], F_OK
) == 0) {
2627 diskpath
= default_disks
[i
];
2632 diskpath
= default_disks
[0]; /* default, used for "cannot open" */
2634 diskpath
= argv
[optind
];
2636 rc
= fdisk_assign_device(cf
->cxt
, diskpath
, 0);
2638 rc
= fdisk_assign_device(cf
->cxt
, diskpath
, 1);
2640 err(EXIT_FAILURE
, _("cannot open %s"), diskpath
);
2642 /* Don't use err(), warn() from this point */
2647 cfdisk_free_lines(cf
);
2650 fdisk_unref_table(cf
->table
);
2651 #ifdef HAVE_LIBMOUNT
2652 mnt_unref_table(cf
->fstab
);
2653 mnt_unref_table(cf
->mtab
);
2654 mnt_unref_cache(cf
->mntcache
);
2656 rc
= fdisk_deassign_device(cf
->cxt
, cf
->nwrites
== 0);
2657 fdisk_unref_context(cf
->cxt
);
2658 DBG(MISC
, ul_debug("bye! [rc=%d]", rc
));
2659 return rc
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
;