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 <blkid.h> /* keep it optional */
31 # include <libmount.h> /* keep it optional for non-linux systems */
36 #elif defined(HAVE_SLANG_SLANG_H)
37 # include <slang/slang.h>
40 #ifdef HAVE_SLCURSES_H
41 # include <slcurses.h>
42 #elif defined(HAVE_SLANG_SLCURSES_H)
43 # include <slang/slcurses.h>
44 #elif defined(HAVE_NCURSESW_NCURSES_H) && defined(HAVE_WIDECHAR)
45 # include <ncursesw/ncurses.h>
46 #elif defined(HAVE_NCURSES_H)
48 #elif defined(HAVE_NCURSES_NCURSES_H)
49 # include <ncurses/ncurses.h>
58 #include "closestream.h"
68 # define DEFAULT_DEVICE "/dev/hd0"
69 # define ALTERNATE_DEVICE "/dev/sd0"
70 #elif defined(__FreeBSD__)
71 # define DEFAULT_DEVICE "/dev/ad0"
72 # define ALTERNATE_DEVICE "/dev/da0"
74 # define DEFAULT_DEVICE "/dev/sda"
75 # define ALTERNATE_DEVICE "/dev/hda"
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'
118 static const int color_pairs
[][2] = {
119 /* color foreground, background */
120 [CFDISK_CL_WARNING
] = { COLOR_RED
, -1 },
121 [CFDISK_CL_FREESPACE
] = { COLOR_GREEN
, -1 },
122 [CFDISK_CL_INFO
] = { COLOR_BLUE
, -1 }
127 static struct cfdisk_menuitem
*menu_get_menuitem(struct cfdisk
*cf
, size_t idx
);
128 static struct cfdisk_menuitem
*menu_get_menuitem_by_key(struct cfdisk
*cf
, int key
, size_t *idx
);
129 static struct cfdisk_menu
*menu_push(struct cfdisk
*cf
, struct cfdisk_menuitem
*item
);
130 static struct cfdisk_menu
*menu_pop(struct cfdisk
*cf
);
131 static void menu_refresh_size(struct cfdisk
*cf
);
133 static int ui_refresh(struct cfdisk
*cf
);
134 static void ui_warnx(const char *fmt
, ...);
135 static void ui_warn(const char *fmt
, ...);
136 static void ui_info(const char *fmt
, ...);
137 static void ui_draw_menu(struct cfdisk
*cf
);
138 static int ui_menu_move(struct cfdisk
*cf
, int key
);
139 static void ui_menu_resize(struct cfdisk
*cf
);
141 static int ui_get_size(struct cfdisk
*cf
, const char *prompt
, uintmax_t *res
,
142 uintmax_t low
, uintmax_t up
, int *expsize
);
144 static int ui_enabled
;
145 static int ui_resize
;
147 /* ncurses LINES and COLS may be actual variables or *macros*, but we need
148 * something portable and writable */
153 struct cfdisk_menuitem
{
154 int key
; /* keyboard shortcut */
155 const char *name
; /* item name */
156 const char *desc
; /* item description (hint) */
162 char *title
; /* optional menu title */
163 struct cfdisk_menuitem
*items
; /* array with menu items */
164 char *ignore
;/* string with keys to ignore */
165 size_t width
; /* maximal width of the menu item */
166 size_t nitems
; /* number of the active menu items */
167 size_t page_sz
;/* when menu longer than screen */
168 size_t idx
; /* the current menu item */
169 int prefkey
;/* preferred menu item */
170 struct cfdisk_menu
*prev
;
172 /* @ignore keys generator */
173 int (*ignore_cb
) (struct cfdisk
*, char *, size_t);
175 unsigned int vertical
: 1; /* enable vertical mode */
179 static struct cfdisk_menuitem main_menuitems
[] = {
180 { 'b', N_("Bootable"), N_("Toggle bootable flag of the current partition") },
181 { 'd', N_("Delete"), N_("Delete the current partition") },
182 { 'n', N_("New"), N_("Create new partition from free space") },
183 { 'q', N_("Quit"), N_("Quit program without writing partition table") },
184 { 't', N_("Type"), N_("Change the partition type") },
185 { 'h', N_("Help"), N_("Print help screen") },
186 { 's', N_("Sort"), N_("Fix partitions order") },
187 { 'W', N_("Write"), N_("Write partition table to disk (this might destroy data)") },
188 { 'u', N_("Dump"), N_("Dump partition table to sfdisk compatible script file") },
192 /* extra partinfo in name:value pairs */
193 struct cfdisk_extra
{
197 struct list_head exs
;
200 /* line and extra partinfo list_head */
202 char *data
; /* line data */
203 struct libscols_table
*extra
; /* extra info ('X') */
204 WINDOW
*w
; /* window with extra info */
207 /* top level control struct */
209 struct fdisk_context
*cxt
; /* libfdisk context */
210 struct fdisk_table
*table
; /* partition table */
212 struct cfdisk_menu
*menu
; /* the current menu */
214 int *fields
; /* output columns IDs */
215 size_t nfields
; /* number of columns IDs */
217 char *linesbuf
; /* table as string */
218 size_t linesbufsz
; /* size of the tb_buf */
220 struct cfdisk_line
*lines
; /* list of lines */
222 size_t nlines
; /* number of lines */
223 size_t lines_idx
; /* current line <0..N>, exclude header */
226 unsigned int nwrites
; /* fdisk_write_disklabel() counter */
228 WINDOW
*act_win
; /* the window currently on the screen */
231 struct libmnt_table
*mtab
;
232 struct libmnt_table
*fstab
;
233 struct libmnt_cache
*mntcache
;
235 unsigned int wrong_order
:1, /* PT not in right order */
236 zero_start
:1, /* ignore existing partition table */
237 show_extra
:1; /* show extra partinfo */
242 * let's use include/debug.h stuff for cfdisk too
244 UL_DEBUG_DEFINE_MASK(cfdisk
);
245 UL_DEBUG_DEFINE_MASKNAMES(cfdisk
) = UL_DEBUG_EMPTY_MASKNAMES
;
247 #define CFDISK_DEBUG_INIT (1 << 1)
248 #define CFDISK_DEBUG_UI (1 << 2)
249 #define CFDISK_DEBUG_MENU (1 << 3)
250 #define CFDISK_DEBUG_MISC (1 << 4)
251 #define CFDISK_DEBUG_TABLE (1 << 5)
252 #define CFDISK_DEBUG_ALL 0xFFFF
254 #define DBG(m, x) __UL_DBG(cfdisk, CFDISK_DEBUG_, m, x)
256 static void cfdisk_init_debug(void)
258 __UL_INIT_DEBUG(cfdisk
, CFDISK_DEBUG_
, 0, CFDISK_DEBUG
);
261 /* Initialize output columns -- we follow libfdisk fields (usually specific
264 static int cols_init(struct cfdisk
*cf
)
272 return fdisk_label_get_fields_ids(NULL
, cf
->cxt
, &cf
->fields
, &cf
->nfields
);
275 static void resize(void)
279 if (ioctl(fileno(stdout
), TIOCGWINSZ
, &ws
) != -1
280 && ws
.ws_row
&& ws
.ws_col
) {
281 ui_lines
= ws
.ws_row
;
284 resizeterm(ws
.ws_row
, ws
.ws_col
);
286 clearok(stdscr
, TRUE
);
290 DBG(UI
, ul_debug("ui: resize refresh ui_cols=%zu, ui_lines=%zu",
295 /* Reads partition in tree-like order from scols
297 static int partition_from_scols(struct fdisk_table
*tb
,
298 struct libscols_line
*ln
)
300 struct fdisk_partition
*pa
= scols_line_get_userdata(ln
);
302 fdisk_table_add_partition(tb
, pa
);
303 fdisk_unref_partition(pa
);
305 if (scols_line_has_children(ln
)) {
306 struct libscols_line
*chln
;
307 struct libscols_iter
*itr
= scols_new_iter(SCOLS_ITER_FORWARD
);
311 while (scols_line_next_child(ln
, itr
, &chln
) == 0)
312 partition_from_scols(tb
, chln
);
313 scols_free_iter(itr
);
318 static char *table_to_string(struct cfdisk
*cf
, struct fdisk_table
*tb
)
320 struct fdisk_partition
*pa
;
321 struct fdisk_label
*lb
;
322 struct fdisk_iter
*itr
= NULL
;
323 struct libscols_table
*table
= NULL
;
324 struct libscols_iter
*s_itr
= NULL
;
328 struct libscols_line
*ln
, *ln_cont
= NULL
;
330 DBG(TABLE
, ul_debug("convert to string"));
337 lb
= fdisk_get_label(cf
->cxt
, NULL
);
340 itr
= fdisk_new_iter(FDISK_ITER_FORWARD
);
344 /* get container (e.g. extended partition) */
345 while (fdisk_table_next_partition(tb
, itr
, &pa
) == 0) {
346 if (fdisk_partition_is_nested(pa
)) {
347 DBG(TABLE
, ul_debug("nested detected, using tree"));
348 tree
= SCOLS_FL_TREE
;
353 table
= scols_new_table();
356 scols_table_enable_maxout(table
, 1);
359 for (i
= 0; i
< cf
->nfields
; i
++) {
361 const struct fdisk_field
*field
=
362 fdisk_label_get_field(lb
, cf
->fields
[i
]);
366 if (fdisk_field_is_number(field
))
367 fl
|= SCOLS_FL_RIGHT
;
368 if (fdisk_field_get_id(field
) == FDISK_FIELD_TYPE
)
369 fl
|= SCOLS_FL_TRUNC
;
370 if (tree
&& fdisk_field_get_id(field
) == FDISK_FIELD_DEVICE
)
373 if (!scols_table_new_column(table
,
374 _(fdisk_field_get_name(field
)),
375 fdisk_field_get_width(field
), fl
))
380 fdisk_reset_iter(itr
, FDISK_ITER_FORWARD
);
382 while (fdisk_table_next_partition(tb
, itr
, &pa
) == 0) {
383 struct libscols_line
*parent
= fdisk_partition_is_nested(pa
) ? ln_cont
: NULL
;
385 ln
= scols_table_new_line(table
, parent
);
388 for (i
= 0; i
< cf
->nfields
; i
++) {
391 if (fdisk_partition_to_string(pa
, cf
->cxt
,
392 cf
->fields
[i
], &cdata
))
394 scols_line_refer_data(ln
, i
, cdata
);
396 if (tree
&& fdisk_partition_is_container(pa
))
399 scols_line_set_userdata(ln
, (void *) pa
);
400 fdisk_ref_partition(pa
);
403 if (scols_table_is_empty(table
))
406 scols_table_reduce_termwidth(table
, ARROW_CURSOR_WIDTH
);
407 scols_print_table_to_string(table
, &res
);
409 /* scols_* code might reorder lines, let's reorder @tb according to the
410 * final output (it's no problem because partitions are addressed by
411 * parno stored within struct fdisk_partition) */
414 fdisk_reset_iter(itr
, FDISK_ITER_FORWARD
);
415 while (fdisk_table_next_partition(tb
, itr
, &pa
) == 0)
416 fdisk_table_remove_partition(tb
, pa
);
418 s_itr
= scols_new_iter(SCOLS_ITER_FORWARD
);
422 /* add all in the right order (don't forget the output is tree) */
423 while (scols_table_next_line(table
, s_itr
, &ln
) == 0) {
424 if (scols_line_get_parent(ln
))
426 if (partition_from_scols(tb
, ln
))
430 scols_unref_table(table
);
431 scols_free_iter(s_itr
);
432 fdisk_free_iter(itr
);
437 static void cfdisk_free_lines(struct cfdisk
*cf
)
440 while(i
< cf
->nlines
) {
441 scols_unref_table(cf
->lines
[i
].extra
);
443 DBG(UI
, ul_debug("delete window: %p",
447 delwin(cf
->lines
[i
].w
);
448 cf
->lines
[i
].w
= NULL
;
456 * Read data about partitions from libfdisk and prepare output lines.
458 static int lines_refresh(struct cfdisk
*cf
)
466 DBG(TABLE
, ul_debug("refreshing buffer"));
469 cfdisk_free_lines(cf
);
475 fdisk_unref_table(cf
->table
);
478 /* read partitions and free spaces into cf->table */
479 rc
= fdisk_get_partitions(cf
->cxt
, &cf
->table
);
481 rc
= fdisk_get_freespaces(cf
->cxt
, &cf
->table
);
485 cf
->linesbuf
= table_to_string(cf
, cf
->table
);
489 cf
->linesbufsz
= strlen(cf
->linesbuf
);
490 cf
->nlines
= fdisk_table_get_nents(cf
->table
) + 1; /* 1 for header line */
492 cf
->wrong_order
= fdisk_table_wrong_order(cf
->table
) ? 1 : 0;
494 if (MENU_START_LINE
- TABLE_START_LINE
< cf
->nlines
)
495 cf
->page_sz
= MENU_START_LINE
- TABLE_START_LINE
- 1;
497 cf
->lines
= xcalloc(cf
->nlines
, sizeof(struct cfdisk_line
));
499 for (p
= cf
->linesbuf
, i
= 0; p
&& i
< cf
->nlines
; i
++) {
503 cf
->lines
[i
].data
= begin
;
504 p
= strchr(begin
, '\n');
505 sz
= p
? (size_t) (p
- begin
) : strlen(begin
);
510 /* libsmartcols reduces columns width as much as possible to
511 * fit terminal width, but for very small terminals it preffers
512 * long lines rather than remove columns from output. This is fine
513 * for normal utils, but it's problematic for ncurses -- so we
514 * manually cut the end of the line to fit terminal width. */
515 if (sz
+ ARROW_CURSOR_WIDTH
> ui_cols
)
516 *(begin
+ (ui_cols
- ARROW_CURSOR_WIDTH
)) = '\0';
517 cf
->lines
[i
].extra
= scols_new_table();
518 scols_table_enable_noheadings(cf
->lines
[i
].extra
, 1);
519 scols_table_new_column(cf
->lines
[i
].extra
, NULL
, 0, SCOLS_FL_RIGHT
);
520 scols_table_new_column(cf
->lines
[i
].extra
, NULL
, 0, SCOLS_FL_TRUNC
);
526 static struct fdisk_partition
*get_current_partition(struct cfdisk
*cf
)
531 return fdisk_table_get_partition(cf
->table
, cf
->lines_idx
);
534 static int is_freespace(struct cfdisk
*cf
, size_t i
)
536 struct fdisk_partition
*pa
;
541 pa
= fdisk_table_get_partition(cf
->table
, i
);
542 return fdisk_partition_is_freespace(pa
);
545 /* converts libfdisk FDISK_ASKTYPE_MENU to cfdisk menu and returns user's
546 * responseback to libfdisk
548 static int ask_menu(struct fdisk_ask
*ask
, struct cfdisk
*cf
)
550 struct cfdisk_menuitem
*d
, *cm
;
552 size_t i
= 0, nitems
;
553 const char *name
, *desc
;
558 /* create cfdisk menu according to libfdisk ask-menu, note that the
559 * last cm[] item has to be empty -- so nitems + 1 */
560 nitems
= fdisk_ask_menu_get_nitems(ask
);
561 cm
= xcalloc(nitems
+ 1, sizeof(struct cfdisk_menuitem
));
563 for (i
= 0; i
< nitems
; i
++) {
564 if (fdisk_ask_menu_get_item(ask
, i
, &key
, &name
, &desc
))
571 /* make the new menu active */
582 if (ui_menu_move(cf
, key
) == 0)
589 d
= menu_get_menuitem(cf
, cf
->menu
->idx
);
591 fdisk_ask_menu_set_result(ask
, d
->key
);
605 static int ask_callback(struct fdisk_context
*cxt
, struct fdisk_ask
*ask
,
606 void *data
__attribute__((__unused__
)))
613 switch(fdisk_ask_get_type(ask
)) {
614 case FDISK_ASKTYPE_INFO
:
615 ui_info(fdisk_ask_print_get_mesg(ask
));
617 case FDISK_ASKTYPE_WARNX
:
618 ui_warnx(fdisk_ask_print_get_mesg(ask
));
620 case FDISK_ASKTYPE_WARN
:
621 ui_warn(fdisk_ask_print_get_mesg(ask
));
623 case FDISK_ASKTYPE_MENU
:
624 ask_menu(ask
, (struct cfdisk
*) data
);
627 ui_warnx(_("internal error: unsupported dialog type %d"),
628 fdisk_ask_get_type(ask
));
634 static int ui_end(void)
639 #if defined(HAVE_SLCURSES_H) || defined(HAVE_SLANG_SLCURSES_H)
640 SLsmg_gotorc(ui_lines
- 1, 0);
643 mvcur(0, ui_cols
- 1, ui_lines
-1, 0);
653 static void ui_vprint_center(size_t line
, int attrs
, const char *fmt
, va_list ap
)
661 xvasprintf(&buf
, fmt
, ap
);
663 width
= mbs_safe_width(buf
);
664 if (width
> (size_t) ui_cols
) {
665 char *p
= strrchr(buf
+ ui_cols
, ' ');
669 if (line
+ 1 >= ui_lines
)
672 mvaddstr(line
, 0, buf
);
673 mvaddstr(line
+ 1, 0, p
+1);
677 mvaddstr(line
, (ui_cols
- width
) / 2, buf
);
683 static void ui_center(size_t line
, const char *fmt
, ...)
687 ui_vprint_center(line
, 0, fmt
, ap
);
691 static void ui_warnx(const char *fmt
, ...)
696 ui_vprint_center(WARN_LINE
,
697 colors_wanted() ? COLOR_PAIR(CFDISK_CL_WARNING
) : 0,
700 vfprintf(stderr
, fmt
, ap
);
706 static void ui_warn(const char *fmt
, ...)
711 xasprintf(&fmt_m
, "%s: %m", fmt
);
715 ui_vprint_center(WARN_LINE
,
716 colors_wanted() ? COLOR_PAIR(CFDISK_CL_WARNING
) : 0,
719 vfprintf(stderr
, fmt_m
, ap
);
726 static void ui_clean_warn(void)
732 static int __attribute__((__noreturn__
)) ui_errx(int rc
, const char *fmt
, ...)
738 fprintf(stderr
, "%s: ", program_invocation_short_name
);
739 vfprintf(stderr
, fmt
, ap
);
746 static void ui_info(const char *fmt
, ...)
751 ui_vprint_center(INFO_LINE
,
752 colors_wanted() ? COLOR_PAIR(CFDISK_CL_INFO
) : 0,
755 vfprintf(stdout
, fmt
, ap
);
761 static void ui_clean_info(void)
767 static void ui_hint(const char *fmt
, ...)
772 ui_vprint_center(HINT_LINE
, A_BOLD
, fmt
, ap
);
774 vfprintf(stdout
, fmt
, ap
);
780 static void ui_clean_hint(void)
786 static void die_on_signal(int dummy
__attribute__((__unused__
)))
788 DBG(MISC
, ul_debug("die on signal."));
793 static void resize_on_signal(int dummy
__attribute__((__unused__
)))
795 DBG(MISC
, ul_debug("resize on signal."));
799 static void menu_refresh_size(struct cfdisk
*cf
)
801 if (cf
->menu
&& cf
->menu
->nitems
)
802 cf
->menu
->page_sz
= (cf
->menu
->nitems
/ (ui_lines
- 4)) ? ui_lines
- 4 : 0;
805 static void menu_update_ignore(struct cfdisk
*cf
)
807 char ignore
[128] = { 0 };
809 struct cfdisk_menu
*m
;
810 struct cfdisk_menuitem
*d
, *org
= NULL
;
815 assert(cf
->menu
->ignore_cb
);
818 DBG(MENU
, ul_debug("update menu ignored keys"));
820 i
= m
->ignore_cb(cf
, ignore
, sizeof(ignore
));
823 /* return if no change */
824 if ((!m
->ignore
&& !*ignore
)
825 || (m
->ignore
&& *ignore
&& strcmp(m
->ignore
, ignore
) == 0)) {
830 org
= menu_get_menuitem(cf
, m
->idx
);
833 m
->ignore
= xstrdup(ignore
);
836 for (d
= m
->items
; d
->name
; d
++) {
837 if (m
->ignore
&& strchr(m
->ignore
, d
->key
))
842 DBG(MENU
, ul_debug("update menu preferred keys"));
844 /* refresh menu index to be at the same menuitem or go to the first */
845 if (org
&& menu_get_menuitem_by_key(cf
, org
->key
, &idx
))
847 else if (m
->prefkey
&& menu_get_menuitem_by_key(cf
, m
->prefkey
, &idx
))
852 menu_refresh_size(cf
);
855 static struct cfdisk_menu
*menu_push(
857 struct cfdisk_menuitem
*items
)
859 struct cfdisk_menu
*m
= xcalloc(1, sizeof(*m
));
860 struct cfdisk_menuitem
*d
;
864 DBG(MENU
, ul_debug("new menu"));
869 for (d
= m
->items
; d
->name
; d
++) {
870 const char *name
= _(d
->name
);
871 size_t len
= mbs_safe_width(name
);
879 menu_refresh_size(cf
);
883 static struct cfdisk_menu
*menu_pop(struct cfdisk
*cf
)
885 struct cfdisk_menu
*m
= NULL
;
889 DBG(MENU
, ul_debug("pop menu"));
893 free(cf
->menu
->ignore
);
894 free(cf
->menu
->title
);
901 static void menu_set_title(struct cfdisk_menu
*m
, const char *title
)
906 size_t len
= mbs_safe_width(title
);
907 if (len
+ MENU_TITLE_PADDING
> m
->width
)
908 m
->width
= len
+ MENU_TITLE_PADDING
;
909 str
= xstrdup(title
);
915 static int ui_init(struct cfdisk
*cf
__attribute__((__unused__
)))
919 DBG(UI
, ul_debug("init"));
921 /* setup SIGCHLD handler */
922 sigemptyset(&sa
.sa_mask
);
924 sa
.sa_handler
= die_on_signal
;
925 sigaction(SIGINT
, &sa
, NULL
);
926 sigaction(SIGTERM
, &sa
, NULL
);
928 sa
.sa_handler
= resize_on_signal
;
929 sigaction(SIGWINCH
, &sa
, NULL
);
934 #ifdef HAVE_USE_DEFAULT_COLORS
935 if (colors_wanted() && has_colors()) {
939 use_default_colors();
940 for (i
= 1; i
< ARRAY_SIZE(color_pairs
); i
++) /* yeah, start from 1! */
941 init_pair(i
, color_pairs
[i
][0], color_pairs
[i
][1]);
951 keypad(stdscr
, TRUE
);
957 #define MENU_H_ITEMWIDTH(m) ( MENU_H_PRESTR_SZ \
963 #define MENU_V_ITEMWIDTH(m) (MENU_V_SPADDING + (m)->width + MENU_V_SPADDING)
966 static size_t menuitem_get_line(struct cfdisk
*cf
, size_t idx
)
968 struct cfdisk_menu
*m
= cf
->menu
;
971 if (!m
->page_sz
) /* small menu */
972 return (ui_lines
- (cf
->menu
->nitems
+ 1)) / 2 + idx
;
973 return (idx
% m
->page_sz
) + 1;
975 size_t len
= MENU_H_ITEMWIDTH(m
) + MENU_H_BETWEEN
; /** item width */
976 size_t items
= ui_cols
/ len
; /* items per line */
980 return MENU_START_LINE
+ ((idx
/ items
));
984 static int menuitem_get_column(struct cfdisk
*cf
, size_t idx
)
986 if (cf
->menu
->vertical
) {
987 size_t nc
= MENU_V_ITEMWIDTH(cf
->menu
);
988 if ((size_t) ui_cols
<= nc
)
990 return (ui_cols
- nc
) / 2;
992 size_t len
= MENU_H_ITEMWIDTH(cf
->menu
) + MENU_H_BETWEEN
; /* item width */
993 size_t items
= ui_cols
/ len
; /* items per line */
994 size_t extra
= items
< cf
->menu
->nitems
? /* extra space on line */
995 ui_cols
% len
: /* - multi-line menu */
996 ui_cols
- (cf
->menu
->nitems
* len
); /* - one line menu */
999 return 0; /* hmm... no space */
1001 extra
+= MENU_H_BETWEEN
; /* add padding after last item to extra */
1004 return (idx
* len
) + (extra
/ 2);
1005 return ((idx
% items
) * len
) + (extra
/ 2);
1009 static int menuitem_on_page(struct cfdisk
*cf
, size_t idx
)
1011 struct cfdisk_menu
*m
= cf
->menu
;
1013 if (m
->page_sz
== 0 ||
1014 m
->idx
/ m
->page_sz
== idx
/ m
->page_sz
)
1019 static struct cfdisk_menuitem
*menu_get_menuitem(struct cfdisk
*cf
, size_t idx
)
1021 struct cfdisk_menuitem
*d
;
1024 for (i
= 0, d
= cf
->menu
->items
; d
->name
; d
++) {
1025 if (cf
->menu
->ignore
&& strchr(cf
->menu
->ignore
, d
->key
))
1034 static struct cfdisk_menuitem
*menu_get_menuitem_by_key(struct cfdisk
*cf
,
1035 int key
, size_t *idx
)
1037 struct cfdisk_menuitem
*d
;
1039 for (*idx
= 0, d
= cf
->menu
->items
; d
->name
; d
++) {
1040 if (cf
->menu
->ignore
&& strchr(cf
->menu
->ignore
, d
->key
))
1050 static void ui_draw_menuitem(struct cfdisk
*cf
,
1051 struct cfdisk_menuitem
*d
,
1054 char buf
[80 * MB_CUR_MAX
], *ptr
= buf
;
1057 int ln
, cl
, vert
= cf
->menu
->vertical
;
1059 if (!menuitem_on_page(cf
, idx
))
1060 return; /* no visible item */
1061 ln
= menuitem_get_line(cf
, idx
);
1062 cl
= menuitem_get_column(cf
, idx
);
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
, sizeof(buf
), &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
);
1093 if (cf
->menu
->idx
== idx
) {
1096 ui_hint(_(d
->desc
));
1100 static void ui_clean_menu(struct cfdisk
*cf
)
1104 struct cfdisk_menu
*m
= cf
->menu
;
1105 size_t ln
= menuitem_get_line(cf
, 0);
1108 lastline
= ln
+ (m
->page_sz
? m
->page_sz
: m
->nitems
);
1110 lastline
= menuitem_get_line(cf
, m
->nitems
);
1112 for (i
= ln
; i
<= lastline
; i
++) {
1115 DBG(MENU
, ul_debug("clean_menu: line %zu", i
));
1124 static void ui_draw_menu(struct cfdisk
*cf
)
1126 struct cfdisk_menuitem
*d
;
1127 struct cfdisk_menu
*m
;
1129 size_t ln
= menuitem_get_line(cf
, 0);
1135 DBG(MENU
, ul_debug("draw start"));
1141 nlines
= m
->page_sz
? m
->page_sz
: m
->nitems
;
1143 nlines
= menuitem_get_line(cf
, m
->nitems
);
1146 menu_update_ignore(cf
);
1148 while ((d
= menu_get_menuitem(cf
, i
)))
1149 ui_draw_menuitem(cf
, d
, i
++);
1152 size_t cl
= menuitem_get_column(cf
, 0);
1153 size_t curpg
= m
->page_sz
? m
->idx
/ m
->page_sz
: 0;
1155 /* corners and horizontal lines */
1156 mvaddch(ln
- 1, cl
- 1, ACS_ULCORNER
);
1157 mvaddch(ln
+ nlines
, cl
- 1, ACS_LLCORNER
);
1159 for (i
= 0; i
< MENU_V_ITEMWIDTH(m
); i
++) {
1160 mvaddch(ln
- 1, cl
+ i
, ACS_HLINE
);
1161 mvaddch(ln
+ nlines
, cl
+ i
, ACS_HLINE
);
1164 mvaddch(ln
- 1, cl
+ i
, ACS_URCORNER
);
1165 mvaddch(ln
+ nlines
, cl
+ i
, ACS_LRCORNER
);
1167 /* draw also lines around empty lines on last page */
1169 m
->nitems
/ m
->page_sz
== m
->idx
/ m
->page_sz
) {
1170 for (i
= m
->nitems
% m
->page_sz
+ 1; i
<= m
->page_sz
; i
++) {
1171 mvaddch(i
, cl
- 1, ACS_VLINE
);
1172 mvaddch(i
, cl
+ MENU_V_ITEMWIDTH(m
), ACS_VLINE
);
1177 mvprintw(ln
- 1, cl
, " %s ", m
->title
);
1181 mvaddch(ln
- 1, cl
+ MENU_V_ITEMWIDTH(m
) - 2, ACS_UARROW
);
1182 if (m
->page_sz
&& curpg
< m
->nitems
/ m
->page_sz
)
1183 mvaddch(ln
+ nlines
, cl
+ MENU_V_ITEMWIDTH(m
) - 2, ACS_DARROW
);
1186 DBG(MENU
, ul_debug("draw end."));
1189 inline static int extra_insert_pair(struct cfdisk_line
*l
, const char *name
, const char *data
)
1191 struct libscols_line
*lsl
;
1197 if (!data
|| !*data
)
1200 lsl
= scols_table_new_line(l
->extra
, NULL
);
1204 rc
= scols_line_set_data(lsl
, 0, name
);
1206 rc
= scols_line_set_data(lsl
, 1, data
);
1211 #ifdef HAVE_LIBMOUNT
1212 static char *get_mountpoint(struct cfdisk
*cf
, const char *uuid
, const char *label
)
1214 struct libmnt_fs
*fs
= NULL
;
1215 char *target
= NULL
;
1218 assert(uuid
|| label
);
1220 DBG(UI
, ul_debug("asking for mountpoint [uuid=%s, label=%s]", uuid
, label
));
1223 cf
->mntcache
= mnt_new_cache();
1225 /* 1st try between mounted filesystems */
1227 cf
->mtab
= mnt_new_table();
1229 mnt_table_set_cache(cf
->mtab
, cf
->mntcache
);
1230 mnt_table_parse_mtab(cf
->mtab
, NULL
);
1235 fs
= mnt_table_find_tag(cf
->mtab
,
1236 uuid
? "UUID" : "LABEL",
1237 uuid
? : label
, MNT_ITER_FORWARD
);
1242 cf
->fstab
= mnt_new_table();
1244 mnt_table_set_cache(cf
->fstab
, cf
->mntcache
);
1245 mnt_table_parse_fstab(cf
->fstab
, NULL
);
1249 fs
= mnt_table_find_tag(cf
->fstab
,
1250 uuid
? "UUID" : "LABEL",
1251 uuid
? : label
, MNT_ITER_FORWARD
);
1257 xasprintf(&target
, _("%s (mounted)"), mnt_fs_get_target(fs
));
1259 target
= xstrdup(mnt_fs_get_target(fs
));
1264 #endif /* HAVE_LIBMOUNT */
1266 static void extra_prepare_data(struct cfdisk
*cf
)
1268 struct fdisk_partition
*pa
= get_current_partition(cf
);
1269 struct cfdisk_line
*l
= &cf
->lines
[cf
->lines_idx
];
1271 char *devuuid
= NULL
, *devlabel
= NULL
;
1273 DBG(UI
, ul_debug("preparing extra data"));
1275 /* string data should not equal an empty string */
1276 if (!fdisk_partition_to_string(pa
, cf
->cxt
, FDISK_FIELD_NAME
, &data
) && data
) {
1277 extra_insert_pair(l
, _("Partition name:"), data
);
1281 if (!fdisk_partition_to_string(pa
, cf
->cxt
, FDISK_FIELD_UUID
, &data
) && data
) {
1282 extra_insert_pair(l
, _("Partition UUID:"), data
);
1286 if (!fdisk_partition_to_string(pa
, cf
->cxt
, FDISK_FIELD_TYPE
, &data
) && data
) {
1287 char *code
= NULL
, *type
= NULL
;
1289 fdisk_partition_to_string(pa
, cf
->cxt
, FDISK_FIELD_TYPEID
, &code
);
1290 xasprintf(&type
, "%s (%s)", data
, code
);
1292 extra_insert_pair(l
, _("Partition type:"), type
);
1298 if (!fdisk_partition_to_string(pa
, cf
->cxt
, FDISK_FIELD_ATTR
, &data
) && data
) {
1299 extra_insert_pair(l
, _("Attributes:"), data
);
1303 /* for numeric data, only show non-zero rows */
1304 if (!fdisk_partition_to_string(pa
, cf
->cxt
, FDISK_FIELD_BSIZE
, &data
) && data
) {
1306 extra_insert_pair(l
, "BSIZE:", data
);
1310 if (!fdisk_partition_to_string(pa
, cf
->cxt
, FDISK_FIELD_CPG
, &data
) && data
) {
1312 extra_insert_pair(l
, "CPG:", data
);
1316 if (!fdisk_partition_to_string(pa
, cf
->cxt
, FDISK_FIELD_FSIZE
, &data
) && data
) {
1318 extra_insert_pair(l
, "FSIZE:", data
);
1322 #ifdef HAVE_LIBBLKID
1323 if (fdisk_partition_has_start(pa
) && fdisk_partition_has_size(pa
)) {
1325 uintmax_t start
, size
;
1326 blkid_probe pr
= blkid_new_probe();
1331 DBG(UI
, ul_debug("blkid prober: %p", pr
));
1333 start
= fdisk_partition_get_start(pa
) * fdisk_get_sector_size(cf
->cxt
);
1334 size
= fdisk_partition_get_size(pa
) * fdisk_get_sector_size(cf
->cxt
);
1335 fd
= fdisk_get_devfd(cf
->cxt
);
1337 if (blkid_probe_set_device(pr
, fd
, start
, size
) == 0 &&
1338 blkid_do_fullprobe(pr
) == 0) {
1339 const char *bdata
= NULL
;
1341 if (!blkid_probe_lookup_value(pr
, "TYPE", &bdata
, NULL
))
1342 extra_insert_pair(l
, _("Filesystem:"), bdata
);
1343 if (!blkid_probe_lookup_value(pr
, "LABEL", &bdata
, NULL
)) {
1344 extra_insert_pair(l
, _("Filesystem label:"), bdata
);
1345 devlabel
= xstrdup(bdata
);
1347 if (!blkid_probe_lookup_value(pr
, "UUID", &bdata
, NULL
)) {
1348 extra_insert_pair(l
, _("Filesystem UUID:"), bdata
);
1349 devuuid
= xstrdup(bdata
);
1352 blkid_free_probe(pr
);
1354 #endif /* HAVE_LIBBLKID */
1356 #ifdef HAVE_LIBMOUNT
1357 if (devuuid
|| devlabel
) {
1358 data
= get_mountpoint(cf
, devuuid
, devlabel
);
1360 extra_insert_pair(l
, _("Mountpoint:"), data
);
1364 #endif /* HAVE_LIBMOUNT */
1370 static int ui_draw_extra(struct cfdisk
*cf
)
1374 struct cfdisk_line
*ln
= &cf
->lines
[cf
->lines_idx
];
1375 char *tbstr
= NULL
, *end
;
1376 int win_ex_start_line
, win_height
, tblen
;
1379 if (!cf
->show_extra
)
1382 DBG(UI
, ul_debug("draw extra"));
1387 wclear(cf
->act_win
);
1391 if (scols_table_is_empty(ln
->extra
)) {
1392 extra_prepare_data(cf
);
1393 if (scols_table_is_empty(ln
->extra
))
1397 ndatalines
= fdisk_table_get_nents(cf
->table
) + 1;
1399 /* nents + header + one free line */
1400 win_ex_start_line
= TABLE_START_LINE
+ ndatalines
;
1401 win_height
= MENU_START_LINE
- win_ex_start_line
;
1402 tblen
= scols_table_get_nlines(ln
->extra
);
1404 /* we can't get a single line of data under the partlist*/
1408 /* number of data lines + 2 for top/bottom lines */
1409 win_height
= win_height
< tblen
+ 2 ? win_height
: tblen
+ 2;
1411 if ((size_t) win_ex_start_line
+ win_height
+ 1 < MENU_START_LINE
)
1412 win_ex_start_line
= MENU_START_LINE
- win_height
;
1414 win_ex
= subwin(stdscr
, win_height
, ui_cols
- 2, win_ex_start_line
, 1);
1416 scols_table_reduce_termwidth(ln
->extra
, 4);
1417 scols_print_table_to_string(ln
->extra
, &tbstr
);
1420 while ((end
= strchr(end
, '\n')))
1426 while (--win_height
> 1) {
1427 mvwaddstr(win_ex
, wline
++, 1 /* window column*/, tbstr
);
1428 tbstr
+= strlen(tbstr
) + 1;
1435 DBG(UI
, ul_debug("draw window: %p", win_ex
));
1439 cf
->act_win
= ln
->w
= win_ex
;
1443 static void ui_menu_goto(struct cfdisk
*cf
, int where
)
1445 struct cfdisk_menuitem
*d
;
1448 /* stop and begin/end for vertical menus */
1449 if (cf
->menu
->vertical
) {
1452 else if (where
> (int) cf
->menu
->nitems
- 1)
1453 where
= cf
->menu
->nitems
- 1;
1455 /* continue from begin/end */
1457 where
= cf
->menu
->nitems
- 1;
1458 else if ((size_t) where
> cf
->menu
->nitems
- 1)
1461 if ((size_t) where
== cf
->menu
->idx
)
1466 old
= cf
->menu
->idx
;
1467 cf
->menu
->idx
= where
;
1469 if (!menuitem_on_page(cf
, old
)) {
1474 d
= menu_get_menuitem(cf
, old
);
1475 ui_draw_menuitem(cf
, d
, old
);
1477 d
= menu_get_menuitem(cf
, where
);
1478 ui_draw_menuitem(cf
, d
, where
);
1482 static int ui_menu_move(struct cfdisk
*cf
, int key
)
1484 struct cfdisk_menu
*m
;
1490 return 0; /* ignore errors */
1494 DBG(MENU
, ul_debug("menu move key >%c<.", key
));
1500 case '\016': /* ^N */
1501 case 'j': /* Vi-like alternative */
1502 ui_menu_goto(cf
, m
->idx
+ 1);
1505 case '\020': /* ^P */
1506 case 'k': /* Vi-like alternative */
1507 ui_menu_goto(cf
, (int) m
->idx
- 1);
1511 ui_menu_goto(cf
, (int) m
->idx
- m
->page_sz
);
1516 ui_menu_goto(cf
, 0);
1520 ui_menu_goto(cf
, m
->idx
+ m
->page_sz
);
1525 ui_menu_goto(cf
, m
->nitems
);
1532 ui_menu_goto(cf
, m
->idx
+ 1);
1538 ui_menu_goto(cf
, (int) m
->idx
- 1);
1543 return 1; /* key irrelevant for menu move */
1546 /* but don't call me from ui_run(), this is for pop-up menus only */
1547 static void ui_menu_resize(struct cfdisk
*cf
)
1551 menu_refresh_size(cf
);
1556 static int partition_on_page(struct cfdisk
*cf
, size_t i
)
1558 if (cf
->page_sz
== 0 ||
1559 cf
->lines_idx
/ cf
->page_sz
== i
/ cf
->page_sz
)
1564 static void ui_draw_partition(struct cfdisk
*cf
, size_t i
)
1566 int ln
= TABLE_START_LINE
+ 1 + i
; /* skip table header */
1567 int cl
= ARROW_CURSOR_WIDTH
; /* we need extra space for cursor */
1568 int cur
= cf
->lines_idx
== i
;
1572 if (!partition_on_page(cf
, i
))
1574 ln
= TABLE_START_LINE
+ (i
% cf
->page_sz
) + 1;
1575 curpg
= cf
->lines_idx
/ cf
->page_sz
;
1579 "draw partition %zu [page_sz=%zu, "
1580 "line=%d, idx=%zu]",
1581 i
, cf
->page_sz
, ln
, cf
->lines_idx
));
1585 mvaddstr(ln
, 0, ARROW_CURSOR_STRING
);
1586 mvaddstr(ln
, cl
, cf
->lines
[i
+ 1].data
);
1591 if (colors_wanted() && is_freespace(cf
, i
)) {
1592 attron(COLOR_PAIR(CFDISK_CL_FREESPACE
));
1595 mvaddstr(ln
, 0, ARROW_CURSOR_DUMMY
);
1596 mvaddstr(ln
, cl
, cf
->lines
[i
+ 1].data
);
1598 attroff(COLOR_PAIR(CFDISK_CL_FREESPACE
));
1601 if ((size_t) ln
== MENU_START_LINE
- 1 &&
1602 cf
->page_sz
&& curpg
< cf
->nlines
/ cf
->page_sz
) {
1605 mvaddch(ln
, ui_cols
- 1, ACS_DARROW
);
1606 mvaddch(ln
, 0, ACS_DARROW
);
1613 static int ui_draw_table(struct cfdisk
*cf
)
1615 int cl
= ARROW_CURSOR_WIDTH
;
1616 size_t i
, nparts
= fdisk_table_get_nents(cf
->table
);
1617 size_t curpg
= cf
->page_sz
? cf
->lines_idx
/ cf
->page_sz
: 0;
1619 DBG(UI
, ul_debug("draw table"));
1621 for (i
= TABLE_START_LINE
; i
<= TABLE_START_LINE
+ cf
->page_sz
; i
++) {
1626 if (nparts
== 0 || (size_t) cf
->lines_idx
> nparts
- 1)
1627 cf
->lines_idx
= nparts
? nparts
- 1 : 0;
1631 mvaddstr(TABLE_START_LINE
, cl
, cf
->lines
[0].data
);
1634 /* print partitions */
1635 for (i
= 0; i
< nparts
; i
++)
1636 ui_draw_partition(cf
, i
);
1639 mvaddch(TABLE_START_LINE
, ui_cols
- 1, ACS_UARROW
);
1640 mvaddch(TABLE_START_LINE
, 0, ACS_UARROW
);
1642 if (cf
->page_sz
&& curpg
< cf
->nlines
/ cf
->page_sz
) {
1643 mvaddch(MENU_START_LINE
- 1, ui_cols
- 1, ACS_DARROW
);
1644 mvaddch(MENU_START_LINE
- 1, 0, ACS_DARROW
);
1649 static int ui_table_goto(struct cfdisk
*cf
, int where
)
1652 size_t nparts
= fdisk_table_get_nents(cf
->table
);
1654 DBG(UI
, ul_debug("goto table %d", where
));
1658 else if ((size_t) where
> nparts
- 1)
1661 if ((size_t) where
== cf
->lines_idx
)
1664 old
= cf
->lines_idx
;
1665 cf
->lines_idx
= where
;
1667 if (!partition_on_page(cf
, old
) ||!partition_on_page(cf
, where
))
1670 ui_draw_partition(cf
, old
); /* cleanup old */
1671 ui_draw_partition(cf
, where
); /* draw new */
1681 static int ui_refresh(struct cfdisk
*cf
)
1683 struct fdisk_label
*lb
;
1685 uint64_t bytes
= fdisk_get_nsectors(cf
->cxt
) * fdisk_get_sector_size(cf
->cxt
);
1691 strsz
= size_to_human_string(SIZE_SUFFIX_SPACE
1692 | SIZE_SUFFIX_3LETTER
, bytes
);
1694 lb
= fdisk_get_label(cf
->cxt
, NULL
);
1701 ui_center(0, _("Disk: %s"), fdisk_get_devname(cf
->cxt
));
1703 ui_center(1, _("Size: %s, %ju bytes, %ju sectors"),
1704 strsz
, bytes
, (uintmax_t) fdisk_get_nsectors(cf
->cxt
));
1705 if (fdisk_get_disklabel_id(cf
->cxt
, &id
) == 0 && id
)
1706 ui_center(2, _("Label: %s, identifier: %s"),
1707 fdisk_label_get_name(lb
), id
);
1709 ui_center(2, _("Label: %s"), fdisk_label_get_name(lb
));
1718 static ssize_t
ui_get_string(struct cfdisk
*cf
, const char *prompt
,
1719 const char *hint
, char *buf
, size_t len
)
1722 ssize_t i
= 0, rc
= -1;
1723 int ln
= MENU_START_LINE
, cl
= 1;
1736 mvaddstr(ln
, cl
, (char *) prompt
);
1737 cl
+= mbs_safe_width(prompt
);
1743 cells
= mbs_safe_width(buf
);
1744 mvaddstr(ln
, cl
, buf
);
1752 move(ln
, cl
+ cells
);
1757 #if !defined(HAVE_SLCURSES_H) && !defined(HAVE_SLANG_SLCURSES_H) && \
1758 defined(HAVE_LIBNCURSESW) && defined(HAVE_WIDECHAR)
1760 if (get_wch(&c
) == ERR
) {
1763 if ((c
= getch()) == ERR
) {
1769 if (!isatty(STDIN_FILENO
))
1774 if (c
== '\r' || c
== '\n' || c
== KEY_ENTER
)
1779 rc
= -CFDISK_ERR_ESC
;
1781 case KEY_LEFT
: /* TODO: implement full buffer editor */
1794 i
= mbs_truncate(buf
, &cells
);
1797 mvaddch(ln
, cl
+ cells
, ' ');
1798 move(ln
, cl
+ cells
);
1804 #if defined(HAVE_LIBNCURSESW) && defined(HAVE_WIDECHAR)
1805 if (i
+ 1 < (ssize_t
) len
&& iswprint(c
)) {
1806 wchar_t wc
= (wchar_t) c
;
1807 char s
[MB_CUR_MAX
+ 1];
1808 int sz
= wctomb(s
, wc
);
1810 if (sz
> 0 && sz
+ i
< (ssize_t
) len
) {
1812 mvaddnstr(ln
, cl
+ cells
, s
, sz
);
1813 memcpy(buf
+ i
, s
, sz
);
1816 cells
+= wcwidth(wc
);
1821 if (i
+ 1 < (ssize_t
) len
&& isprint(c
)) {
1822 mvaddch(ln
, cl
+ cells
, c
);
1834 rc
= i
; /* success */
1844 /* @res is default value as well as result in bytes */
1845 static int ui_get_size(struct cfdisk
*cf
, const char *prompt
, uintmax_t *res
,
1846 uintmax_t low
, uintmax_t up
, int *expsize
)
1851 char *dflt
= size_to_human_string(0, *res
);
1853 DBG(UI
, ul_debug("get_size (default=%ju)", *res
));
1857 snprintf(buf
, sizeof(buf
), "%s", dflt
);
1860 int pwr
= 0, insec
= 0;
1862 rc
= ui_get_string(cf
, prompt
,
1863 _("May be followed by M for MiB, G for GiB, "
1864 "T for TiB, or S for sectors."),
1869 ui_warnx(_("Please, specify size."));
1870 continue; /* nothing specified */
1871 } else if (rc
== -CFDISK_ERR_ESC
)
1872 break; /* cancel dialog */
1874 if (strcmp(buf
, dflt
) == 0)
1875 user
= *res
, rc
= 0; /* no change, use default */
1877 size_t len
= strlen(buf
);
1878 if (buf
[len
- 1] == 'S' || buf
[len
- 1] == 's') {
1880 buf
[len
- 1] = '\0';
1882 rc
= parse_size(buf
, &user
, &pwr
); /* parse */
1886 DBG(UI
, ul_debug("get_size user=%ju, power=%d, sectors=%s",
1887 user
, pwr
, insec
? "yes" : "no"));
1889 user
*= fdisk_get_sector_size(cf
->cxt
);
1891 ui_warnx(_("Minimum size is %ju bytes."), low
);
1894 if (user
> up
&& pwr
&& user
< up
+ (1ULL << pwr
* 10))
1895 /* ignore when the user specified size overflow
1896 * with in range specified by suffix (e.g. MiB) */
1900 ui_warnx(_("Maximum size is %ju bytes."), up
);
1903 if (rc
== 0 && insec
&& expsize
)
1907 ui_warnx(_("Failed to parse size."));
1914 DBG(UI
, ul_debug("get_size (result=%ju, rc=%zd)", *res
, rc
));
1918 static struct fdisk_parttype
*ui_get_parttype(struct cfdisk
*cf
,
1919 struct fdisk_parttype
*cur
)
1921 struct cfdisk_menuitem
*d
, *cm
;
1922 size_t i
= 0, nitems
, idx
= 0;
1923 struct fdisk_parttype
*t
= NULL
;
1924 struct fdisk_label
*lb
;
1927 DBG(UI
, ul_debug("asking for parttype."));
1929 lb
= fdisk_get_label(cf
->cxt
, NULL
);
1931 /* create cfdisk menu according to label types, note that the
1932 * last cm[] item has to be empty -- so nitems + 1 */
1933 nitems
= fdisk_label_get_nparttypes(lb
);
1937 cm
= xcalloc(nitems
+ 1, sizeof(struct cfdisk_menuitem
));
1941 codetypes
= fdisk_label_has_code_parttypes(lb
);
1943 for (i
= 0; i
< nitems
; i
++) {
1944 const struct fdisk_parttype
*x
= fdisk_label_get_parttype(lb
, i
);
1947 cm
[i
].userdata
= (void *) x
;
1949 xasprintf(&name
, "%2x %s",
1950 fdisk_parttype_get_code(x
),
1951 _(fdisk_parttype_get_name(x
)));
1953 name
= (char *) _(fdisk_parttype_get_name(x
));
1954 cm
[i
].desc
= fdisk_parttype_get_string(x
);
1961 /* make the new menu active */
1963 cf
->menu
->vertical
= 1;
1964 cf
->menu
->idx
= idx
;
1965 menu_set_title(cf
->menu
, _("Select partition type"));
1974 if (ui_menu_move(cf
, key
) == 0)
1981 d
= menu_get_menuitem(cf
, cf
->menu
->idx
);
1983 t
= (struct fdisk_parttype
*) d
->userdata
;
1995 for (i
= 0; i
< nitems
; i
++)
1996 free((char *) cm
[i
].name
);
1999 DBG(UI
, ul_debug("get parrtype done [type=%s] ", t
?
2000 fdisk_parttype_get_name(t
) : NULL
));
2004 static int ui_script_read(struct cfdisk
*cf
)
2006 struct fdisk_script
*sc
= NULL
;
2007 char buf
[PATH_MAX
] = { 0 };
2011 rc
= ui_get_string(cf
, _("Enter script file name: "),
2012 _("The script file will be applied to in-memory partition table."),
2019 sc
= fdisk_new_script_from_file(cf
->cxt
, buf
);
2021 ui_warn(_("Cannot open %s"), buf
);
2023 ui_warnx(_("Failed to parse script file %s"), buf
);
2024 else if (fdisk_apply_script(cf
->cxt
, sc
) != 0)
2025 ui_warnx(_("Failed to apply script %s"), buf
);
2030 fdisk_unref_script(sc
);
2034 static int ui_script_write(struct cfdisk
*cf
)
2036 struct fdisk_script
*sc
= NULL
;
2037 char buf
[PATH_MAX
] = { 0 };
2041 rc
= ui_get_string(cf
, _("Enter script file name: "),
2042 _("The current in-memory partition table will be dumped to the file."),
2048 sc
= fdisk_new_script(cf
->cxt
);
2050 ui_warn(_("Failed to allocate script handler"));
2054 rc
= fdisk_script_read_context(sc
, NULL
);
2056 ui_warnx(_("Failed to read disk layout into script."));
2060 DBG(UI
, ul_debug("writing dump into: '%s'", buf
));
2061 f
= fopen(buf
, "w");
2063 ui_warn(_("Cannot open %s"), buf
);
2068 rc
= fdisk_script_write_file(sc
, f
);
2070 ui_info(_("Disk layout successfully dumped."));
2073 ui_warn(_("Failed to write script %s"), buf
);
2076 fdisk_unref_script(sc
);
2080 /* prints menu with libfdisk labels and waits for users response */
2081 static int ui_create_label(struct cfdisk
*cf
)
2083 struct cfdisk_menuitem
*d
, *cm
;
2084 int rc
= 1, refresh_menu
= 1;
2085 size_t i
= 0, nitems
;
2086 struct fdisk_label
*lb
= NULL
;
2090 DBG(UI
, ul_debug("asking for new disklabe."));
2092 /* create cfdisk menu according to libfdisk labels, note that the
2093 * last cm[] item has to be empty -- so nitems + 1 */
2094 nitems
= fdisk_get_nlabels(cf
->cxt
);
2095 cm
= xcalloc(nitems
+ 1, sizeof(struct cfdisk_menuitem
));
2097 while (fdisk_next_label(cf
->cxt
, &lb
) == 0) {
2098 if (fdisk_label_is_disabled(lb
) ||
2099 fdisk_label_get_type(lb
) == FDISK_DISKLABEL_BSD
)
2101 cm
[i
++].name
= fdisk_label_get_name(lb
);
2106 /* make the new menu active */
2108 cf
->menu
->vertical
= 1;
2109 menu_set_title(cf
->menu
, _("Select label type"));
2111 if (!cf
->zero_start
)
2112 ui_info(_("Device does not contain a recognized partition table."));
2118 ui_hint(_("Select a type to create a new label or press 'L' to load script file."));
2127 if (ui_menu_move(cf
, key
) == 0)
2133 d
= menu_get_menuitem(cf
, cf
->menu
->idx
);
2135 rc
= fdisk_create_disklabel(cf
->cxt
, d
->name
);
2143 rc
= ui_script_read(cf
);
2154 DBG(UI
, ul_debug("create label done [rc=%d] ", rc
));
2159 static int ui_help(void)
2162 static const char *help
[] = {
2163 N_("This is cfdisk, a curses-based disk partitioning program."),
2164 N_("It lets you create, delete, and modify partitions on a block device."),
2166 N_("Command Meaning"),
2167 N_("------- -------"),
2168 N_(" b Toggle bootable flag of the current partition"),
2169 N_(" d Delete the current partition"),
2170 N_(" h Print this screen"),
2171 N_(" n Create new partition from free space"),
2172 N_(" q Quit program without writing partition table"),
2173 N_(" s Fix partitions order (only when in disarray)"),
2174 N_(" t Change the partition type"),
2175 N_(" u Dump disk layout to sfdisk compatible script file"),
2176 N_(" W Write partition table to disk (you must enter uppercase W);"),
2177 N_(" since this might destroy data on the disk, you must either"),
2178 N_(" confirm or deny the write by entering 'yes' or 'no'"),
2179 N_(" x Display/hide extra information about a partition"),
2180 N_("Up Arrow Move cursor to the previous partition"),
2181 N_("Down Arrow Move cursor to the next partition"),
2182 N_("Left Arrow Move cursor to the previous menu item"),
2183 N_("Right Arrow Move cursor to the next menu item"),
2185 N_("Note: All of the commands can be entered with either upper or lower"),
2186 N_("case letters (except for Write)."),
2188 N_("Use lsblk(8) or partx(8) to see more details about the device.")
2192 for (i
= 0; i
< ARRAY_SIZE(help
); i
++)
2193 mvaddstr(i
, 1, _(help
[i
]));
2195 ui_info(_("Press a key to continue."));
2201 /* TODO: use @sz, now 128bytes */
2202 static int main_menu_ignore_keys(struct cfdisk
*cf
, char *ignore
,
2203 size_t sz
__attribute__((__unused__
)))
2205 struct fdisk_partition
*pa
= get_current_partition(cf
);
2210 if (fdisk_partition_is_freespace(pa
)) {
2211 ignore
[i
++] = 'd'; /* delete */
2212 ignore
[i
++] = 't'; /* set type */
2213 ignore
[i
++] = 'b'; /* set bootable */
2214 cf
->menu
->prefkey
= 'n';
2216 cf
->menu
->prefkey
= 'q';
2218 if (!fdisk_is_label(cf
->cxt
, DOS
) &&
2219 !fdisk_is_label(cf
->cxt
, SGI
))
2223 if (!cf
->wrong_order
)
2226 if (fdisk_is_readonly(cf
->cxt
))
2233 /* returns: error: < 0, success: 0, quit: 1 */
2234 static int main_menu_action(struct cfdisk
*cf
, int key
)
2237 int ref
= 0, rc
, org_order
= cf
->wrong_order
;
2238 const char *info
= NULL
, *warn
= NULL
;
2239 struct fdisk_partition
*pa
;
2246 struct cfdisk_menuitem
*d
= menu_get_menuitem(cf
, cf
->menu
->idx
);
2251 } else if (key
!= 'w' && key
!= 'W')
2252 key
= tolower(key
); /* case insensitive except 'W'rite */
2254 DBG(MENU
, ul_debug("main menu action: key=%c", key
));
2256 if (cf
->menu
->ignore
&& strchr(cf
->menu
->ignore
, key
)) {
2257 DBG(MENU
, ul_debug(" ignore '%c'", key
));
2261 pa
= get_current_partition(cf
);
2264 n
= fdisk_partition_get_partno(pa
);
2266 DBG(MENU
, ul_debug("menu action on %p", pa
));
2271 case 'b': /* Bootable flag */
2273 int fl
= fdisk_is_label(cf
->cxt
, DOS
) ? DOS_FLAG_ACTIVE
:
2274 fdisk_is_label(cf
->cxt
, SGI
) ? SGI_FLAG_BOOT
: 0;
2276 if (fl
&& fdisk_toggle_partition_flag(cf
->cxt
, n
, fl
))
2277 warn
= _("Could not toggle the flag.");
2285 case 'd': /* Delete */
2286 if (fdisk_delete_partition(cf
->cxt
, n
) != 0)
2287 warn
= _("Could not delete partition %zu.");
2289 info
= _("Partition %zu has been deleted.");
2292 case 'h': /* Help */
2299 uint64_t start
, size
, dflt_size
, secs
;
2300 struct fdisk_partition
*npa
; /* the new partition */
2301 int expsize
= 0; /* size specified explicitly in sectors */
2303 if (!fdisk_partition_is_freespace(pa
) || !fdisk_partition_has_start(pa
))
2306 /* free space range */
2307 start
= fdisk_partition_get_start(pa
);
2308 size
= dflt_size
= fdisk_partition_get_size(pa
) * fdisk_get_sector_size(cf
->cxt
);
2310 if (ui_get_size(cf
, _("Partition size: "), &size
,
2311 fdisk_get_sector_size(cf
->cxt
),
2312 size
, &expsize
) == -CFDISK_ERR_ESC
)
2315 secs
= size
/ fdisk_get_sector_size(cf
->cxt
);
2317 npa
= fdisk_new_partition();
2321 if (dflt_size
== size
) /* default is to fillin all free space */
2322 fdisk_partition_end_follow_default(npa
, 1);
2324 fdisk_partition_set_size(npa
, secs
);
2327 fdisk_partition_size_explicit(pa
, 1);
2329 fdisk_partition_set_start(npa
, start
);
2330 fdisk_partition_partno_follow_default(npa
, 1);
2331 /* add to disk label -- libfdisk will ask for missing details */
2332 rc
= fdisk_add_partition(cf
->cxt
, npa
, NULL
);
2333 fdisk_unref_partition(npa
);
2338 case 'q': /* Quit */
2340 case 't': /* Type */
2342 struct fdisk_parttype
*t
;
2344 if (fdisk_partition_is_freespace(pa
))
2346 t
= (struct fdisk_parttype
*) fdisk_partition_get_type(pa
);
2347 t
= ui_get_parttype(cf
, t
);
2350 if (t
&& fdisk_set_partition_type(cf
->cxt
, n
, t
) == 0)
2351 info
= _("Changed type of partition %zu.");
2353 info
= _("The type of partition %zu is unchanged.");
2356 case 's': /* Sort */
2357 if (cf
->wrong_order
) {
2358 fdisk_reorder_partitions(cf
->cxt
);
2362 case 'u': /* dUmp */
2363 ui_script_write(cf
);
2365 case 'W': /* Write */
2367 char buf
[64] = { 0 };
2369 if (fdisk_is_readonly(cf
->cxt
)) {
2370 warn
= _("Device is open in read-only mode.");
2374 rc
= ui_get_string(cf
,
2375 _("Are you sure you want to write the partition "
2377 _("Type \"yes\" or \"no\", or press ESC to leave this dialog."),
2381 if (rc
<= 0 || (strcasecmp(buf
, "yes") != 0 &&
2382 strcasecmp(buf
, _("yes")) != 0)) {
2383 info
= _("Did not write partition table to disk.");
2386 rc
= fdisk_write_disklabel(cf
->cxt
);
2388 warn
= _("Failed to write disklabel.");
2390 fdisk_reread_partition_table(cf
->cxt
);
2391 info
= _("The partition table has been altered.");
2410 ui_warnx(warn
, n
+ 1);
2412 ui_info(info
, n
+ 1);
2413 else if (key
== 'n' && cf
->wrong_order
&& org_order
== 0)
2414 ui_info(_("Note that partition table entries are not in disk order now."));
2419 static void ui_resize_refresh(struct cfdisk
*cf
)
2422 menu_refresh_size(cf
);
2428 static void toggle_show_extra(struct cfdisk
*cf
)
2430 if (cf
->show_extra
&& cf
->act_win
) {
2431 wclear(cf
->act_win
);
2434 cf
->show_extra
= cf
->show_extra
? 0 : 1;
2438 DBG(MENU
, ul_debug("extra: %s", cf
->show_extra
? "ENABLED" : "DISABLED" ));
2441 static int ui_run(struct cfdisk
*cf
)
2447 DBG(UI
, ul_debug("start cols=%zu, lines=%zu", ui_cols
, ui_lines
));
2449 if (!fdisk_has_label(cf
->cxt
) || cf
->zero_start
) {
2450 rc
= ui_create_label(cf
);
2452 ui_errx(EXIT_FAILURE
,
2453 _("failed to create a new disklabel"));
2459 rc
= lines_refresh(cf
);
2461 ui_errx(EXIT_FAILURE
, _("failed to read partitions"));
2463 menu_push(cf
, main_menuitems
);
2464 cf
->menu
->ignore_cb
= main_menu_ignore_keys
;
2466 rc
= ui_refresh(cf
);
2473 if (fdisk_is_readonly(cf
->cxt
))
2474 ui_warnx(_("Device is open in read-only mode."));
2475 else if (cf
->wrong_order
)
2476 ui_info(_("Note that partition table entries are not in disk order now."));
2483 /* Note that ncurses getch() returns ERR when interrupted
2484 * by signal, but SLang does not interrupt at all. */
2485 ui_resize_refresh(cf
);
2488 if (ui_menu_move(cf
, key
) == 0)
2491 DBG(UI
, ul_debug("main action key >%c<.", key
));
2495 case '\016': /* ^N */
2496 case 'j': /* Vi-like alternative */
2497 ui_table_goto(cf
, cf
->lines_idx
+ 1);
2500 case '\020': /* ^P */
2501 case 'k': /* Vi-like alternative */
2502 ui_table_goto(cf
, (int) cf
->lines_idx
- 1);
2506 ui_table_goto(cf
, (int) cf
->lines_idx
- cf
->page_sz
);
2510 ui_table_goto(cf
, 0);
2514 ui_table_goto(cf
, cf
->lines_idx
+ cf
->page_sz
);
2518 ui_table_goto(cf
, (int) cf
->nlines
- 1);
2523 rc
= main_menu_action(cf
, 0);
2526 case 'x': /* Extra */
2527 toggle_show_extra(cf
);
2530 rc
= main_menu_action(cf
, key
);
2542 DBG(UI
, ul_debug("end"));
2546 static void __attribute__ ((__noreturn__
)) usage(FILE *out
)
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)"));
2566 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
2569 int main(int argc
, char *argv
[])
2571 const char *diskpath
;
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' },
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
;
2609 colors_init(colormode
, "cfdisk");
2611 fdisk_init_debug(0);
2612 scols_init_debug(0);
2613 cfdisk_init_debug();
2614 cf
->cxt
= fdisk_new_context();
2616 err(EXIT_FAILURE
, _("failed to allocate libfdisk context"));
2618 fdisk_set_ask(cf
->cxt
, ask_callback
, (void *) cf
);
2621 diskpath
= access(DEFAULT_DEVICE
, F_OK
) == 0 ?
2622 DEFAULT_DEVICE
: ALTERNATE_DEVICE
;
2624 diskpath
= argv
[optind
];
2626 rc
= fdisk_assign_device(cf
->cxt
, diskpath
, 0);
2628 rc
= fdisk_assign_device(cf
->cxt
, diskpath
, 1);
2630 err(EXIT_FAILURE
, _("cannot open %s"),
2631 optind
== argc
? DEFAULT_DEVICE
: diskpath
);
2633 /* Don't use err(), warn() from this point */
2638 cfdisk_free_lines(cf
);
2641 fdisk_unref_table(cf
->table
);
2642 #ifdef HAVE_LIBMOUNT
2643 mnt_unref_table(cf
->fstab
);
2644 mnt_unref_table(cf
->mtab
);
2645 mnt_unref_cache(cf
->mntcache
);
2647 rc
= fdisk_deassign_device(cf
->cxt
, cf
->nwrites
== 0);
2648 fdisk_unref_context(cf
->cxt
);
2649 DBG(MISC
, ul_debug("bye! [rc=%d]", rc
));
2650 return rc
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
;