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>
28 # include <libmount.h> /* keep it optional for non-linux systems */
33 #elif defined(HAVE_SLANG_SLANG_H)
34 # include <slang/slang.h>
38 # define _XOPEN_SOURCE 500 /* for inclusion of get_wch */
41 #ifdef HAVE_SLCURSES_H
42 # include <slcurses.h>
43 #elif defined(HAVE_SLANG_SLCURSES_H)
44 # include <slang/slcurses.h>
45 #elif defined(HAVE_NCURSESW_NCURSES_H) && defined(HAVE_WIDECHAR)
46 # include <ncursesw/ncurses.h>
47 #elif defined(HAVE_NCURSES_H)
49 #elif defined(HAVE_NCURSES_NCURSES_H)
50 # include <ncurses/ncurses.h>
59 #include "closestream.h"
70 static const char *default_disks
[] = {
74 #elif defined(__FreeBSD__)
84 #define ARROW_CURSOR_STRING ">> "
85 #define ARROW_CURSOR_DUMMY " "
86 #define ARROW_CURSOR_WIDTH (sizeof(ARROW_CURSOR_STRING) - 1)
89 #define MENU_V_SPADDING 1 /* space around menu item string */
92 #define MENU_H_SPADDING 0 /* space around menu item string */
93 #define MENU_H_BETWEEN 2 /* space between menu items */
94 #define MENU_H_PRESTR "["
95 #define MENU_H_POSTSTR "]"
97 #define MENU_TITLE_PADDING 3
99 #define MENU_H_PRESTR_SZ (sizeof(MENU_H_PRESTR) - 1)
100 #define MENU_H_POSTSTR_SZ (sizeof(MENU_H_POSTSTR) - 1)
102 #define TABLE_START_LINE 4
103 #define MENU_START_LINE (ui_lines - 4) /* The menu maybe use two lines */
104 #define INFO_LINE (ui_lines - 2)
105 #define WARN_LINE INFO_LINE
106 #define HINT_LINE (ui_lines - 1)
108 #define CFDISK_ERR_ESC 5000
111 # define KEY_ESC '\033'
114 # define KEY_DELETE '\177'
128 #ifdef HAVE_USE_DEFAULT_COLORS
129 static const int color_pairs
[][2] = {
130 /* color foreground, background */
131 [CFDISK_CL_WARNING
] = { COLOR_RED
, -1 },
132 [CFDISK_CL_FREESPACE
] = { COLOR_GREEN
, -1 },
133 [CFDISK_CL_INFO
] = { COLOR_BLUE
, -1 }
139 static struct cfdisk_menuitem
*menu_get_menuitem(struct cfdisk
*cf
, size_t idx
);
140 static struct cfdisk_menuitem
*menu_get_menuitem_by_key(struct cfdisk
*cf
, int key
, size_t *idx
);
141 static struct cfdisk_menu
*menu_push(struct cfdisk
*cf
, struct cfdisk_menuitem
*item
);
142 static struct cfdisk_menu
*menu_pop(struct cfdisk
*cf
);
143 static void menu_refresh_size(struct cfdisk
*cf
);
145 static int ui_end(void);
146 static int ui_refresh(struct cfdisk
*cf
);
148 static void ui_warnx(const char *fmt
, ...)
149 __attribute__((__format__ (__printf__
, 1, 2)));
150 static void ui_warn(const char *fmt
, ...)
151 __attribute__((__format__ (__printf__
, 1, 2)));
152 static void ui_info(const char *fmt
, ...)
153 __attribute__((__format__ (__printf__
, 1, 2)));
155 static void ui_draw_menu(struct cfdisk
*cf
);
156 static int ui_menu_move(struct cfdisk
*cf
, int key
);
157 static void ui_menu_resize(struct cfdisk
*cf
);
159 static int ui_get_size(struct cfdisk
*cf
, const char *prompt
, uint64_t *res
,
160 uint64_t low
, uint64_t up
, int *expsize
);
162 static int ui_enabled
;
163 static volatile sig_atomic_t sig_resize
;
164 static volatile sig_atomic_t sig_die
;
166 /* ncurses LINES and COLS may be actual variables or *macros*, but we need
167 * something portable and writable */
168 static size_t ui_lines
;
169 static size_t ui_cols
;
172 struct cfdisk_menuitem
{
173 int key
; /* keyboard shortcut */
174 const char *name
; /* item name */
175 const char *desc
; /* item description (hint) */
181 char *title
; /* optional menu title */
182 struct cfdisk_menuitem
*items
; /* array with menu items */
183 char *ignore
;/* string with keys to ignore */
184 size_t width
; /* maximal width of the menu item */
185 size_t nitems
; /* number of the active menu items */
186 size_t page_sz
;/* when menu longer than screen */
187 size_t idx
; /* the current menu item */
188 int prefkey
;/* preferred menu item */
189 struct cfdisk_menu
*prev
;
191 /* @ignore keys generator */
192 int (*ignore_cb
) (struct cfdisk
*, char *, size_t);
194 unsigned int vertical
: 1; /* enable vertical mode */
198 static struct cfdisk_menuitem main_menuitems
[] = {
199 { 'b', N_("Bootable"), N_("Toggle bootable flag of the current partition") },
200 { 'd', N_("Delete"), N_("Delete the current partition") },
201 { 'r', N_("Resize"), N_("Reduce or enlarge the current partition") },
202 { 'n', N_("New"), N_("Create new partition from free space") },
203 { 'q', N_("Quit"), N_("Quit program without writing changes") },
204 { 't', N_("Type"), N_("Change the partition type") },
205 { 'h', N_("Help"), N_("Print help screen") },
206 { 's', N_("Sort"), N_("Fix partitions order") },
207 { 'W', N_("Write"), N_("Write partition table to disk (this might destroy data)") },
208 { 'u', N_("Dump"), N_("Dump partition table to sfdisk compatible script file") },
212 /* extra partinfo in name:value pairs */
213 struct cfdisk_extra
{
217 struct list_head exs
;
220 /* line and extra partinfo list_head */
222 char *data
; /* line data */
223 struct libscols_table
*extra
; /* extra info ('X') */
224 WINDOW
*w
; /* window with extra info */
227 /* top level control struct */
229 struct fdisk_context
*cxt
; /* libfdisk context */
230 struct fdisk_table
*table
; /* partition table */
231 struct fdisk_table
*original_layout
; /* original on-disk PT */
233 struct cfdisk_menu
*menu
; /* the current menu */
235 int *fields
; /* output columns IDs */
236 size_t nfields
; /* number of columns IDs */
238 char *linesbuf
; /* table as string */
239 size_t linesbufsz
; /* size of the tb_buf */
241 struct cfdisk_line
*lines
; /* list of lines */
243 size_t nlines
; /* number of lines */
244 size_t lines_idx
; /* current line <0..N>, exclude header */
247 unsigned int nwrites
; /* fdisk_write_disklabel() counter */
249 WINDOW
*act_win
; /* the window currently on the screen */
252 struct libmnt_table
*mtab
;
253 struct libmnt_table
*fstab
;
254 struct libmnt_cache
*mntcache
;
256 unsigned int wrong_order
:1, /* PT not in right order */
257 zero_start
:1, /* ignore existing partition table */
258 device_is_used
: 1, /* don't use re-read ioctl */
259 show_extra
:1; /* show extra partinfo */
264 * let's use include/debug.h stuff for cfdisk too
266 static UL_DEBUG_DEFINE_MASK(cfdisk
);
267 UL_DEBUG_DEFINE_MASKNAMES(cfdisk
) = UL_DEBUG_EMPTY_MASKNAMES
;
269 #define CFDISK_DEBUG_INIT (1 << 1)
270 #define CFDISK_DEBUG_UI (1 << 2)
271 #define CFDISK_DEBUG_MENU (1 << 3)
272 #define CFDISK_DEBUG_MISC (1 << 4)
273 #define CFDISK_DEBUG_TABLE (1 << 5)
274 #define CFDISK_DEBUG_ALL 0xFFFF
276 #define DBG(m, x) __UL_DBG(cfdisk, CFDISK_DEBUG_, m, x)
278 static void cfdisk_init_debug(void)
280 __UL_INIT_DEBUG_FROM_ENV(cfdisk
, CFDISK_DEBUG_
, 0, CFDISK_DEBUG
);
283 /* Initialize output columns -- we follow libfdisk fields (usually specific
286 static int cols_init(struct cfdisk
*cf
)
294 return fdisk_label_get_fields_ids(NULL
, cf
->cxt
, &cf
->fields
, &cf
->nfields
);
297 static void die_on_signal(void)
299 DBG(MISC
, ul_debug("die on signal."));
304 static void resize(void)
308 if (ioctl(fileno(stdout
), TIOCGWINSZ
, &ws
) != -1
309 && ws
.ws_row
&& ws
.ws_col
) {
310 ui_lines
= ws
.ws_row
;
313 resizeterm(ws
.ws_row
, ws
.ws_col
);
315 clearok(stdscr
, TRUE
);
319 DBG(UI
, ul_debug("ui: resize refresh ui_cols=%zu, ui_lines=%zu",
324 /* Reads partition in tree-like order from scols
326 static int partition_from_scols(struct fdisk_table
*tb
,
327 struct libscols_line
*ln
)
329 struct fdisk_partition
*pa
= scols_line_get_userdata(ln
);
331 fdisk_table_add_partition(tb
, pa
);
332 fdisk_unref_partition(pa
);
334 if (scols_line_has_children(ln
)) {
335 struct libscols_line
*chln
;
336 struct libscols_iter
*itr
= scols_new_iter(SCOLS_ITER_FORWARD
);
340 while (scols_line_next_child(ln
, itr
, &chln
) == 0)
341 partition_from_scols(tb
, chln
);
342 scols_free_iter(itr
);
347 static char *table_to_string(struct cfdisk
*cf
, struct fdisk_table
*tb
)
349 struct fdisk_partition
*pa
;
350 struct fdisk_label
*lb
;
351 struct fdisk_iter
*itr
;
352 struct libscols_table
*table
= NULL
;
353 struct libscols_iter
*s_itr
= NULL
;
357 struct libscols_line
*ln
, *ln_cont
= NULL
;
359 DBG(TABLE
, ul_debug("convert to string"));
366 lb
= fdisk_get_label(cf
->cxt
, NULL
);
369 itr
= fdisk_new_iter(FDISK_ITER_FORWARD
);
373 /* get container (e.g. extended partition) */
374 while (fdisk_table_next_partition(tb
, itr
, &pa
) == 0) {
375 if (fdisk_partition_is_nested(pa
)) {
376 DBG(TABLE
, ul_debug("nested detected, using tree"));
377 tree
= SCOLS_FL_TREE
;
382 table
= scols_new_table();
385 scols_table_enable_maxout(table
, 1);
386 scols_table_enable_nowrap(table
, 1);
388 #if !defined(HAVE_LIBNCURSESW) || !defined(HAVE_WIDECHAR)
389 scols_table_enable_ascii(table
, 1);
393 for (i
= 0; i
< cf
->nfields
; i
++) {
395 const struct fdisk_field
*field
=
396 fdisk_label_get_field(lb
, cf
->fields
[i
]);
400 if (fdisk_field_is_number(field
))
401 fl
|= SCOLS_FL_RIGHT
;
402 if (fdisk_field_get_id(field
) == FDISK_FIELD_TYPE
)
403 fl
|= SCOLS_FL_TRUNC
;
404 if (tree
&& fdisk_field_get_id(field
) == FDISK_FIELD_DEVICE
)
407 if (!scols_table_new_column(table
,
408 _(fdisk_field_get_name(field
)),
409 fdisk_field_get_width(field
), fl
))
414 fdisk_reset_iter(itr
, FDISK_ITER_FORWARD
);
416 while (fdisk_table_next_partition(tb
, itr
, &pa
) == 0) {
417 struct libscols_line
*parent
= fdisk_partition_is_nested(pa
) ? ln_cont
: NULL
;
419 ln
= scols_table_new_line(table
, parent
);
422 for (i
= 0; i
< cf
->nfields
; i
++) {
425 if (fdisk_partition_to_string(pa
, cf
->cxt
,
426 cf
->fields
[i
], &cdata
))
428 scols_line_refer_data(ln
, i
, cdata
);
430 if (tree
&& fdisk_partition_is_container(pa
))
433 scols_line_set_userdata(ln
, (void *) pa
);
434 fdisk_ref_partition(pa
);
437 if (scols_table_is_empty(table
))
440 scols_table_reduce_termwidth(table
, ARROW_CURSOR_WIDTH
);
441 scols_print_table_to_string(table
, &res
);
443 /* scols_* code might reorder lines, let's reorder @tb according to the
444 * final output (it's no problem because partitions are addressed by
445 * parno stored within struct fdisk_partition) */
448 fdisk_reset_table(tb
);
450 s_itr
= scols_new_iter(SCOLS_ITER_FORWARD
);
454 /* add all in the right order (don't forget the output is tree) */
455 while (scols_table_next_line(table
, s_itr
, &ln
) == 0) {
456 if (scols_line_get_parent(ln
))
458 if (partition_from_scols(tb
, ln
))
462 scols_unref_table(table
);
463 scols_free_iter(s_itr
);
464 fdisk_free_iter(itr
);
469 static void cfdisk_free_lines(struct cfdisk
*cf
)
472 while(i
< cf
->nlines
) {
473 scols_unref_table(cf
->lines
[i
].extra
);
475 DBG(UI
, ul_debug("delete window: %p",
479 delwin(cf
->lines
[i
].w
);
480 cf
->lines
[i
].w
= NULL
;
488 * Read data about partitions from libfdisk and prepare output lines.
490 static int lines_refresh(struct cfdisk
*cf
)
498 DBG(TABLE
, ul_debug("refreshing buffer"));
501 cfdisk_free_lines(cf
);
507 fdisk_unref_table(cf
->table
);
510 /* read partitions and free spaces into cf->table */
511 rc
= fdisk_get_partitions(cf
->cxt
, &cf
->table
);
513 rc
= fdisk_get_freespaces(cf
->cxt
, &cf
->table
);
517 cf
->linesbuf
= table_to_string(cf
, cf
->table
);
521 cf
->linesbufsz
= strlen(cf
->linesbuf
);
522 cf
->nlines
= fdisk_table_get_nents(cf
->table
) + 1; /* 1 for header line */
524 cf
->wrong_order
= fdisk_table_wrong_order(cf
->table
) ? 1 : 0;
526 if (MENU_START_LINE
- TABLE_START_LINE
< cf
->nlines
)
527 cf
->page_sz
= MENU_START_LINE
- TABLE_START_LINE
- 1;
529 cf
->lines
= xcalloc(cf
->nlines
, sizeof(struct cfdisk_line
));
531 for (p
= cf
->linesbuf
, i
= 0; p
&& i
< cf
->nlines
; i
++) {
532 cf
->lines
[i
].data
= p
;
538 cf
->lines
[i
].extra
= scols_new_table();
539 scols_table_enable_noheadings(cf
->lines
[i
].extra
, 1);
540 scols_table_new_column(cf
->lines
[i
].extra
, NULL
, 0, SCOLS_FL_RIGHT
);
541 scols_table_new_column(cf
->lines
[i
].extra
, NULL
, 0, SCOLS_FL_TRUNC
);
547 static struct fdisk_partition
*get_current_partition(struct cfdisk
*cf
)
552 return fdisk_table_get_partition(cf
->table
, cf
->lines_idx
);
555 static int is_freespace(struct cfdisk
*cf
, size_t i
)
557 struct fdisk_partition
*pa
;
562 pa
= fdisk_table_get_partition(cf
->table
, i
);
563 return fdisk_partition_is_freespace(pa
);
566 /* converts libfdisk FDISK_ASKTYPE_MENU to cfdisk menu and returns user's
567 * response back to libfdisk
569 static int ask_menu(struct fdisk_ask
*ask
, struct cfdisk
*cf
)
571 struct cfdisk_menuitem
*d
, *cm
;
573 size_t i
= 0, nitems
;
574 const char *name
, *desc
;
579 /* create cfdisk menu according to libfdisk ask-menu, note that the
580 * last cm[] item has to be empty -- so nitems + 1 */
581 nitems
= fdisk_ask_menu_get_nitems(ask
);
582 cm
= xcalloc(nitems
+ 1, sizeof(struct cfdisk_menuitem
));
584 for (i
= 0; i
< nitems
; i
++) {
585 if (fdisk_ask_menu_get_item(ask
, i
, &key
, &name
, &desc
))
592 /* make the new menu active */
605 if (ui_menu_move(cf
, key
) == 0)
612 d
= menu_get_menuitem(cf
, cf
->menu
->idx
);
614 fdisk_ask_menu_set_result(ask
, d
->key
);
631 static int ask_callback(struct fdisk_context
*cxt
__attribute__((__unused__
)),
632 struct fdisk_ask
*ask
,
633 void *data
__attribute__((__unused__
)))
639 switch(fdisk_ask_get_type(ask
)) {
640 case FDISK_ASKTYPE_INFO
:
641 ui_info("%s", fdisk_ask_print_get_mesg(ask
));
643 case FDISK_ASKTYPE_WARNX
:
644 ui_warnx("%s", fdisk_ask_print_get_mesg(ask
));
646 case FDISK_ASKTYPE_WARN
:
647 ui_warn("%s", fdisk_ask_print_get_mesg(ask
));
649 case FDISK_ASKTYPE_MENU
:
650 ask_menu(ask
, (struct cfdisk
*) data
);
653 ui_warnx(_("internal error: unsupported dialog type %d"),
654 fdisk_ask_get_type(ask
));
660 static int ui_end(void)
665 #if defined(HAVE_SLCURSES_H) || defined(HAVE_SLANG_SLCURSES_H)
666 SLsmg_gotorc(ui_lines
- 1, 0);
669 mvcur(0, ui_cols
- 1, ui_lines
-1, 0);
679 static void __attribute__((__format__ (__printf__
, 3, 0)))
680 ui_vprint_center(size_t line
, int attrs
, const char *fmt
, va_list ap
)
688 xvasprintf(&buf
, fmt
, ap
);
690 width
= mbs_safe_width(buf
);
691 if (width
> (size_t) ui_cols
) {
692 char *p
= strrchr(buf
+ ui_cols
, ' ');
696 if (line
+ 1 >= ui_lines
)
699 mvaddstr(line
, 0, buf
);
700 mvaddstr(line
+ 1, 0, p
+1);
704 mvaddstr(line
, (ui_cols
- width
) / 2, buf
);
710 static void __attribute__((__format__ (__printf__
, 2, 3)))
711 ui_center(size_t line
, const char *fmt
, ...)
715 ui_vprint_center(line
, 0, fmt
, ap
);
719 static void __attribute__((__format__ (__printf__
, 1, 2)))
720 ui_warnx(const char *fmt
, ...)
725 ui_vprint_center(WARN_LINE
,
726 colors_wanted() ? COLOR_PAIR(CFDISK_CL_WARNING
) : 0,
729 vfprintf(stderr
, fmt
, ap
);
735 static void __attribute__((__format__ (__printf__
, 1, 2)))
736 ui_warn(const char *fmt
, ...)
741 xasprintf(&fmt_m
, "%s: %m", fmt
);
745 ui_vprint_center(WARN_LINE
,
746 colors_wanted() ? COLOR_PAIR(CFDISK_CL_WARNING
) : 0,
749 vfprintf(stderr
, fmt_m
, ap
);
756 static void ui_clean_warn(void)
762 static int __attribute__((__noreturn__
))
763 __attribute__((__format__ (__printf__
, 2, 3)))
764 ui_err(int rc
, const char *fmt
, ...)
770 fprintf(stderr
, "%s: ", program_invocation_short_name
);
771 vfprintf(stderr
, fmt
, ap
);
772 fprintf(stderr
, ": %s\n", strerror(errno
));
778 static int __attribute__((__noreturn__
))
779 __attribute__((__format__ (__printf__
, 2, 3)))
780 ui_errx(int rc
, const char *fmt
, ...)
786 fprintf(stderr
, "%s: ", program_invocation_short_name
);
787 vfprintf(stderr
, fmt
, ap
);
794 static void __attribute__((__format__ (__printf__
, 1, 2)))
795 ui_info(const char *fmt
, ...)
800 ui_vprint_center(INFO_LINE
,
801 colors_wanted() ? COLOR_PAIR(CFDISK_CL_INFO
) : 0,
804 vfprintf(stdout
, fmt
, ap
);
810 static void ui_clean_info(void)
816 static void __attribute__((__format__ (__printf__
, 1, 2)))
817 ui_hint(const char *fmt
, ...)
822 ui_vprint_center(HINT_LINE
, A_BOLD
, fmt
, ap
);
824 vfprintf(stdout
, fmt
, ap
);
830 static void ui_clean_hint(void)
837 static void sig_handler_die(int dummy
__attribute__((__unused__
)))
842 static void sig_handler_resize(int dummy
__attribute__((__unused__
)))
847 static void menu_refresh_size(struct cfdisk
*cf
)
849 if (cf
->menu
&& cf
->menu
->nitems
)
850 cf
->menu
->page_sz
= (cf
->menu
->nitems
/ (ui_lines
- 4)) ? ui_lines
- 4 : 0;
853 static void menu_update_ignore(struct cfdisk
*cf
)
855 char ignore
[128] = { 0 };
857 struct cfdisk_menu
*m
;
858 struct cfdisk_menuitem
*d
, *org
= NULL
;
863 assert(cf
->menu
->ignore_cb
);
866 DBG(MENU
, ul_debug("update menu ignored keys"));
868 i
= m
->ignore_cb(cf
, ignore
, sizeof(ignore
));
871 /* return if no change */
872 if ((!m
->ignore
&& !*ignore
)
873 || (m
->ignore
&& *ignore
&& strcmp(m
->ignore
, ignore
) == 0)) {
878 org
= menu_get_menuitem(cf
, m
->idx
);
881 m
->ignore
= xstrdup(ignore
);
884 for (d
= m
->items
; d
->name
; d
++) {
885 if (m
->ignore
&& strchr(m
->ignore
, d
->key
))
890 DBG(MENU
, ul_debug("update menu preferred keys"));
892 /* refresh menu index to be at the same menuitem or go to the first */
893 if (org
&& menu_get_menuitem_by_key(cf
, org
->key
, &idx
))
895 else if (m
->prefkey
&& menu_get_menuitem_by_key(cf
, m
->prefkey
, &idx
))
900 menu_refresh_size(cf
);
903 static struct cfdisk_menu
*menu_push(
905 struct cfdisk_menuitem
*items
)
907 struct cfdisk_menu
*m
= xcalloc(1, sizeof(*m
));
908 struct cfdisk_menuitem
*d
;
912 DBG(MENU
, ul_debug("new menu"));
917 for (d
= m
->items
; d
->name
; d
++) {
918 const char *name
= _(d
->name
);
919 size_t len
= mbs_safe_width(name
);
927 menu_refresh_size(cf
);
931 static struct cfdisk_menu
*menu_pop(struct cfdisk
*cf
)
933 struct cfdisk_menu
*m
= NULL
;
937 DBG(MENU
, ul_debug("pop menu"));
941 free(cf
->menu
->ignore
);
942 free(cf
->menu
->title
);
949 static void menu_set_title(struct cfdisk_menu
*m
, const char *title
)
954 size_t len
= mbs_safe_width(title
);
955 if (len
+ MENU_TITLE_PADDING
> m
->width
)
956 m
->width
= len
+ MENU_TITLE_PADDING
;
957 str
= xstrdup(title
);
963 static int ui_init(struct cfdisk
*cf
__attribute__((__unused__
)))
967 DBG(UI
, ul_debug("init"));
969 /* setup SIGCHLD handler */
970 sigemptyset(&sa
.sa_mask
);
972 sa
.sa_handler
= sig_handler_die
;
973 sigaction(SIGINT
, &sa
, NULL
);
974 sigaction(SIGTERM
, &sa
, NULL
);
976 sa
.sa_handler
= sig_handler_resize
;
977 sigaction(SIGWINCH
, &sa
, NULL
);
982 #ifdef HAVE_USE_DEFAULT_COLORS
983 if (colors_wanted() && has_colors()) {
987 use_default_colors();
988 for (i
= 1; i
< ARRAY_SIZE(color_pairs
); i
++) /* yeah, start from 1! */
989 init_pair(i
, color_pairs
[i
][0], color_pairs
[i
][1]);
999 keypad(stdscr
, TRUE
);
1005 #define MENU_H_ITEMWIDTH(m) ( MENU_H_PRESTR_SZ \
1009 + MENU_H_POSTSTR_SZ)
1011 #define MENU_V_ITEMWIDTH(m) (MENU_V_SPADDING + (m)->width + MENU_V_SPADDING)
1014 static size_t menuitem_get_line(struct cfdisk
*cf
, size_t idx
)
1016 struct cfdisk_menu
*m
= cf
->menu
;
1019 if (!m
->page_sz
) /* small menu */
1020 return (ui_lines
- (cf
->menu
->nitems
+ 1)) / 2 + idx
;
1021 return (idx
% m
->page_sz
) + 1;
1025 size_t len
= MENU_H_ITEMWIDTH(m
) + MENU_H_BETWEEN
; /** item width */
1026 size_t items
= ui_cols
/ len
; /* items per line */
1030 return MENU_START_LINE
+ ((idx
/ items
));
1034 static int menuitem_get_column(struct cfdisk
*cf
, size_t idx
)
1036 if (cf
->menu
->vertical
) {
1037 size_t nc
= MENU_V_ITEMWIDTH(cf
->menu
);
1038 if ((size_t) ui_cols
<= nc
)
1040 return (ui_cols
- nc
) / 2;
1044 size_t len
= MENU_H_ITEMWIDTH(cf
->menu
) + MENU_H_BETWEEN
; /* item width */
1045 size_t items
= ui_cols
/ len
; /* items per line */
1046 size_t extra
= items
< cf
->menu
->nitems
? /* extra space on line */
1047 ui_cols
% len
: /* - multi-line menu */
1048 ui_cols
- (cf
->menu
->nitems
* len
); /* - one line menu */
1051 return 0; /* hmm... no space */
1053 extra
+= MENU_H_BETWEEN
; /* add padding after last item to extra */
1056 return (idx
* len
) + (extra
/ 2);
1057 return ((idx
% items
) * len
) + (extra
/ 2);
1061 static int menuitem_on_page(struct cfdisk
*cf
, size_t idx
)
1063 struct cfdisk_menu
*m
= cf
->menu
;
1065 if (m
->page_sz
== 0 ||
1066 m
->idx
/ m
->page_sz
== idx
/ m
->page_sz
)
1071 static struct cfdisk_menuitem
*menu_get_menuitem(struct cfdisk
*cf
, size_t idx
)
1073 struct cfdisk_menuitem
*d
;
1076 for (i
= 0, d
= cf
->menu
->items
; d
->name
; d
++) {
1077 if (cf
->menu
->ignore
&& strchr(cf
->menu
->ignore
, d
->key
))
1086 static struct cfdisk_menuitem
*menu_get_menuitem_by_key(struct cfdisk
*cf
,
1087 int key
, size_t *idx
)
1089 struct cfdisk_menuitem
*d
;
1091 for (*idx
= 0, d
= cf
->menu
->items
; d
->name
; d
++) {
1092 if (cf
->menu
->ignore
&& strchr(cf
->menu
->ignore
, d
->key
))
1102 static void ui_draw_menuitem(struct cfdisk
*cf
,
1103 struct cfdisk_menuitem
*d
,
1109 const size_t buf_sz
= 80 * MB_CUR_MAX
;
1110 int ln
, cl
, vert
= cf
->menu
->vertical
;
1112 if (!menuitem_on_page(cf
, idx
))
1113 return; /* no visible item */
1114 ln
= menuitem_get_line(cf
, idx
);
1115 cl
= menuitem_get_column(cf
, idx
);
1117 ptr
= buf
= xmalloc(buf_sz
);
1120 width
= cf
->menu
->width
+ MENU_V_SPADDING
;
1121 memset(ptr
, ' ', MENU_V_SPADDING
);
1122 ptr
+= MENU_V_SPADDING
;
1124 width
= MENU_H_SPADDING
+ cf
->menu
->width
+ MENU_H_SPADDING
;
1127 mbsalign(name
, ptr
, buf_sz
, &width
,
1128 vert
? MBS_ALIGN_LEFT
: MBS_ALIGN_CENTER
,
1131 DBG(MENU
, ul_debug("menuitem: cl=%d, ln=%d, item='%s'",
1135 mvaddch(ln
, cl
- 1, ACS_VLINE
);
1136 mvaddch(ln
, cl
+ MENU_V_ITEMWIDTH(cf
->menu
), ACS_VLINE
);
1139 if (cf
->menu
->idx
== idx
)
1143 mvprintw(ln
, cl
, "%s", buf
);
1145 mvprintw(ln
, cl
, "%s%s%s", MENU_H_PRESTR
, buf
, MENU_H_POSTSTR
);
1148 if (cf
->menu
->idx
== idx
) {
1151 ui_hint("%s", _(d
->desc
));
1155 static void ui_clean_menu(struct cfdisk
*cf
)
1159 struct cfdisk_menu
*m
= cf
->menu
;
1160 size_t ln
= menuitem_get_line(cf
, 0);
1163 lastline
= ln
+ (m
->page_sz
? m
->page_sz
: m
->nitems
);
1165 lastline
= menuitem_get_line(cf
, m
->nitems
);
1167 for (i
= ln
; i
<= lastline
; i
++) {
1170 DBG(MENU
, ul_debug("clean_menu: line %zu", i
));
1179 static void ui_draw_menu(struct cfdisk
*cf
)
1181 struct cfdisk_menuitem
*d
;
1182 struct cfdisk_menu
*m
;
1184 size_t ln
= menuitem_get_line(cf
, 0);
1190 DBG(MENU
, ul_debug("draw start"));
1196 nlines
= m
->page_sz
? m
->page_sz
: m
->nitems
;
1198 nlines
= menuitem_get_line(cf
, m
->nitems
);
1201 menu_update_ignore(cf
);
1203 while ((d
= menu_get_menuitem(cf
, i
)))
1204 ui_draw_menuitem(cf
, d
, i
++);
1207 size_t cl
= menuitem_get_column(cf
, 0);
1208 size_t curpg
= m
->page_sz
? m
->idx
/ m
->page_sz
: 0;
1210 /* corners and horizontal lines */
1211 mvaddch(ln
- 1, cl
- 1, ACS_ULCORNER
);
1212 mvaddch(ln
+ nlines
, cl
- 1, ACS_LLCORNER
);
1214 for (i
= 0; i
< MENU_V_ITEMWIDTH(m
); i
++) {
1215 mvaddch(ln
- 1, cl
+ i
, ACS_HLINE
);
1216 mvaddch(ln
+ nlines
, cl
+ i
, ACS_HLINE
);
1219 mvaddch(ln
- 1, cl
+ i
, ACS_URCORNER
);
1220 mvaddch(ln
+ nlines
, cl
+ i
, ACS_LRCORNER
);
1222 /* draw also lines around empty lines on last page */
1224 m
->nitems
/ m
->page_sz
== m
->idx
/ m
->page_sz
) {
1225 for (i
= m
->nitems
% m
->page_sz
+ 1; i
<= m
->page_sz
; i
++) {
1226 mvaddch(i
, cl
- 1, ACS_VLINE
);
1227 mvaddch(i
, cl
+ MENU_V_ITEMWIDTH(m
), ACS_VLINE
);
1232 mvprintw(ln
- 1, cl
, " %s ", m
->title
);
1236 mvaddch(ln
- 1, cl
+ MENU_V_ITEMWIDTH(m
) - 2, ACS_UARROW
);
1237 if (m
->page_sz
&& curpg
< m
->nitems
/ m
->page_sz
)
1238 mvaddch(ln
+ nlines
, cl
+ MENU_V_ITEMWIDTH(m
) - 2, ACS_DARROW
);
1241 DBG(MENU
, ul_debug("draw end."));
1244 inline static int extra_insert_pair(struct cfdisk_line
*l
, const char *name
, const char *data
)
1246 struct libscols_line
*lsl
;
1252 if (!data
|| !*data
)
1255 lsl
= scols_table_new_line(l
->extra
, NULL
);
1259 rc
= scols_line_set_data(lsl
, 0, name
);
1261 rc
= scols_line_set_data(lsl
, 1, data
);
1266 #ifndef HAVE_LIBMOUNT
1267 static char *get_mountpoint( struct cfdisk
*cf
__attribute__((unused
)),
1268 const char *tagname
__attribute__((unused
)),
1269 const char *tagdata
__attribute__((unused
)))
1274 static char *get_mountpoint(struct cfdisk
*cf
, const char *tagname
, const char *tagdata
)
1276 struct libmnt_fs
*fs
= NULL
;
1277 char *target
= NULL
;
1283 DBG(UI
, ul_debug("asking for mountpoint [%s=%s]", tagname
, tagdata
));
1286 cf
->mntcache
= mnt_new_cache();
1288 /* 1st try between mounted filesystems */
1290 cf
->mtab
= mnt_new_table();
1292 mnt_table_set_cache(cf
->mtab
, cf
->mntcache
);
1293 mnt_table_parse_mtab(cf
->mtab
, NULL
);
1298 fs
= mnt_table_find_tag(cf
->mtab
, tagname
, tagdata
, MNT_ITER_FORWARD
);
1303 cf
->fstab
= mnt_new_table();
1305 mnt_table_set_cache(cf
->fstab
, cf
->mntcache
);
1306 if (mnt_table_parse_fstab(cf
->fstab
, NULL
) != 0) {
1307 mnt_unref_table(cf
->fstab
);
1313 fs
= mnt_table_find_tag(cf
->fstab
, tagname
, tagdata
, MNT_ITER_FORWARD
);
1319 xasprintf(&target
, _("%s (mounted)"), mnt_fs_get_target(fs
));
1321 target
= xstrdup(mnt_fs_get_target(fs
));
1326 #endif /* HAVE_LIBMOUNT */
1328 static inline int iszero(const char *str
)
1332 for (p
= str
; p
&& *p
== '0'; p
++);
1334 return !p
|| *p
== '\0';
1337 static int has_uuid(struct fdisk_table
*tb
, const char *uuid
)
1339 struct fdisk_partition
*pa
;
1340 struct fdisk_iter
*itr
;
1343 if (!tb
|| !uuid
|| fdisk_table_is_empty(tb
))
1346 itr
= fdisk_new_iter(FDISK_ITER_FORWARD
);
1347 while (rc
== 0 && fdisk_table_next_partition(tb
, itr
, &pa
) == 0) {
1348 const char *x
= fdisk_partition_get_uuid(pa
);
1350 rc
= strcmp(x
, uuid
) == 0;
1352 fdisk_free_iter(itr
);
1356 static void extra_prepare_data(struct cfdisk
*cf
)
1358 struct fdisk_partition
*pa
= get_current_partition(cf
);
1359 struct cfdisk_line
*l
= &cf
->lines
[cf
->lines_idx
];
1361 char *mountpoint
= NULL
;
1363 DBG(UI
, ul_debug("preparing extra data"));
1365 /* string data should not equal an empty string */
1366 if (!fdisk_partition_to_string(pa
, cf
->cxt
, FDISK_FIELD_NAME
, &data
) && data
) {
1367 extra_insert_pair(l
, _("Partition name:"), data
);
1369 mountpoint
= get_mountpoint(cf
, "PARTLABEL", data
);
1373 if (!fdisk_partition_to_string(pa
, cf
->cxt
, FDISK_FIELD_UUID
, &data
) && data
) {
1374 extra_insert_pair(l
, _("Partition UUID:"), data
);
1376 /* Search for mountpoint by PARTUUID= means that we need to
1377 * check fstab and convert PARTUUID to the device name. This is
1378 * unnecessary and overkill for newly created partitions. Let's
1379 * check if the UUID already exist in the old layout, otherwise
1382 if (!mountpoint
&& has_uuid(cf
->original_layout
, data
))
1383 mountpoint
= get_mountpoint(cf
, "PARTUUID", data
);
1387 if (!fdisk_partition_to_string(pa
, cf
->cxt
, FDISK_FIELD_TYPE
, &data
) && data
) {
1388 char *code
= NULL
, *type
= NULL
;
1390 fdisk_partition_to_string(pa
, cf
->cxt
, FDISK_FIELD_TYPEID
, &code
);
1391 xasprintf(&type
, "%s (%s)", data
, code
);
1393 extra_insert_pair(l
, _("Partition type:"), type
);
1399 if (!fdisk_partition_to_string(pa
, cf
->cxt
, FDISK_FIELD_ATTR
, &data
) && data
) {
1400 extra_insert_pair(l
, _("Attributes:"), data
);
1404 /* for numeric data, only show non-zero rows */
1405 if (!fdisk_partition_to_string(pa
, cf
->cxt
, FDISK_FIELD_BSIZE
, &data
) && data
) {
1407 extra_insert_pair(l
, "BSIZE:", data
);
1411 if (!fdisk_partition_to_string(pa
, cf
->cxt
, FDISK_FIELD_CPG
, &data
) && data
) {
1413 extra_insert_pair(l
, "CPG:", data
);
1417 if (!fdisk_partition_to_string(pa
, cf
->cxt
, FDISK_FIELD_FSIZE
, &data
) && data
) {
1419 extra_insert_pair(l
, "FSIZE:", data
);
1423 if (!fdisk_partition_to_string(pa
, cf
->cxt
, FDISK_FIELD_FSUUID
, &data
) && data
) {
1424 extra_insert_pair(l
, _("Filesystem UUID:"), data
);
1426 mountpoint
= get_mountpoint(cf
, "UUID", data
);
1430 if (!fdisk_partition_to_string(pa
, cf
->cxt
, FDISK_FIELD_FSLABEL
, &data
) && data
) {
1431 extra_insert_pair(l
, _("Filesystem LABEL:"), data
);
1433 mountpoint
= get_mountpoint(cf
, "LABEL", data
);
1436 if (!fdisk_partition_to_string(pa
, cf
->cxt
, FDISK_FIELD_FSTYPE
, &data
) && data
) {
1437 extra_insert_pair(l
, _("Filesystem:"), data
);
1442 extra_insert_pair(l
, _("Mountpoint:"), mountpoint
);
1447 static int ui_draw_extra(struct cfdisk
*cf
)
1451 struct cfdisk_line
*ln
= &cf
->lines
[cf
->lines_idx
];
1452 char *tbstr
= NULL
, *end
;
1453 int win_ex_start_line
, win_height
, tblen
;
1456 if (!cf
->show_extra
)
1459 DBG(UI
, ul_debug("draw extra"));
1464 wclear(cf
->act_win
);
1468 if (scols_table_is_empty(ln
->extra
)) {
1469 extra_prepare_data(cf
);
1470 if (scols_table_is_empty(ln
->extra
))
1474 ndatalines
= fdisk_table_get_nents(cf
->table
) + 1;
1476 /* nents + header + one free line */
1477 win_ex_start_line
= TABLE_START_LINE
+ ndatalines
;
1478 win_height
= MENU_START_LINE
- win_ex_start_line
;
1479 tblen
= scols_table_get_nlines(ln
->extra
);
1481 /* we can't get a single line of data under the partlist*/
1485 /* number of data lines + 2 for top/bottom lines */
1486 win_height
= win_height
< tblen
+ 2 ? win_height
: tblen
+ 2;
1488 if ((size_t) win_ex_start_line
+ win_height
+ 1 < MENU_START_LINE
)
1489 win_ex_start_line
= MENU_START_LINE
- win_height
;
1491 win_ex
= subwin(stdscr
, win_height
, ui_cols
- 2, win_ex_start_line
, 1);
1493 scols_table_reduce_termwidth(ln
->extra
, 4);
1494 scols_print_table_to_string(ln
->extra
, &tbstr
);
1497 while ((end
= strchr(end
, '\n')))
1503 while (--win_height
> 1) {
1504 mvwaddstr(win_ex
, wline
++, 1 /* window column*/, tbstr
);
1505 tbstr
+= strlen(tbstr
) + 1;
1512 DBG(UI
, ul_debug("draw window: %p", win_ex
));
1516 cf
->act_win
= ln
->w
= win_ex
;
1520 static void ui_menu_goto(struct cfdisk
*cf
, int where
)
1522 struct cfdisk_menuitem
*d
;
1525 /* stop and begin/end for vertical menus */
1526 if (cf
->menu
->vertical
) {
1529 else if (where
> (int) cf
->menu
->nitems
- 1)
1530 where
= cf
->menu
->nitems
- 1;
1532 /* continue from begin/end */
1534 where
= cf
->menu
->nitems
- 1;
1535 else if ((size_t) where
> cf
->menu
->nitems
- 1)
1538 if ((size_t) where
== cf
->menu
->idx
)
1543 old
= cf
->menu
->idx
;
1544 cf
->menu
->idx
= where
;
1546 if (!menuitem_on_page(cf
, old
)) {
1551 d
= menu_get_menuitem(cf
, old
);
1552 ui_draw_menuitem(cf
, d
, old
);
1554 d
= menu_get_menuitem(cf
, where
);
1555 ui_draw_menuitem(cf
, d
, where
);
1559 static int ui_menu_move(struct cfdisk
*cf
, int key
)
1561 struct cfdisk_menu
*m
;
1566 if (key
== (int) ERR
)
1567 return 0; /* ignore errors */
1571 DBG(MENU
, ul_debug("menu move key >%c<.", key
));
1577 case '\016': /* ^N */
1578 case 'j': /* Vi-like alternative */
1579 ui_menu_goto(cf
, m
->idx
+ 1);
1582 case '\020': /* ^P */
1583 case 'k': /* Vi-like alternative */
1584 ui_menu_goto(cf
, (int) m
->idx
- 1);
1588 ui_menu_goto(cf
, (int) m
->idx
- m
->page_sz
);
1593 ui_menu_goto(cf
, 0);
1597 ui_menu_goto(cf
, m
->idx
+ m
->page_sz
);
1602 ui_menu_goto(cf
, m
->nitems
);
1609 ui_menu_goto(cf
, m
->idx
+ 1);
1615 ui_menu_goto(cf
, (int) m
->idx
- 1);
1620 if (key
== '\014') { /* ^L refresh */
1625 DBG(MENU
, ul_debug(" no menu move key"));
1629 /* but don't call me from ui_run(), this is for pop-up menus only */
1630 static void ui_menu_resize(struct cfdisk
*cf
)
1632 DBG(MENU
, ul_debug("menu resize/refresh"));
1635 menu_refresh_size(cf
);
1640 static int partition_on_page(struct cfdisk
*cf
, size_t i
)
1642 if (cf
->page_sz
== 0 ||
1643 cf
->lines_idx
/ cf
->page_sz
== i
/ cf
->page_sz
)
1648 static void ui_draw_partition(struct cfdisk
*cf
, size_t i
)
1650 int ln
= TABLE_START_LINE
+ 1 + i
; /* skip table header */
1651 int cl
= ARROW_CURSOR_WIDTH
; /* we need extra space for cursor */
1652 int cur
= cf
->lines_idx
== i
;
1656 if (!partition_on_page(cf
, i
))
1658 ln
= TABLE_START_LINE
+ (i
% cf
->page_sz
) + 1;
1659 curpg
= cf
->lines_idx
/ cf
->page_sz
;
1663 "draw partition %zu [page_sz=%zu, "
1664 "line=%d, idx=%zu]",
1665 i
, cf
->page_sz
, ln
, cf
->lines_idx
));
1669 mvaddstr(ln
, 0, ARROW_CURSOR_STRING
);
1670 mvaddstr(ln
, cl
, cf
->lines
[i
+ 1].data
);
1675 if (colors_wanted() && is_freespace(cf
, i
)) {
1676 attron(COLOR_PAIR(CFDISK_CL_FREESPACE
));
1679 mvaddstr(ln
, 0, ARROW_CURSOR_DUMMY
);
1680 mvaddstr(ln
, cl
, cf
->lines
[i
+ 1].data
);
1682 attroff(COLOR_PAIR(CFDISK_CL_FREESPACE
));
1685 if ((size_t) ln
== MENU_START_LINE
- 1 &&
1686 cf
->page_sz
&& curpg
< cf
->nlines
/ cf
->page_sz
) {
1689 mvaddch(ln
, ui_cols
- 1, ACS_DARROW
);
1690 mvaddch(ln
, 0, ACS_DARROW
);
1697 static int ui_draw_table(struct cfdisk
*cf
)
1699 int cl
= ARROW_CURSOR_WIDTH
;
1700 size_t i
, nparts
= fdisk_table_get_nents(cf
->table
);
1701 size_t curpg
= cf
->page_sz
? cf
->lines_idx
/ cf
->page_sz
: 0;
1703 DBG(UI
, ul_debug("draw table"));
1705 for (i
= TABLE_START_LINE
; i
<= TABLE_START_LINE
+ cf
->page_sz
; i
++) {
1710 if (nparts
== 0 || (size_t) cf
->lines_idx
> nparts
- 1)
1711 cf
->lines_idx
= nparts
? nparts
- 1 : 0;
1715 mvaddstr(TABLE_START_LINE
, cl
, cf
->lines
[0].data
);
1718 /* print partitions */
1719 for (i
= 0; i
< nparts
; i
++)
1720 ui_draw_partition(cf
, i
);
1723 mvaddch(TABLE_START_LINE
, ui_cols
- 1, ACS_UARROW
);
1724 mvaddch(TABLE_START_LINE
, 0, ACS_UARROW
);
1726 if (cf
->page_sz
&& curpg
< cf
->nlines
/ cf
->page_sz
) {
1727 mvaddch(MENU_START_LINE
- 1, ui_cols
- 1, ACS_DARROW
);
1728 mvaddch(MENU_START_LINE
- 1, 0, ACS_DARROW
);
1733 static int ui_table_goto(struct cfdisk
*cf
, int where
)
1736 size_t nparts
= fdisk_table_get_nents(cf
->table
);
1738 DBG(UI
, ul_debug("goto table %d", where
));
1742 else if ((size_t) where
> nparts
- 1)
1745 if ((size_t) where
== cf
->lines_idx
)
1748 old
= cf
->lines_idx
;
1749 cf
->lines_idx
= where
;
1751 if (!partition_on_page(cf
, old
) ||!partition_on_page(cf
, where
))
1754 ui_draw_partition(cf
, old
); /* cleanup old */
1755 ui_draw_partition(cf
, where
); /* draw new */
1765 static int ui_refresh(struct cfdisk
*cf
)
1767 struct fdisk_label
*lb
;
1769 uint64_t bytes
= fdisk_get_nsectors(cf
->cxt
) * fdisk_get_sector_size(cf
->cxt
);
1775 strsz
= size_to_human_string(SIZE_DECIMAL_2DIGITS
1777 | SIZE_SUFFIX_3LETTER
, bytes
);
1779 lb
= fdisk_get_label(cf
->cxt
, NULL
);
1786 ui_center(0, _("Disk: %s"), fdisk_get_devname(cf
->cxt
));
1788 ui_center(1, _("Size: %s, %"PRIu64
" bytes, %ju sectors"),
1789 strsz
, bytes
, (uintmax_t) fdisk_get_nsectors(cf
->cxt
));
1790 if (fdisk_get_disklabel_id(cf
->cxt
, &id
) == 0 && id
)
1791 ui_center(2, _("Label: %s, identifier: %s"),
1792 fdisk_label_get_name(lb
), id
);
1794 ui_center(2, _("Label: %s"), fdisk_label_get_name(lb
));
1804 static ssize_t
ui_get_string(const char *prompt
,
1805 const char *hint
, char *buf
, size_t len
)
1807 int ln
= MENU_START_LINE
, cl
= 1;
1809 struct mbs_editor
*edit
;
1811 DBG(UI
, ul_debug("ui get string"));
1823 mvaddstr(ln
, cl
, prompt
);
1824 cl
+= mbs_safe_width(prompt
);
1827 edit
= mbs_new_edit(buf
, len
, ui_cols
- cl
);
1831 mbs_edit_goto(edit
, MBS_EDIT_END
);
1834 ui_hint("%s", hint
);
1841 wint_t c
; /* we have fallback in widechar.h */
1845 mvaddstr(ln
, cl
, edit
->buf
);
1846 move(ln
, cl
+ edit
->cursor_cells
);
1849 #if !defined(HAVE_SLCURSES_H) && !defined(HAVE_SLANG_SLCURSES_H) && \
1850 defined(HAVE_LIBNCURSESW) && defined(HAVE_WIDECHAR)
1851 if (get_wch(&c
) == ERR
) {
1853 if ((c
= getch()) == (wint_t) ERR
) {
1861 if (!isatty(STDIN_FILENO
))
1867 DBG(UI
, ul_debug("ui get string: key=%lc", c
));
1869 if (c
== '\r' || c
== '\n' || c
== KEY_ENTER
)
1876 rc
= -CFDISK_ERR_ESC
;
1879 rc
= mbs_edit_goto(edit
, MBS_EDIT_LEFT
);
1882 rc
= mbs_edit_goto(edit
, MBS_EDIT_RIGHT
);
1885 rc
= mbs_edit_goto(edit
, MBS_EDIT_END
);
1888 rc
= mbs_edit_goto(edit
, MBS_EDIT_HOME
);
1894 rc
= mbs_edit_delete(edit
);
1899 rc
= mbs_edit_backspace(edit
);
1902 rc
= mbs_edit_insert(edit
, c
);
1912 rc
= strlen(edit
->buf
); /* success */
1918 mbs_free_edit(edit
);
1923 static int ui_get_size(struct cfdisk
*cf
, /* context */
1924 const char *prompt
, /* UI dialog string */
1925 uint64_t *res
, /* result in bytes */
1926 uint64_t low
, /* minimal size */
1927 uint64_t up
, /* maximal size */
1928 int *expsize
) /* explicitly specified size */
1933 char *dflt
= size_to_human_string(0, *res
);
1935 DBG(UI
, ul_debug("get_size (default=%"PRIu64
")", *res
));
1939 snprintf(buf
, sizeof(buf
), "%s", dflt
);
1942 int pwr
= 0, insec
= 0;
1944 rc
= ui_get_string(prompt
,
1945 _("May be followed by M for MiB, G for GiB, "
1946 "T for TiB, or S for sectors."),
1951 ui_warnx(_("Please, specify size."));
1952 continue; /* nothing specified */
1953 } if (rc
== -CFDISK_ERR_ESC
)
1954 break; /* cancel dialog */
1956 if (strcmp(buf
, dflt
) == 0)
1957 user
= *res
, rc
= 0; /* no change, use default */
1959 size_t len
= strlen(buf
);
1960 if (buf
[len
- 1] == 'S' || buf
[len
- 1] == 's') {
1962 buf
[len
- 1] = '\0';
1964 rc
= parse_size(buf
, (uintmax_t *)&user
, &pwr
); /* parse */
1968 DBG(UI
, ul_debug("get_size user=%"PRIu64
", power=%d, in-sectors=%s",
1969 user
, pwr
, insec
? "yes" : "no"));
1971 user
*= fdisk_get_sector_size(cf
->cxt
);
1973 ui_warnx(_("Minimum size is %"PRIu64
" bytes."), low
);
1976 if (user
> up
&& pwr
&& user
< up
+ (1ULL << pwr
* 10))
1977 /* ignore when the user specified size overflow
1978 * with in range specified by suffix (e.g. MiB) */
1982 ui_warnx(_("Maximum size is %"PRIu64
" bytes."), up
);
1985 if (rc
== 0 && insec
&& expsize
)
1989 ui_warnx(_("Failed to parse size."));
1996 DBG(UI
, ul_debug("get_size (result=%"PRIu64
", rc=%zd)", *res
, rc
));
2000 static struct fdisk_parttype
*ui_get_parttype(struct cfdisk
*cf
,
2001 struct fdisk_parttype
*cur
)
2003 struct cfdisk_menuitem
*d
, *cm
;
2004 size_t i
= 0, nitems
, idx
= 0;
2005 struct fdisk_parttype
*t
= NULL
;
2006 struct fdisk_label
*lb
;
2009 DBG(UI
, ul_debug("asking for parttype."));
2011 lb
= fdisk_get_label(cf
->cxt
, NULL
);
2013 /* create cfdisk menu according to label types, note that the
2014 * last cm[] item has to be empty -- so nitems + 1 */
2015 nitems
= fdisk_label_get_nparttypes(lb
);
2019 cm
= xcalloc(nitems
+ 1, sizeof(struct cfdisk_menuitem
));
2023 codetypes
= fdisk_label_has_code_parttypes(lb
);
2025 for (i
= 0; i
< nitems
; i
++) {
2026 const struct fdisk_parttype
*x
= fdisk_label_get_parttype(lb
, i
);
2029 cm
[i
].userdata
= (void *) x
;
2031 xasprintf(&name
, "%2x %s",
2032 fdisk_parttype_get_code(x
),
2033 _(fdisk_parttype_get_name(x
)));
2035 name
= (char *) _(fdisk_parttype_get_name(x
));
2036 cm
[i
].desc
= fdisk_parttype_get_string(x
);
2043 /* make the new menu active */
2045 cf
->menu
->vertical
= 1;
2046 cf
->menu
->idx
= idx
;
2047 menu_set_title(cf
->menu
, _("Select partition type"));
2058 if (ui_menu_move(cf
, key
) == 0)
2065 d
= menu_get_menuitem(cf
, cf
->menu
->idx
);
2067 t
= (struct fdisk_parttype
*) d
->userdata
;
2081 for (i
= 0; i
< nitems
; i
++)
2082 free((char *) cm
[i
].name
);
2085 DBG(UI
, ul_debug("get parrtype done [type=%s] ", t
?
2086 fdisk_parttype_get_name(t
) : ""));
2090 static int ui_script_read(struct cfdisk
*cf
)
2092 struct fdisk_script
*sc
= NULL
;
2093 char buf
[PATH_MAX
] = { 0 };
2097 rc
= ui_get_string( _("Enter script file name: "),
2098 _("The script file will be applied to in-memory partition table."),
2105 sc
= fdisk_new_script_from_file(cf
->cxt
, buf
);
2107 ui_warn(_("Cannot open %s"), buf
);
2109 ui_warnx(_("Failed to parse script file %s"), buf
);
2110 else if (fdisk_apply_script(cf
->cxt
, sc
) != 0)
2111 ui_warnx(_("Failed to apply script %s"), buf
);
2116 fdisk_unref_script(sc
);
2120 static int ui_script_write(struct cfdisk
*cf
)
2122 struct fdisk_script
*sc
= NULL
;
2123 char buf
[PATH_MAX
] = { 0 };
2127 rc
= ui_get_string( _("Enter script file name: "),
2128 _("The current in-memory partition table will be dumped to the file."),
2134 sc
= fdisk_new_script(cf
->cxt
);
2136 ui_warn(_("Failed to allocate script handler"));
2140 rc
= fdisk_script_read_context(sc
, NULL
);
2142 ui_warnx(_("Failed to read disk layout into script."));
2146 DBG(UI
, ul_debug("writing dump into: '%s'", buf
));
2147 f
= fopen(buf
, "w");
2149 ui_warn(_("Cannot open %s"), buf
);
2154 rc
= fdisk_script_write_file(sc
, f
);
2156 ui_info(_("Disk layout successfully dumped."));
2159 ui_warn(_("Failed to write script %s"), buf
);
2162 fdisk_unref_script(sc
);
2166 /* prints menu with libfdisk labels and waits for users response */
2167 static int ui_create_label(struct cfdisk
*cf
)
2169 struct cfdisk_menuitem
*d
, *cm
;
2170 int rc
= 1, refresh_menu
= 1;
2171 size_t i
= 0, nitems
;
2172 struct fdisk_label
*lb
= NULL
;
2176 DBG(UI
, ul_debug("asking for new disklabe."));
2178 /* create cfdisk menu according to libfdisk labels, note that the
2179 * last cm[] item has to be empty -- so nitems + 1 */
2180 nitems
= fdisk_get_nlabels(cf
->cxt
);
2181 cm
= xcalloc(nitems
+ 1, sizeof(struct cfdisk_menuitem
));
2183 while (fdisk_next_label(cf
->cxt
, &lb
) == 0) {
2184 if (fdisk_label_is_disabled(lb
) ||
2185 fdisk_label_get_type(lb
) == FDISK_DISKLABEL_BSD
)
2187 cm
[i
++].name
= fdisk_label_get_name(lb
);
2192 /* make the new menu active */
2194 cf
->menu
->vertical
= 1;
2195 menu_set_title(cf
->menu
, _("Select label type"));
2197 if (!cf
->zero_start
)
2198 ui_info(_("Device does not contain a recognized partition table."));
2206 ui_hint(_("Select a type to create a new label, press 'L' to load script file, 'Q' quits."));
2217 if (ui_menu_move(cf
, key
) == 0)
2223 d
= menu_get_menuitem(cf
, cf
->menu
->idx
);
2225 rc
= fdisk_create_disklabel(cf
->cxt
, d
->name
);
2233 rc
= ui_script_read(cf
);
2246 DBG(UI
, ul_debug("create label done [rc=%d] ", rc
));
2251 static int ui_help(void)
2254 static const char *help
[] = {
2255 N_("This is cfdisk, a curses-based disk partitioning program."),
2256 N_("It lets you create, delete, and modify partitions on a block device."),
2258 N_("Command Meaning"),
2259 N_("------- -------"),
2260 N_(" b Toggle bootable flag of the current partition"),
2261 N_(" d Delete the current partition"),
2262 N_(" h Print this screen"),
2263 N_(" n Create new partition from free space"),
2264 N_(" q Quit program without writing partition table"),
2265 N_(" r Reduce or enlarge the current partition"),
2266 N_(" s Fix partitions order (only when in disarray)"),
2267 N_(" t Change the partition type"),
2268 N_(" u Dump disk layout to sfdisk compatible script file"),
2269 N_(" W Write partition table to disk (you must enter uppercase W);"),
2270 N_(" since this might destroy data on the disk, you must either"),
2271 N_(" confirm or deny the write by entering 'yes' or 'no'"),
2272 N_(" x Display/hide extra information about a partition"),
2273 N_("Up Arrow Move cursor to the previous partition"),
2274 N_("Down Arrow Move cursor to the next partition"),
2275 N_("Left Arrow Move cursor to the previous menu item"),
2276 N_("Right Arrow Move cursor to the next menu item"),
2278 N_("Note: All of the commands can be entered with either upper or lower"),
2279 N_("case letters (except for Write)."),
2281 N_("Use lsblk(8) or partx(8) to see more details about the device."),
2284 "Copyright (C) 2014-2017 Karel Zak <kzak@redhat.com>"
2288 for (i
= 0; i
< ARRAY_SIZE(help
); i
++)
2289 mvaddstr(i
, 1, _(help
[i
]));
2291 ui_info(_("Press a key to continue."));
2300 /* TODO: use @sz, now 128bytes */
2301 static int main_menu_ignore_keys(struct cfdisk
*cf
, char *ignore
,
2302 size_t sz
__attribute__((__unused__
)))
2304 struct fdisk_partition
*pa
= get_current_partition(cf
);
2309 if (fdisk_partition_is_freespace(pa
)) {
2310 ignore
[i
++] = 'd'; /* delete */
2311 ignore
[i
++] = 't'; /* set type */
2312 ignore
[i
++] = 'b'; /* set bootable */
2313 ignore
[i
++] = 'r'; /* resize */
2314 cf
->menu
->prefkey
= 'n';
2316 cf
->menu
->prefkey
= 'q';
2318 if (!fdisk_is_label(cf
->cxt
, DOS
) &&
2319 !fdisk_is_label(cf
->cxt
, SGI
))
2323 if (!cf
->wrong_order
)
2326 if (fdisk_is_readonly(cf
->cxt
))
2333 /* returns: error: < 0, success: 0, quit: 1 */
2334 static int main_menu_action(struct cfdisk
*cf
, int key
)
2337 int ref
= 0, rc
, org_order
= cf
->wrong_order
;
2338 const char *info
= NULL
, *warn
= NULL
;
2339 struct fdisk_partition
*pa
;
2346 struct cfdisk_menuitem
*d
= menu_get_menuitem(cf
, cf
->menu
->idx
);
2351 } else if (key
!= 'w' && key
!= 'W')
2352 key
= tolower(key
); /* case insensitive except 'W'rite */
2354 DBG(MENU
, ul_debug("main menu action: key=%c", key
));
2356 if (cf
->menu
->ignore
&& strchr(cf
->menu
->ignore
, key
)) {
2357 DBG(MENU
, ul_debug(" ignore '%c'", key
));
2361 pa
= get_current_partition(cf
);
2364 n
= fdisk_partition_get_partno(pa
);
2366 DBG(MENU
, ul_debug("menu action on %p", pa
));
2371 case 'b': /* Bootable flag */
2373 int fl
= fdisk_is_label(cf
->cxt
, DOS
) ? DOS_FLAG_ACTIVE
:
2374 fdisk_is_label(cf
->cxt
, SGI
) ? SGI_FLAG_BOOT
: 0;
2376 if (fl
&& fdisk_toggle_partition_flag(cf
->cxt
, n
, fl
))
2377 warn
= _("Could not toggle the flag.");
2385 case 'd': /* Delete */
2386 if (fdisk_delete_partition(cf
->cxt
, n
) != 0)
2387 warn
= _("Could not delete partition %zu.");
2389 info
= _("Partition %zu has been deleted.");
2392 case 'h': /* Help */
2399 uint64_t start
, size
, dflt_size
, secs
, max_size
;
2400 struct fdisk_partition
*npa
; /* the new partition */
2401 int expsize
= 0; /* size specified explicitly in sectors */
2403 if (!fdisk_partition_is_freespace(pa
) || !fdisk_partition_has_start(pa
))
2406 /* free space range */
2407 start
= fdisk_partition_get_start(pa
);
2408 size
= max_size
= dflt_size
= fdisk_partition_get_size(pa
) * fdisk_get_sector_size(cf
->cxt
);
2410 if (ui_get_size(cf
, _("Partition size: "), &size
,
2411 fdisk_get_sector_size(cf
->cxt
),
2412 max_size
, &expsize
) == -CFDISK_ERR_ESC
)
2415 secs
= size
/ fdisk_get_sector_size(cf
->cxt
);
2417 npa
= fdisk_new_partition();
2421 if (dflt_size
== size
) /* default is to fillin all free space */
2422 fdisk_partition_end_follow_default(npa
, 1);
2424 fdisk_partition_set_size(npa
, secs
);
2427 fdisk_partition_size_explicit(pa
, 1);
2429 fdisk_partition_set_start(npa
, start
);
2430 fdisk_partition_partno_follow_default(npa
, 1);
2431 /* add to disk label -- libfdisk will ask for missing details */
2432 rc
= fdisk_add_partition(cf
->cxt
, npa
, NULL
);
2433 fdisk_unref_partition(npa
);
2438 case 'q': /* Quit */
2440 case 't': /* Type */
2442 struct fdisk_parttype
*t
;
2444 if (fdisk_partition_is_freespace(pa
))
2446 t
= (struct fdisk_parttype
*) fdisk_partition_get_type(pa
);
2447 t
= ui_get_parttype(cf
, t
);
2450 if (t
&& fdisk_set_partition_type(cf
->cxt
, n
, t
) == 0)
2451 info
= _("Changed type of partition %zu.");
2453 info
= _("The type of partition %zu is unchanged.");
2456 case 'r': /* resize */
2458 struct fdisk_partition
*npa
, *next
;
2459 uint64_t size
, max_size
, secs
;
2461 if (fdisk_partition_is_freespace(pa
) || !fdisk_partition_has_start(pa
))
2464 size
= fdisk_partition_get_size(pa
);
2466 /* is the next freespace? */
2467 next
= fdisk_table_get_partition(cf
->table
, cf
->lines_idx
+ 1);
2468 if (next
&& fdisk_partition_is_freespace(next
))
2469 size
+= fdisk_partition_get_size(next
);
2471 size
*= fdisk_get_sector_size(cf
->cxt
);
2474 if (ui_get_size(cf
, _("New size: "), &size
,
2475 fdisk_get_sector_size(cf
->cxt
),
2476 max_size
, NULL
) == -CFDISK_ERR_ESC
)
2478 secs
= size
/ fdisk_get_sector_size(cf
->cxt
);
2479 npa
= fdisk_new_partition();
2483 fdisk_partition_set_size(npa
, secs
);
2485 rc
= fdisk_set_partition(cf
->cxt
, n
, npa
);
2486 fdisk_unref_partition(npa
);
2489 info
= _("Partition %zu resized.");
2493 case 's': /* Sort */
2494 if (cf
->wrong_order
) {
2495 fdisk_reorder_partitions(cf
->cxt
);
2499 case 'u': /* dUmp */
2500 ui_script_write(cf
);
2502 case 'W': /* Write */
2504 char buf
[64] = { 0 };
2506 if (fdisk_is_readonly(cf
->cxt
)) {
2507 warn
= _("Device is open in read-only mode.");
2512 _("Are you sure you want to write the partition "
2514 _("Type \"yes\" or \"no\", or press ESC to leave this dialog."),
2518 if (rc
<= 0 || (strcasecmp(buf
, "yes") != 0 &&
2519 strcasecmp(buf
, _("yes")) != 0)) {
2520 info
= _("Did not write partition table to disk.");
2523 rc
= fdisk_write_disklabel(cf
->cxt
);
2525 warn
= _("Failed to write disklabel.");
2527 if (cf
->device_is_used
)
2528 fdisk_reread_changes(cf
->cxt
, cf
->original_layout
);
2530 fdisk_reread_partition_table(cf
->cxt
);
2531 info
= _("The partition table has been altered.");
2550 ui_warnx(warn
, n
+ 1);
2552 ui_info(info
, n
+ 1);
2553 else if (key
== 'n' && cf
->wrong_order
&& org_order
== 0)
2554 ui_info(_("Note that partition table entries are not in disk order now."));
2559 static void ui_resize_refresh(struct cfdisk
*cf
)
2561 DBG(UI
, ul_debug("ui resize/refresh"));
2563 menu_refresh_size(cf
);
2569 static void toggle_show_extra(struct cfdisk
*cf
)
2571 if (cf
->show_extra
&& cf
->act_win
) {
2572 wclear(cf
->act_win
);
2575 cf
->show_extra
= cf
->show_extra
? 0 : 1;
2579 DBG(MENU
, ul_debug("extra: %s", cf
->show_extra
? "ENABLED" : "DISABLED" ));
2582 static int ui_run(struct cfdisk
*cf
)
2588 DBG(UI
, ul_debug("start cols=%zu, lines=%zu", ui_cols
, ui_lines
));
2590 if (fdisk_get_collision(cf
->cxt
)) {
2591 ui_warnx(_("Device already contains a %s signature."), fdisk_get_collision(cf
->cxt
));
2592 if (fdisk_is_readonly(cf
->cxt
)) {
2593 ui_hint(_("Press a key to continue."));
2596 char buf
[64] = { 0 };
2597 rc
= ui_get_string(_("Do you want to remove it? [Y]es/[N]o: "), NULL
,
2599 fdisk_enable_wipe(cf
->cxt
,
2600 rc
> 0 && rpmatch(buf
) == RPMATCH_YES
? 1 : 0);
2604 if (!fdisk_has_label(cf
->cxt
) || cf
->zero_start
) {
2605 rc
= ui_create_label(cf
);
2608 ui_err(EXIT_FAILURE
,
2609 _("failed to create a new disklabel"));
2616 rc
= lines_refresh(cf
);
2618 ui_errx(EXIT_FAILURE
, _("failed to read partitions"));
2620 menu_push(cf
, main_menuitems
);
2621 cf
->menu
->ignore_cb
= main_menu_ignore_keys
;
2623 rc
= ui_refresh(cf
);
2630 if (fdisk_is_readonly(cf
->cxt
))
2631 ui_warnx(_("Device is open in read-only mode. Changes will remain in memory only."));
2632 else if (cf
->device_is_used
)
2633 ui_warnx(_("Device is currently in use, repartitioning is probably a bad idea."));
2634 else if (cf
->wrong_order
)
2635 ui_info(_("Note that partition table entries are not in disk order now."));
2645 /* Note that ncurses getch() returns ERR when interrupted
2646 * by signal, but SLang does not interrupt at all. */
2647 ui_resize_refresh(cf
);
2650 if (key
== '\014') { /* ^L refresh */
2651 ui_resize_refresh(cf
);
2654 if (ui_menu_move(cf
, key
) == 0)
2657 DBG(UI
, ul_debug("main action key >%1$c< [\\0%1$o].", key
));
2661 case '\016': /* ^N */
2662 case 'j': /* Vi-like alternative */
2663 ui_table_goto(cf
, cf
->lines_idx
+ 1);
2666 case '\020': /* ^P */
2667 case 'k': /* Vi-like alternative */
2668 ui_table_goto(cf
, (int) cf
->lines_idx
- 1);
2672 ui_table_goto(cf
, (int) cf
->lines_idx
- cf
->page_sz
);
2677 ui_table_goto(cf
, 0);
2681 ui_table_goto(cf
, cf
->lines_idx
+ cf
->page_sz
);
2686 ui_table_goto(cf
, (int) cf
->nlines
- 1);
2691 rc
= main_menu_action(cf
, 0);
2694 case 'x': /* Extra */
2695 toggle_show_extra(cf
);
2698 rc
= main_menu_action(cf
, key
);
2710 DBG(UI
, ul_debug("end"));
2714 static void __attribute__((__noreturn__
)) usage(void)
2717 fputs(USAGE_HEADER
, out
);
2719 _(" %1$s [options] <disk>\n"), program_invocation_short_name
);
2721 fputs(USAGE_SEPARATOR
, out
);
2722 fputs(_("Display or manipulate a disk partition table.\n"), out
);
2724 fputs(USAGE_OPTIONS
, out
);
2726 _(" -L, --color[=<when>] colorize output (%s, %s or %s)\n"), "auto", "always", "never");
2728 " %s\n", USAGE_COLORS_DEFAULT
);
2729 fputs(_(" -z, --zero start with zeroed partition table\n"), out
);
2731 _(" --lock[=<mode>] use exclusive device lock (%s, %s or %s)\n"), "yes", "no", "nonblock");
2732 fputs(_(" -r, --read-only forced open cfdisk in read-only mode\n"), out
);
2734 fputs(USAGE_SEPARATOR
, out
);
2735 printf(USAGE_HELP_OPTIONS(26));
2737 printf(USAGE_MAN_TAIL("cfdisk(8)"));
2741 int main(int argc
, char *argv
[])
2743 const char *diskpath
= NULL
, *lockmode
= NULL
;
2744 int rc
, c
, colormode
= UL_COLORMODE_UNDEF
;
2746 struct cfdisk _cf
= { .lines_idx
= 0 },
2749 OPT_LOCK
= CHAR_MAX
+ 1
2751 static const struct option longopts
[] = {
2752 { "color", optional_argument
, NULL
, 'L' },
2753 { "lock", optional_argument
, NULL
, OPT_LOCK
},
2754 { "help", no_argument
, NULL
, 'h' },
2755 { "version", no_argument
, NULL
, 'V' },
2756 { "zero", no_argument
, NULL
, 'z' },
2757 { "read-only", no_argument
, NULL
, 'r' },
2758 { NULL
, 0, NULL
, 0 },
2761 setlocale(LC_ALL
, "");
2762 bindtextdomain(PACKAGE
, LOCALEDIR
);
2763 textdomain(PACKAGE
);
2764 close_stdout_atexit();
2766 while((c
= getopt_long(argc
, argv
, "L::hVzr", longopts
, NULL
)) != -1) {
2772 colormode
= UL_COLORMODE_AUTO
;
2774 colormode
= colormode_or_err(optarg
,
2775 _("unsupported color mode"));
2781 print_version(EXIT_SUCCESS
);
2794 errtryhelp(EXIT_FAILURE
);
2798 colors_init(colormode
, "cfdisk");
2800 fdisk_init_debug(0);
2801 scols_init_debug(0);
2802 cfdisk_init_debug();
2803 cf
->cxt
= fdisk_new_context();
2805 err(EXIT_FAILURE
, _("failed to allocate libfdisk context"));
2807 fdisk_set_ask(cf
->cxt
, ask_callback
, (void *) cf
);
2809 if (optind
== argc
) {
2812 for (i
= 0; i
< ARRAY_SIZE(default_disks
); i
++) {
2813 if (access(default_disks
[i
], F_OK
) == 0) {
2814 diskpath
= default_disks
[i
];
2819 diskpath
= default_disks
[0]; /* default, used for "cannot open" */
2821 diskpath
= argv
[optind
];
2823 rc
= fdisk_assign_device(cf
->cxt
, diskpath
, read_only
);
2824 if (rc
== -EACCES
&& read_only
== 0)
2825 rc
= fdisk_assign_device(cf
->cxt
, diskpath
, 1);
2827 err(EXIT_FAILURE
, _("cannot open %s"), diskpath
);
2829 if (!fdisk_is_readonly(cf
->cxt
)) {
2830 if (blkdev_lock(fdisk_get_devfd(cf
->cxt
), diskpath
, lockmode
) != 0)
2831 return EXIT_FAILURE
;
2833 cf
->device_is_used
= fdisk_device_is_used(cf
->cxt
);
2834 fdisk_get_partitions(cf
->cxt
, &cf
->original_layout
);
2837 /* Don't use err(), warn() from this point */
2842 cfdisk_free_lines(cf
);
2846 fdisk_unref_table(cf
->table
);
2847 #ifdef HAVE_LIBMOUNT
2848 mnt_unref_table(cf
->fstab
);
2849 mnt_unref_table(cf
->mtab
);
2850 mnt_unref_cache(cf
->mntcache
);
2852 rc
= fdisk_deassign_device(cf
->cxt
, cf
->nwrites
== 0);
2853 fdisk_unref_context(cf
->cxt
);
2854 DBG(MISC
, ul_debug("bye! [rc=%d]", rc
));
2855 return rc
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
;