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 # define DEFAULT_DEVICE "/dev/hd0"
65 # define ALTERNATE_DEVICE "/dev/sd0"
66 #elif defined(__FreeBSD__)
67 # define DEFAULT_DEVICE "/dev/ad0"
68 # define ALTERNATE_DEVICE "/dev/da0"
70 # define DEFAULT_DEVICE "/dev/sda"
71 # define ALTERNATE_DEVICE "/dev/hda"
74 #define ARROW_CURSOR_STRING ">> "
75 #define ARROW_CURSOR_DUMMY " "
76 #define ARROW_CURSOR_WIDTH (sizeof(ARROW_CURSOR_STRING) - 1)
79 #define MENU_V_SPADDING 1 /* space around menu item string */
82 #define MENU_H_SPADDING 0 /* space around menu item string */
83 #define MENU_H_BETWEEN 2 /* space between menu items */
84 #define MENU_H_PRESTR "["
85 #define MENU_H_POSTSTR "]"
87 #define MENU_TITLE_PADDING 3
89 #define MENU_H_PRESTR_SZ (sizeof(MENU_H_PRESTR) - 1)
90 #define MENU_H_POSTSTR_SZ (sizeof(MENU_H_POSTSTR) - 1)
92 #define TABLE_START_LINE 4
93 #define MENU_START_LINE (ui_lines - 4) /* The menu maybe use two lines */
94 #define INFO_LINE (ui_lines - 2)
95 #define WARN_LINE INFO_LINE
96 #define HINT_LINE (ui_lines - 1)
98 #define CFDISK_ERR_ESC 5000
101 # define KEY_ESC '\033'
104 # define KEY_DELETE '\177'
114 static const int color_pairs
[][2] = {
115 /* color foreground, background */
116 [CFDISK_CL_WARNING
] = { COLOR_RED
, -1 },
117 [CFDISK_CL_FREESPACE
] = { COLOR_GREEN
, -1 },
118 [CFDISK_CL_INFO
] = { COLOR_BLUE
, -1 }
123 static struct cfdisk_menuitem
*menu_get_menuitem(struct cfdisk
*cf
, size_t idx
);
124 static struct cfdisk_menuitem
*menu_get_menuitem_by_key(struct cfdisk
*cf
, int key
, size_t *idx
);
125 static struct cfdisk_menu
*menu_push(struct cfdisk
*cf
, struct cfdisk_menuitem
*item
);
126 static struct cfdisk_menu
*menu_pop(struct cfdisk
*cf
);
127 static void menu_refresh_size(struct cfdisk
*cf
);
129 static int ui_refresh(struct cfdisk
*cf
);
130 static void ui_warnx(const char *fmt
, ...);
131 static void ui_warn(const char *fmt
, ...);
132 static void ui_info(const char *fmt
, ...);
133 static void ui_draw_menu(struct cfdisk
*cf
);
134 static int ui_menu_move(struct cfdisk
*cf
, int key
);
135 static void ui_menu_resize(struct cfdisk
*cf
);
137 static int ui_get_size(struct cfdisk
*cf
, const char *prompt
, uintmax_t *res
,
138 uintmax_t low
, uintmax_t up
, int *expsize
);
140 static int ui_enabled
;
141 static int ui_resize
;
143 /* ncurses LINES and COLS may be actual variables or *macros*, but we need
144 * something portable and writable */
149 struct cfdisk_menuitem
{
150 int key
; /* keyboard shortcut */
151 const char *name
; /* item name */
152 const char *desc
; /* item description (hint) */
158 char *title
; /* optional menu title */
159 struct cfdisk_menuitem
*items
; /* array with menu items */
160 char *ignore
;/* string with keys to ignore */
161 size_t width
; /* maximal width of the menu item */
162 size_t nitems
; /* number of the active menu items */
163 size_t page_sz
;/* when menu longer than screen */
164 size_t idx
; /* the current menu item */
165 int prefkey
;/* preferred menu item */
166 struct cfdisk_menu
*prev
;
168 /* @ignore keys generator */
169 int (*ignore_cb
) (struct cfdisk
*, char *, size_t);
171 unsigned int vertical
: 1; /* enable vertical mode */
175 static struct cfdisk_menuitem main_menuitems
[] = {
176 { 'b', N_("Bootable"), N_("Toggle bootable flag of the current partition") },
177 { 'd', N_("Delete"), N_("Delete the current partition") },
178 { 'n', N_("New"), N_("Create new partition from free space") },
179 { 'q', N_("Quit"), N_("Quit program without writing changes") },
180 { 't', N_("Type"), N_("Change the partition type") },
181 { 'h', N_("Help"), N_("Print help screen") },
182 { 's', N_("Sort"), N_("Fix partitions order") },
183 { 'W', N_("Write"), N_("Write partition table to disk (this might destroy data)") },
184 { 'u', N_("Dump"), N_("Dump partition table to sfdisk compatible script file") },
188 /* extra partinfo in name:value pairs */
189 struct cfdisk_extra
{
193 struct list_head exs
;
196 /* line and extra partinfo list_head */
198 char *data
; /* line data */
199 struct libscols_table
*extra
; /* extra info ('X') */
200 WINDOW
*w
; /* window with extra info */
203 /* top level control struct */
205 struct fdisk_context
*cxt
; /* libfdisk context */
206 struct fdisk_table
*table
; /* partition table */
208 struct cfdisk_menu
*menu
; /* the current menu */
210 int *fields
; /* output columns IDs */
211 size_t nfields
; /* number of columns IDs */
213 char *linesbuf
; /* table as string */
214 size_t linesbufsz
; /* size of the tb_buf */
216 struct cfdisk_line
*lines
; /* list of lines */
218 size_t nlines
; /* number of lines */
219 size_t lines_idx
; /* current line <0..N>, exclude header */
222 unsigned int nwrites
; /* fdisk_write_disklabel() counter */
224 WINDOW
*act_win
; /* the window currently on the screen */
227 struct libmnt_table
*mtab
;
228 struct libmnt_table
*fstab
;
229 struct libmnt_cache
*mntcache
;
231 unsigned int wrong_order
:1, /* PT not in right order */
232 zero_start
:1, /* ignore existing partition table */
233 show_extra
:1; /* show extra partinfo */
238 * let's use include/debug.h stuff for cfdisk too
240 UL_DEBUG_DEFINE_MASK(cfdisk
);
241 UL_DEBUG_DEFINE_MASKNAMES(cfdisk
) = UL_DEBUG_EMPTY_MASKNAMES
;
243 #define CFDISK_DEBUG_INIT (1 << 1)
244 #define CFDISK_DEBUG_UI (1 << 2)
245 #define CFDISK_DEBUG_MENU (1 << 3)
246 #define CFDISK_DEBUG_MISC (1 << 4)
247 #define CFDISK_DEBUG_TABLE (1 << 5)
248 #define CFDISK_DEBUG_ALL 0xFFFF
250 #define DBG(m, x) __UL_DBG(cfdisk, CFDISK_DEBUG_, m, x)
252 static void cfdisk_init_debug(void)
254 __UL_INIT_DEBUG(cfdisk
, CFDISK_DEBUG_
, 0, CFDISK_DEBUG
);
257 /* Initialize output columns -- we follow libfdisk fields (usually specific
260 static int cols_init(struct cfdisk
*cf
)
268 return fdisk_label_get_fields_ids(NULL
, cf
->cxt
, &cf
->fields
, &cf
->nfields
);
271 static void resize(void)
275 if (ioctl(fileno(stdout
), TIOCGWINSZ
, &ws
) != -1
276 && ws
.ws_row
&& ws
.ws_col
) {
277 ui_lines
= ws
.ws_row
;
280 resizeterm(ws
.ws_row
, ws
.ws_col
);
282 clearok(stdscr
, TRUE
);
286 DBG(UI
, ul_debug("ui: resize refresh ui_cols=%zu, ui_lines=%zu",
291 /* Reads partition in tree-like order from scols
293 static int partition_from_scols(struct fdisk_table
*tb
,
294 struct libscols_line
*ln
)
296 struct fdisk_partition
*pa
= scols_line_get_userdata(ln
);
298 fdisk_table_add_partition(tb
, pa
);
299 fdisk_unref_partition(pa
);
301 if (scols_line_has_children(ln
)) {
302 struct libscols_line
*chln
;
303 struct libscols_iter
*itr
= scols_new_iter(SCOLS_ITER_FORWARD
);
307 while (scols_line_next_child(ln
, itr
, &chln
) == 0)
308 partition_from_scols(tb
, chln
);
309 scols_free_iter(itr
);
314 static char *table_to_string(struct cfdisk
*cf
, struct fdisk_table
*tb
)
316 struct fdisk_partition
*pa
;
317 struct fdisk_label
*lb
;
318 struct fdisk_iter
*itr
= NULL
;
319 struct libscols_table
*table
= NULL
;
320 struct libscols_iter
*s_itr
= NULL
;
324 struct libscols_line
*ln
, *ln_cont
= NULL
;
326 DBG(TABLE
, ul_debug("convert to string"));
333 lb
= fdisk_get_label(cf
->cxt
, NULL
);
336 itr
= fdisk_new_iter(FDISK_ITER_FORWARD
);
340 /* get container (e.g. extended partition) */
341 while (fdisk_table_next_partition(tb
, itr
, &pa
) == 0) {
342 if (fdisk_partition_is_nested(pa
)) {
343 DBG(TABLE
, ul_debug("nested detected, using tree"));
344 tree
= SCOLS_FL_TREE
;
349 table
= scols_new_table();
352 scols_table_enable_maxout(table
, 1);
353 scols_table_enable_nowrap(table
, 1);
356 for (i
= 0; i
< cf
->nfields
; i
++) {
358 const struct fdisk_field
*field
=
359 fdisk_label_get_field(lb
, cf
->fields
[i
]);
363 if (fdisk_field_is_number(field
))
364 fl
|= SCOLS_FL_RIGHT
;
365 if (fdisk_field_get_id(field
) == FDISK_FIELD_TYPE
)
366 fl
|= SCOLS_FL_TRUNC
;
367 if (tree
&& fdisk_field_get_id(field
) == FDISK_FIELD_DEVICE
)
370 if (!scols_table_new_column(table
,
371 _(fdisk_field_get_name(field
)),
372 fdisk_field_get_width(field
), fl
))
377 fdisk_reset_iter(itr
, FDISK_ITER_FORWARD
);
379 while (fdisk_table_next_partition(tb
, itr
, &pa
) == 0) {
380 struct libscols_line
*parent
= fdisk_partition_is_nested(pa
) ? ln_cont
: NULL
;
382 ln
= scols_table_new_line(table
, parent
);
385 for (i
= 0; i
< cf
->nfields
; i
++) {
388 if (fdisk_partition_to_string(pa
, cf
->cxt
,
389 cf
->fields
[i
], &cdata
))
391 scols_line_refer_data(ln
, i
, cdata
);
393 if (tree
&& fdisk_partition_is_container(pa
))
396 scols_line_set_userdata(ln
, (void *) pa
);
397 fdisk_ref_partition(pa
);
400 if (scols_table_is_empty(table
))
403 scols_table_reduce_termwidth(table
, ARROW_CURSOR_WIDTH
);
404 scols_print_table_to_string(table
, &res
);
406 /* scols_* code might reorder lines, let's reorder @tb according to the
407 * final output (it's no problem because partitions are addressed by
408 * parno stored within struct fdisk_partition) */
411 fdisk_reset_iter(itr
, FDISK_ITER_FORWARD
);
412 while (fdisk_table_next_partition(tb
, itr
, &pa
) == 0)
413 fdisk_table_remove_partition(tb
, pa
);
415 s_itr
= scols_new_iter(SCOLS_ITER_FORWARD
);
419 /* add all in the right order (don't forget the output is tree) */
420 while (scols_table_next_line(table
, s_itr
, &ln
) == 0) {
421 if (scols_line_get_parent(ln
))
423 if (partition_from_scols(tb
, ln
))
427 scols_unref_table(table
);
428 scols_free_iter(s_itr
);
429 fdisk_free_iter(itr
);
434 static void cfdisk_free_lines(struct cfdisk
*cf
)
437 while(i
< cf
->nlines
) {
438 scols_unref_table(cf
->lines
[i
].extra
);
440 DBG(UI
, ul_debug("delete window: %p",
444 delwin(cf
->lines
[i
].w
);
445 cf
->lines
[i
].w
= NULL
;
453 * Read data about partitions from libfdisk and prepare output lines.
455 static int lines_refresh(struct cfdisk
*cf
)
463 DBG(TABLE
, ul_debug("refreshing buffer"));
466 cfdisk_free_lines(cf
);
472 fdisk_unref_table(cf
->table
);
475 /* read partitions and free spaces into cf->table */
476 rc
= fdisk_get_partitions(cf
->cxt
, &cf
->table
);
478 rc
= fdisk_get_freespaces(cf
->cxt
, &cf
->table
);
482 cf
->linesbuf
= table_to_string(cf
, cf
->table
);
486 cf
->linesbufsz
= strlen(cf
->linesbuf
);
487 cf
->nlines
= fdisk_table_get_nents(cf
->table
) + 1; /* 1 for header line */
489 cf
->wrong_order
= fdisk_table_wrong_order(cf
->table
) ? 1 : 0;
491 if (MENU_START_LINE
- TABLE_START_LINE
< cf
->nlines
)
492 cf
->page_sz
= MENU_START_LINE
- TABLE_START_LINE
- 1;
494 cf
->lines
= xcalloc(cf
->nlines
, sizeof(struct cfdisk_line
));
496 for (p
= cf
->linesbuf
, i
= 0; p
&& i
< cf
->nlines
; i
++) {
497 cf
->lines
[i
].data
= p
;
503 cf
->lines
[i
].extra
= scols_new_table();
504 scols_table_enable_noheadings(cf
->lines
[i
].extra
, 1);
505 scols_table_new_column(cf
->lines
[i
].extra
, NULL
, 0, SCOLS_FL_RIGHT
);
506 scols_table_new_column(cf
->lines
[i
].extra
, NULL
, 0, SCOLS_FL_TRUNC
);
512 static struct fdisk_partition
*get_current_partition(struct cfdisk
*cf
)
517 return fdisk_table_get_partition(cf
->table
, cf
->lines_idx
);
520 static int is_freespace(struct cfdisk
*cf
, size_t i
)
522 struct fdisk_partition
*pa
;
527 pa
= fdisk_table_get_partition(cf
->table
, i
);
528 return fdisk_partition_is_freespace(pa
);
531 /* converts libfdisk FDISK_ASKTYPE_MENU to cfdisk menu and returns user's
532 * response back to libfdisk
534 static int ask_menu(struct fdisk_ask
*ask
, struct cfdisk
*cf
)
536 struct cfdisk_menuitem
*d
, *cm
;
538 size_t i
= 0, nitems
;
539 const char *name
, *desc
;
544 /* create cfdisk menu according to libfdisk ask-menu, note that the
545 * last cm[] item has to be empty -- so nitems + 1 */
546 nitems
= fdisk_ask_menu_get_nitems(ask
);
547 cm
= xcalloc(nitems
+ 1, sizeof(struct cfdisk_menuitem
));
549 for (i
= 0; i
< nitems
; i
++) {
550 if (fdisk_ask_menu_get_item(ask
, i
, &key
, &name
, &desc
))
557 /* make the new menu active */
568 if (ui_menu_move(cf
, key
) == 0)
575 d
= menu_get_menuitem(cf
, cf
->menu
->idx
);
577 fdisk_ask_menu_set_result(ask
, d
->key
);
591 static int ask_callback(struct fdisk_context
*cxt
__attribute__((__unused__
)),
592 struct fdisk_ask
*ask
,
593 void *data
__attribute__((__unused__
)))
599 switch(fdisk_ask_get_type(ask
)) {
600 case FDISK_ASKTYPE_INFO
:
601 ui_info(fdisk_ask_print_get_mesg(ask
));
603 case FDISK_ASKTYPE_WARNX
:
604 ui_warnx(fdisk_ask_print_get_mesg(ask
));
606 case FDISK_ASKTYPE_WARN
:
607 ui_warn(fdisk_ask_print_get_mesg(ask
));
609 case FDISK_ASKTYPE_MENU
:
610 ask_menu(ask
, (struct cfdisk
*) data
);
613 ui_warnx(_("internal error: unsupported dialog type %d"),
614 fdisk_ask_get_type(ask
));
620 static int ui_end(void)
625 #if defined(HAVE_SLCURSES_H) || defined(HAVE_SLANG_SLCURSES_H)
626 SLsmg_gotorc(ui_lines
- 1, 0);
629 mvcur(0, ui_cols
- 1, ui_lines
-1, 0);
639 static void ui_vprint_center(size_t line
, int attrs
, const char *fmt
, va_list ap
)
647 xvasprintf(&buf
, fmt
, ap
);
649 width
= mbs_safe_width(buf
);
650 if (width
> (size_t) ui_cols
) {
651 char *p
= strrchr(buf
+ ui_cols
, ' ');
655 if (line
+ 1 >= ui_lines
)
658 mvaddstr(line
, 0, buf
);
659 mvaddstr(line
+ 1, 0, p
+1);
663 mvaddstr(line
, (ui_cols
- width
) / 2, buf
);
669 static void ui_center(size_t line
, const char *fmt
, ...)
673 ui_vprint_center(line
, 0, fmt
, ap
);
677 static void ui_warnx(const char *fmt
, ...)
682 ui_vprint_center(WARN_LINE
,
683 colors_wanted() ? COLOR_PAIR(CFDISK_CL_WARNING
) : 0,
686 vfprintf(stderr
, fmt
, ap
);
692 static void ui_warn(const char *fmt
, ...)
697 xasprintf(&fmt_m
, "%s: %m", fmt
);
701 ui_vprint_center(WARN_LINE
,
702 colors_wanted() ? COLOR_PAIR(CFDISK_CL_WARNING
) : 0,
705 vfprintf(stderr
, fmt_m
, ap
);
712 static void ui_clean_warn(void)
718 static int __attribute__((__noreturn__
)) ui_errx(int rc
, const char *fmt
, ...)
724 fprintf(stderr
, "%s: ", program_invocation_short_name
);
725 vfprintf(stderr
, fmt
, ap
);
732 static void ui_info(const char *fmt
, ...)
737 ui_vprint_center(INFO_LINE
,
738 colors_wanted() ? COLOR_PAIR(CFDISK_CL_INFO
) : 0,
741 vfprintf(stdout
, fmt
, ap
);
747 static void ui_clean_info(void)
753 static void ui_hint(const char *fmt
, ...)
758 ui_vprint_center(HINT_LINE
, A_BOLD
, fmt
, ap
);
760 vfprintf(stdout
, fmt
, ap
);
766 static void ui_clean_hint(void)
772 static void die_on_signal(int dummy
__attribute__((__unused__
)))
774 DBG(MISC
, ul_debug("die on signal."));
779 static void resize_on_signal(int dummy
__attribute__((__unused__
)))
781 DBG(MISC
, ul_debug("resize on signal."));
785 static void menu_refresh_size(struct cfdisk
*cf
)
787 if (cf
->menu
&& cf
->menu
->nitems
)
788 cf
->menu
->page_sz
= (cf
->menu
->nitems
/ (ui_lines
- 4)) ? ui_lines
- 4 : 0;
791 static void menu_update_ignore(struct cfdisk
*cf
)
793 char ignore
[128] = { 0 };
795 struct cfdisk_menu
*m
;
796 struct cfdisk_menuitem
*d
, *org
= NULL
;
801 assert(cf
->menu
->ignore_cb
);
804 DBG(MENU
, ul_debug("update menu ignored keys"));
806 i
= m
->ignore_cb(cf
, ignore
, sizeof(ignore
));
809 /* return if no change */
810 if ((!m
->ignore
&& !*ignore
)
811 || (m
->ignore
&& *ignore
&& strcmp(m
->ignore
, ignore
) == 0)) {
816 org
= menu_get_menuitem(cf
, m
->idx
);
819 m
->ignore
= xstrdup(ignore
);
822 for (d
= m
->items
; d
->name
; d
++) {
823 if (m
->ignore
&& strchr(m
->ignore
, d
->key
))
828 DBG(MENU
, ul_debug("update menu preferred keys"));
830 /* refresh menu index to be at the same menuitem or go to the first */
831 if (org
&& menu_get_menuitem_by_key(cf
, org
->key
, &idx
))
833 else if (m
->prefkey
&& menu_get_menuitem_by_key(cf
, m
->prefkey
, &idx
))
838 menu_refresh_size(cf
);
841 static struct cfdisk_menu
*menu_push(
843 struct cfdisk_menuitem
*items
)
845 struct cfdisk_menu
*m
= xcalloc(1, sizeof(*m
));
846 struct cfdisk_menuitem
*d
;
850 DBG(MENU
, ul_debug("new menu"));
855 for (d
= m
->items
; d
->name
; d
++) {
856 const char *name
= _(d
->name
);
857 size_t len
= mbs_safe_width(name
);
865 menu_refresh_size(cf
);
869 static struct cfdisk_menu
*menu_pop(struct cfdisk
*cf
)
871 struct cfdisk_menu
*m
= NULL
;
875 DBG(MENU
, ul_debug("pop menu"));
879 free(cf
->menu
->ignore
);
880 free(cf
->menu
->title
);
887 static void menu_set_title(struct cfdisk_menu
*m
, const char *title
)
892 size_t len
= mbs_safe_width(title
);
893 if (len
+ MENU_TITLE_PADDING
> m
->width
)
894 m
->width
= len
+ MENU_TITLE_PADDING
;
895 str
= xstrdup(title
);
901 static int ui_init(struct cfdisk
*cf
__attribute__((__unused__
)))
905 DBG(UI
, ul_debug("init"));
907 /* setup SIGCHLD handler */
908 sigemptyset(&sa
.sa_mask
);
910 sa
.sa_handler
= die_on_signal
;
911 sigaction(SIGINT
, &sa
, NULL
);
912 sigaction(SIGTERM
, &sa
, NULL
);
914 sa
.sa_handler
= resize_on_signal
;
915 sigaction(SIGWINCH
, &sa
, NULL
);
920 #ifdef HAVE_USE_DEFAULT_COLORS
921 if (colors_wanted() && has_colors()) {
925 use_default_colors();
926 for (i
= 1; i
< ARRAY_SIZE(color_pairs
); i
++) /* yeah, start from 1! */
927 init_pair(i
, color_pairs
[i
][0], color_pairs
[i
][1]);
937 keypad(stdscr
, TRUE
);
943 #define MENU_H_ITEMWIDTH(m) ( MENU_H_PRESTR_SZ \
949 #define MENU_V_ITEMWIDTH(m) (MENU_V_SPADDING + (m)->width + MENU_V_SPADDING)
952 static size_t menuitem_get_line(struct cfdisk
*cf
, size_t idx
)
954 struct cfdisk_menu
*m
= cf
->menu
;
957 if (!m
->page_sz
) /* small menu */
958 return (ui_lines
- (cf
->menu
->nitems
+ 1)) / 2 + idx
;
959 return (idx
% m
->page_sz
) + 1;
961 size_t len
= MENU_H_ITEMWIDTH(m
) + MENU_H_BETWEEN
; /** item width */
962 size_t items
= ui_cols
/ len
; /* items per line */
966 return MENU_START_LINE
+ ((idx
/ items
));
970 static int menuitem_get_column(struct cfdisk
*cf
, size_t idx
)
972 if (cf
->menu
->vertical
) {
973 size_t nc
= MENU_V_ITEMWIDTH(cf
->menu
);
974 if ((size_t) ui_cols
<= nc
)
976 return (ui_cols
- nc
) / 2;
978 size_t len
= MENU_H_ITEMWIDTH(cf
->menu
) + MENU_H_BETWEEN
; /* item width */
979 size_t items
= ui_cols
/ len
; /* items per line */
980 size_t extra
= items
< cf
->menu
->nitems
? /* extra space on line */
981 ui_cols
% len
: /* - multi-line menu */
982 ui_cols
- (cf
->menu
->nitems
* len
); /* - one line menu */
985 return 0; /* hmm... no space */
987 extra
+= MENU_H_BETWEEN
; /* add padding after last item to extra */
990 return (idx
* len
) + (extra
/ 2);
991 return ((idx
% items
) * len
) + (extra
/ 2);
995 static int menuitem_on_page(struct cfdisk
*cf
, size_t idx
)
997 struct cfdisk_menu
*m
= cf
->menu
;
999 if (m
->page_sz
== 0 ||
1000 m
->idx
/ m
->page_sz
== idx
/ m
->page_sz
)
1005 static struct cfdisk_menuitem
*menu_get_menuitem(struct cfdisk
*cf
, size_t idx
)
1007 struct cfdisk_menuitem
*d
;
1010 for (i
= 0, d
= cf
->menu
->items
; d
->name
; d
++) {
1011 if (cf
->menu
->ignore
&& strchr(cf
->menu
->ignore
, d
->key
))
1020 static struct cfdisk_menuitem
*menu_get_menuitem_by_key(struct cfdisk
*cf
,
1021 int key
, size_t *idx
)
1023 struct cfdisk_menuitem
*d
;
1025 for (*idx
= 0, d
= cf
->menu
->items
; d
->name
; d
++) {
1026 if (cf
->menu
->ignore
&& strchr(cf
->menu
->ignore
, d
->key
))
1036 static void ui_draw_menuitem(struct cfdisk
*cf
,
1037 struct cfdisk_menuitem
*d
,
1040 char buf
[80 * MB_CUR_MAX
], *ptr
= buf
;
1043 int ln
, cl
, vert
= cf
->menu
->vertical
;
1045 if (!menuitem_on_page(cf
, idx
))
1046 return; /* no visible item */
1047 ln
= menuitem_get_line(cf
, idx
);
1048 cl
= menuitem_get_column(cf
, idx
);
1052 width
= cf
->menu
->width
+ MENU_V_SPADDING
;
1053 memset(ptr
, ' ', MENU_V_SPADDING
);
1054 ptr
+= MENU_V_SPADDING
;
1056 width
= MENU_H_SPADDING
+ cf
->menu
->width
+ MENU_H_SPADDING
;
1059 mbsalign(name
, ptr
, sizeof(buf
), &width
,
1060 vert
? MBS_ALIGN_LEFT
: MBS_ALIGN_CENTER
,
1063 DBG(MENU
, ul_debug("menuitem: cl=%d, ln=%d, item='%s'",
1067 mvaddch(ln
, cl
- 1, ACS_VLINE
);
1068 mvaddch(ln
, cl
+ MENU_V_ITEMWIDTH(cf
->menu
), ACS_VLINE
);
1071 if (cf
->menu
->idx
== idx
)
1075 mvprintw(ln
, cl
, "%s", buf
);
1077 mvprintw(ln
, cl
, "%s%s%s", MENU_H_PRESTR
, buf
, MENU_H_POSTSTR
);
1079 if (cf
->menu
->idx
== idx
) {
1082 ui_hint(_(d
->desc
));
1086 static void ui_clean_menu(struct cfdisk
*cf
)
1090 struct cfdisk_menu
*m
= cf
->menu
;
1091 size_t ln
= menuitem_get_line(cf
, 0);
1094 lastline
= ln
+ (m
->page_sz
? m
->page_sz
: m
->nitems
);
1096 lastline
= menuitem_get_line(cf
, m
->nitems
);
1098 for (i
= ln
; i
<= lastline
; i
++) {
1101 DBG(MENU
, ul_debug("clean_menu: line %zu", i
));
1110 static void ui_draw_menu(struct cfdisk
*cf
)
1112 struct cfdisk_menuitem
*d
;
1113 struct cfdisk_menu
*m
;
1115 size_t ln
= menuitem_get_line(cf
, 0);
1121 DBG(MENU
, ul_debug("draw start"));
1127 nlines
= m
->page_sz
? m
->page_sz
: m
->nitems
;
1129 nlines
= menuitem_get_line(cf
, m
->nitems
);
1132 menu_update_ignore(cf
);
1134 while ((d
= menu_get_menuitem(cf
, i
)))
1135 ui_draw_menuitem(cf
, d
, i
++);
1138 size_t cl
= menuitem_get_column(cf
, 0);
1139 size_t curpg
= m
->page_sz
? m
->idx
/ m
->page_sz
: 0;
1141 /* corners and horizontal lines */
1142 mvaddch(ln
- 1, cl
- 1, ACS_ULCORNER
);
1143 mvaddch(ln
+ nlines
, cl
- 1, ACS_LLCORNER
);
1145 for (i
= 0; i
< MENU_V_ITEMWIDTH(m
); i
++) {
1146 mvaddch(ln
- 1, cl
+ i
, ACS_HLINE
);
1147 mvaddch(ln
+ nlines
, cl
+ i
, ACS_HLINE
);
1150 mvaddch(ln
- 1, cl
+ i
, ACS_URCORNER
);
1151 mvaddch(ln
+ nlines
, cl
+ i
, ACS_LRCORNER
);
1153 /* draw also lines around empty lines on last page */
1155 m
->nitems
/ m
->page_sz
== m
->idx
/ m
->page_sz
) {
1156 for (i
= m
->nitems
% m
->page_sz
+ 1; i
<= m
->page_sz
; i
++) {
1157 mvaddch(i
, cl
- 1, ACS_VLINE
);
1158 mvaddch(i
, cl
+ MENU_V_ITEMWIDTH(m
), ACS_VLINE
);
1163 mvprintw(ln
- 1, cl
, " %s ", m
->title
);
1167 mvaddch(ln
- 1, cl
+ MENU_V_ITEMWIDTH(m
) - 2, ACS_UARROW
);
1168 if (m
->page_sz
&& curpg
< m
->nitems
/ m
->page_sz
)
1169 mvaddch(ln
+ nlines
, cl
+ MENU_V_ITEMWIDTH(m
) - 2, ACS_DARROW
);
1172 DBG(MENU
, ul_debug("draw end."));
1175 inline static int extra_insert_pair(struct cfdisk_line
*l
, const char *name
, const char *data
)
1177 struct libscols_line
*lsl
;
1183 if (!data
|| !*data
)
1186 lsl
= scols_table_new_line(l
->extra
, NULL
);
1190 rc
= scols_line_set_data(lsl
, 0, name
);
1192 rc
= scols_line_set_data(lsl
, 1, data
);
1197 #ifdef HAVE_LIBMOUNT
1198 static char *get_mountpoint(struct cfdisk
*cf
, const char *uuid
, const char *label
)
1200 struct libmnt_fs
*fs
= NULL
;
1201 char *target
= NULL
;
1204 assert(uuid
|| label
);
1206 DBG(UI
, ul_debug("asking for mountpoint [uuid=%s, label=%s]", uuid
, label
));
1209 cf
->mntcache
= mnt_new_cache();
1211 /* 1st try between mounted filesystems */
1213 cf
->mtab
= mnt_new_table();
1215 mnt_table_set_cache(cf
->mtab
, cf
->mntcache
);
1216 mnt_table_parse_mtab(cf
->mtab
, NULL
);
1221 fs
= mnt_table_find_tag(cf
->mtab
,
1222 uuid
? "UUID" : "LABEL",
1223 uuid
? : label
, MNT_ITER_FORWARD
);
1228 cf
->fstab
= mnt_new_table();
1230 mnt_table_set_cache(cf
->fstab
, cf
->mntcache
);
1231 mnt_table_parse_fstab(cf
->fstab
, NULL
);
1235 fs
= mnt_table_find_tag(cf
->fstab
,
1236 uuid
? "UUID" : "LABEL",
1237 uuid
? : label
, MNT_ITER_FORWARD
);
1243 xasprintf(&target
, _("%s (mounted)"), mnt_fs_get_target(fs
));
1245 target
= xstrdup(mnt_fs_get_target(fs
));
1250 #endif /* HAVE_LIBMOUNT */
1252 static void extra_prepare_data(struct cfdisk
*cf
)
1254 struct fdisk_partition
*pa
= get_current_partition(cf
);
1255 struct cfdisk_line
*l
= &cf
->lines
[cf
->lines_idx
];
1257 char *devuuid
= NULL
, *devlabel
= NULL
;
1259 DBG(UI
, ul_debug("preparing extra data"));
1261 /* string data should not equal an empty string */
1262 if (!fdisk_partition_to_string(pa
, cf
->cxt
, FDISK_FIELD_NAME
, &data
) && data
) {
1263 extra_insert_pair(l
, _("Partition name:"), data
);
1267 if (!fdisk_partition_to_string(pa
, cf
->cxt
, FDISK_FIELD_UUID
, &data
) && data
) {
1268 extra_insert_pair(l
, _("Partition UUID:"), data
);
1272 if (!fdisk_partition_to_string(pa
, cf
->cxt
, FDISK_FIELD_TYPE
, &data
) && data
) {
1273 char *code
= NULL
, *type
= NULL
;
1275 fdisk_partition_to_string(pa
, cf
->cxt
, FDISK_FIELD_TYPEID
, &code
);
1276 xasprintf(&type
, "%s (%s)", data
, code
);
1278 extra_insert_pair(l
, _("Partition type:"), type
);
1284 if (!fdisk_partition_to_string(pa
, cf
->cxt
, FDISK_FIELD_ATTR
, &data
) && data
) {
1285 extra_insert_pair(l
, _("Attributes:"), data
);
1289 /* for numeric data, only show non-zero rows */
1290 if (!fdisk_partition_to_string(pa
, cf
->cxt
, FDISK_FIELD_BSIZE
, &data
) && data
) {
1292 extra_insert_pair(l
, "BSIZE:", data
);
1296 if (!fdisk_partition_to_string(pa
, cf
->cxt
, FDISK_FIELD_CPG
, &data
) && data
) {
1298 extra_insert_pair(l
, "CPG:", data
);
1302 if (!fdisk_partition_to_string(pa
, cf
->cxt
, FDISK_FIELD_FSIZE
, &data
) && data
) {
1304 extra_insert_pair(l
, "FSIZE:", data
);
1308 if (!fdisk_partition_to_string(pa
, cf
->cxt
, FDISK_FIELD_FSUUID
, &data
) && data
) {
1309 extra_insert_pair(l
, _("Filesystem UUID:"), data
);
1313 if (!fdisk_partition_to_string(pa
, cf
->cxt
, FDISK_FIELD_FSLABEL
, &data
) && data
) {
1314 extra_insert_pair(l
, _("Filesystem LABEL:"), data
);
1317 if (!fdisk_partition_to_string(pa
, cf
->cxt
, FDISK_FIELD_FSTYPE
, &data
) && data
) {
1318 extra_insert_pair(l
, _("Filesystem:"), data
);
1322 #ifdef HAVE_LIBMOUNT
1323 if (devuuid
|| devlabel
) {
1324 data
= get_mountpoint(cf
, devuuid
, devlabel
);
1326 extra_insert_pair(l
, _("Mountpoint:"), data
);
1330 #endif /* HAVE_LIBMOUNT */
1335 static int ui_draw_extra(struct cfdisk
*cf
)
1339 struct cfdisk_line
*ln
= &cf
->lines
[cf
->lines_idx
];
1340 char *tbstr
= NULL
, *end
;
1341 int win_ex_start_line
, win_height
, tblen
;
1344 if (!cf
->show_extra
)
1347 DBG(UI
, ul_debug("draw extra"));
1352 wclear(cf
->act_win
);
1356 if (scols_table_is_empty(ln
->extra
)) {
1357 extra_prepare_data(cf
);
1358 if (scols_table_is_empty(ln
->extra
))
1362 ndatalines
= fdisk_table_get_nents(cf
->table
) + 1;
1364 /* nents + header + one free line */
1365 win_ex_start_line
= TABLE_START_LINE
+ ndatalines
;
1366 win_height
= MENU_START_LINE
- win_ex_start_line
;
1367 tblen
= scols_table_get_nlines(ln
->extra
);
1369 /* we can't get a single line of data under the partlist*/
1373 /* number of data lines + 2 for top/bottom lines */
1374 win_height
= win_height
< tblen
+ 2 ? win_height
: tblen
+ 2;
1376 if ((size_t) win_ex_start_line
+ win_height
+ 1 < MENU_START_LINE
)
1377 win_ex_start_line
= MENU_START_LINE
- win_height
;
1379 win_ex
= subwin(stdscr
, win_height
, ui_cols
- 2, win_ex_start_line
, 1);
1381 scols_table_reduce_termwidth(ln
->extra
, 4);
1382 scols_print_table_to_string(ln
->extra
, &tbstr
);
1385 while ((end
= strchr(end
, '\n')))
1391 while (--win_height
> 1) {
1392 mvwaddstr(win_ex
, wline
++, 1 /* window column*/, tbstr
);
1393 tbstr
+= strlen(tbstr
) + 1;
1400 DBG(UI
, ul_debug("draw window: %p", win_ex
));
1404 cf
->act_win
= ln
->w
= win_ex
;
1408 static void ui_menu_goto(struct cfdisk
*cf
, int where
)
1410 struct cfdisk_menuitem
*d
;
1413 /* stop and begin/end for vertical menus */
1414 if (cf
->menu
->vertical
) {
1417 else if (where
> (int) cf
->menu
->nitems
- 1)
1418 where
= cf
->menu
->nitems
- 1;
1420 /* continue from begin/end */
1422 where
= cf
->menu
->nitems
- 1;
1423 else if ((size_t) where
> cf
->menu
->nitems
- 1)
1426 if ((size_t) where
== cf
->menu
->idx
)
1431 old
= cf
->menu
->idx
;
1432 cf
->menu
->idx
= where
;
1434 if (!menuitem_on_page(cf
, old
)) {
1439 d
= menu_get_menuitem(cf
, old
);
1440 ui_draw_menuitem(cf
, d
, old
);
1442 d
= menu_get_menuitem(cf
, where
);
1443 ui_draw_menuitem(cf
, d
, where
);
1447 static int ui_menu_move(struct cfdisk
*cf
, int key
)
1449 struct cfdisk_menu
*m
;
1455 return 0; /* ignore errors */
1459 DBG(MENU
, ul_debug("menu move key >%c<.", key
));
1465 case '\016': /* ^N */
1466 case 'j': /* Vi-like alternative */
1467 ui_menu_goto(cf
, m
->idx
+ 1);
1470 case '\020': /* ^P */
1471 case 'k': /* Vi-like alternative */
1472 ui_menu_goto(cf
, (int) m
->idx
- 1);
1476 ui_menu_goto(cf
, (int) m
->idx
- m
->page_sz
);
1481 ui_menu_goto(cf
, 0);
1485 ui_menu_goto(cf
, m
->idx
+ m
->page_sz
);
1490 ui_menu_goto(cf
, m
->nitems
);
1497 ui_menu_goto(cf
, m
->idx
+ 1);
1503 ui_menu_goto(cf
, (int) m
->idx
- 1);
1508 return 1; /* key irrelevant for menu move */
1511 /* but don't call me from ui_run(), this is for pop-up menus only */
1512 static void ui_menu_resize(struct cfdisk
*cf
)
1516 menu_refresh_size(cf
);
1521 static int partition_on_page(struct cfdisk
*cf
, size_t i
)
1523 if (cf
->page_sz
== 0 ||
1524 cf
->lines_idx
/ cf
->page_sz
== i
/ cf
->page_sz
)
1529 static void ui_draw_partition(struct cfdisk
*cf
, size_t i
)
1531 int ln
= TABLE_START_LINE
+ 1 + i
; /* skip table header */
1532 int cl
= ARROW_CURSOR_WIDTH
; /* we need extra space for cursor */
1533 int cur
= cf
->lines_idx
== i
;
1537 if (!partition_on_page(cf
, i
))
1539 ln
= TABLE_START_LINE
+ (i
% cf
->page_sz
) + 1;
1540 curpg
= cf
->lines_idx
/ cf
->page_sz
;
1544 "draw partition %zu [page_sz=%zu, "
1545 "line=%d, idx=%zu]",
1546 i
, cf
->page_sz
, ln
, cf
->lines_idx
));
1550 mvaddstr(ln
, 0, ARROW_CURSOR_STRING
);
1551 mvaddstr(ln
, cl
, cf
->lines
[i
+ 1].data
);
1556 if (colors_wanted() && is_freespace(cf
, i
)) {
1557 attron(COLOR_PAIR(CFDISK_CL_FREESPACE
));
1560 mvaddstr(ln
, 0, ARROW_CURSOR_DUMMY
);
1561 mvaddstr(ln
, cl
, cf
->lines
[i
+ 1].data
);
1563 attroff(COLOR_PAIR(CFDISK_CL_FREESPACE
));
1566 if ((size_t) ln
== MENU_START_LINE
- 1 &&
1567 cf
->page_sz
&& curpg
< cf
->nlines
/ cf
->page_sz
) {
1570 mvaddch(ln
, ui_cols
- 1, ACS_DARROW
);
1571 mvaddch(ln
, 0, ACS_DARROW
);
1578 static int ui_draw_table(struct cfdisk
*cf
)
1580 int cl
= ARROW_CURSOR_WIDTH
;
1581 size_t i
, nparts
= fdisk_table_get_nents(cf
->table
);
1582 size_t curpg
= cf
->page_sz
? cf
->lines_idx
/ cf
->page_sz
: 0;
1584 DBG(UI
, ul_debug("draw table"));
1586 for (i
= TABLE_START_LINE
; i
<= TABLE_START_LINE
+ cf
->page_sz
; i
++) {
1591 if (nparts
== 0 || (size_t) cf
->lines_idx
> nparts
- 1)
1592 cf
->lines_idx
= nparts
? nparts
- 1 : 0;
1596 mvaddstr(TABLE_START_LINE
, cl
, cf
->lines
[0].data
);
1599 /* print partitions */
1600 for (i
= 0; i
< nparts
; i
++)
1601 ui_draw_partition(cf
, i
);
1604 mvaddch(TABLE_START_LINE
, ui_cols
- 1, ACS_UARROW
);
1605 mvaddch(TABLE_START_LINE
, 0, ACS_UARROW
);
1607 if (cf
->page_sz
&& curpg
< cf
->nlines
/ cf
->page_sz
) {
1608 mvaddch(MENU_START_LINE
- 1, ui_cols
- 1, ACS_DARROW
);
1609 mvaddch(MENU_START_LINE
- 1, 0, ACS_DARROW
);
1614 static int ui_table_goto(struct cfdisk
*cf
, int where
)
1617 size_t nparts
= fdisk_table_get_nents(cf
->table
);
1619 DBG(UI
, ul_debug("goto table %d", where
));
1623 else if ((size_t) where
> nparts
- 1)
1626 if ((size_t) where
== cf
->lines_idx
)
1629 old
= cf
->lines_idx
;
1630 cf
->lines_idx
= where
;
1632 if (!partition_on_page(cf
, old
) ||!partition_on_page(cf
, where
))
1635 ui_draw_partition(cf
, old
); /* cleanup old */
1636 ui_draw_partition(cf
, where
); /* draw new */
1646 static int ui_refresh(struct cfdisk
*cf
)
1648 struct fdisk_label
*lb
;
1650 uint64_t bytes
= fdisk_get_nsectors(cf
->cxt
) * fdisk_get_sector_size(cf
->cxt
);
1656 strsz
= size_to_human_string(SIZE_SUFFIX_SPACE
1657 | SIZE_SUFFIX_3LETTER
, bytes
);
1659 lb
= fdisk_get_label(cf
->cxt
, NULL
);
1666 ui_center(0, _("Disk: %s"), fdisk_get_devname(cf
->cxt
));
1668 ui_center(1, _("Size: %s, %ju bytes, %ju sectors"),
1669 strsz
, bytes
, (uintmax_t) fdisk_get_nsectors(cf
->cxt
));
1670 if (fdisk_get_disklabel_id(cf
->cxt
, &id
) == 0 && id
)
1671 ui_center(2, _("Label: %s, identifier: %s"),
1672 fdisk_label_get_name(lb
), id
);
1674 ui_center(2, _("Label: %s"), fdisk_label_get_name(lb
));
1683 static ssize_t
ui_get_string(const char *prompt
,
1684 const char *hint
, char *buf
, size_t len
)
1687 ssize_t i
= 0, rc
= -1;
1688 int ln
= MENU_START_LINE
, cl
= 1;
1700 mvaddstr(ln
, cl
, (char *) prompt
);
1701 cl
+= mbs_safe_width(prompt
);
1707 cells
= mbs_safe_width(buf
);
1708 mvaddstr(ln
, cl
, buf
);
1716 move(ln
, cl
+ cells
);
1721 #if !defined(HAVE_SLCURSES_H) && !defined(HAVE_SLANG_SLCURSES_H) && \
1722 defined(HAVE_LIBNCURSESW) && defined(HAVE_WIDECHAR)
1724 if (get_wch(&c
) == ERR
) {
1727 if ((c
= getch()) == ERR
) {
1733 if (!isatty(STDIN_FILENO
))
1738 if (c
== '\r' || c
== '\n' || c
== KEY_ENTER
)
1743 rc
= -CFDISK_ERR_ESC
;
1745 case KEY_LEFT
: /* TODO: implement full buffer editor */
1758 i
= mbs_truncate(buf
, &cells
);
1761 mvaddch(ln
, cl
+ cells
, ' ');
1762 move(ln
, cl
+ cells
);
1768 #if defined(HAVE_LIBNCURSESW) && defined(HAVE_WIDECHAR)
1769 if (i
+ 1 < (ssize_t
) len
&& iswprint(c
)) {
1770 wchar_t wc
= (wchar_t) c
;
1771 char s
[MB_CUR_MAX
+ 1];
1772 int sz
= wctomb(s
, wc
);
1774 if (sz
> 0 && sz
+ i
< (ssize_t
) len
) {
1776 mvaddnstr(ln
, cl
+ cells
, s
, sz
);
1777 memcpy(buf
+ i
, s
, sz
);
1780 cells
+= wcwidth(wc
);
1785 if (i
+ 1 < (ssize_t
) len
&& isprint(c
)) {
1786 mvaddch(ln
, cl
+ cells
, c
);
1798 rc
= i
; /* success */
1808 /* @res is default value as well as result in bytes */
1809 static int ui_get_size(struct cfdisk
*cf
, const char *prompt
, uintmax_t *res
,
1810 uintmax_t low
, uintmax_t up
, int *expsize
)
1815 char *dflt
= size_to_human_string(0, *res
);
1817 DBG(UI
, ul_debug("get_size (default=%ju)", *res
));
1821 snprintf(buf
, sizeof(buf
), "%s", dflt
);
1824 int pwr
= 0, insec
= 0;
1826 rc
= ui_get_string(prompt
,
1827 _("May be followed by M for MiB, G for GiB, "
1828 "T for TiB, or S for sectors."),
1833 ui_warnx(_("Please, specify size."));
1834 continue; /* nothing specified */
1835 } else if (rc
== -CFDISK_ERR_ESC
)
1836 break; /* cancel dialog */
1838 if (strcmp(buf
, dflt
) == 0)
1839 user
= *res
, rc
= 0; /* no change, use default */
1841 size_t len
= strlen(buf
);
1842 if (buf
[len
- 1] == 'S' || buf
[len
- 1] == 's') {
1844 buf
[len
- 1] = '\0';
1846 rc
= parse_size(buf
, &user
, &pwr
); /* parse */
1850 DBG(UI
, ul_debug("get_size user=%ju, power=%d, sectors=%s",
1851 user
, pwr
, insec
? "yes" : "no"));
1853 user
*= fdisk_get_sector_size(cf
->cxt
);
1855 ui_warnx(_("Minimum size is %ju bytes."), low
);
1858 if (user
> up
&& pwr
&& user
< up
+ (1ULL << pwr
* 10))
1859 /* ignore when the user specified size overflow
1860 * with in range specified by suffix (e.g. MiB) */
1864 ui_warnx(_("Maximum size is %ju bytes."), up
);
1867 if (rc
== 0 && insec
&& expsize
)
1871 ui_warnx(_("Failed to parse size."));
1878 DBG(UI
, ul_debug("get_size (result=%ju, rc=%zd)", *res
, rc
));
1882 static struct fdisk_parttype
*ui_get_parttype(struct cfdisk
*cf
,
1883 struct fdisk_parttype
*cur
)
1885 struct cfdisk_menuitem
*d
, *cm
;
1886 size_t i
= 0, nitems
, idx
= 0;
1887 struct fdisk_parttype
*t
= NULL
;
1888 struct fdisk_label
*lb
;
1891 DBG(UI
, ul_debug("asking for parttype."));
1893 lb
= fdisk_get_label(cf
->cxt
, NULL
);
1895 /* create cfdisk menu according to label types, note that the
1896 * last cm[] item has to be empty -- so nitems + 1 */
1897 nitems
= fdisk_label_get_nparttypes(lb
);
1901 cm
= xcalloc(nitems
+ 1, sizeof(struct cfdisk_menuitem
));
1905 codetypes
= fdisk_label_has_code_parttypes(lb
);
1907 for (i
= 0; i
< nitems
; i
++) {
1908 const struct fdisk_parttype
*x
= fdisk_label_get_parttype(lb
, i
);
1911 cm
[i
].userdata
= (void *) x
;
1913 xasprintf(&name
, "%2x %s",
1914 fdisk_parttype_get_code(x
),
1915 _(fdisk_parttype_get_name(x
)));
1917 name
= (char *) _(fdisk_parttype_get_name(x
));
1918 cm
[i
].desc
= fdisk_parttype_get_string(x
);
1925 /* make the new menu active */
1927 cf
->menu
->vertical
= 1;
1928 cf
->menu
->idx
= idx
;
1929 menu_set_title(cf
->menu
, _("Select partition type"));
1938 if (ui_menu_move(cf
, key
) == 0)
1945 d
= menu_get_menuitem(cf
, cf
->menu
->idx
);
1947 t
= (struct fdisk_parttype
*) d
->userdata
;
1959 for (i
= 0; i
< nitems
; i
++)
1960 free((char *) cm
[i
].name
);
1963 DBG(UI
, ul_debug("get parrtype done [type=%s] ", t
?
1964 fdisk_parttype_get_name(t
) : NULL
));
1968 static int ui_script_read(struct cfdisk
*cf
)
1970 struct fdisk_script
*sc
= NULL
;
1971 char buf
[PATH_MAX
] = { 0 };
1975 rc
= ui_get_string( _("Enter script file name: "),
1976 _("The script file will be applied to in-memory partition table."),
1983 sc
= fdisk_new_script_from_file(cf
->cxt
, buf
);
1985 ui_warn(_("Cannot open %s"), buf
);
1987 ui_warnx(_("Failed to parse script file %s"), buf
);
1988 else if (fdisk_apply_script(cf
->cxt
, sc
) != 0)
1989 ui_warnx(_("Failed to apply script %s"), buf
);
1994 fdisk_unref_script(sc
);
1998 static int ui_script_write(struct cfdisk
*cf
)
2000 struct fdisk_script
*sc
= NULL
;
2001 char buf
[PATH_MAX
] = { 0 };
2005 rc
= ui_get_string( _("Enter script file name: "),
2006 _("The current in-memory partition table will be dumped to the file."),
2012 sc
= fdisk_new_script(cf
->cxt
);
2014 ui_warn(_("Failed to allocate script handler"));
2018 rc
= fdisk_script_read_context(sc
, NULL
);
2020 ui_warnx(_("Failed to read disk layout into script."));
2024 DBG(UI
, ul_debug("writing dump into: '%s'", buf
));
2025 f
= fopen(buf
, "w");
2027 ui_warn(_("Cannot open %s"), buf
);
2032 rc
= fdisk_script_write_file(sc
, f
);
2034 ui_info(_("Disk layout successfully dumped."));
2037 ui_warn(_("Failed to write script %s"), buf
);
2040 fdisk_unref_script(sc
);
2044 /* prints menu with libfdisk labels and waits for users response */
2045 static int ui_create_label(struct cfdisk
*cf
)
2047 struct cfdisk_menuitem
*d
, *cm
;
2048 int rc
= 1, refresh_menu
= 1;
2049 size_t i
= 0, nitems
;
2050 struct fdisk_label
*lb
= NULL
;
2054 DBG(UI
, ul_debug("asking for new disklabe."));
2056 /* create cfdisk menu according to libfdisk labels, note that the
2057 * last cm[] item has to be empty -- so nitems + 1 */
2058 nitems
= fdisk_get_nlabels(cf
->cxt
);
2059 cm
= xcalloc(nitems
+ 1, sizeof(struct cfdisk_menuitem
));
2061 while (fdisk_next_label(cf
->cxt
, &lb
) == 0) {
2062 if (fdisk_label_is_disabled(lb
) ||
2063 fdisk_label_get_type(lb
) == FDISK_DISKLABEL_BSD
)
2065 cm
[i
++].name
= fdisk_label_get_name(lb
);
2070 /* make the new menu active */
2072 cf
->menu
->vertical
= 1;
2073 menu_set_title(cf
->menu
, _("Select label type"));
2075 if (!cf
->zero_start
)
2076 ui_info(_("Device does not contain a recognized partition table."));
2082 ui_hint(_("Select a type to create a new label or press 'L' to load script file."));
2091 if (ui_menu_move(cf
, key
) == 0)
2097 d
= menu_get_menuitem(cf
, cf
->menu
->idx
);
2099 rc
= fdisk_create_disklabel(cf
->cxt
, d
->name
);
2107 rc
= ui_script_read(cf
);
2118 DBG(UI
, ul_debug("create label done [rc=%d] ", rc
));
2123 static int ui_help(void)
2126 static const char *help
[] = {
2127 N_("This is cfdisk, a curses-based disk partitioning program."),
2128 N_("It lets you create, delete, and modify partitions on a block device."),
2130 N_("Command Meaning"),
2131 N_("------- -------"),
2132 N_(" b Toggle bootable flag of the current partition"),
2133 N_(" d Delete the current partition"),
2134 N_(" h Print this screen"),
2135 N_(" n Create new partition from free space"),
2136 N_(" q Quit program without writing partition table"),
2137 N_(" s Fix partitions order (only when in disarray)"),
2138 N_(" t Change the partition type"),
2139 N_(" u Dump disk layout to sfdisk compatible script file"),
2140 N_(" W Write partition table to disk (you must enter uppercase W);"),
2141 N_(" since this might destroy data on the disk, you must either"),
2142 N_(" confirm or deny the write by entering 'yes' or 'no'"),
2143 N_(" x Display/hide extra information about a partition"),
2144 N_("Up Arrow Move cursor to the previous partition"),
2145 N_("Down Arrow Move cursor to the next partition"),
2146 N_("Left Arrow Move cursor to the previous menu item"),
2147 N_("Right Arrow Move cursor to the next menu item"),
2149 N_("Note: All of the commands can be entered with either upper or lower"),
2150 N_("case letters (except for Write)."),
2152 N_("Use lsblk(8) or partx(8) to see more details about the device.")
2156 for (i
= 0; i
< ARRAY_SIZE(help
); i
++)
2157 mvaddstr(i
, 1, _(help
[i
]));
2159 ui_info(_("Press a key to continue."));
2165 /* TODO: use @sz, now 128bytes */
2166 static int main_menu_ignore_keys(struct cfdisk
*cf
, char *ignore
,
2167 size_t sz
__attribute__((__unused__
)))
2169 struct fdisk_partition
*pa
= get_current_partition(cf
);
2174 if (fdisk_partition_is_freespace(pa
)) {
2175 ignore
[i
++] = 'd'; /* delete */
2176 ignore
[i
++] = 't'; /* set type */
2177 ignore
[i
++] = 'b'; /* set bootable */
2178 cf
->menu
->prefkey
= 'n';
2180 cf
->menu
->prefkey
= 'q';
2182 if (!fdisk_is_label(cf
->cxt
, DOS
) &&
2183 !fdisk_is_label(cf
->cxt
, SGI
))
2187 if (!cf
->wrong_order
)
2190 if (fdisk_is_readonly(cf
->cxt
))
2197 /* returns: error: < 0, success: 0, quit: 1 */
2198 static int main_menu_action(struct cfdisk
*cf
, int key
)
2201 int ref
= 0, rc
, org_order
= cf
->wrong_order
;
2202 const char *info
= NULL
, *warn
= NULL
;
2203 struct fdisk_partition
*pa
;
2210 struct cfdisk_menuitem
*d
= menu_get_menuitem(cf
, cf
->menu
->idx
);
2215 } else if (key
!= 'w' && key
!= 'W')
2216 key
= tolower(key
); /* case insensitive except 'W'rite */
2218 DBG(MENU
, ul_debug("main menu action: key=%c", key
));
2220 if (cf
->menu
->ignore
&& strchr(cf
->menu
->ignore
, key
)) {
2221 DBG(MENU
, ul_debug(" ignore '%c'", key
));
2225 pa
= get_current_partition(cf
);
2228 n
= fdisk_partition_get_partno(pa
);
2230 DBG(MENU
, ul_debug("menu action on %p", pa
));
2235 case 'b': /* Bootable flag */
2237 int fl
= fdisk_is_label(cf
->cxt
, DOS
) ? DOS_FLAG_ACTIVE
:
2238 fdisk_is_label(cf
->cxt
, SGI
) ? SGI_FLAG_BOOT
: 0;
2240 if (fl
&& fdisk_toggle_partition_flag(cf
->cxt
, n
, fl
))
2241 warn
= _("Could not toggle the flag.");
2249 case 'd': /* Delete */
2250 if (fdisk_delete_partition(cf
->cxt
, n
) != 0)
2251 warn
= _("Could not delete partition %zu.");
2253 info
= _("Partition %zu has been deleted.");
2256 case 'h': /* Help */
2263 uint64_t start
, size
, dflt_size
, secs
;
2264 struct fdisk_partition
*npa
; /* the new partition */
2265 int expsize
= 0; /* size specified explicitly in sectors */
2267 if (!fdisk_partition_is_freespace(pa
) || !fdisk_partition_has_start(pa
))
2270 /* free space range */
2271 start
= fdisk_partition_get_start(pa
);
2272 size
= dflt_size
= fdisk_partition_get_size(pa
) * fdisk_get_sector_size(cf
->cxt
);
2274 if (ui_get_size(cf
, _("Partition size: "), &size
,
2275 fdisk_get_sector_size(cf
->cxt
),
2276 size
, &expsize
) == -CFDISK_ERR_ESC
)
2279 secs
= size
/ fdisk_get_sector_size(cf
->cxt
);
2281 npa
= fdisk_new_partition();
2285 if (dflt_size
== size
) /* default is to fillin all free space */
2286 fdisk_partition_end_follow_default(npa
, 1);
2288 fdisk_partition_set_size(npa
, secs
);
2291 fdisk_partition_size_explicit(pa
, 1);
2293 fdisk_partition_set_start(npa
, start
);
2294 fdisk_partition_partno_follow_default(npa
, 1);
2295 /* add to disk label -- libfdisk will ask for missing details */
2296 rc
= fdisk_add_partition(cf
->cxt
, npa
, NULL
);
2297 fdisk_unref_partition(npa
);
2302 case 'q': /* Quit */
2304 case 't': /* Type */
2306 struct fdisk_parttype
*t
;
2308 if (fdisk_partition_is_freespace(pa
))
2310 t
= (struct fdisk_parttype
*) fdisk_partition_get_type(pa
);
2311 t
= ui_get_parttype(cf
, t
);
2314 if (t
&& fdisk_set_partition_type(cf
->cxt
, n
, t
) == 0)
2315 info
= _("Changed type of partition %zu.");
2317 info
= _("The type of partition %zu is unchanged.");
2320 case 's': /* Sort */
2321 if (cf
->wrong_order
) {
2322 fdisk_reorder_partitions(cf
->cxt
);
2326 case 'u': /* dUmp */
2327 ui_script_write(cf
);
2329 case 'W': /* Write */
2331 char buf
[64] = { 0 };
2333 if (fdisk_is_readonly(cf
->cxt
)) {
2334 warn
= _("Device is open in read-only mode.");
2339 _("Are you sure you want to write the partition "
2341 _("Type \"yes\" or \"no\", or press ESC to leave this dialog."),
2345 if (rc
<= 0 || (strcasecmp(buf
, "yes") != 0 &&
2346 strcasecmp(buf
, _("yes")) != 0)) {
2347 info
= _("Did not write partition table to disk.");
2350 rc
= fdisk_write_disklabel(cf
->cxt
);
2352 warn
= _("Failed to write disklabel.");
2354 fdisk_reread_partition_table(cf
->cxt
);
2355 info
= _("The partition table has been altered.");
2374 ui_warnx(warn
, n
+ 1);
2376 ui_info(info
, n
+ 1);
2377 else if (key
== 'n' && cf
->wrong_order
&& org_order
== 0)
2378 ui_info(_("Note that partition table entries are not in disk order now."));
2383 static void ui_resize_refresh(struct cfdisk
*cf
)
2386 menu_refresh_size(cf
);
2392 static void toggle_show_extra(struct cfdisk
*cf
)
2394 if (cf
->show_extra
&& cf
->act_win
) {
2395 wclear(cf
->act_win
);
2398 cf
->show_extra
= cf
->show_extra
? 0 : 1;
2402 DBG(MENU
, ul_debug("extra: %s", cf
->show_extra
? "ENABLED" : "DISABLED" ));
2405 static int ui_run(struct cfdisk
*cf
)
2411 DBG(UI
, ul_debug("start cols=%zu, lines=%zu", ui_cols
, ui_lines
));
2413 if (fdisk_get_collision(cf
->cxt
)) {
2414 ui_warnx(_("Device already contains a %s signature; it will be removed by a write command."),
2415 fdisk_get_collision(cf
->cxt
));
2416 fdisk_enable_wipe(cf
->cxt
, 1);
2417 ui_hint(_("Press a key to continue."));
2421 if (!fdisk_has_label(cf
->cxt
) || cf
->zero_start
) {
2422 rc
= ui_create_label(cf
);
2424 ui_errx(EXIT_FAILURE
,
2425 _("failed to create a new disklabel"));
2431 rc
= lines_refresh(cf
);
2433 ui_errx(EXIT_FAILURE
, _("failed to read partitions"));
2435 menu_push(cf
, main_menuitems
);
2436 cf
->menu
->ignore_cb
= main_menu_ignore_keys
;
2438 rc
= ui_refresh(cf
);
2445 if (fdisk_is_readonly(cf
->cxt
))
2446 ui_warnx(_("Device is open in read-only mode."));
2447 else if (cf
->wrong_order
)
2448 ui_info(_("Note that partition table entries are not in disk order now."));
2455 /* Note that ncurses getch() returns ERR when interrupted
2456 * by signal, but SLang does not interrupt at all. */
2457 ui_resize_refresh(cf
);
2460 if (ui_menu_move(cf
, key
) == 0)
2463 DBG(UI
, ul_debug("main action key >%c<.", key
));
2467 case '\016': /* ^N */
2468 case 'j': /* Vi-like alternative */
2469 ui_table_goto(cf
, cf
->lines_idx
+ 1);
2472 case '\020': /* ^P */
2473 case 'k': /* Vi-like alternative */
2474 ui_table_goto(cf
, (int) cf
->lines_idx
- 1);
2478 ui_table_goto(cf
, (int) cf
->lines_idx
- cf
->page_sz
);
2482 ui_table_goto(cf
, 0);
2486 ui_table_goto(cf
, cf
->lines_idx
+ cf
->page_sz
);
2490 ui_table_goto(cf
, (int) cf
->nlines
- 1);
2495 rc
= main_menu_action(cf
, 0);
2498 case 'x': /* Extra */
2499 toggle_show_extra(cf
);
2502 rc
= main_menu_action(cf
, key
);
2514 DBG(UI
, ul_debug("end"));
2518 static void __attribute__ ((__noreturn__
)) usage(FILE *out
)
2520 fputs(USAGE_HEADER
, out
);
2522 _(" %1$s [options] <disk>\n"), program_invocation_short_name
);
2524 fputs(USAGE_SEPARATOR
, out
);
2525 fputs(_("Display or manipulate a disk partition table.\n"), out
);
2527 fputs(USAGE_OPTIONS
, out
);
2528 fputs(_(" -L, --color[=<when>] colorize output (auto, always or never)\n"), out
);
2530 " %s\n", USAGE_COLORS_DEFAULT
);
2531 fputs(_(" -z, --zero start with zeroed partition table\n"), out
);
2533 fputs(USAGE_SEPARATOR
, out
);
2534 fputs(USAGE_HELP
, out
);
2535 fputs(USAGE_VERSION
, out
);
2537 fprintf(out
, USAGE_MAN_TAIL("cfdisk(8)"));
2538 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
2541 int main(int argc
, char *argv
[])
2543 const char *diskpath
;
2544 int rc
, c
, colormode
= UL_COLORMODE_UNDEF
;
2545 struct cfdisk _cf
= { .lines_idx
= 0 },
2548 static const struct option longopts
[] = {
2549 { "color", optional_argument
, NULL
, 'L' },
2550 { "help", no_argument
, NULL
, 'h' },
2551 { "version", no_argument
, NULL
, 'V' },
2552 { "zero", no_argument
, NULL
, 'z' },
2556 setlocale(LC_ALL
, "");
2557 bindtextdomain(PACKAGE
, LOCALEDIR
);
2558 textdomain(PACKAGE
);
2559 atexit(close_stdout
);
2561 while((c
= getopt_long(argc
, argv
, "L::hVz", longopts
, NULL
)) != -1) {
2567 colormode
= UL_COLORMODE_AUTO
;
2569 colormode
= colormode_or_err(optarg
,
2570 _("unsupported color mode"));
2573 printf(UTIL_LINUX_VERSION
);
2574 return EXIT_SUCCESS
;
2581 colors_init(colormode
, "cfdisk");
2583 fdisk_init_debug(0);
2584 scols_init_debug(0);
2585 cfdisk_init_debug();
2586 cf
->cxt
= fdisk_new_context();
2588 err(EXIT_FAILURE
, _("failed to allocate libfdisk context"));
2590 fdisk_set_ask(cf
->cxt
, ask_callback
, (void *) cf
);
2593 diskpath
= access(DEFAULT_DEVICE
, F_OK
) == 0 ?
2594 DEFAULT_DEVICE
: ALTERNATE_DEVICE
;
2596 diskpath
= argv
[optind
];
2598 rc
= fdisk_assign_device(cf
->cxt
, diskpath
, 0);
2600 rc
= fdisk_assign_device(cf
->cxt
, diskpath
, 1);
2602 err(EXIT_FAILURE
, _("cannot open %s"),
2603 optind
== argc
? DEFAULT_DEVICE
: diskpath
);
2605 /* Don't use err(), warn() from this point */
2610 cfdisk_free_lines(cf
);
2613 fdisk_unref_table(cf
->table
);
2614 #ifdef HAVE_LIBMOUNT
2615 mnt_unref_table(cf
->fstab
);
2616 mnt_unref_table(cf
->mtab
);
2617 mnt_unref_cache(cf
->mntcache
);
2619 rc
= fdisk_deassign_device(cf
->cxt
, cf
->nwrites
== 0);
2620 fdisk_unref_context(cf
->cxt
);
2621 DBG(MISC
, ul_debug("bye! [rc=%d]", rc
));
2622 return rc
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
;