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'
108 static const int color_pairs
[][2] = {
109 /* color foreground, background */
110 [CFDISK_CL_WARNING
] = { COLOR_RED
, -1 },
111 [CFDISK_CL_FREESPACE
] = { COLOR_GREEN
, -1 }
116 static struct cfdisk_menuitem
*menu_get_menuitem(struct cfdisk
*cf
, size_t idx
);
117 static struct cfdisk_menuitem
*menu_get_menuitem_by_key(struct cfdisk
*cf
, int key
, size_t *idx
);
118 static struct cfdisk_menu
*menu_push(struct cfdisk
*cf
, struct cfdisk_menuitem
*item
);
119 static struct cfdisk_menu
*menu_pop(struct cfdisk
*cf
);
120 static void menu_refresh_size(struct cfdisk
*cf
);
122 static int ui_refresh(struct cfdisk
*cf
);
123 static void ui_warnx(const char *fmt
, ...);
124 static void ui_warn(const char *fmt
, ...);
125 static void ui_info(const char *fmt
, ...);
126 static void ui_draw_menu(struct cfdisk
*cf
);
127 static int ui_menu_move(struct cfdisk
*cf
, int key
);
128 static void ui_menu_resize(struct cfdisk
*cf
);
130 static int ui_get_size(struct cfdisk
*cf
, const char *prompt
, uintmax_t *res
,
131 uintmax_t low
, uintmax_t up
, int *expsize
);
133 static int ui_enabled
;
134 static int ui_resize
;
136 /* ncurses LINES and COLS may be actual variables or *macros*, but we need
137 * something portable and writable */
142 struct cfdisk_menuitem
{
143 int key
; /* keyboard shortcut */
144 const char *name
; /* item name */
145 const char *desc
; /* item description (hint) */
151 char *title
; /* optional menu title */
152 struct cfdisk_menuitem
*items
; /* array with menu items */
153 char *ignore
;/* string with keys to ignore */
154 size_t width
; /* maximal width of the menu item */
155 size_t nitems
; /* number of the active menu items */
156 size_t page_sz
;/* when menu longer than screen */
157 size_t idx
; /* the current menu item */
158 struct cfdisk_menu
*prev
;
160 /* @ignore keys generator */
161 int (*ignore_cb
) (struct cfdisk
*, char *, size_t);
163 unsigned int vertical
: 1; /* enable vertical mode */
167 static struct cfdisk_menuitem main_menuitems
[] = {
168 { 'b', N_("Bootable"), N_("Toggle bootable flag of the current partition") },
169 { 'd', N_("Delete"), N_("Delete the current partition") },
170 { 'n', N_("New"), N_("Create new partition from free space") },
171 { 'q', N_("Quit"), N_("Quit program without writing partition table") },
172 { 't', N_("Type"), N_("Change the partition type") },
173 { 'h', N_("Help"), N_("Print help screen") },
174 { 's', N_("Sort"), N_("Fix partitions order") },
175 { 'W', N_("Write"), N_("Write partition table to disk (this might destroy data)") },
176 { 'u', N_("Dump"), N_("Dump partition table to sfdisk compatible script file") },
180 /* top level control struct */
182 struct fdisk_context
*cxt
; /* libfdisk context */
183 struct fdisk_table
*table
; /* partition table */
184 struct cfdisk_menu
*menu
; /* the current menu */
186 int *fields
; /* output columns IDs */
187 size_t nfields
; /* number of columns IDs */
189 char *linesbuf
; /* table as string */
190 size_t linesbufsz
; /* size of the tb_buf */
192 char **lines
; /* array with lines */
193 size_t nlines
; /* number of lines */
194 size_t lines_idx
; /* current line <0..N>, exclude header */
197 unsigned int nwrites
; /* fdisk_write_disklabel() counter */
199 unsigned int wrong_order
:1, /* PT not in right order */
200 zero_start
:1; /* ignore existing partition table */
205 * let's use include/debug.h stuff for cfdisk too
207 UL_DEBUG_DEFINE_MASK(cfdisk
);
208 UL_DEBUG_DEFINE_MASKNAMES(cfdisk
) = UL_DEBUG_EMPTY_MASKNAMES
;
210 #define CFDISK_DEBUG_INIT (1 << 1)
211 #define CFDISK_DEBUG_UI (1 << 2)
212 #define CFDISK_DEBUG_MENU (1 << 3)
213 #define CFDISK_DEBUG_MISC (1 << 4)
214 #define CFDISK_DEBUG_TABLE (1 << 5)
215 #define CFDISK_DEBUG_ALL 0xFFFF
217 #define DBG(m, x) __UL_DBG(cfdisk, CFDISK_DEBUG_, m, x)
219 static void cfdisk_init_debug(void)
221 __UL_INIT_DEBUG(cfdisk
, CFDISK_DEBUG_
, 0, CFDISK_DEBUG
);
224 /* Initialize output columns -- we follow libfdisk fields (usually specific
227 static int cols_init(struct cfdisk
*cf
)
235 return fdisk_label_get_fields_ids(NULL
, cf
->cxt
, &cf
->fields
, &cf
->nfields
);
238 static void resize(void)
242 if (ioctl(fileno(stdout
), TIOCGWINSZ
, &ws
) != -1
243 && ws
.ws_row
&& ws
.ws_col
) {
244 ui_lines
= ws
.ws_row
;
247 resizeterm(ws
.ws_row
, ws
.ws_col
);
249 clearok(stdscr
, TRUE
);
253 DBG(UI
, ul_debug("ui: resize refresh ui_cols=%zu, ui_lines=%zu",
258 /* Reads partition in tree-like order from scols
260 static int partition_from_scols(struct fdisk_table
*tb
,
261 struct libscols_line
*ln
)
263 struct fdisk_partition
*pa
= scols_line_get_userdata(ln
);
265 fdisk_table_add_partition(tb
, pa
);
266 fdisk_unref_partition(pa
);
268 if (scols_line_has_children(ln
)) {
269 struct libscols_line
*chln
;
270 struct libscols_iter
*itr
= scols_new_iter(SCOLS_ITER_FORWARD
);
274 while (scols_line_next_child(ln
, itr
, &chln
) == 0)
275 partition_from_scols(tb
, chln
);
276 scols_free_iter(itr
);
281 static char *table_to_string(struct cfdisk
*cf
, struct fdisk_table
*tb
)
283 struct fdisk_partition
*pa
;
284 struct fdisk_label
*lb
;
285 struct fdisk_iter
*itr
= NULL
;
286 struct libscols_table
*table
= NULL
;
287 struct libscols_iter
*s_itr
= NULL
;
291 struct libscols_line
*ln
, *ln_cont
= NULL
;
293 DBG(TABLE
, ul_debug("convert to string"));
300 lb
= fdisk_get_label(cf
->cxt
, NULL
);
303 itr
= fdisk_new_iter(FDISK_ITER_FORWARD
);
307 /* get container (e.g. extended partition) */
308 while (fdisk_table_next_partition(tb
, itr
, &pa
) == 0) {
309 if (fdisk_partition_is_nested(pa
)) {
310 DBG(TABLE
, ul_debug("nested detected, using tree"));
311 tree
= SCOLS_FL_TREE
;
316 table
= scols_new_table();
319 scols_table_enable_maxout(table
, 1);
322 for (i
= 0; i
< cf
->nfields
; i
++) {
324 const struct fdisk_field
*field
=
325 fdisk_label_get_field(lb
, cf
->fields
[i
]);
329 if (fdisk_field_is_number(field
))
330 fl
|= SCOLS_FL_RIGHT
;
331 if (fdisk_field_get_id(field
) == FDISK_FIELD_TYPE
)
332 fl
|= SCOLS_FL_TRUNC
;
333 if (tree
&& fdisk_field_get_id(field
) == FDISK_FIELD_DEVICE
)
336 if (!scols_table_new_column(table
,
337 _(fdisk_field_get_name(field
)),
338 fdisk_field_get_width(field
), fl
))
343 fdisk_reset_iter(itr
, FDISK_ITER_FORWARD
);
345 while (fdisk_table_next_partition(tb
, itr
, &pa
) == 0) {
346 struct libscols_line
*parent
= fdisk_partition_is_nested(pa
) ? ln_cont
: NULL
;
348 ln
= scols_table_new_line(table
, parent
);
351 for (i
= 0; i
< cf
->nfields
; i
++) {
354 if (fdisk_partition_to_string(pa
, cf
->cxt
,
355 cf
->fields
[i
], &cdata
))
357 scols_line_refer_data(ln
, i
, cdata
);
359 if (tree
&& fdisk_partition_is_container(pa
))
362 scols_line_set_userdata(ln
, (void *) pa
);
363 fdisk_ref_partition(pa
);
366 if (scols_table_is_empty(table
))
369 scols_table_reduce_termwidth(table
, ARROW_CURSOR_WIDTH
);
370 scols_print_table_to_string(table
, &res
);
372 /* scols_* code might reorder lines, let's reorder @tb according to the
373 * final output (it's no problem because partitions are addressed by
374 * parno stored within struct fdisk_partition) */
377 fdisk_reset_iter(itr
, FDISK_ITER_FORWARD
);
378 while (fdisk_table_next_partition(tb
, itr
, &pa
) == 0)
379 fdisk_table_remove_partition(tb
, pa
);
381 s_itr
= scols_new_iter(SCOLS_ITER_FORWARD
);
385 /* add all in the right order (don't forget the output is tree) */
386 while (scols_table_next_line(table
, s_itr
, &ln
) == 0) {
387 if (scols_line_get_parent(ln
))
389 if (partition_from_scols(tb
, ln
))
393 scols_unref_table(table
);
394 scols_free_iter(s_itr
);
395 fdisk_free_iter(itr
);
401 * Read data about partitions from libfdisk and prepare output lines.
403 static int lines_refresh(struct cfdisk
*cf
)
411 DBG(TABLE
, ul_debug("refreshing buffer"));
420 fdisk_unref_table(cf
->table
);
423 /* read partitions and free spaces into cf->table */
424 rc
= fdisk_get_partitions(cf
->cxt
, &cf
->table
);
426 rc
= fdisk_get_freespaces(cf
->cxt
, &cf
->table
);
430 cf
->linesbuf
= table_to_string(cf
, cf
->table
);
434 cf
->linesbufsz
= strlen(cf
->linesbuf
);
435 cf
->nlines
= fdisk_table_get_nents(cf
->table
) + 1; /* 1 for header line */
437 cf
->wrong_order
= fdisk_table_wrong_order(cf
->table
) ? 1 : 0;
439 if (MENU_START_LINE
- TABLE_START_LINE
< cf
->nlines
)
440 cf
->page_sz
= MENU_START_LINE
- TABLE_START_LINE
- 1;
442 cf
->lines
= xcalloc(cf
->nlines
, sizeof(char *));
444 for (p
= cf
->linesbuf
, i
= 0; p
&& i
< cf
->nlines
; i
++) {
456 static struct fdisk_partition
*get_current_partition(struct cfdisk
*cf
)
461 return fdisk_table_get_partition(cf
->table
, cf
->lines_idx
);
464 static int is_freespace(struct cfdisk
*cf
, size_t i
)
466 struct fdisk_partition
*pa
;
471 pa
= fdisk_table_get_partition(cf
->table
, i
);
472 return fdisk_partition_is_freespace(pa
);
475 /* converts libfdisk FDISK_ASKTYPE_MENU to cfdisk menu and returns user's
476 * responseback to libfdisk
478 static int ask_menu(struct fdisk_ask
*ask
, struct cfdisk
*cf
)
480 struct cfdisk_menuitem
*d
, *cm
;
482 size_t i
= 0, nitems
;
483 const char *name
, *desc
;
488 /* create cfdisk menu according to libfdisk ask-menu, note that the
489 * last cm[] item has to be empty -- so nitems + 1 */
490 nitems
= fdisk_ask_menu_get_nitems(ask
);
491 cm
= xcalloc(nitems
+ 1, sizeof(struct cfdisk_menuitem
));
493 for (i
= 0; i
< nitems
; i
++) {
494 if (fdisk_ask_menu_get_item(ask
, i
, &key
, &name
, &desc
))
501 /* make the new menu active */
512 if (ui_menu_move(cf
, key
) == 0)
519 d
= menu_get_menuitem(cf
, cf
->menu
->idx
);
521 fdisk_ask_menu_set_result(ask
, d
->key
);
535 static int ask_callback(struct fdisk_context
*cxt
, struct fdisk_ask
*ask
,
536 void *data
__attribute__((__unused__
)))
543 switch(fdisk_ask_get_type(ask
)) {
544 case FDISK_ASKTYPE_INFO
:
545 ui_info(fdisk_ask_print_get_mesg(ask
));
547 case FDISK_ASKTYPE_WARNX
:
548 ui_warnx(fdisk_ask_print_get_mesg(ask
));
550 case FDISK_ASKTYPE_WARN
:
551 ui_warn(fdisk_ask_print_get_mesg(ask
));
553 case FDISK_ASKTYPE_MENU
:
554 ask_menu(ask
, (struct cfdisk
*) data
);
557 ui_warnx(_("internal error: unsupported dialog type %d"),
558 fdisk_ask_get_type(ask
));
564 static int ui_end(void)
569 #if defined(HAVE_SLCURSES_H) || defined(HAVE_SLANG_SLCURSES_H)
570 SLsmg_gotorc(ui_lines
- 1, 0);
573 mvcur(0, ui_cols
- 1, ui_lines
-1, 0);
583 static void ui_vprint_center(size_t line
, int attrs
, const char *fmt
, va_list ap
)
591 xvasprintf(&buf
, fmt
, ap
);
593 width
= mbs_safe_width(buf
);
594 if (width
> (size_t) ui_cols
) {
595 char *p
= strrchr(buf
+ ui_cols
, ' ');
599 if (line
+ 1 >= ui_lines
)
602 mvaddstr(line
, 0, buf
);
603 mvaddstr(line
+ 1, 0, p
+1);
607 mvaddstr(line
, (ui_cols
- width
) / 2, buf
);
613 static void ui_center(size_t line
, const char *fmt
, ...)
617 ui_vprint_center(line
, 0, fmt
, ap
);
621 static void ui_warnx(const char *fmt
, ...)
626 ui_vprint_center(WARN_LINE
,
627 colors_wanted() ? COLOR_PAIR(CFDISK_CL_WARNING
) : 0,
630 vfprintf(stderr
, fmt
, ap
);
636 static void ui_warn(const char *fmt
, ...)
641 xasprintf(&fmt_m
, "%s: %m", fmt
);
645 ui_vprint_center(WARN_LINE
,
646 colors_wanted() ? COLOR_PAIR(CFDISK_CL_WARNING
) : 0,
649 vfprintf(stderr
, fmt_m
, ap
);
656 static int __attribute__((__noreturn__
)) ui_errx(int rc
, const char *fmt
, ...)
662 fprintf(stderr
, "%s: ", program_invocation_short_name
);
663 vfprintf(stderr
, fmt
, ap
);
670 static void ui_info(const char *fmt
, ...)
675 ui_vprint_center(INFO_LINE
, A_BOLD
, fmt
, ap
);
677 vfprintf(stdout
, fmt
, ap
);
683 static void ui_clean_info(void)
689 static void ui_hint(const char *fmt
, ...)
694 ui_vprint_center(HINT_LINE
, A_BOLD
, fmt
, ap
);
696 vfprintf(stdout
, fmt
, ap
);
702 static void ui_clean_hint(void)
708 static void die_on_signal(int dummy
__attribute__((__unused__
)))
710 DBG(MISC
, ul_debug("die on signal."));
715 static void resize_on_signal(int dummy
__attribute__((__unused__
)))
717 DBG(MISC
, ul_debug("resize on signal."));
721 static void menu_refresh_size(struct cfdisk
*cf
)
723 if (cf
->menu
&& cf
->menu
->nitems
)
724 cf
->menu
->page_sz
= (cf
->menu
->nitems
/ (ui_lines
- 4)) ? ui_lines
- 4 : 0;
727 static void menu_update_ignore(struct cfdisk
*cf
)
729 char ignore
[128] = { 0 };
731 struct cfdisk_menu
*m
;
732 struct cfdisk_menuitem
*d
, *org
;
737 assert(cf
->menu
->ignore_cb
);
740 org
= menu_get_menuitem(cf
, m
->idx
);
742 DBG(MENU
, ul_debug("update menu ignored keys"));
744 i
= m
->ignore_cb(cf
, ignore
, sizeof(ignore
));
747 /* return if no change */
748 if ( (!m
->ignore
&& !*ignore
)
749 || (m
->ignore
&& *ignore
&& strcmp(m
->ignore
, ignore
) == 0)) {
754 m
->ignore
= xstrdup(ignore
);
757 for (d
= m
->items
; d
->name
; d
++) {
758 if (m
->ignore
&& strchr(m
->ignore
, d
->key
))
763 /* refresh menu index to be at the same menuitem or go to the first */
764 if (org
&& menu_get_menuitem_by_key(cf
, org
->key
, &idx
))
769 menu_refresh_size(cf
);
772 static struct cfdisk_menu
*menu_push(
774 struct cfdisk_menuitem
*items
)
776 struct cfdisk_menu
*m
= xcalloc(1, sizeof(*m
));
777 struct cfdisk_menuitem
*d
;
781 DBG(MENU
, ul_debug("new menu"));
786 for (d
= m
->items
; d
->name
; d
++) {
787 const char *name
= _(d
->name
);
788 size_t len
= mbs_safe_width(name
);
795 menu_refresh_size(cf
);
799 static struct cfdisk_menu
*menu_pop(struct cfdisk
*cf
)
801 struct cfdisk_menu
*m
= NULL
;
805 DBG(MENU
, ul_debug("pop menu"));
809 free(cf
->menu
->ignore
);
810 free(cf
->menu
->title
);
817 static void menu_set_title(struct cfdisk_menu
*m
, const char *title
)
822 size_t len
= mbs_safe_width(title
);
823 if (len
+ MENU_TITLE_PADDING
> m
->width
)
824 m
->width
= len
+ MENU_TITLE_PADDING
;
825 str
= xstrdup(title
);
831 static int ui_init(struct cfdisk
*cf
__attribute__((__unused__
)))
835 DBG(UI
, ul_debug("init"));
837 /* setup SIGCHLD handler */
838 sigemptyset(&sa
.sa_mask
);
840 sa
.sa_handler
= die_on_signal
;
841 sigaction(SIGINT
, &sa
, NULL
);
842 sigaction(SIGTERM
, &sa
, NULL
);
844 sa
.sa_handler
= resize_on_signal
;
845 sigaction(SIGWINCH
, &sa
, NULL
);
850 #ifdef HAVE_USE_DEFAULT_COLORS
851 if (colors_wanted() && has_colors()) {
855 use_default_colors();
856 for (i
= 1; i
< ARRAY_SIZE(color_pairs
); i
++) /* yeah, start from 1! */
857 init_pair(i
, color_pairs
[i
][0], color_pairs
[i
][1]);
867 keypad(stdscr
, TRUE
);
873 #define MENU_H_ITEMWIDTH(m) ( MENU_H_PRESTR_SZ \
879 #define MENU_V_ITEMWIDTH(m) (MENU_V_SPADDING + (m)->width + MENU_V_SPADDING)
882 static size_t menuitem_get_line(struct cfdisk
*cf
, size_t idx
)
884 struct cfdisk_menu
*m
= cf
->menu
;
887 if (!m
->page_sz
) /* small menu */
888 return (ui_lines
- (cf
->menu
->nitems
+ 1)) / 2 + idx
;
889 return (idx
% m
->page_sz
) + 1;
891 size_t len
= MENU_H_ITEMWIDTH(m
) + MENU_H_BETWEEN
; /** item width */
892 size_t items
= ui_cols
/ len
; /* items per line */
896 return MENU_START_LINE
+ ((idx
/ items
));
900 static int menuitem_get_column(struct cfdisk
*cf
, size_t idx
)
902 if (cf
->menu
->vertical
) {
903 size_t nc
= MENU_V_ITEMWIDTH(cf
->menu
);
904 if ((size_t) ui_cols
<= nc
)
906 return (ui_cols
- nc
) / 2;
908 size_t len
= MENU_H_ITEMWIDTH(cf
->menu
) + MENU_H_BETWEEN
; /* item width */
909 size_t items
= ui_cols
/ len
; /* items per line */
910 size_t extra
= items
< cf
->menu
->nitems
? /* extra space on line */
911 ui_cols
% len
: /* - multi-line menu */
912 ui_cols
- (cf
->menu
->nitems
* len
); /* - one line menu */
915 return 0; /* hmm... no space */
917 extra
+= MENU_H_BETWEEN
; /* add padding after last item to extra */
920 return (idx
* len
) + (extra
/ 2);
921 return ((idx
% items
) * len
) + (extra
/ 2);
925 static int menuitem_on_page(struct cfdisk
*cf
, size_t idx
)
927 struct cfdisk_menu
*m
= cf
->menu
;
929 if (m
->page_sz
== 0 ||
930 m
->idx
/ m
->page_sz
== idx
/ m
->page_sz
)
935 static struct cfdisk_menuitem
*menu_get_menuitem(struct cfdisk
*cf
, size_t idx
)
937 struct cfdisk_menuitem
*d
;
940 for (i
= 0, d
= cf
->menu
->items
; d
->name
; d
++) {
941 if (cf
->menu
->ignore
&& strchr(cf
->menu
->ignore
, d
->key
))
950 static struct cfdisk_menuitem
*menu_get_menuitem_by_key(struct cfdisk
*cf
,
951 int key
, size_t *idx
)
953 struct cfdisk_menuitem
*d
;
955 for (*idx
= 0, d
= cf
->menu
->items
; d
->name
; d
++) {
956 if (cf
->menu
->ignore
&& strchr(cf
->menu
->ignore
, d
->key
))
966 static void ui_draw_menuitem(struct cfdisk
*cf
,
967 struct cfdisk_menuitem
*d
,
970 char buf
[80 * MB_CUR_MAX
], *ptr
= buf
;
973 int ln
, cl
, vert
= cf
->menu
->vertical
;
975 if (!menuitem_on_page(cf
, idx
))
976 return; /* no visible item */
977 ln
= menuitem_get_line(cf
, idx
);
978 cl
= menuitem_get_column(cf
, idx
);
982 width
= cf
->menu
->width
+ MENU_V_SPADDING
;
983 memset(ptr
, ' ', MENU_V_SPADDING
);
984 ptr
+= MENU_V_SPADDING
;
986 width
= MENU_H_SPADDING
+ cf
->menu
->width
+ MENU_H_SPADDING
;
989 mbsalign(name
, ptr
, sizeof(buf
), &width
,
990 vert
? MBS_ALIGN_LEFT
: MBS_ALIGN_CENTER
,
993 DBG(MENU
, ul_debug("menuitem: cl=%d, ln=%d, item='%s'",
997 mvaddch(ln
, cl
- 1, ACS_VLINE
);
998 mvaddch(ln
, cl
+ MENU_V_ITEMWIDTH(cf
->menu
), ACS_VLINE
);
1001 if (cf
->menu
->idx
== idx
)
1005 mvprintw(ln
, cl
, "%s", buf
);
1007 mvprintw(ln
, cl
, "%s%s%s", MENU_H_PRESTR
, buf
, MENU_H_POSTSTR
);
1009 if (cf
->menu
->idx
== idx
) {
1012 ui_hint(_(d
->desc
));
1016 static void ui_clean_menu(struct cfdisk
*cf
)
1020 struct cfdisk_menu
*m
= cf
->menu
;
1021 size_t ln
= menuitem_get_line(cf
, 0);
1024 lastline
= ln
+ (m
->page_sz
? m
->page_sz
: m
->nitems
);
1026 lastline
= menuitem_get_line(cf
, m
->nitems
);
1028 for (i
= ln
; i
<= lastline
; i
++) {
1031 DBG(MENU
, ul_debug("clean_menu: line %zu", i
));
1040 static void ui_draw_menu(struct cfdisk
*cf
)
1042 struct cfdisk_menuitem
*d
;
1043 struct cfdisk_menu
*m
;
1045 size_t ln
= menuitem_get_line(cf
, 0);
1051 DBG(MENU
, ul_debug("draw start"));
1057 nlines
= m
->page_sz
? m
->page_sz
: m
->nitems
;
1059 nlines
= menuitem_get_line(cf
, m
->nitems
);
1062 menu_update_ignore(cf
);
1064 while ((d
= menu_get_menuitem(cf
, i
)))
1065 ui_draw_menuitem(cf
, d
, i
++);
1068 size_t cl
= menuitem_get_column(cf
, 0);
1069 size_t curpg
= m
->page_sz
? m
->idx
/ m
->page_sz
: 0;
1071 /* corners and horizontal lines */
1072 mvaddch(ln
- 1, cl
- 1, ACS_ULCORNER
);
1073 mvaddch(ln
+ nlines
, cl
- 1, ACS_LLCORNER
);
1075 for (i
= 0; i
< MENU_V_ITEMWIDTH(m
); i
++) {
1076 mvaddch(ln
- 1, cl
+ i
, ACS_HLINE
);
1077 mvaddch(ln
+ nlines
, cl
+ i
, ACS_HLINE
);
1080 mvaddch(ln
- 1, cl
+ i
, ACS_URCORNER
);
1081 mvaddch(ln
+ nlines
, cl
+ i
, ACS_LRCORNER
);
1083 /* draw also lines around empty lines on last page */
1085 m
->nitems
/ m
->page_sz
== m
->idx
/ m
->page_sz
) {
1086 for (i
= m
->nitems
% m
->page_sz
+ 1; i
<= m
->page_sz
; i
++) {
1087 mvaddch(i
, cl
- 1, ACS_VLINE
);
1088 mvaddch(i
, cl
+ MENU_V_ITEMWIDTH(m
), ACS_VLINE
);
1093 mvprintw(ln
- 1, cl
, " %s ", m
->title
);
1097 mvaddch(ln
- 1, cl
+ MENU_V_ITEMWIDTH(m
) - 2, ACS_UARROW
);
1098 if (m
->page_sz
&& curpg
< m
->nitems
/ m
->page_sz
)
1099 mvaddch(ln
+ nlines
, cl
+ MENU_V_ITEMWIDTH(m
) - 2, ACS_DARROW
);
1102 DBG(MENU
, ul_debug("draw end."));
1105 static void ui_menu_goto(struct cfdisk
*cf
, int where
)
1107 struct cfdisk_menuitem
*d
;
1110 /* stop and begin/end for vertical menus */
1111 if (cf
->menu
->vertical
) {
1114 else if (where
> (int) cf
->menu
->nitems
- 1)
1115 where
= cf
->menu
->nitems
- 1;
1117 /* continue from begin/end */
1119 where
= cf
->menu
->nitems
- 1;
1120 else if ((size_t) where
> cf
->menu
->nitems
- 1)
1123 if ((size_t) where
== cf
->menu
->idx
)
1128 old
= cf
->menu
->idx
;
1129 cf
->menu
->idx
= where
;
1131 if (!menuitem_on_page(cf
, old
)) {
1136 d
= menu_get_menuitem(cf
, old
);
1137 ui_draw_menuitem(cf
, d
, old
);
1139 d
= menu_get_menuitem(cf
, where
);
1140 ui_draw_menuitem(cf
, d
, where
);
1143 static int ui_menu_move(struct cfdisk
*cf
, int key
)
1145 struct cfdisk_menu
*m
;
1151 return 0; /* ignore errors */
1155 DBG(MENU
, ul_debug("menu move key >%c<.", key
));
1161 case '\016': /* ^N */
1162 case 'j': /* Vi-like alternative */
1163 ui_menu_goto(cf
, m
->idx
+ 1);
1166 case '\020': /* ^P */
1167 case 'k': /* Vi-like alternative */
1168 ui_menu_goto(cf
, (int) m
->idx
- 1);
1172 ui_menu_goto(cf
, (int) m
->idx
- m
->page_sz
);
1177 ui_menu_goto(cf
, 0);
1181 ui_menu_goto(cf
, m
->idx
+ m
->page_sz
);
1186 ui_menu_goto(cf
, m
->nitems
);
1193 ui_menu_goto(cf
, m
->idx
+ 1);
1199 ui_menu_goto(cf
, (int) m
->idx
- 1);
1204 return 1; /* key irrelevant for menu move */
1207 /* but don't call me from ui_run(), this is for pop-up menus only */
1208 static void ui_menu_resize(struct cfdisk
*cf
)
1212 menu_refresh_size(cf
);
1217 static int partition_on_page(struct cfdisk
*cf
, size_t i
)
1219 if (cf
->page_sz
== 0 ||
1220 cf
->lines_idx
/ cf
->page_sz
== i
/ cf
->page_sz
)
1225 static void ui_draw_partition(struct cfdisk
*cf
, size_t i
)
1227 int ln
= TABLE_START_LINE
+ 1 + i
; /* skip table header */
1228 int cl
= ARROW_CURSOR_WIDTH
; /* we need extra space for cursor */
1229 int cur
= cf
->lines_idx
== i
;
1233 if (!partition_on_page(cf
, i
))
1235 ln
= TABLE_START_LINE
+ (i
% cf
->page_sz
) + 1;
1236 curpg
= cf
->lines_idx
/ cf
->page_sz
;
1240 "draw partition %zu [page_sz=%zu, "
1241 "line=%d, idx=%zu]",
1242 i
, cf
->page_sz
, ln
, cf
->lines_idx
));
1246 mvaddstr(ln
, 0, ARROW_CURSOR_STRING
);
1247 mvaddstr(ln
, cl
, cf
->lines
[i
+ 1]);
1252 if (colors_wanted() && is_freespace(cf
, i
)) {
1253 attron(COLOR_PAIR(CFDISK_CL_FREESPACE
));
1256 mvaddstr(ln
, 0, ARROW_CURSOR_DUMMY
);
1257 mvaddstr(ln
, cl
, cf
->lines
[i
+ 1]);
1259 attroff(COLOR_PAIR(CFDISK_CL_FREESPACE
));
1262 if ((size_t) ln
== MENU_START_LINE
- 1 &&
1263 cf
->page_sz
&& curpg
< cf
->nlines
/ cf
->page_sz
) {
1266 mvaddch(ln
, ui_cols
- 1, ACS_DARROW
);
1267 mvaddch(ln
, 0, ACS_DARROW
);
1273 static int ui_draw_table(struct cfdisk
*cf
)
1275 int cl
= ARROW_CURSOR_WIDTH
;
1276 size_t i
, nparts
= fdisk_table_get_nents(cf
->table
);
1277 size_t curpg
= cf
->page_sz
? cf
->lines_idx
/ cf
->page_sz
: 0;
1279 DBG(UI
, ul_debug("draw table"));
1281 for (i
= TABLE_START_LINE
; i
<= TABLE_START_LINE
+ cf
->page_sz
; i
++) {
1286 if ((size_t) cf
->lines_idx
> nparts
- 1)
1287 cf
->lines_idx
= nparts
? nparts
- 1 : 0;
1291 mvaddstr(TABLE_START_LINE
, cl
, cf
->lines
[0]);
1294 /* print partitions */
1295 for (i
= 0; i
< nparts
; i
++)
1296 ui_draw_partition(cf
, i
);
1299 mvaddch(TABLE_START_LINE
, ui_cols
- 1, ACS_UARROW
);
1300 mvaddch(TABLE_START_LINE
, 0, ACS_UARROW
);
1302 if (cf
->page_sz
&& curpg
< cf
->nlines
/ cf
->page_sz
) {
1303 mvaddch(MENU_START_LINE
- 1, ui_cols
- 1, ACS_DARROW
);
1304 mvaddch(MENU_START_LINE
- 1, 0, ACS_DARROW
);
1309 static int ui_table_goto(struct cfdisk
*cf
, int where
)
1312 size_t nparts
= fdisk_table_get_nents(cf
->table
);
1314 DBG(UI
, ul_debug("goto table %d", where
));
1318 else if ((size_t) where
> nparts
- 1)
1321 if ((size_t) where
== cf
->lines_idx
)
1324 old
= cf
->lines_idx
;
1325 cf
->lines_idx
= where
;
1327 if (!partition_on_page(cf
, old
) ||!partition_on_page(cf
, where
))
1330 ui_draw_partition(cf
, old
); /* cleanup old */
1331 ui_draw_partition(cf
, where
); /* draw new */
1339 static int ui_refresh(struct cfdisk
*cf
)
1341 struct fdisk_label
*lb
;
1343 uint64_t bytes
= fdisk_get_nsectors(cf
->cxt
) * fdisk_get_sector_size(cf
->cxt
);
1350 strsz
= size_to_human_string(SIZE_SUFFIX_SPACE
1351 | SIZE_SUFFIX_3LETTER
, bytes
);
1353 lb
= fdisk_get_label(cf
->cxt
, NULL
);
1358 ui_center(0, _("Disk: %s"), fdisk_get_devname(cf
->cxt
));
1360 ui_center(1, _("Size: %s, %ju bytes, %ju sectors"),
1361 strsz
, bytes
, (uintmax_t) fdisk_get_nsectors(cf
->cxt
));
1362 if (fdisk_get_disklabel_id(cf
->cxt
, &id
) == 0 && id
)
1363 ui_center(2, _("Label: %s, identifier: %s"),
1364 fdisk_label_get_name(lb
), id
);
1366 ui_center(2, _("Label: %s"), fdisk_label_get_name(lb
));
1375 static ssize_t
ui_get_string(struct cfdisk
*cf
, const char *prompt
,
1376 const char *hint
, char *buf
, size_t len
)
1379 ssize_t i
= 0, rc
= -1;
1380 int ln
= MENU_START_LINE
, cl
= 1;
1393 mvaddstr(ln
, cl
, (char *) prompt
);
1394 cl
+= mbs_safe_width(prompt
);
1400 cells
= mbs_safe_width(buf
);
1401 mvaddstr(ln
, cl
, buf
);
1409 move(ln
, cl
+ cells
);
1414 #if !defined(HAVE_SLCURSES_H) && !defined(HAVE_SLANG_SLCURSES_H) && \
1415 defined(HAVE_LIBNCURSESW) && defined(HAVE_WIDECHAR)
1417 if (get_wch(&c
) == ERR
) {
1420 if ((c
= getch()) == ERR
) {
1426 if (!isatty(STDIN_FILENO
))
1431 if (c
== '\r' || c
== '\n' || c
== KEY_ENTER
)
1436 rc
= -CFDISK_ERR_ESC
;
1443 i
= mbs_truncate(buf
, &cells
);
1446 mvaddch(ln
, cl
+ cells
, ' ');
1447 move(ln
, cl
+ cells
);
1452 #if defined(HAVE_LIBNCURSESW) && defined(HAVE_WIDECHAR)
1453 if (i
+ 1 < (ssize_t
) len
&& iswprint(c
)) {
1454 wchar_t wc
= (wchar_t) c
;
1455 char s
[MB_CUR_MAX
+ 1];
1456 int sz
= wctomb(s
, wc
);
1458 if (sz
> 0 && sz
+ i
< (ssize_t
) len
) {
1460 mvaddnstr(ln
, cl
+ cells
, s
, sz
);
1461 memcpy(buf
+ i
, s
, sz
);
1464 cells
+= wcwidth(wc
);
1469 if (i
+ 1 < (ssize_t
) len
&& isprint(c
)) {
1470 mvaddch(ln
, cl
+ cells
, c
);
1482 rc
= i
; /* success */
1492 /* @res is default value as well as result in bytes */
1493 static int ui_get_size(struct cfdisk
*cf
, const char *prompt
, uintmax_t *res
,
1494 uintmax_t low
, uintmax_t up
, int *expsize
)
1499 char *dflt
= size_to_human_string(0, *res
);
1501 DBG(UI
, ul_debug("get_size (default=%ju)", *res
));
1506 int pwr
= 0, insec
= 0;
1508 snprintf(buf
, sizeof(buf
), "%s", dflt
);
1509 rc
= ui_get_string(cf
, prompt
,
1510 _("May be followed by M for MiB, G for GiB, "
1511 "T for TiB, or S for sectors."),
1514 ui_warnx(_("Please, specify size."));
1515 continue; /* nothing specified */
1516 } else if (rc
== -CFDISK_ERR_ESC
)
1517 break; /* cancel dialog */
1519 if (strcmp(buf
, dflt
) == 0)
1520 user
= *res
, rc
= 0; /* no change, use default */
1522 size_t len
= strlen(buf
);
1523 if (buf
[len
- 1] == 'S' || buf
[len
- 1] == 's') {
1525 buf
[len
- 1] = '\0';
1527 rc
= parse_size(buf
, &user
, &pwr
); /* parse */
1531 DBG(UI
, ul_debug("get_size user=%ju, power=%d, sectors=%s",
1532 user
, pwr
, insec
? "yes" : "no"));
1534 user
*= fdisk_get_sector_size(cf
->cxt
);
1536 ui_warnx(_("Minimum size is %ju bytes."), low
);
1539 if (user
> up
&& pwr
&& user
< up
+ (1ULL << pwr
* 10))
1540 /* ignore when the user specified size overflow
1541 * with in range specified by suffix (e.g. MiB) */
1545 ui_warnx(_("Maximum size is %ju bytes."), up
);
1548 if (rc
== 0 && insec
&& expsize
)
1552 ui_warnx(_("Failed to parse size."));
1559 DBG(UI
, ul_debug("get_size (result=%ju, rc=%zd)", *res
, rc
));
1563 static struct fdisk_parttype
*ui_get_parttype(struct cfdisk
*cf
,
1564 struct fdisk_parttype
*cur
)
1566 struct cfdisk_menuitem
*d
, *cm
;
1567 size_t i
= 0, nitems
, idx
= 0;
1568 struct fdisk_parttype
*t
= NULL
;
1569 struct fdisk_label
*lb
;
1572 DBG(UI
, ul_debug("asking for parttype."));
1574 lb
= fdisk_get_label(cf
->cxt
, NULL
);
1576 /* create cfdisk menu according to label types, note that the
1577 * last cm[] item has to be empty -- so nitems + 1 */
1578 nitems
= fdisk_label_get_nparttypes(lb
);
1582 cm
= xcalloc(nitems
+ 1, sizeof(struct cfdisk_menuitem
));
1586 codetypes
= fdisk_label_has_code_parttypes(lb
);
1588 for (i
= 0; i
< nitems
; i
++) {
1589 const struct fdisk_parttype
*x
= fdisk_label_get_parttype(lb
, i
);
1592 cm
[i
].userdata
= (void *) x
;
1594 xasprintf(&name
, "%2x %s",
1595 fdisk_parttype_get_code(x
),
1596 _(fdisk_parttype_get_name(x
)));
1598 name
= (char *) _(fdisk_parttype_get_name(x
));
1599 cm
[i
].desc
= fdisk_parttype_get_string(x
);
1606 /* make the new menu active */
1608 cf
->menu
->vertical
= 1;
1609 cf
->menu
->idx
= idx
;
1610 menu_set_title(cf
->menu
, _("Select partition type"));
1619 if (ui_menu_move(cf
, key
) == 0)
1626 d
= menu_get_menuitem(cf
, cf
->menu
->idx
);
1628 t
= (struct fdisk_parttype
*) d
->userdata
;
1640 for (i
= 0; i
< nitems
; i
++)
1641 free((char *) cm
[i
].name
);
1644 DBG(UI
, ul_debug("get parrtype done [type=%s] ", t
?
1645 fdisk_parttype_get_name(t
) : NULL
));
1649 static int ui_script_read(struct cfdisk
*cf
)
1651 struct fdisk_script
*sc
= NULL
;
1652 char buf
[PATH_MAX
] = { 0 };
1655 rc
= ui_get_string(cf
, _("Enter script file name: "),
1656 _("The script file will be applied to in-memory partition table."),
1663 sc
= fdisk_new_script_from_file(cf
->cxt
, buf
);
1665 ui_warn(_("Cannot open %s"), buf
);
1667 ui_warnx(_("Failed to parse script file %s"), buf
);
1668 else if (fdisk_apply_script(cf
->cxt
, sc
) != 0)
1669 ui_warnx(_("Failed to apply script %s"), buf
);
1673 fdisk_unref_script(sc
);
1677 static int ui_script_write(struct cfdisk
*cf
)
1679 struct fdisk_script
*sc
= NULL
;
1680 char buf
[PATH_MAX
] = { 0 };
1684 rc
= ui_get_string(cf
, _("Enter script file name: "),
1685 _("The current in-memory partition table will be dumped to the file."),
1691 sc
= fdisk_new_script(cf
->cxt
);
1693 ui_warn(_("Failed to allocate script handler"));
1697 rc
= fdisk_script_read_context(sc
, NULL
);
1699 ui_warnx(_("Failed to read disk layout into script."));
1703 DBG(UI
, ul_debug("writing dump into: '%s'", buf
));
1704 f
= fopen(buf
, "w");
1706 ui_warn(_("Cannot open %s"), buf
);
1711 rc
= fdisk_script_write_file(sc
, f
);
1713 ui_info(_("Disk layout successfully dumped."));
1716 ui_warn(_("Failed to write script %s"), buf
);
1719 fdisk_unref_script(sc
);
1723 /* prints menu with libfdisk labels and waits for users response */
1724 static int ui_create_label(struct cfdisk
*cf
)
1726 struct cfdisk_menuitem
*d
, *cm
;
1728 size_t i
= 0, nitems
;
1729 struct fdisk_label
*lb
= NULL
;
1733 DBG(UI
, ul_debug("asking for new disklabe."));
1735 /* create cfdisk menu according to libfdisk labels, note that the
1736 * last cm[] item has to be empty -- so nitems + 1 */
1737 nitems
= fdisk_get_nlabels(cf
->cxt
);
1738 cm
= xcalloc(nitems
+ 1, sizeof(struct cfdisk_menuitem
));
1740 while (fdisk_next_label(cf
->cxt
, &lb
) == 0) {
1741 if (fdisk_label_is_disabled(lb
) ||
1742 fdisk_label_get_type(lb
) == FDISK_DISKLABEL_BSD
)
1744 cm
[i
++].name
= fdisk_label_get_name(lb
);
1748 ui_center(ui_lines
- 4,
1749 _("Device does not contain a recognized partition table."));
1750 ui_center(ui_lines
- 3,
1751 _("Select a type to create a new label or press 'L' to load script file."));
1753 /* make the new menu active */
1755 cf
->menu
->vertical
= 1;
1756 menu_set_title(cf
->menu
, _("Select label type"));
1765 if (ui_menu_move(cf
, key
) == 0)
1771 d
= menu_get_menuitem(cf
, cf
->menu
->idx
);
1773 rc
= fdisk_create_disklabel(cf
->cxt
, d
->name
);
1783 rc
= ui_script_read(cf
);
1793 DBG(UI
, ul_debug("create label done [rc=%d] ", rc
));
1798 static int ui_help(void)
1801 static const char *help
[] = {
1802 N_("This is cfdisk, a curses-based disk partitioning program."),
1803 N_("It lets you create, delete, and modify partitions on a block device."),
1805 N_("Command Meaning"),
1806 N_("------- -------"),
1807 N_(" b Toggle bootable flag of the current partition"),
1808 N_(" d Delete the current partition"),
1809 N_(" h Print this screen"),
1810 N_(" n Create new partition from free space"),
1811 N_(" q Quit program without writing partition table"),
1812 N_(" s Fix partitions order (only when in disarray)"),
1813 N_(" t Change the partition type"),
1814 N_(" u Dump disk layout to sfdisk compatible script file"),
1815 N_(" W Write partition table to disk (you must enter uppercase W);"),
1816 N_(" since this might destroy data on the disk, you must either"),
1817 N_(" confirm or deny the write by entering 'yes' or 'no'"),
1818 N_("Up Arrow Move cursor to the previous partition"),
1819 N_("Down Arrow Move cursor to the next partition"),
1820 N_("Left Arrow Move cursor to the previous menu item"),
1821 N_("Right Arrow Move cursor to the next menu item"),
1823 N_("Note: All of the commands can be entered with either upper or lower"),
1824 N_("case letters (except for Write)."),
1826 N_("Use lsblk(8) or partx(8) to see more details about the device.")
1830 for (i
= 0; i
< ARRAY_SIZE(help
); i
++)
1831 mvaddstr(i
, 1, _(help
[i
]));
1833 ui_info(_("Press a key to continue."));
1839 /* TODO: use @sz, now 128bytes */
1840 static int main_menu_ignore_keys(struct cfdisk
*cf
, char *ignore
,
1841 size_t sz
__attribute__((__unused__
)))
1843 struct fdisk_partition
*pa
= get_current_partition(cf
);
1848 if (fdisk_partition_is_freespace(pa
)) {
1849 ignore
[i
++] = 'd'; /* delete */
1850 ignore
[i
++] = 't'; /* set type */
1851 ignore
[i
++] = 'b'; /* set bootable */
1854 if (!fdisk_is_label(cf
->cxt
, DOS
) &&
1855 !fdisk_is_label(cf
->cxt
, SGI
))
1859 if (!cf
->wrong_order
)
1861 if (fdisk_is_readonly(cf
->cxt
))
1867 /* returns: error: < 0, success: 0, quit: 1 */
1868 static int main_menu_action(struct cfdisk
*cf
, int key
)
1871 int ref
= 0, rc
, org_order
= cf
->wrong_order
;
1872 const char *info
= NULL
, *warn
= NULL
;
1873 struct fdisk_partition
*pa
;
1880 struct cfdisk_menuitem
*d
= menu_get_menuitem(cf
, cf
->menu
->idx
);
1885 } else if (key
!= 'w' && key
!= 'W')
1886 key
= tolower(key
); /* case insensitive except 'W'rite */
1888 DBG(MENU
, ul_debug("main menu action: key=%c", key
));
1890 if (cf
->menu
->ignore
&& strchr(cf
->menu
->ignore
, key
)) {
1891 DBG(MENU
, ul_debug(" ignore '%c'", key
));
1895 pa
= get_current_partition(cf
);
1898 n
= fdisk_partition_get_partno(pa
);
1900 DBG(MENU
, ul_debug("menu action on %p", pa
));
1905 case 'b': /* Bootable flag */
1907 int fl
= fdisk_is_label(cf
->cxt
, DOS
) ? DOS_FLAG_ACTIVE
:
1908 fdisk_is_label(cf
->cxt
, SGI
) ? SGI_FLAG_BOOT
: 0;
1910 if (fl
&& fdisk_toggle_partition_flag(cf
->cxt
, n
, fl
))
1911 warn
= _("Could not toggle the flag.");
1919 case 'd': /* Delete */
1920 if (fdisk_delete_partition(cf
->cxt
, n
) != 0)
1921 warn
= _("Could not delete partition %zu.");
1923 info
= _("Partition %zu has been deleted.");
1926 case 'h': /* Help */
1933 uint64_t start
, size
, dflt_size
, secs
;
1934 struct fdisk_partition
*npa
; /* the new partition */
1935 int expsize
= 0; /* size specified explicitly in sectors */
1937 if (!fdisk_partition_is_freespace(pa
) || !fdisk_partition_has_start(pa
))
1940 /* free space range */
1941 start
= fdisk_partition_get_start(pa
);
1942 size
= dflt_size
= fdisk_partition_get_size(pa
) * fdisk_get_sector_size(cf
->cxt
);
1944 if (ui_get_size(cf
, _("Partition size: "), &size
, 1, size
, &expsize
)
1948 secs
= size
/ fdisk_get_sector_size(cf
->cxt
);
1949 if (size
&& secs
< 1) {
1950 warn
= _("Too small partition size specified.");
1954 npa
= fdisk_new_partition();
1958 if (dflt_size
== size
) /* default is to fillin all free space */
1959 fdisk_partition_end_follow_default(npa
, 1);
1961 fdisk_partition_set_size(npa
, secs
);
1964 fdisk_partition_size_explicit(pa
, 1);
1966 fdisk_partition_set_start(npa
, start
);
1967 fdisk_partition_partno_follow_default(npa
, 1);
1968 /* add to disk label -- libfdisk will ask for missing details */
1969 rc
= fdisk_add_partition(cf
->cxt
, npa
, NULL
);
1970 fdisk_unref_partition(npa
);
1975 case 'q': /* Quit */
1977 case 't': /* Type */
1979 struct fdisk_parttype
*t
;
1981 if (fdisk_partition_is_freespace(pa
))
1983 t
= (struct fdisk_parttype
*) fdisk_partition_get_type(pa
);
1984 t
= ui_get_parttype(cf
, t
);
1987 if (t
&& fdisk_set_partition_type(cf
->cxt
, n
, t
) == 0)
1988 info
= _("Changed type of partition %zu.");
1990 info
= _("The type of partition %zu is unchanged.");
1993 case 's': /* Sort */
1994 if (cf
->wrong_order
) {
1995 fdisk_reorder_partitions(cf
->cxt
);
1999 case 'u': /* dUmp */
2000 ui_script_write(cf
);
2002 case 'W': /* Write */
2004 char buf
[64] = { 0 };
2006 if (fdisk_is_readonly(cf
->cxt
)) {
2007 warn
= _("Device is open in read-only mode.");
2011 rc
= ui_get_string(cf
,
2012 _("Are you sure you want to write the partition "
2014 _("Type \"yes\" or \"no\", or press ESC to leave this dialog."),
2018 if (rc
<= 0 || (strcasecmp(buf
, "yes") != 0 &&
2019 strcasecmp(buf
, _("yes")) != 0)) {
2020 info
= _("Did not write partition table to disk.");
2023 rc
= fdisk_write_disklabel(cf
->cxt
);
2025 warn
= _("Failed to write disklabel.");
2027 fdisk_reread_partition_table(cf
->cxt
);
2028 info
= _("The partition table has been altered.");
2046 ui_warnx(warn
, n
+ 1);
2048 ui_info(info
, n
+ 1);
2049 else if (key
== 'n' && cf
->wrong_order
&& org_order
== 0)
2050 ui_info(_("Note that partition table entries are not in disk order now."));
2055 static void ui_resize_refresh(struct cfdisk
*cf
)
2058 menu_refresh_size(cf
);
2063 static int ui_run(struct cfdisk
*cf
)
2069 DBG(UI
, ul_debug("start cols=%zu, lines=%zu", ui_cols
, ui_lines
));
2071 if (!fdisk_has_label(cf
->cxt
) || cf
->zero_start
) {
2072 rc
= ui_create_label(cf
);
2074 ui_errx(EXIT_FAILURE
,
2075 _("failed to create a new disklabel"));
2081 rc
= lines_refresh(cf
);
2083 ui_errx(EXIT_FAILURE
, _("failed to read partitions"));
2085 menu_push(cf
, main_menuitems
);
2086 cf
->menu
->ignore_cb
= main_menu_ignore_keys
;
2088 rc
= ui_refresh(cf
);
2092 if (fdisk_is_readonly(cf
->cxt
))
2093 ui_warnx(_("Device open in read-only mode."));
2100 /* Note that ncurses getch() returns ERR when interrupted
2101 * by signal, but SLang does not interrupt at all. */
2102 ui_resize_refresh(cf
);
2105 if (ui_menu_move(cf
, key
) == 0)
2108 DBG(UI
, ul_debug("main action key >%c<.", key
));
2112 case '\016': /* ^N */
2113 case 'j': /* Vi-like alternative */
2114 ui_table_goto(cf
, cf
->lines_idx
+ 1);
2117 case '\020': /* ^P */
2118 case 'k': /* Vi-like alternative */
2119 ui_table_goto(cf
, (int) cf
->lines_idx
- 1);
2123 ui_table_goto(cf
, (int) cf
->lines_idx
- cf
->page_sz
);
2127 ui_table_goto(cf
, 0);
2131 ui_table_goto(cf
, cf
->lines_idx
+ cf
->page_sz
);
2135 ui_table_goto(cf
, (int) cf
->nlines
- 1);
2140 rc
= main_menu_action(cf
, 0);
2143 rc
= main_menu_action(cf
, key
);
2155 DBG(UI
, ul_debug("end"));
2159 static void __attribute__ ((__noreturn__
)) usage(FILE *out
)
2161 fputs(USAGE_HEADER
, out
);
2163 _(" %1$s [options] <disk>\n"), program_invocation_short_name
);
2165 fputs(USAGE_SEPARATOR
, out
);
2166 fputs(_("Display or manipulate a disk partition table.\n"), out
);
2168 fputs(USAGE_OPTIONS
, out
);
2169 fputs(_(" -L --color[=<when>] colorize output (auto, always or never)\n"), out
);
2170 fputs(_(" -z --zero start with zeroed partition table\n"), out
);
2172 fputs(USAGE_SEPARATOR
, out
);
2173 fputs(USAGE_HELP
, out
);
2174 fputs(USAGE_VERSION
, out
);
2176 fprintf(out
, USAGE_MAN_TAIL("cfdisk(8)"));
2177 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
2180 int main(int argc
, char *argv
[])
2182 const char *diskpath
;
2183 int rc
, c
, colormode
= UL_COLORMODE_UNDEF
;
2184 struct cfdisk _cf
= { .lines_idx
= 0 },
2187 static const struct option longopts
[] = {
2188 { "color", optional_argument
, NULL
, 'L' },
2189 { "help", no_argument
, NULL
, 'h' },
2190 { "version", no_argument
, NULL
, 'V' },
2191 { "zero", no_argument
, NULL
, 'z' },
2195 setlocale(LC_ALL
, "");
2196 bindtextdomain(PACKAGE
, LOCALEDIR
);
2197 textdomain(PACKAGE
);
2198 atexit(close_stdout
);
2200 while((c
= getopt_long(argc
, argv
, "L::hVz", longopts
, NULL
)) != -1) {
2206 colormode
= UL_COLORMODE_AUTO
;
2208 colormode
= colormode_or_err(optarg
,
2209 _("unsupported color mode"));
2212 printf(UTIL_LINUX_VERSION
);
2213 return EXIT_SUCCESS
;
2220 colors_init(colormode
, "cfdisk");
2222 fdisk_init_debug(0);
2223 scols_init_debug(0);
2224 cfdisk_init_debug();
2225 cf
->cxt
= fdisk_new_context();
2227 err(EXIT_FAILURE
, _("failed to allocate libfdisk context"));
2229 fdisk_set_ask(cf
->cxt
, ask_callback
, (void *) cf
);
2232 diskpath
= access(DEFAULT_DEVICE
, F_OK
) == 0 ?
2233 DEFAULT_DEVICE
: ALTERNATE_DEVICE
;
2235 diskpath
= argv
[optind
];
2237 rc
= fdisk_assign_device(cf
->cxt
, diskpath
, 0);
2239 rc
= fdisk_assign_device(cf
->cxt
, diskpath
, 1);
2241 err(EXIT_FAILURE
, _("cannot open %s"),
2242 optind
== argc
? DEFAULT_DEVICE
: diskpath
);
2244 /* Don't use err(), warn() from this point */
2251 fdisk_unref_table(cf
->table
);
2253 rc
= fdisk_deassign_device(cf
->cxt
, cf
->nwrites
== 0);
2254 fdisk_unref_context(cf
->cxt
);
2255 DBG(MISC
, ul_debug("bye! [rc=%d]", rc
));
2256 return rc
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
;