2 * cfdisk.c - Display or manipulate a disk partition table.
4 * Copyright (C) 2014 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 #elif defined(HAVE_SLANG_SLANG_H)
29 # include <slang/slang.h>
32 #ifdef HAVE_SLCURSES_H
33 # include <slcurses.h>
34 #elif defined(HAVE_SLANG_SLCURSES_H)
35 # include <slang/slcurses.h>
36 #elif defined(HAVE_NCURSESW_NCURSES_H) && defined(HAVE_WIDECHAR)
37 # include <ncursesw/ncurses.h>
38 #elif defined(HAVE_NCURSES_H)
40 #elif defined(HAVE_NCURSES_NCURSES_H)
41 # include <ncurses/ncurses.h>
50 #include "closestream.h"
59 # define DEFAULT_DEVICE "/dev/hd0"
60 # define ALTERNATE_DEVICE "/dev/sd0"
61 #elif defined(__FreeBSD__)
62 # define DEFAULT_DEVICE "/dev/ad0"
63 # define ALTERNATE_DEVICE "/dev/da0"
65 # define DEFAULT_DEVICE "/dev/sda"
66 # define ALTERNATE_DEVICE "/dev/hda"
69 #define ARROW_CURSOR_STRING ">> "
70 #define ARROW_CURSOR_DUMMY " "
71 #define ARROW_CURSOR_WIDTH (sizeof(ARROW_CURSOR_STRING) - 1)
74 #define MENU_V_SPADDING 1 /* space around menu item string */
77 #define MENU_H_SPADDING 0 /* space around menu item string */
78 #define MENU_H_BETWEEN 2 /* space between menu items */
79 #define MENU_H_PRESTR "["
80 #define MENU_H_POSTSTR "]"
82 #define MENU_TITLE_PADDING 3
84 #define MENU_H_PRESTR_SZ (sizeof(MENU_H_PRESTR) - 1)
85 #define MENU_H_POSTSTR_SZ (sizeof(MENU_H_POSTSTR) - 1)
87 #define TABLE_START_LINE 4
88 #define MENU_START_LINE (ui_lines - 5)
89 #define INFO_LINE (ui_lines - 2)
90 #define WARN_LINE INFO_LINE
91 #define HINT_LINE (ui_lines - 1)
93 #define CFDISK_ERR_ESC 5000
96 # define KEY_ESC '\033'
99 # define KEY_DELETE '\177'
109 static const int color_pairs
[][2] = {
110 /* color foreground, background */
111 [CFDISK_CL_WARNING
] = { COLOR_RED
, -1 },
112 [CFDISK_CL_FREESPACE
] = { COLOR_GREEN
, -1 },
113 [CFDISK_CL_INFO
] = { COLOR_BLUE
, -1 }
118 static struct cfdisk_menuitem
*menu_get_menuitem(struct cfdisk
*cf
, size_t idx
);
119 static struct cfdisk_menuitem
*menu_get_menuitem_by_key(struct cfdisk
*cf
, int key
, size_t *idx
);
120 static struct cfdisk_menu
*menu_push(struct cfdisk
*cf
, struct cfdisk_menuitem
*item
);
121 static struct cfdisk_menu
*menu_pop(struct cfdisk
*cf
);
122 static void menu_refresh_size(struct cfdisk
*cf
);
124 static int ui_refresh(struct cfdisk
*cf
);
125 static void ui_warnx(const char *fmt
, ...);
126 static void ui_warn(const char *fmt
, ...);
127 static void ui_info(const char *fmt
, ...);
128 static void ui_draw_menu(struct cfdisk
*cf
);
129 static int ui_menu_move(struct cfdisk
*cf
, int key
);
130 static void ui_menu_resize(struct cfdisk
*cf
);
132 static int ui_get_size(struct cfdisk
*cf
, const char *prompt
, uintmax_t *res
,
133 uintmax_t low
, uintmax_t up
, int *expsize
);
135 static int ui_enabled
;
136 static int ui_resize
;
138 /* ncurses LINES and COLS may be actual variables or *macros*, but we need
139 * something portable and writable */
144 struct cfdisk_menuitem
{
145 int key
; /* keyboard shortcut */
146 const char *name
; /* item name */
147 const char *desc
; /* item description (hint) */
153 char *title
; /* optional menu title */
154 struct cfdisk_menuitem
*items
; /* array with menu items */
155 char *ignore
;/* string with keys to ignore */
156 size_t width
; /* maximal width of the menu item */
157 size_t nitems
; /* number of the active menu items */
158 size_t page_sz
;/* when menu longer than screen */
159 size_t idx
; /* the current menu item */
160 int prefkey
;/* preferred menu item */
161 struct cfdisk_menu
*prev
;
163 /* @ignore keys generator */
164 int (*ignore_cb
) (struct cfdisk
*, char *, size_t);
166 unsigned int vertical
: 1; /* enable vertical mode */
170 static struct cfdisk_menuitem main_menuitems
[] = {
171 { 'b', N_("Bootable"), N_("Toggle bootable flag of the current partition") },
172 { 'd', N_("Delete"), N_("Delete the current partition") },
173 { 'n', N_("New"), N_("Create new partition from free space") },
174 { 'q', N_("Quit"), N_("Quit program without writing partition table") },
175 { 't', N_("Type"), N_("Change the partition type") },
176 { 'h', N_("Help"), N_("Print help screen") },
177 { 's', N_("Sort"), N_("Fix partitions order") },
178 { 'W', N_("Write"), N_("Write partition table to disk (this might destroy data)") },
179 { 'u', N_("Dump"), N_("Dump partition table to sfdisk compatible script file") },
183 /* top level control struct */
185 struct fdisk_context
*cxt
; /* libfdisk context */
186 struct fdisk_table
*table
; /* partition table */
187 struct cfdisk_menu
*menu
; /* the current menu */
189 int *fields
; /* output columns IDs */
190 size_t nfields
; /* number of columns IDs */
192 char *linesbuf
; /* table as string */
193 size_t linesbufsz
; /* size of the tb_buf */
195 char **lines
; /* array with lines */
196 size_t nlines
; /* number of lines */
197 size_t lines_idx
; /* current line <0..N>, exclude header */
200 unsigned int nwrites
; /* fdisk_write_disklabel() counter */
202 unsigned int wrong_order
:1, /* PT not in right order */
203 zero_start
:1; /* ignore existing partition table */
208 * let's use include/debug.h stuff for cfdisk too
210 UL_DEBUG_DEFINE_MASK(cfdisk
);
211 UL_DEBUG_DEFINE_MASKNAMES(cfdisk
) = UL_DEBUG_EMPTY_MASKNAMES
;
213 #define CFDISK_DEBUG_INIT (1 << 1)
214 #define CFDISK_DEBUG_UI (1 << 2)
215 #define CFDISK_DEBUG_MENU (1 << 3)
216 #define CFDISK_DEBUG_MISC (1 << 4)
217 #define CFDISK_DEBUG_TABLE (1 << 5)
218 #define CFDISK_DEBUG_ALL 0xFFFF
220 #define DBG(m, x) __UL_DBG(cfdisk, CFDISK_DEBUG_, m, x)
222 static void cfdisk_init_debug(void)
224 __UL_INIT_DEBUG(cfdisk
, CFDISK_DEBUG_
, 0, CFDISK_DEBUG
);
227 /* Initialize output columns -- we follow libfdisk fields (usually specific
230 static int cols_init(struct cfdisk
*cf
)
238 return fdisk_label_get_fields_ids(NULL
, cf
->cxt
, &cf
->fields
, &cf
->nfields
);
241 static void resize(void)
245 if (ioctl(fileno(stdout
), TIOCGWINSZ
, &ws
) != -1
246 && ws
.ws_row
&& ws
.ws_col
) {
247 ui_lines
= ws
.ws_row
;
250 resizeterm(ws
.ws_row
, ws
.ws_col
);
252 clearok(stdscr
, TRUE
);
256 DBG(UI
, ul_debug("ui: resize refresh ui_cols=%zu, ui_lines=%zu",
261 /* Reads partition in tree-like order from scols
263 static int partition_from_scols(struct fdisk_table
*tb
,
264 struct libscols_line
*ln
)
266 struct fdisk_partition
*pa
= scols_line_get_userdata(ln
);
268 fdisk_table_add_partition(tb
, pa
);
269 fdisk_unref_partition(pa
);
271 if (scols_line_has_children(ln
)) {
272 struct libscols_line
*chln
;
273 struct libscols_iter
*itr
= scols_new_iter(SCOLS_ITER_FORWARD
);
277 while (scols_line_next_child(ln
, itr
, &chln
) == 0)
278 partition_from_scols(tb
, chln
);
279 scols_free_iter(itr
);
284 static char *table_to_string(struct cfdisk
*cf
, struct fdisk_table
*tb
)
286 struct fdisk_partition
*pa
;
287 struct fdisk_label
*lb
;
288 struct fdisk_iter
*itr
= NULL
;
289 struct libscols_table
*table
= NULL
;
290 struct libscols_iter
*s_itr
= NULL
;
294 struct libscols_line
*ln
, *ln_cont
= NULL
;
296 DBG(TABLE
, ul_debug("convert to string"));
303 lb
= fdisk_get_label(cf
->cxt
, NULL
);
306 itr
= fdisk_new_iter(FDISK_ITER_FORWARD
);
310 /* get container (e.g. extended partition) */
311 while (fdisk_table_next_partition(tb
, itr
, &pa
) == 0) {
312 if (fdisk_partition_is_nested(pa
)) {
313 DBG(TABLE
, ul_debug("nested detected, using tree"));
314 tree
= SCOLS_FL_TREE
;
319 table
= scols_new_table();
322 scols_table_enable_maxout(table
, 1);
325 for (i
= 0; i
< cf
->nfields
; i
++) {
327 const struct fdisk_field
*field
=
328 fdisk_label_get_field(lb
, cf
->fields
[i
]);
332 if (fdisk_field_is_number(field
))
333 fl
|= SCOLS_FL_RIGHT
;
334 if (fdisk_field_get_id(field
) == FDISK_FIELD_TYPE
)
335 fl
|= SCOLS_FL_TRUNC
;
336 if (tree
&& fdisk_field_get_id(field
) == FDISK_FIELD_DEVICE
)
339 if (!scols_table_new_column(table
,
340 _(fdisk_field_get_name(field
)),
341 fdisk_field_get_width(field
), fl
))
346 fdisk_reset_iter(itr
, FDISK_ITER_FORWARD
);
348 while (fdisk_table_next_partition(tb
, itr
, &pa
) == 0) {
349 struct libscols_line
*parent
= fdisk_partition_is_nested(pa
) ? ln_cont
: NULL
;
351 ln
= scols_table_new_line(table
, parent
);
354 for (i
= 0; i
< cf
->nfields
; i
++) {
357 if (fdisk_partition_to_string(pa
, cf
->cxt
,
358 cf
->fields
[i
], &cdata
))
360 scols_line_refer_data(ln
, i
, cdata
);
362 if (tree
&& fdisk_partition_is_container(pa
))
365 scols_line_set_userdata(ln
, (void *) pa
);
366 fdisk_ref_partition(pa
);
369 if (scols_table_is_empty(table
))
372 scols_table_reduce_termwidth(table
, ARROW_CURSOR_WIDTH
);
373 scols_print_table_to_string(table
, &res
);
375 /* scols_* code might reorder lines, let's reorder @tb according to the
376 * final output (it's no problem because partitions are addressed by
377 * parno stored within struct fdisk_partition) */
380 fdisk_reset_iter(itr
, FDISK_ITER_FORWARD
);
381 while (fdisk_table_next_partition(tb
, itr
, &pa
) == 0)
382 fdisk_table_remove_partition(tb
, pa
);
384 s_itr
= scols_new_iter(SCOLS_ITER_FORWARD
);
388 /* add all in the right order (don't forget the output is tree) */
389 while (scols_table_next_line(table
, s_itr
, &ln
) == 0) {
390 if (scols_line_get_parent(ln
))
392 if (partition_from_scols(tb
, ln
))
396 scols_unref_table(table
);
397 scols_free_iter(s_itr
);
398 fdisk_free_iter(itr
);
404 * Read data about partitions from libfdisk and prepare output lines.
406 static int lines_refresh(struct cfdisk
*cf
)
414 DBG(TABLE
, ul_debug("refreshing buffer"));
423 fdisk_unref_table(cf
->table
);
426 /* read partitions and free spaces into cf->table */
427 rc
= fdisk_get_partitions(cf
->cxt
, &cf
->table
);
429 rc
= fdisk_get_freespaces(cf
->cxt
, &cf
->table
);
433 cf
->linesbuf
= table_to_string(cf
, cf
->table
);
437 cf
->linesbufsz
= strlen(cf
->linesbuf
);
438 cf
->nlines
= fdisk_table_get_nents(cf
->table
) + 1; /* 1 for header line */
440 cf
->wrong_order
= fdisk_table_wrong_order(cf
->table
) ? 1 : 0;
442 if (MENU_START_LINE
- TABLE_START_LINE
< cf
->nlines
)
443 cf
->page_sz
= MENU_START_LINE
- TABLE_START_LINE
- 1;
445 cf
->lines
= xcalloc(cf
->nlines
, sizeof(char *));
447 for (p
= cf
->linesbuf
, i
= 0; p
&& i
< cf
->nlines
; i
++) {
459 static struct fdisk_partition
*get_current_partition(struct cfdisk
*cf
)
464 return fdisk_table_get_partition(cf
->table
, cf
->lines_idx
);
467 static int is_freespace(struct cfdisk
*cf
, size_t i
)
469 struct fdisk_partition
*pa
;
474 pa
= fdisk_table_get_partition(cf
->table
, i
);
475 return fdisk_partition_is_freespace(pa
);
478 /* converts libfdisk FDISK_ASKTYPE_MENU to cfdisk menu and returns user's
479 * responseback to libfdisk
481 static int ask_menu(struct fdisk_ask
*ask
, struct cfdisk
*cf
)
483 struct cfdisk_menuitem
*d
, *cm
;
485 size_t i
= 0, nitems
;
486 const char *name
, *desc
;
491 /* create cfdisk menu according to libfdisk ask-menu, note that the
492 * last cm[] item has to be empty -- so nitems + 1 */
493 nitems
= fdisk_ask_menu_get_nitems(ask
);
494 cm
= xcalloc(nitems
+ 1, sizeof(struct cfdisk_menuitem
));
496 for (i
= 0; i
< nitems
; i
++) {
497 if (fdisk_ask_menu_get_item(ask
, i
, &key
, &name
, &desc
))
504 /* make the new menu active */
515 if (ui_menu_move(cf
, key
) == 0)
522 d
= menu_get_menuitem(cf
, cf
->menu
->idx
);
524 fdisk_ask_menu_set_result(ask
, d
->key
);
538 static int ask_callback(struct fdisk_context
*cxt
, struct fdisk_ask
*ask
,
539 void *data
__attribute__((__unused__
)))
546 switch(fdisk_ask_get_type(ask
)) {
547 case FDISK_ASKTYPE_INFO
:
548 ui_info(fdisk_ask_print_get_mesg(ask
));
550 case FDISK_ASKTYPE_WARNX
:
551 ui_warnx(fdisk_ask_print_get_mesg(ask
));
553 case FDISK_ASKTYPE_WARN
:
554 ui_warn(fdisk_ask_print_get_mesg(ask
));
556 case FDISK_ASKTYPE_MENU
:
557 ask_menu(ask
, (struct cfdisk
*) data
);
560 ui_warnx(_("internal error: unsupported dialog type %d"),
561 fdisk_ask_get_type(ask
));
567 static int ui_end(void)
572 #if defined(HAVE_SLCURSES_H) || defined(HAVE_SLANG_SLCURSES_H)
573 SLsmg_gotorc(ui_lines
- 1, 0);
576 mvcur(0, ui_cols
- 1, ui_lines
-1, 0);
586 static void ui_vprint_center(size_t line
, int attrs
, const char *fmt
, va_list ap
)
594 xvasprintf(&buf
, fmt
, ap
);
596 width
= mbs_safe_width(buf
);
597 if (width
> (size_t) ui_cols
) {
598 char *p
= strrchr(buf
+ ui_cols
, ' ');
602 if (line
+ 1 >= ui_lines
)
605 mvaddstr(line
, 0, buf
);
606 mvaddstr(line
+ 1, 0, p
+1);
610 mvaddstr(line
, (ui_cols
- width
) / 2, buf
);
616 static void ui_center(size_t line
, const char *fmt
, ...)
620 ui_vprint_center(line
, 0, fmt
, ap
);
624 static void ui_warnx(const char *fmt
, ...)
629 ui_vprint_center(WARN_LINE
,
630 colors_wanted() ? COLOR_PAIR(CFDISK_CL_WARNING
) : 0,
633 vfprintf(stderr
, fmt
, ap
);
639 static void ui_warn(const char *fmt
, ...)
644 xasprintf(&fmt_m
, "%s: %m", fmt
);
648 ui_vprint_center(WARN_LINE
,
649 colors_wanted() ? COLOR_PAIR(CFDISK_CL_WARNING
) : 0,
652 vfprintf(stderr
, fmt_m
, ap
);
659 static void ui_clean_warn(void)
665 static int __attribute__((__noreturn__
)) ui_errx(int rc
, const char *fmt
, ...)
671 fprintf(stderr
, "%s: ", program_invocation_short_name
);
672 vfprintf(stderr
, fmt
, ap
);
679 static void ui_info(const char *fmt
, ...)
684 ui_vprint_center(INFO_LINE
,
685 colors_wanted() ? COLOR_PAIR(CFDISK_CL_INFO
) : 0,
688 vfprintf(stdout
, fmt
, ap
);
694 static void ui_clean_info(void)
700 static void ui_hint(const char *fmt
, ...)
705 ui_vprint_center(HINT_LINE
, A_BOLD
, fmt
, ap
);
707 vfprintf(stdout
, fmt
, ap
);
713 static void ui_clean_hint(void)
719 static void die_on_signal(int dummy
__attribute__((__unused__
)))
721 DBG(MISC
, ul_debug("die on signal."));
726 static void resize_on_signal(int dummy
__attribute__((__unused__
)))
728 DBG(MISC
, ul_debug("resize on signal."));
732 static void menu_refresh_size(struct cfdisk
*cf
)
734 if (cf
->menu
&& cf
->menu
->nitems
)
735 cf
->menu
->page_sz
= (cf
->menu
->nitems
/ (ui_lines
- 4)) ? ui_lines
- 4 : 0;
738 static void menu_update_ignore(struct cfdisk
*cf
)
740 char ignore
[128] = { 0 };
742 struct cfdisk_menu
*m
;
743 struct cfdisk_menuitem
*d
, *org
= NULL
;
748 assert(cf
->menu
->ignore_cb
);
751 DBG(MENU
, ul_debug("update menu ignored keys"));
753 i
= m
->ignore_cb(cf
, ignore
, sizeof(ignore
));
756 /* return if no change */
757 if ((!m
->ignore
&& !*ignore
)
758 || (m
->ignore
&& *ignore
&& strcmp(m
->ignore
, ignore
) == 0)) {
763 org
= menu_get_menuitem(cf
, m
->idx
);
766 m
->ignore
= xstrdup(ignore
);
769 for (d
= m
->items
; d
->name
; d
++) {
770 if (m
->ignore
&& strchr(m
->ignore
, d
->key
))
775 DBG(MENU
, ul_debug("update menu preferred keys"));
777 /* refresh menu index to be at the same menuitem or go to the first */
778 if (org
&& menu_get_menuitem_by_key(cf
, org
->key
, &idx
))
780 else if (m
->prefkey
&& menu_get_menuitem_by_key(cf
, m
->prefkey
, &idx
))
785 menu_refresh_size(cf
);
788 static struct cfdisk_menu
*menu_push(
790 struct cfdisk_menuitem
*items
)
792 struct cfdisk_menu
*m
= xcalloc(1, sizeof(*m
));
793 struct cfdisk_menuitem
*d
;
797 DBG(MENU
, ul_debug("new menu"));
802 for (d
= m
->items
; d
->name
; d
++) {
803 const char *name
= _(d
->name
);
804 size_t len
= mbs_safe_width(name
);
812 menu_refresh_size(cf
);
816 static struct cfdisk_menu
*menu_pop(struct cfdisk
*cf
)
818 struct cfdisk_menu
*m
= NULL
;
822 DBG(MENU
, ul_debug("pop menu"));
826 free(cf
->menu
->ignore
);
827 free(cf
->menu
->title
);
834 static void menu_set_title(struct cfdisk_menu
*m
, const char *title
)
839 size_t len
= mbs_safe_width(title
);
840 if (len
+ MENU_TITLE_PADDING
> m
->width
)
841 m
->width
= len
+ MENU_TITLE_PADDING
;
842 str
= xstrdup(title
);
848 static int ui_init(struct cfdisk
*cf
__attribute__((__unused__
)))
852 DBG(UI
, ul_debug("init"));
854 /* setup SIGCHLD handler */
855 sigemptyset(&sa
.sa_mask
);
857 sa
.sa_handler
= die_on_signal
;
858 sigaction(SIGINT
, &sa
, NULL
);
859 sigaction(SIGTERM
, &sa
, NULL
);
861 sa
.sa_handler
= resize_on_signal
;
862 sigaction(SIGWINCH
, &sa
, NULL
);
867 #ifdef HAVE_USE_DEFAULT_COLORS
868 if (colors_wanted() && has_colors()) {
872 use_default_colors();
873 for (i
= 1; i
< ARRAY_SIZE(color_pairs
); i
++) /* yeah, start from 1! */
874 init_pair(i
, color_pairs
[i
][0], color_pairs
[i
][1]);
884 keypad(stdscr
, TRUE
);
890 #define MENU_H_ITEMWIDTH(m) ( MENU_H_PRESTR_SZ \
896 #define MENU_V_ITEMWIDTH(m) (MENU_V_SPADDING + (m)->width + MENU_V_SPADDING)
899 static size_t menuitem_get_line(struct cfdisk
*cf
, size_t idx
)
901 struct cfdisk_menu
*m
= cf
->menu
;
904 if (!m
->page_sz
) /* small menu */
905 return (ui_lines
- (cf
->menu
->nitems
+ 1)) / 2 + idx
;
906 return (idx
% m
->page_sz
) + 1;
908 size_t len
= MENU_H_ITEMWIDTH(m
) + MENU_H_BETWEEN
; /** item width */
909 size_t items
= ui_cols
/ len
; /* items per line */
913 return MENU_START_LINE
+ ((idx
/ items
));
917 static int menuitem_get_column(struct cfdisk
*cf
, size_t idx
)
919 if (cf
->menu
->vertical
) {
920 size_t nc
= MENU_V_ITEMWIDTH(cf
->menu
);
921 if ((size_t) ui_cols
<= nc
)
923 return (ui_cols
- nc
) / 2;
925 size_t len
= MENU_H_ITEMWIDTH(cf
->menu
) + MENU_H_BETWEEN
; /* item width */
926 size_t items
= ui_cols
/ len
; /* items per line */
927 size_t extra
= items
< cf
->menu
->nitems
? /* extra space on line */
928 ui_cols
% len
: /* - multi-line menu */
929 ui_cols
- (cf
->menu
->nitems
* len
); /* - one line menu */
932 return 0; /* hmm... no space */
934 extra
+= MENU_H_BETWEEN
; /* add padding after last item to extra */
937 return (idx
* len
) + (extra
/ 2);
938 return ((idx
% items
) * len
) + (extra
/ 2);
942 static int menuitem_on_page(struct cfdisk
*cf
, size_t idx
)
944 struct cfdisk_menu
*m
= cf
->menu
;
946 if (m
->page_sz
== 0 ||
947 m
->idx
/ m
->page_sz
== idx
/ m
->page_sz
)
952 static struct cfdisk_menuitem
*menu_get_menuitem(struct cfdisk
*cf
, size_t idx
)
954 struct cfdisk_menuitem
*d
;
957 for (i
= 0, d
= cf
->menu
->items
; d
->name
; d
++) {
958 if (cf
->menu
->ignore
&& strchr(cf
->menu
->ignore
, d
->key
))
967 static struct cfdisk_menuitem
*menu_get_menuitem_by_key(struct cfdisk
*cf
,
968 int key
, size_t *idx
)
970 struct cfdisk_menuitem
*d
;
972 for (*idx
= 0, d
= cf
->menu
->items
; d
->name
; d
++) {
973 if (cf
->menu
->ignore
&& strchr(cf
->menu
->ignore
, d
->key
))
983 static void ui_draw_menuitem(struct cfdisk
*cf
,
984 struct cfdisk_menuitem
*d
,
987 char buf
[80 * MB_CUR_MAX
], *ptr
= buf
;
990 int ln
, cl
, vert
= cf
->menu
->vertical
;
992 if (!menuitem_on_page(cf
, idx
))
993 return; /* no visible item */
994 ln
= menuitem_get_line(cf
, idx
);
995 cl
= menuitem_get_column(cf
, idx
);
999 width
= cf
->menu
->width
+ MENU_V_SPADDING
;
1000 memset(ptr
, ' ', MENU_V_SPADDING
);
1001 ptr
+= MENU_V_SPADDING
;
1003 width
= MENU_H_SPADDING
+ cf
->menu
->width
+ MENU_H_SPADDING
;
1006 mbsalign(name
, ptr
, sizeof(buf
), &width
,
1007 vert
? MBS_ALIGN_LEFT
: MBS_ALIGN_CENTER
,
1010 DBG(MENU
, ul_debug("menuitem: cl=%d, ln=%d, item='%s'",
1014 mvaddch(ln
, cl
- 1, ACS_VLINE
);
1015 mvaddch(ln
, cl
+ MENU_V_ITEMWIDTH(cf
->menu
), ACS_VLINE
);
1018 if (cf
->menu
->idx
== idx
)
1022 mvprintw(ln
, cl
, "%s", buf
);
1024 mvprintw(ln
, cl
, "%s%s%s", MENU_H_PRESTR
, buf
, MENU_H_POSTSTR
);
1026 if (cf
->menu
->idx
== idx
) {
1029 ui_hint(_(d
->desc
));
1033 static void ui_clean_menu(struct cfdisk
*cf
)
1037 struct cfdisk_menu
*m
= cf
->menu
;
1038 size_t ln
= menuitem_get_line(cf
, 0);
1041 lastline
= ln
+ (m
->page_sz
? m
->page_sz
: m
->nitems
);
1043 lastline
= menuitem_get_line(cf
, m
->nitems
);
1045 for (i
= ln
; i
<= lastline
; i
++) {
1048 DBG(MENU
, ul_debug("clean_menu: line %zu", i
));
1057 static void ui_draw_menu(struct cfdisk
*cf
)
1059 struct cfdisk_menuitem
*d
;
1060 struct cfdisk_menu
*m
;
1062 size_t ln
= menuitem_get_line(cf
, 0);
1068 DBG(MENU
, ul_debug("draw start"));
1074 nlines
= m
->page_sz
? m
->page_sz
: m
->nitems
;
1076 nlines
= menuitem_get_line(cf
, m
->nitems
);
1079 menu_update_ignore(cf
);
1081 while ((d
= menu_get_menuitem(cf
, i
)))
1082 ui_draw_menuitem(cf
, d
, i
++);
1085 size_t cl
= menuitem_get_column(cf
, 0);
1086 size_t curpg
= m
->page_sz
? m
->idx
/ m
->page_sz
: 0;
1088 /* corners and horizontal lines */
1089 mvaddch(ln
- 1, cl
- 1, ACS_ULCORNER
);
1090 mvaddch(ln
+ nlines
, cl
- 1, ACS_LLCORNER
);
1092 for (i
= 0; i
< MENU_V_ITEMWIDTH(m
); i
++) {
1093 mvaddch(ln
- 1, cl
+ i
, ACS_HLINE
);
1094 mvaddch(ln
+ nlines
, cl
+ i
, ACS_HLINE
);
1097 mvaddch(ln
- 1, cl
+ i
, ACS_URCORNER
);
1098 mvaddch(ln
+ nlines
, cl
+ i
, ACS_LRCORNER
);
1100 /* draw also lines around empty lines on last page */
1102 m
->nitems
/ m
->page_sz
== m
->idx
/ m
->page_sz
) {
1103 for (i
= m
->nitems
% m
->page_sz
+ 1; i
<= m
->page_sz
; i
++) {
1104 mvaddch(i
, cl
- 1, ACS_VLINE
);
1105 mvaddch(i
, cl
+ MENU_V_ITEMWIDTH(m
), ACS_VLINE
);
1110 mvprintw(ln
- 1, cl
, " %s ", m
->title
);
1114 mvaddch(ln
- 1, cl
+ MENU_V_ITEMWIDTH(m
) - 2, ACS_UARROW
);
1115 if (m
->page_sz
&& curpg
< m
->nitems
/ m
->page_sz
)
1116 mvaddch(ln
+ nlines
, cl
+ MENU_V_ITEMWIDTH(m
) - 2, ACS_DARROW
);
1119 DBG(MENU
, ul_debug("draw end."));
1122 static void ui_menu_goto(struct cfdisk
*cf
, int where
)
1124 struct cfdisk_menuitem
*d
;
1127 /* stop and begin/end for vertical menus */
1128 if (cf
->menu
->vertical
) {
1131 else if (where
> (int) cf
->menu
->nitems
- 1)
1132 where
= cf
->menu
->nitems
- 1;
1134 /* continue from begin/end */
1136 where
= cf
->menu
->nitems
- 1;
1137 else if ((size_t) where
> cf
->menu
->nitems
- 1)
1140 if ((size_t) where
== cf
->menu
->idx
)
1145 old
= cf
->menu
->idx
;
1146 cf
->menu
->idx
= where
;
1148 if (!menuitem_on_page(cf
, old
)) {
1153 d
= menu_get_menuitem(cf
, old
);
1154 ui_draw_menuitem(cf
, d
, old
);
1156 d
= menu_get_menuitem(cf
, where
);
1157 ui_draw_menuitem(cf
, d
, where
);
1160 static int ui_menu_move(struct cfdisk
*cf
, int key
)
1162 struct cfdisk_menu
*m
;
1168 return 0; /* ignore errors */
1172 DBG(MENU
, ul_debug("menu move key >%c<.", key
));
1178 case '\016': /* ^N */
1179 case 'j': /* Vi-like alternative */
1180 ui_menu_goto(cf
, m
->idx
+ 1);
1183 case '\020': /* ^P */
1184 case 'k': /* Vi-like alternative */
1185 ui_menu_goto(cf
, (int) m
->idx
- 1);
1189 ui_menu_goto(cf
, (int) m
->idx
- m
->page_sz
);
1194 ui_menu_goto(cf
, 0);
1198 ui_menu_goto(cf
, m
->idx
+ m
->page_sz
);
1203 ui_menu_goto(cf
, m
->nitems
);
1210 ui_menu_goto(cf
, m
->idx
+ 1);
1216 ui_menu_goto(cf
, (int) m
->idx
- 1);
1221 return 1; /* key irrelevant for menu move */
1224 /* but don't call me from ui_run(), this is for pop-up menus only */
1225 static void ui_menu_resize(struct cfdisk
*cf
)
1229 menu_refresh_size(cf
);
1234 static int partition_on_page(struct cfdisk
*cf
, size_t i
)
1236 if (cf
->page_sz
== 0 ||
1237 cf
->lines_idx
/ cf
->page_sz
== i
/ cf
->page_sz
)
1242 static void ui_draw_partition(struct cfdisk
*cf
, size_t i
)
1244 int ln
= TABLE_START_LINE
+ 1 + i
; /* skip table header */
1245 int cl
= ARROW_CURSOR_WIDTH
; /* we need extra space for cursor */
1246 int cur
= cf
->lines_idx
== i
;
1250 if (!partition_on_page(cf
, i
))
1252 ln
= TABLE_START_LINE
+ (i
% cf
->page_sz
) + 1;
1253 curpg
= cf
->lines_idx
/ cf
->page_sz
;
1257 "draw partition %zu [page_sz=%zu, "
1258 "line=%d, idx=%zu]",
1259 i
, cf
->page_sz
, ln
, cf
->lines_idx
));
1263 mvaddstr(ln
, 0, ARROW_CURSOR_STRING
);
1264 mvaddstr(ln
, cl
, cf
->lines
[i
+ 1]);
1269 if (colors_wanted() && is_freespace(cf
, i
)) {
1270 attron(COLOR_PAIR(CFDISK_CL_FREESPACE
));
1273 mvaddstr(ln
, 0, ARROW_CURSOR_DUMMY
);
1274 mvaddstr(ln
, cl
, cf
->lines
[i
+ 1]);
1276 attroff(COLOR_PAIR(CFDISK_CL_FREESPACE
));
1279 if ((size_t) ln
== MENU_START_LINE
- 1 &&
1280 cf
->page_sz
&& curpg
< cf
->nlines
/ cf
->page_sz
) {
1283 mvaddch(ln
, ui_cols
- 1, ACS_DARROW
);
1284 mvaddch(ln
, 0, ACS_DARROW
);
1290 static int ui_draw_table(struct cfdisk
*cf
)
1292 int cl
= ARROW_CURSOR_WIDTH
;
1293 size_t i
, nparts
= fdisk_table_get_nents(cf
->table
);
1294 size_t curpg
= cf
->page_sz
? cf
->lines_idx
/ cf
->page_sz
: 0;
1296 DBG(UI
, ul_debug("draw table"));
1298 for (i
= TABLE_START_LINE
; i
<= TABLE_START_LINE
+ cf
->page_sz
; i
++) {
1303 if ((size_t) cf
->lines_idx
> nparts
- 1)
1304 cf
->lines_idx
= nparts
? nparts
- 1 : 0;
1308 mvaddstr(TABLE_START_LINE
, cl
, cf
->lines
[0]);
1311 /* print partitions */
1312 for (i
= 0; i
< nparts
; i
++)
1313 ui_draw_partition(cf
, i
);
1316 mvaddch(TABLE_START_LINE
, ui_cols
- 1, ACS_UARROW
);
1317 mvaddch(TABLE_START_LINE
, 0, ACS_UARROW
);
1319 if (cf
->page_sz
&& curpg
< cf
->nlines
/ cf
->page_sz
) {
1320 mvaddch(MENU_START_LINE
- 1, ui_cols
- 1, ACS_DARROW
);
1321 mvaddch(MENU_START_LINE
- 1, 0, ACS_DARROW
);
1326 static int ui_table_goto(struct cfdisk
*cf
, int where
)
1329 size_t nparts
= fdisk_table_get_nents(cf
->table
);
1331 DBG(UI
, ul_debug("goto table %d", where
));
1335 else if ((size_t) where
> nparts
- 1)
1338 if ((size_t) where
== cf
->lines_idx
)
1341 old
= cf
->lines_idx
;
1342 cf
->lines_idx
= where
;
1344 if (!partition_on_page(cf
, old
) ||!partition_on_page(cf
, where
))
1347 ui_draw_partition(cf
, old
); /* cleanup old */
1348 ui_draw_partition(cf
, where
); /* draw new */
1356 static int ui_refresh(struct cfdisk
*cf
)
1358 struct fdisk_label
*lb
;
1360 uint64_t bytes
= fdisk_get_nsectors(cf
->cxt
) * fdisk_get_sector_size(cf
->cxt
);
1367 strsz
= size_to_human_string(SIZE_SUFFIX_SPACE
1368 | SIZE_SUFFIX_3LETTER
, bytes
);
1370 lb
= fdisk_get_label(cf
->cxt
, NULL
);
1375 ui_center(0, _("Disk: %s"), fdisk_get_devname(cf
->cxt
));
1377 ui_center(1, _("Size: %s, %ju bytes, %ju sectors"),
1378 strsz
, bytes
, (uintmax_t) fdisk_get_nsectors(cf
->cxt
));
1379 if (fdisk_get_disklabel_id(cf
->cxt
, &id
) == 0 && id
)
1380 ui_center(2, _("Label: %s, identifier: %s"),
1381 fdisk_label_get_name(lb
), id
);
1383 ui_center(2, _("Label: %s"), fdisk_label_get_name(lb
));
1392 static ssize_t
ui_get_string(struct cfdisk
*cf
, const char *prompt
,
1393 const char *hint
, char *buf
, size_t len
)
1396 ssize_t i
= 0, rc
= -1;
1397 int ln
= MENU_START_LINE
, cl
= 1;
1410 mvaddstr(ln
, cl
, (char *) prompt
);
1411 cl
+= mbs_safe_width(prompt
);
1417 cells
= mbs_safe_width(buf
);
1418 mvaddstr(ln
, cl
, buf
);
1426 move(ln
, cl
+ cells
);
1431 #if !defined(HAVE_SLCURSES_H) && !defined(HAVE_SLANG_SLCURSES_H) && \
1432 defined(HAVE_LIBNCURSESW) && defined(HAVE_WIDECHAR)
1434 if (get_wch(&c
) == ERR
) {
1437 if ((c
= getch()) == ERR
) {
1443 if (!isatty(STDIN_FILENO
))
1448 if (c
== '\r' || c
== '\n' || c
== KEY_ENTER
)
1453 rc
= -CFDISK_ERR_ESC
;
1455 case KEY_LEFT
: /* TODO: implement full buffer editor */
1468 i
= mbs_truncate(buf
, &cells
);
1471 mvaddch(ln
, cl
+ cells
, ' ');
1472 move(ln
, cl
+ cells
);
1478 #if defined(HAVE_LIBNCURSESW) && defined(HAVE_WIDECHAR)
1479 if (i
+ 1 < (ssize_t
) len
&& iswprint(c
)) {
1480 wchar_t wc
= (wchar_t) c
;
1481 char s
[MB_CUR_MAX
+ 1];
1482 int sz
= wctomb(s
, wc
);
1484 if (sz
> 0 && sz
+ i
< (ssize_t
) len
) {
1486 mvaddnstr(ln
, cl
+ cells
, s
, sz
);
1487 memcpy(buf
+ i
, s
, sz
);
1490 cells
+= wcwidth(wc
);
1495 if (i
+ 1 < (ssize_t
) len
&& isprint(c
)) {
1496 mvaddch(ln
, cl
+ cells
, c
);
1508 rc
= i
; /* success */
1518 /* @res is default value as well as result in bytes */
1519 static int ui_get_size(struct cfdisk
*cf
, const char *prompt
, uintmax_t *res
,
1520 uintmax_t low
, uintmax_t up
, int *expsize
)
1525 char *dflt
= size_to_human_string(0, *res
);
1527 DBG(UI
, ul_debug("get_size (default=%ju)", *res
));
1531 snprintf(buf
, sizeof(buf
), "%s", dflt
);
1534 int pwr
= 0, insec
= 0;
1536 rc
= ui_get_string(cf
, prompt
,
1537 _("May be followed by M for MiB, G for GiB, "
1538 "T for TiB, or S for sectors."),
1543 ui_warnx(_("Please, specify size."));
1544 continue; /* nothing specified */
1545 } else if (rc
== -CFDISK_ERR_ESC
)
1546 break; /* cancel dialog */
1548 if (strcmp(buf
, dflt
) == 0)
1549 user
= *res
, rc
= 0; /* no change, use default */
1551 size_t len
= strlen(buf
);
1552 if (buf
[len
- 1] == 'S' || buf
[len
- 1] == 's') {
1554 buf
[len
- 1] = '\0';
1556 rc
= parse_size(buf
, &user
, &pwr
); /* parse */
1560 DBG(UI
, ul_debug("get_size user=%ju, power=%d, sectors=%s",
1561 user
, pwr
, insec
? "yes" : "no"));
1563 user
*= fdisk_get_sector_size(cf
->cxt
);
1565 ui_warnx(_("Minimum size is %ju bytes."), low
);
1568 if (user
> up
&& pwr
&& user
< up
+ (1ULL << pwr
* 10))
1569 /* ignore when the user specified size overflow
1570 * with in range specified by suffix (e.g. MiB) */
1574 ui_warnx(_("Maximum size is %ju bytes."), up
);
1577 if (rc
== 0 && insec
&& expsize
)
1581 ui_warnx(_("Failed to parse size."));
1588 DBG(UI
, ul_debug("get_size (result=%ju, rc=%zd)", *res
, rc
));
1592 static struct fdisk_parttype
*ui_get_parttype(struct cfdisk
*cf
,
1593 struct fdisk_parttype
*cur
)
1595 struct cfdisk_menuitem
*d
, *cm
;
1596 size_t i
= 0, nitems
, idx
= 0;
1597 struct fdisk_parttype
*t
= NULL
;
1598 struct fdisk_label
*lb
;
1601 DBG(UI
, ul_debug("asking for parttype."));
1603 lb
= fdisk_get_label(cf
->cxt
, NULL
);
1605 /* create cfdisk menu according to label types, note that the
1606 * last cm[] item has to be empty -- so nitems + 1 */
1607 nitems
= fdisk_label_get_nparttypes(lb
);
1611 cm
= xcalloc(nitems
+ 1, sizeof(struct cfdisk_menuitem
));
1615 codetypes
= fdisk_label_has_code_parttypes(lb
);
1617 for (i
= 0; i
< nitems
; i
++) {
1618 const struct fdisk_parttype
*x
= fdisk_label_get_parttype(lb
, i
);
1621 cm
[i
].userdata
= (void *) x
;
1623 xasprintf(&name
, "%2x %s",
1624 fdisk_parttype_get_code(x
),
1625 _(fdisk_parttype_get_name(x
)));
1627 name
= (char *) _(fdisk_parttype_get_name(x
));
1628 cm
[i
].desc
= fdisk_parttype_get_string(x
);
1635 /* make the new menu active */
1637 cf
->menu
->vertical
= 1;
1638 cf
->menu
->idx
= idx
;
1639 menu_set_title(cf
->menu
, _("Select partition type"));
1648 if (ui_menu_move(cf
, key
) == 0)
1655 d
= menu_get_menuitem(cf
, cf
->menu
->idx
);
1657 t
= (struct fdisk_parttype
*) d
->userdata
;
1669 for (i
= 0; i
< nitems
; i
++)
1670 free((char *) cm
[i
].name
);
1673 DBG(UI
, ul_debug("get parrtype done [type=%s] ", t
?
1674 fdisk_parttype_get_name(t
) : NULL
));
1678 static int ui_script_read(struct cfdisk
*cf
)
1680 struct fdisk_script
*sc
= NULL
;
1681 char buf
[PATH_MAX
] = { 0 };
1685 rc
= ui_get_string(cf
, _("Enter script file name: "),
1686 _("The script file will be applied to in-memory partition table."),
1693 sc
= fdisk_new_script_from_file(cf
->cxt
, buf
);
1695 ui_warn(_("Cannot open %s"), buf
);
1697 ui_warnx(_("Failed to parse script file %s"), buf
);
1698 else if (fdisk_apply_script(cf
->cxt
, sc
) != 0)
1699 ui_warnx(_("Failed to apply script %s"), buf
);
1704 fdisk_unref_script(sc
);
1708 static int ui_script_write(struct cfdisk
*cf
)
1710 struct fdisk_script
*sc
= NULL
;
1711 char buf
[PATH_MAX
] = { 0 };
1715 rc
= ui_get_string(cf
, _("Enter script file name: "),
1716 _("The current in-memory partition table will be dumped to the file."),
1722 sc
= fdisk_new_script(cf
->cxt
);
1724 ui_warn(_("Failed to allocate script handler"));
1728 rc
= fdisk_script_read_context(sc
, NULL
);
1730 ui_warnx(_("Failed to read disk layout into script."));
1734 DBG(UI
, ul_debug("writing dump into: '%s'", buf
));
1735 f
= fopen(buf
, "w");
1737 ui_warn(_("Cannot open %s"), buf
);
1742 rc
= fdisk_script_write_file(sc
, f
);
1744 ui_info(_("Disk layout successfully dumped."));
1747 ui_warn(_("Failed to write script %s"), buf
);
1750 fdisk_unref_script(sc
);
1754 /* prints menu with libfdisk labels and waits for users response */
1755 static int ui_create_label(struct cfdisk
*cf
)
1757 struct cfdisk_menuitem
*d
, *cm
;
1758 int rc
= 1, refresh_menu
= 1;
1759 size_t i
= 0, nitems
;
1760 struct fdisk_label
*lb
= NULL
;
1764 DBG(UI
, ul_debug("asking for new disklabe."));
1766 /* create cfdisk menu according to libfdisk labels, note that the
1767 * last cm[] item has to be empty -- so nitems + 1 */
1768 nitems
= fdisk_get_nlabels(cf
->cxt
);
1769 cm
= xcalloc(nitems
+ 1, sizeof(struct cfdisk_menuitem
));
1771 while (fdisk_next_label(cf
->cxt
, &lb
) == 0) {
1772 if (fdisk_label_is_disabled(lb
) ||
1773 fdisk_label_get_type(lb
) == FDISK_DISKLABEL_BSD
)
1775 cm
[i
++].name
= fdisk_label_get_name(lb
);
1780 /* make the new menu active */
1782 cf
->menu
->vertical
= 1;
1783 menu_set_title(cf
->menu
, _("Select label type"));
1785 if (!cf
->zero_start
)
1786 ui_info(_("Device does not contain a recognized partition table."));
1792 ui_hint(_("Select a type to create a new label or press 'L' to load script file."));
1801 if (ui_menu_move(cf
, key
) == 0)
1807 d
= menu_get_menuitem(cf
, cf
->menu
->idx
);
1809 rc
= fdisk_create_disklabel(cf
->cxt
, d
->name
);
1817 rc
= ui_script_read(cf
);
1828 DBG(UI
, ul_debug("create label done [rc=%d] ", rc
));
1833 static int ui_help(void)
1836 static const char *help
[] = {
1837 N_("This is cfdisk, a curses-based disk partitioning program."),
1838 N_("It lets you create, delete, and modify partitions on a block device."),
1840 N_("Command Meaning"),
1841 N_("------- -------"),
1842 N_(" b Toggle bootable flag of the current partition"),
1843 N_(" d Delete the current partition"),
1844 N_(" h Print this screen"),
1845 N_(" n Create new partition from free space"),
1846 N_(" q Quit program without writing partition table"),
1847 N_(" s Fix partitions order (only when in disarray)"),
1848 N_(" t Change the partition type"),
1849 N_(" u Dump disk layout to sfdisk compatible script file"),
1850 N_(" W Write partition table to disk (you must enter uppercase W);"),
1851 N_(" since this might destroy data on the disk, you must either"),
1852 N_(" confirm or deny the write by entering 'yes' or 'no'"),
1853 N_("Up Arrow Move cursor to the previous partition"),
1854 N_("Down Arrow Move cursor to the next partition"),
1855 N_("Left Arrow Move cursor to the previous menu item"),
1856 N_("Right Arrow Move cursor to the next menu item"),
1858 N_("Note: All of the commands can be entered with either upper or lower"),
1859 N_("case letters (except for Write)."),
1861 N_("Use lsblk(8) or partx(8) to see more details about the device.")
1865 for (i
= 0; i
< ARRAY_SIZE(help
); i
++)
1866 mvaddstr(i
, 1, _(help
[i
]));
1868 ui_info(_("Press a key to continue."));
1874 /* TODO: use @sz, now 128bytes */
1875 static int main_menu_ignore_keys(struct cfdisk
*cf
, char *ignore
,
1876 size_t sz
__attribute__((__unused__
)))
1878 struct fdisk_partition
*pa
= get_current_partition(cf
);
1883 if (fdisk_partition_is_freespace(pa
)) {
1884 ignore
[i
++] = 'd'; /* delete */
1885 ignore
[i
++] = 't'; /* set type */
1886 ignore
[i
++] = 'b'; /* set bootable */
1887 cf
->menu
->prefkey
= 'n';
1889 cf
->menu
->prefkey
= 'q';
1891 if (!fdisk_is_label(cf
->cxt
, DOS
) &&
1892 !fdisk_is_label(cf
->cxt
, SGI
))
1896 if (!cf
->wrong_order
)
1899 if (fdisk_is_readonly(cf
->cxt
))
1905 /* returns: error: < 0, success: 0, quit: 1 */
1906 static int main_menu_action(struct cfdisk
*cf
, int key
)
1909 int ref
= 0, rc
, org_order
= cf
->wrong_order
;
1910 const char *info
= NULL
, *warn
= NULL
;
1911 struct fdisk_partition
*pa
;
1918 struct cfdisk_menuitem
*d
= menu_get_menuitem(cf
, cf
->menu
->idx
);
1923 } else if (key
!= 'w' && key
!= 'W')
1924 key
= tolower(key
); /* case insensitive except 'W'rite */
1926 DBG(MENU
, ul_debug("main menu action: key=%c", key
));
1928 if (cf
->menu
->ignore
&& strchr(cf
->menu
->ignore
, key
)) {
1929 DBG(MENU
, ul_debug(" ignore '%c'", key
));
1933 pa
= get_current_partition(cf
);
1936 n
= fdisk_partition_get_partno(pa
);
1938 DBG(MENU
, ul_debug("menu action on %p", pa
));
1943 case 'b': /* Bootable flag */
1945 int fl
= fdisk_is_label(cf
->cxt
, DOS
) ? DOS_FLAG_ACTIVE
:
1946 fdisk_is_label(cf
->cxt
, SGI
) ? SGI_FLAG_BOOT
: 0;
1948 if (fl
&& fdisk_toggle_partition_flag(cf
->cxt
, n
, fl
))
1949 warn
= _("Could not toggle the flag.");
1957 case 'd': /* Delete */
1958 if (fdisk_delete_partition(cf
->cxt
, n
) != 0)
1959 warn
= _("Could not delete partition %zu.");
1961 info
= _("Partition %zu has been deleted.");
1964 case 'h': /* Help */
1971 uint64_t start
, size
, dflt_size
, secs
;
1972 struct fdisk_partition
*npa
; /* the new partition */
1973 int expsize
= 0; /* size specified explicitly in sectors */
1975 if (!fdisk_partition_is_freespace(pa
) || !fdisk_partition_has_start(pa
))
1978 /* free space range */
1979 start
= fdisk_partition_get_start(pa
);
1980 size
= dflt_size
= fdisk_partition_get_size(pa
) * fdisk_get_sector_size(cf
->cxt
);
1982 if (ui_get_size(cf
, _("Partition size: "), &size
,
1983 fdisk_get_sector_size(cf
->cxt
),
1984 size
, &expsize
) == -CFDISK_ERR_ESC
)
1987 secs
= size
/ fdisk_get_sector_size(cf
->cxt
);
1989 npa
= fdisk_new_partition();
1993 if (dflt_size
== size
) /* default is to fillin all free space */
1994 fdisk_partition_end_follow_default(npa
, 1);
1996 fdisk_partition_set_size(npa
, secs
);
1999 fdisk_partition_size_explicit(pa
, 1);
2001 fdisk_partition_set_start(npa
, start
);
2002 fdisk_partition_partno_follow_default(npa
, 1);
2003 /* add to disk label -- libfdisk will ask for missing details */
2004 rc
= fdisk_add_partition(cf
->cxt
, npa
, NULL
);
2005 fdisk_unref_partition(npa
);
2010 case 'q': /* Quit */
2012 case 't': /* Type */
2014 struct fdisk_parttype
*t
;
2016 if (fdisk_partition_is_freespace(pa
))
2018 t
= (struct fdisk_parttype
*) fdisk_partition_get_type(pa
);
2019 t
= ui_get_parttype(cf
, t
);
2022 if (t
&& fdisk_set_partition_type(cf
->cxt
, n
, t
) == 0)
2023 info
= _("Changed type of partition %zu.");
2025 info
= _("The type of partition %zu is unchanged.");
2028 case 's': /* Sort */
2029 if (cf
->wrong_order
) {
2030 fdisk_reorder_partitions(cf
->cxt
);
2034 case 'u': /* dUmp */
2035 ui_script_write(cf
);
2037 case 'W': /* Write */
2039 char buf
[64] = { 0 };
2041 if (fdisk_is_readonly(cf
->cxt
)) {
2042 warn
= _("Device is open in read-only mode.");
2046 rc
= ui_get_string(cf
,
2047 _("Are you sure you want to write the partition "
2049 _("Type \"yes\" or \"no\", or press ESC to leave this dialog."),
2053 if (rc
<= 0 || (strcasecmp(buf
, "yes") != 0 &&
2054 strcasecmp(buf
, _("yes")) != 0)) {
2055 info
= _("Did not write partition table to disk.");
2058 rc
= fdisk_write_disklabel(cf
->cxt
);
2060 warn
= _("Failed to write disklabel.");
2062 fdisk_reread_partition_table(cf
->cxt
);
2063 info
= _("The partition table has been altered.");
2081 ui_warnx(warn
, n
+ 1);
2083 ui_info(info
, n
+ 1);
2084 else if (key
== 'n' && cf
->wrong_order
&& org_order
== 0)
2085 ui_info(_("Note that partition table entries are not in disk order now."));
2090 static void ui_resize_refresh(struct cfdisk
*cf
)
2093 menu_refresh_size(cf
);
2098 static int ui_run(struct cfdisk
*cf
)
2104 DBG(UI
, ul_debug("start cols=%zu, lines=%zu", ui_cols
, ui_lines
));
2106 if (!fdisk_has_label(cf
->cxt
) || cf
->zero_start
) {
2107 rc
= ui_create_label(cf
);
2109 ui_errx(EXIT_FAILURE
,
2110 _("failed to create a new disklabel"));
2116 rc
= lines_refresh(cf
);
2118 ui_errx(EXIT_FAILURE
, _("failed to read partitions"));
2120 menu_push(cf
, main_menuitems
);
2121 cf
->menu
->ignore_cb
= main_menu_ignore_keys
;
2123 rc
= ui_refresh(cf
);
2127 if (fdisk_is_readonly(cf
->cxt
))
2128 ui_warnx(_("Device open in read-only mode."));
2129 else if (cf
->wrong_order
)
2130 ui_info(_("Note that partition table entries are not in disk order now."));
2137 /* Note that ncurses getch() returns ERR when interrupted
2138 * by signal, but SLang does not interrupt at all. */
2139 ui_resize_refresh(cf
);
2142 if (ui_menu_move(cf
, key
) == 0)
2145 DBG(UI
, ul_debug("main action key >%c<.", key
));
2149 case '\016': /* ^N */
2150 case 'j': /* Vi-like alternative */
2151 ui_table_goto(cf
, cf
->lines_idx
+ 1);
2154 case '\020': /* ^P */
2155 case 'k': /* Vi-like alternative */
2156 ui_table_goto(cf
, (int) cf
->lines_idx
- 1);
2160 ui_table_goto(cf
, (int) cf
->lines_idx
- cf
->page_sz
);
2164 ui_table_goto(cf
, 0);
2168 ui_table_goto(cf
, cf
->lines_idx
+ cf
->page_sz
);
2172 ui_table_goto(cf
, (int) cf
->nlines
- 1);
2177 rc
= main_menu_action(cf
, 0);
2180 rc
= main_menu_action(cf
, key
);
2192 DBG(UI
, ul_debug("end"));
2196 static void __attribute__ ((__noreturn__
)) usage(FILE *out
)
2198 fputs(USAGE_HEADER
, out
);
2200 _(" %1$s [options] <disk>\n"), program_invocation_short_name
);
2202 fputs(USAGE_SEPARATOR
, out
);
2203 fputs(_("Display or manipulate a disk partition table.\n"), out
);
2205 fputs(USAGE_OPTIONS
, out
);
2206 fputs(_(" -L, --color[=<when>] colorize output (auto, always or never)\n"), out
);
2208 " %s\n", USAGE_COLORS_DEFAULT
);
2209 fputs(_(" -z, --zero start with zeroed partition table\n"), out
);
2211 fputs(USAGE_SEPARATOR
, out
);
2212 fputs(USAGE_HELP
, out
);
2213 fputs(USAGE_VERSION
, out
);
2215 fprintf(out
, USAGE_MAN_TAIL("cfdisk(8)"));
2216 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
2219 int main(int argc
, char *argv
[])
2221 const char *diskpath
;
2222 int rc
, c
, colormode
= UL_COLORMODE_UNDEF
;
2223 struct cfdisk _cf
= { .lines_idx
= 0 },
2226 static const struct option longopts
[] = {
2227 { "color", optional_argument
, NULL
, 'L' },
2228 { "help", no_argument
, NULL
, 'h' },
2229 { "version", no_argument
, NULL
, 'V' },
2230 { "zero", no_argument
, NULL
, 'z' },
2234 setlocale(LC_ALL
, "");
2235 bindtextdomain(PACKAGE
, LOCALEDIR
);
2236 textdomain(PACKAGE
);
2237 atexit(close_stdout
);
2239 while((c
= getopt_long(argc
, argv
, "L::hVz", longopts
, NULL
)) != -1) {
2245 colormode
= UL_COLORMODE_AUTO
;
2247 colormode
= colormode_or_err(optarg
,
2248 _("unsupported color mode"));
2251 printf(UTIL_LINUX_VERSION
);
2252 return EXIT_SUCCESS
;
2259 colors_init(colormode
, "cfdisk");
2261 fdisk_init_debug(0);
2262 scols_init_debug(0);
2263 cfdisk_init_debug();
2264 cf
->cxt
= fdisk_new_context();
2266 err(EXIT_FAILURE
, _("failed to allocate libfdisk context"));
2268 fdisk_set_ask(cf
->cxt
, ask_callback
, (void *) cf
);
2271 diskpath
= access(DEFAULT_DEVICE
, F_OK
) == 0 ?
2272 DEFAULT_DEVICE
: ALTERNATE_DEVICE
;
2274 diskpath
= argv
[optind
];
2276 rc
= fdisk_assign_device(cf
->cxt
, diskpath
, 0);
2278 rc
= fdisk_assign_device(cf
->cxt
, diskpath
, 1);
2280 err(EXIT_FAILURE
, _("cannot open %s"),
2281 optind
== argc
? DEFAULT_DEVICE
: diskpath
);
2283 /* Don't use err(), warn() from this point */
2290 fdisk_unref_table(cf
->table
);
2292 rc
= fdisk_deassign_device(cf
->cxt
, cf
->nwrites
== 0);
2293 fdisk_unref_context(cf
->cxt
);
2294 DBG(MISC
, ul_debug("bye! [rc=%d]", rc
));
2295 return rc
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
;