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)
73 #define MENU_PADDING 2
74 #define TABLE_START_LINE 4
75 #define MENU_START_LINE (ui_lines - 5)
76 #define INFO_LINE (ui_lines - 2)
77 #define HINT_LINE (ui_lines - 1)
79 #define CFDISK_ERR_ESC 5000
82 # define KEY_ESC '\033'
85 # define KEY_DELETE '\177'
94 static const int color_pairs
[][2] = {
95 /* color foreground, background */
96 [CFDISK_CL_WARNING
] = { COLOR_RED
, -1 },
97 [CFDISK_CL_FREESPACE
] = { COLOR_GREEN
, -1 }
102 static struct cfdisk_menuitem
*menu_get_menuitem(struct cfdisk
*cf
, size_t idx
);
103 static struct cfdisk_menuitem
*menu_get_menuitem_by_key(struct cfdisk
*cf
, int key
, size_t *idx
);
104 static struct cfdisk_menu
*menu_push(struct cfdisk
*cf
, struct cfdisk_menuitem
*item
);
105 static struct cfdisk_menu
*menu_pop(struct cfdisk
*cf
);
106 static void menu_refresh_size(struct cfdisk
*cf
);
108 static int ui_refresh(struct cfdisk
*cf
);
109 static void ui_warnx(const char *fmt
, ...);
110 static void ui_warn(const char *fmt
, ...);
111 static void ui_info(const char *fmt
, ...);
112 static void ui_draw_menu(struct cfdisk
*cf
);
113 static int ui_menu_move(struct cfdisk
*cf
, int key
);
114 static void ui_menu_resize(struct cfdisk
*cf
);
116 static int ui_get_size(struct cfdisk
*cf
, const char *prompt
, uintmax_t *res
,
117 uintmax_t low
, uintmax_t up
, int *expsize
);
119 static int ui_enabled
;
120 static int ui_resize
;
122 /* ncurses LINES and COLS may be actual variables or *macros*, but we need
123 * something portable and writable */
128 struct cfdisk_menuitem
{
129 int key
; /* keyboard shortcut */
130 const char *name
; /* item name */
131 const char *desc
; /* item description (hint) */
137 char *title
; /* optional menu title */
138 struct cfdisk_menuitem
*items
; /* array with menu items */
139 char *ignore
;/* string with keys to ignore */
140 size_t width
; /* maximal width of the menu item */
141 size_t nitems
; /* number of the active menu items */
142 size_t page_sz
;/* when menu longer than screen */
143 size_t idx
; /* the current menu item */
144 struct cfdisk_menu
*prev
;
146 /* @ignore keys generator */
147 int (*ignore_cb
) (struct cfdisk
*, char *, size_t);
149 unsigned int vertical
: 1; /* enable vertical mode */
153 static struct cfdisk_menuitem main_menuitems
[] = {
154 { 'b', N_("Bootable"), N_("Toggle bootable flag of the current partition") },
155 { 'd', N_("Delete"), N_("Delete the current partition") },
156 { 'n', N_("New"), N_("Create new partition from free space") },
157 { 'q', N_("Quit"), N_("Quit program without writing partition table") },
158 { 't', N_("Type"), N_("Change the partition type") },
159 { 'h', N_("Help"), N_("Print help screen") },
160 { 's', N_("Sort"), N_("Fix partitions order") },
161 { 'W', N_("Write"), N_("Write partition table to disk (this might destroy data)") },
165 /* top level control struct */
167 struct fdisk_context
*cxt
; /* libfdisk context */
168 struct fdisk_table
*table
; /* partition table */
169 struct cfdisk_menu
*menu
; /* the current menu */
171 int *fields
; /* output columns IDs */
172 size_t nfields
; /* number of columns IDs */
174 char *linesbuf
; /* table as string */
175 size_t linesbufsz
; /* size of the tb_buf */
177 char **lines
; /* array with lines */
178 size_t nlines
; /* number of lines */
179 size_t lines_idx
; /* current line <0..N>, exclude header */
182 unsigned int nwrites
; /* fdisk_write_disklabel() counter */
184 unsigned int wrong_order
:1, /* PT not in right order */
185 zero_start
:1; /* ignore existing partition table */
190 * let's use include/debug.h stuff for cfdisk too
192 UL_DEBUG_DEFINE_MASK(cfdisk
);
193 UL_DEBUG_DEFINE_MASKANEMS(cfdisk
) = UL_DEBUG_EMPTY_MASKNAMES
;
195 #define CFDISK_DEBUG_INIT (1 << 1)
196 #define CFDISK_DEBUG_UI (1 << 2)
197 #define CFDISK_DEBUG_MENU (1 << 3)
198 #define CFDISK_DEBUG_MISC (1 << 4)
199 #define CFDISK_DEBUG_TABLE (1 << 5)
200 #define CFDISK_DEBUG_ALL 0xFFFF
202 #define DBG(m, x) __UL_DBG(cfdisk, CFDISK_DEBUG_, m, x)
204 static void cfdisk_init_debug(void)
206 __UL_INIT_DEBUG(cfdisk
, CFDISK_DEBUG_
, 0, CFDISK_DEBUG
);
209 /* Initialize output columns -- we follow libfdisk fields (usually specific
212 static int cols_init(struct cfdisk
*cf
)
220 return fdisk_label_get_fields_ids(NULL
, cf
->cxt
, &cf
->fields
, &cf
->nfields
);
223 static void resize(void)
227 if (ioctl(fileno(stdout
), TIOCGWINSZ
, &ws
) != -1
228 && ws
.ws_row
&& ws
.ws_col
) {
229 ui_lines
= ws
.ws_row
;
232 resizeterm(ws
.ws_row
, ws
.ws_col
);
234 clearok(stdscr
, TRUE
);
238 DBG(UI
, ul_debug("ui: resize refresh ui_cols=%zu, ui_lines=%zu",
243 /* Reads partition in tree-like order from scols
245 static int partition_from_scols(struct fdisk_table
*tb
,
246 struct libscols_line
*ln
)
248 struct fdisk_partition
*pa
= scols_line_get_userdata(ln
);
250 fdisk_table_add_partition(tb
, pa
);
251 fdisk_unref_partition(pa
);
253 if (scols_line_has_children(ln
)) {
254 struct libscols_line
*chln
;
255 struct libscols_iter
*itr
= scols_new_iter(SCOLS_ITER_FORWARD
);
259 while (scols_line_next_child(ln
, itr
, &chln
) == 0)
260 partition_from_scols(tb
, chln
);
261 scols_free_iter(itr
);
266 static char *table_to_string(struct cfdisk
*cf
, struct fdisk_table
*tb
)
268 struct fdisk_partition
*pa
;
269 struct fdisk_label
*lb
;
270 struct fdisk_iter
*itr
= NULL
;
271 struct libscols_table
*table
= NULL
;
272 struct libscols_iter
*s_itr
= NULL
;
276 struct libscols_line
*ln
, *ln_cont
= NULL
;
278 DBG(TABLE
, ul_debug("convert to string"));
285 lb
= fdisk_get_label(cf
->cxt
, NULL
);
288 itr
= fdisk_new_iter(FDISK_ITER_FORWARD
);
292 /* get container (e.g. extended partition) */
293 while (fdisk_table_next_partition(tb
, itr
, &pa
) == 0) {
294 if (fdisk_partition_is_nested(pa
)) {
295 DBG(TABLE
, ul_debug("nested detected, using tree"));
296 tree
= SCOLS_FL_TREE
;
301 table
= scols_new_table();
304 scols_table_enable_maxout(table
, 1);
307 for (i
= 0; i
< cf
->nfields
; i
++) {
309 const struct fdisk_field
*field
=
310 fdisk_label_get_field(lb
, cf
->fields
[i
]);
314 if (fdisk_field_is_number(field
))
315 fl
|= SCOLS_FL_RIGHT
;
316 if (fdisk_field_get_id(field
) == FDISK_FIELD_TYPE
)
317 fl
|= SCOLS_FL_TRUNC
;
318 if (tree
&& fdisk_field_get_id(field
) == FDISK_FIELD_DEVICE
)
321 if (!scols_table_new_column(table
,
322 fdisk_field_get_name(field
),
323 fdisk_field_get_width(field
), fl
))
328 fdisk_reset_iter(itr
, FDISK_ITER_FORWARD
);
330 while (fdisk_table_next_partition(tb
, itr
, &pa
) == 0) {
331 struct libscols_line
*parent
= fdisk_partition_is_nested(pa
) ? ln_cont
: NULL
;
333 ln
= scols_table_new_line(table
, parent
);
336 for (i
= 0; i
< cf
->nfields
; i
++) {
339 if (fdisk_partition_to_string(pa
, cf
->cxt
,
340 cf
->fields
[i
], &cdata
))
342 scols_line_refer_data(ln
, i
, cdata
);
344 if (tree
&& fdisk_partition_is_container(pa
))
347 scols_line_set_userdata(ln
, (void *) pa
);
348 fdisk_ref_partition(pa
);
351 if (scols_table_is_empty(table
))
354 scols_table_reduce_termwidth(table
, ARROW_CURSOR_WIDTH
);
355 scols_print_table_to_string(table
, &res
);
357 /* scols_* code might reorder lines, let's reorder @tb according to the
358 * final output (it's no problem because partitions are addressed by
359 * parno stored within struct fdisk_partition) */
362 fdisk_reset_iter(itr
, FDISK_ITER_FORWARD
);
363 while (fdisk_table_next_partition(tb
, itr
, &pa
) == 0)
364 fdisk_table_remove_partition(tb
, pa
);
366 s_itr
= scols_new_iter(SCOLS_ITER_FORWARD
);
370 /* add all in the right order (don't forget the output is tree) */
371 while (scols_table_next_line(table
, s_itr
, &ln
) == 0) {
372 if (scols_line_get_parent(ln
))
374 if (partition_from_scols(tb
, ln
))
378 scols_unref_table(table
);
379 scols_free_iter(s_itr
);
380 fdisk_free_iter(itr
);
386 * Read data about partitions from libfdisk and prepare output lines.
388 static int lines_refresh(struct cfdisk
*cf
)
396 DBG(TABLE
, ul_debug("refreshing buffer"));
405 fdisk_unref_table(cf
->table
);
408 /* read partitions and free spaces into cf->table */
409 rc
= fdisk_get_partitions(cf
->cxt
, &cf
->table
);
411 rc
= fdisk_get_freespaces(cf
->cxt
, &cf
->table
);
415 cf
->linesbuf
= table_to_string(cf
, cf
->table
);
419 cf
->linesbufsz
= strlen(cf
->linesbuf
);
420 cf
->nlines
= fdisk_table_get_nents(cf
->table
) + 1; /* 1 for header line */
422 cf
->wrong_order
= fdisk_table_wrong_order(cf
->table
) ? 1 : 0;
424 if (MENU_START_LINE
- TABLE_START_LINE
< cf
->nlines
)
425 cf
->page_sz
= MENU_START_LINE
- TABLE_START_LINE
- 1;
427 cf
->lines
= xcalloc(cf
->nlines
, sizeof(char *));
429 for (p
= cf
->linesbuf
, i
= 0; p
&& i
< cf
->nlines
; i
++) {
441 static struct fdisk_partition
*get_current_partition(struct cfdisk
*cf
)
446 return fdisk_table_get_partition(cf
->table
, cf
->lines_idx
);
449 static int is_freespace(struct cfdisk
*cf
, size_t i
)
451 struct fdisk_partition
*pa
;
456 pa
= fdisk_table_get_partition(cf
->table
, i
);
457 return fdisk_partition_is_freespace(pa
);
460 /* converts libfdisk FDISK_ASKTYPE_MENU to cfdisk menu and returns user's
461 * responseback to libfdisk
463 static int ask_menu(struct fdisk_ask
*ask
, struct cfdisk
*cf
)
465 struct cfdisk_menuitem
*d
, *cm
;
467 size_t i
= 0, nitems
;
468 const char *name
, *desc
;
473 /* create cfdisk menu according to libfdisk ask-menu, note that the
474 * last cm[] item has to be empty -- so nitems + 1 */
475 nitems
= fdisk_ask_menu_get_nitems(ask
);
476 cm
= xcalloc(nitems
+ 1, sizeof(struct cfdisk_menuitem
));
478 for (i
= 0; i
< nitems
; i
++) {
479 if (fdisk_ask_menu_get_item(ask
, i
, &key
, &name
, &desc
))
486 /* make the new menu active */
497 if (ui_menu_move(cf
, key
) == 0)
504 d
= menu_get_menuitem(cf
, cf
->menu
->idx
);
506 fdisk_ask_menu_set_result(ask
, d
->key
);
520 static int ask_callback(struct fdisk_context
*cxt
, struct fdisk_ask
*ask
,
521 void *data
__attribute__((__unused__
)))
528 switch(fdisk_ask_get_type(ask
)) {
529 case FDISK_ASKTYPE_INFO
:
530 ui_info(fdisk_ask_print_get_mesg(ask
));
532 case FDISK_ASKTYPE_WARNX
:
533 ui_warnx(fdisk_ask_print_get_mesg(ask
));
535 case FDISK_ASKTYPE_WARN
:
536 ui_warn(fdisk_ask_print_get_mesg(ask
));
538 case FDISK_ASKTYPE_MENU
:
539 ask_menu(ask
, (struct cfdisk
*) data
);
542 ui_warnx(_("internal error: unsupported dialog type %d"),
543 fdisk_ask_get_type(ask
));
549 static int ui_end(void)
554 #if defined(HAVE_SLCURSES_H) || defined(HAVE_SLANG_SLCURSES_H)
555 SLsmg_gotorc(ui_lines
- 1, 0);
558 mvcur(0, ui_cols
- 1, ui_lines
-1, 0);
568 static void ui_vprint_center(size_t line
, int attrs
, const char *fmt
, va_list ap
)
576 xvasprintf(&buf
, fmt
, ap
);
578 width
= mbs_safe_width(buf
);
579 if (width
> (size_t) ui_cols
) {
580 char *p
= strrchr(buf
+ ui_cols
, ' ');
584 if (line
+ 1 >= ui_lines
)
587 mvaddstr(line
, 0, buf
);
588 mvaddstr(line
+ 1, 0, p
+1);
592 mvaddstr(line
, (ui_cols
- width
) / 2, buf
);
598 static void ui_center(size_t line
, const char *fmt
, ...)
602 ui_vprint_center(line
, 0, fmt
, ap
);
606 static void ui_warnx(const char *fmt
, ...)
611 ui_vprint_center(INFO_LINE
,
612 colors_wanted() ? COLOR_PAIR(CFDISK_CL_WARNING
) : 0,
615 vfprintf(stderr
, fmt
, ap
);
621 static void ui_warn(const char *fmt
, ...)
626 xasprintf(&fmt_m
, "%s: %m", fmt
);
630 ui_vprint_center(INFO_LINE
,
631 colors_wanted() ? COLOR_PAIR(CFDISK_CL_WARNING
) : 0,
634 vfprintf(stderr
, fmt_m
, ap
);
641 static int __attribute__((__noreturn__
)) ui_errx(int rc
, const char *fmt
, ...)
647 fprintf(stderr
, "%s: ", program_invocation_short_name
);
648 vfprintf(stderr
, fmt
, ap
);
655 static void ui_info(const char *fmt
, ...)
660 ui_vprint_center(INFO_LINE
, A_BOLD
, fmt
, ap
);
662 vfprintf(stdout
, fmt
, ap
);
668 static void ui_clean_info(void)
674 static void ui_hint(const char *fmt
, ...)
679 ui_vprint_center(HINT_LINE
, A_BOLD
, fmt
, ap
);
681 vfprintf(stdout
, fmt
, ap
);
687 static void ui_clean_hint(void)
693 static void die_on_signal(int dummy
__attribute__((__unused__
)))
695 DBG(MISC
, ul_debug("die on signal."));
700 static void resize_on_signal(int dummy
__attribute__((__unused__
)))
702 DBG(MISC
, ul_debug("resize on signal."));
706 static void menu_refresh_size(struct cfdisk
*cf
)
708 if (cf
->menu
&& cf
->menu
->nitems
)
709 cf
->menu
->page_sz
= (cf
->menu
->nitems
/ (ui_lines
- 4)) ? ui_lines
- 4 : 0;
712 static void menu_update_ignore(struct cfdisk
*cf
)
714 char ignore
[128] = { 0 };
716 struct cfdisk_menu
*m
;
717 struct cfdisk_menuitem
*d
, *org
;
722 assert(cf
->menu
->ignore_cb
);
725 org
= menu_get_menuitem(cf
, m
->idx
);
727 DBG(MENU
, ul_debug("update menu ignored keys"));
729 i
= m
->ignore_cb(cf
, ignore
, sizeof(ignore
));
732 /* return if no change */
733 if ( (!m
->ignore
&& !*ignore
)
734 || (m
->ignore
&& *ignore
&& strcmp(m
->ignore
, ignore
) == 0)) {
739 m
->ignore
= xstrdup(ignore
);
742 for (d
= m
->items
; d
->name
; d
++) {
743 if (m
->ignore
&& strchr(m
->ignore
, d
->key
))
748 /* refresh menu index to be at the same menuitem or go to the first */
749 if (org
&& menu_get_menuitem_by_key(cf
, org
->key
, &idx
))
754 menu_refresh_size(cf
);
757 static struct cfdisk_menu
*menu_push(
759 struct cfdisk_menuitem
*items
)
761 struct cfdisk_menu
*m
= xcalloc(1, sizeof(*m
));
762 struct cfdisk_menuitem
*d
;
766 DBG(MENU
, ul_debug("new menu"));
771 for (d
= m
->items
; d
->name
; d
++) {
772 const char *name
= _(d
->name
);
773 size_t len
= mbs_safe_width(name
);
780 menu_refresh_size(cf
);
784 static struct cfdisk_menu
*menu_pop(struct cfdisk
*cf
)
786 struct cfdisk_menu
*m
= NULL
;
790 DBG(MENU
, ul_debug("pop menu"));
794 free(cf
->menu
->ignore
);
795 free(cf
->menu
->title
);
802 static void menu_set_title(struct cfdisk_menu
*m
, const char *title
)
807 size_t len
= mbs_safe_width(title
);
808 if (len
+ 3 > m
->width
)
810 str
= xstrdup(title
);
816 static int ui_init(struct cfdisk
*cf
__attribute__((__unused__
)))
820 DBG(UI
, ul_debug("init"));
822 /* setup SIGCHLD handler */
823 sigemptyset(&sa
.sa_mask
);
825 sa
.sa_handler
= die_on_signal
;
826 sigaction(SIGINT
, &sa
, NULL
);
827 sigaction(SIGTERM
, &sa
, NULL
);
829 sa
.sa_handler
= resize_on_signal
;
830 sigaction(SIGWINCH
, &sa
, NULL
);
835 #ifdef HAVE_USE_DEFAULT_COLORS
836 if (colors_wanted() && has_colors()) {
840 use_default_colors();
841 for (i
= 1; i
< ARRAY_SIZE(color_pairs
); i
++) /* yeah, start from 1! */
842 init_pair(i
, color_pairs
[i
][0], color_pairs
[i
][1]);
852 keypad(stdscr
, TRUE
);
857 static size_t menuitem_get_line(struct cfdisk
*cf
, size_t idx
)
859 struct cfdisk_menu
*m
= cf
->menu
;
862 if (!m
->page_sz
) /* small menu */
863 return (ui_lines
- (cf
->menu
->nitems
+ 1)) / 2 + idx
;
864 return (idx
% m
->page_sz
) + 1;
866 size_t len
= m
->width
+ 4 + MENU_PADDING
; /* item width */
867 size_t items
= ui_cols
/ len
; /* items per line */
872 return MENU_START_LINE
+ ((idx
/ items
));
876 static int menuitem_get_column(struct cfdisk
*cf
, size_t idx
)
878 if (cf
->menu
->vertical
) {
879 size_t nc
= cf
->menu
->width
+ MENU_PADDING
;
880 if ((size_t) ui_cols
<= nc
)
882 return (ui_cols
- nc
) / 2;
884 size_t len
= cf
->menu
->width
+ 4 + MENU_PADDING
; /* item width */
885 size_t items
= ui_cols
/ len
; /* items per line */
886 size_t extra
= items
< cf
->menu
->nitems
? /* extra space on line */
887 ui_cols
% len
: /* - multi-line menu */
888 ui_cols
- (cf
->menu
->nitems
* len
); /* - one line menu */
891 return 0; /* hmm... no space */
893 extra
+= MENU_PADDING
; /* add padding after last item to extra */
896 return (idx
* len
) + (extra
/ 2);
897 return ((idx
% items
) * len
) + (extra
/ 2);
901 static int menuitem_on_page(struct cfdisk
*cf
, size_t idx
)
903 struct cfdisk_menu
*m
= cf
->menu
;
905 if (m
->page_sz
== 0 ||
906 m
->idx
/ m
->page_sz
== idx
/ m
->page_sz
)
911 static struct cfdisk_menuitem
*menu_get_menuitem(struct cfdisk
*cf
, size_t idx
)
913 struct cfdisk_menuitem
*d
;
916 for (i
= 0, d
= cf
->menu
->items
; d
->name
; d
++) {
917 if (cf
->menu
->ignore
&& strchr(cf
->menu
->ignore
, d
->key
))
926 static struct cfdisk_menuitem
*menu_get_menuitem_by_key(struct cfdisk
*cf
,
927 int key
, size_t *idx
)
929 struct cfdisk_menuitem
*d
;
931 for (*idx
= 0, d
= cf
->menu
->items
; d
->name
; d
++) {
932 if (cf
->menu
->ignore
&& strchr(cf
->menu
->ignore
, d
->key
))
942 static void ui_draw_menuitem(struct cfdisk
*cf
,
943 struct cfdisk_menuitem
*d
,
946 char buf
[80 * MB_CUR_MAX
];
948 size_t width
= cf
->menu
->width
+ 2; /* 2 = blank around string */
949 int ln
, cl
, vert
= cf
->menu
->vertical
;
951 if (!menuitem_on_page(cf
, idx
))
952 return; /* no visible item */
953 ln
= menuitem_get_line(cf
, idx
);
954 cl
= menuitem_get_column(cf
, idx
);
957 mbsalign(name
, buf
, sizeof(buf
), &width
,
958 vert
? MBS_ALIGN_LEFT
: MBS_ALIGN_CENTER
,
961 DBG(MENU
, ul_debug("menuitem: cl=%d, ln=%d, item='%s'",
965 mvaddch(ln
, cl
- 1, ACS_VLINE
);
966 mvaddch(ln
, cl
+ cf
->menu
->width
+ 4, ACS_VLINE
);
969 if (cf
->menu
->idx
== idx
) {
971 mvprintw(ln
, cl
, vert
? " %s " : "[%s]", buf
);
976 mvprintw(ln
, cl
, vert
? " %s " : "[%s]", buf
);
979 static void ui_clean_menu(struct cfdisk
*cf
)
983 struct cfdisk_menu
*m
= cf
->menu
;
984 size_t ln
= menuitem_get_line(cf
, 0);
987 nlines
= m
->page_sz
? m
->page_sz
: m
->nitems
;
989 nlines
= menuitem_get_line(cf
, m
->nitems
);
991 for (i
= ln
; i
<= ln
+ nlines
; i
++) {
1002 static void ui_draw_menu(struct cfdisk
*cf
)
1004 struct cfdisk_menuitem
*d
;
1005 struct cfdisk_menu
*m
;
1007 size_t ln
= menuitem_get_line(cf
, 0);
1013 DBG(MENU
, ul_debug("draw start"));
1019 nlines
= m
->page_sz
? m
->page_sz
: m
->nitems
;
1021 nlines
= menuitem_get_line(cf
, m
->nitems
);
1024 menu_update_ignore(cf
);
1026 while ((d
= menu_get_menuitem(cf
, i
)))
1027 ui_draw_menuitem(cf
, d
, i
++);
1030 size_t cl
= menuitem_get_column(cf
, 0);
1031 size_t curpg
= m
->page_sz
? m
->idx
/ m
->page_sz
: 0;
1033 /* corners and horizontal lines */
1034 mvaddch(ln
- 1, cl
- 1, ACS_ULCORNER
);
1035 mvaddch(ln
+ nlines
, cl
- 1, ACS_LLCORNER
);
1037 for (i
= 0; i
< m
->width
+ 4; i
++) {
1038 mvaddch(ln
- 1, cl
+ i
, ACS_HLINE
);
1039 mvaddch(ln
+ nlines
, cl
+ i
, ACS_HLINE
);
1042 mvaddch(ln
- 1, cl
+ i
, ACS_URCORNER
);
1043 mvaddch(ln
+ nlines
, cl
+ i
, ACS_LRCORNER
);
1045 /* draw also lines around empty lines on last page */
1047 m
->nitems
/ m
->page_sz
== m
->idx
/ m
->page_sz
) {
1048 for (i
= m
->nitems
% m
->page_sz
+ 1; i
<= m
->page_sz
; i
++) {
1049 mvaddch(i
, cl
- 1, ACS_VLINE
);
1050 mvaddch(i
, cl
+ cf
->menu
->width
+ 4, ACS_VLINE
);
1055 mvprintw(ln
- 1, cl
, " %s ", m
->title
);
1059 mvaddch(ln
- 1, cl
+ m
->width
+ 3, ACS_UARROW
);
1060 if (m
->page_sz
&& curpg
< m
->nitems
/ m
->page_sz
)
1061 mvaddch(ln
+ nlines
, cl
+ m
->width
+ 3, ACS_DARROW
);
1064 DBG(MENU
, ul_debug("draw end."));
1067 static void ui_menu_goto(struct cfdisk
*cf
, int where
)
1069 struct cfdisk_menuitem
*d
;
1072 /* stop and begin/end for vertical menus */
1073 if (cf
->menu
->vertical
) {
1076 else if (where
> (int) cf
->menu
->nitems
- 1)
1077 where
= cf
->menu
->nitems
- 1;
1079 /* continue from begin/end */
1081 where
= cf
->menu
->nitems
- 1;
1082 else if ((size_t) where
> cf
->menu
->nitems
- 1)
1085 if ((size_t) where
== cf
->menu
->idx
)
1090 old
= cf
->menu
->idx
;
1091 cf
->menu
->idx
= where
;
1093 if (!menuitem_on_page(cf
, old
)) {
1098 d
= menu_get_menuitem(cf
, old
);
1099 ui_draw_menuitem(cf
, d
, old
);
1101 d
= menu_get_menuitem(cf
, where
);
1102 ui_draw_menuitem(cf
, d
, where
);
1105 static int ui_menu_move(struct cfdisk
*cf
, int key
)
1107 struct cfdisk_menu
*m
;
1113 return 0; /* ignore errors */
1117 DBG(MENU
, ul_debug("menu move key >%c<.", key
));
1123 case '\016': /* ^N */
1124 case 'j': /* Vi-like alternative */
1125 ui_menu_goto(cf
, m
->idx
+ 1);
1128 case '\020': /* ^P */
1129 case 'k': /* Vi-like alternative */
1130 ui_menu_goto(cf
, (int) m
->idx
- 1);
1134 ui_menu_goto(cf
, (int) m
->idx
- m
->page_sz
);
1139 ui_menu_goto(cf
, 0);
1143 ui_menu_goto(cf
, m
->idx
+ m
->page_sz
);
1148 ui_menu_goto(cf
, m
->nitems
);
1155 ui_menu_goto(cf
, m
->idx
+ 1);
1161 ui_menu_goto(cf
, (int) m
->idx
- 1);
1166 return 1; /* key irrelevant for menu move */
1169 /* but don't call me from ui_run(), this is for pop-up menus only */
1170 static void ui_menu_resize(struct cfdisk
*cf
)
1174 menu_refresh_size(cf
);
1179 static int partition_on_page(struct cfdisk
*cf
, size_t i
)
1181 if (cf
->page_sz
== 0 ||
1182 cf
->lines_idx
/ cf
->page_sz
== i
/ cf
->page_sz
)
1187 static void ui_draw_partition(struct cfdisk
*cf
, size_t i
)
1189 int ln
= TABLE_START_LINE
+ 1 + i
; /* skip table header */
1190 int cl
= ARROW_CURSOR_WIDTH
; /* we need extra space for cursor */
1191 int cur
= cf
->lines_idx
== i
;
1195 if (!partition_on_page(cf
, i
))
1197 ln
= TABLE_START_LINE
+ (i
% cf
->page_sz
) + 1;
1198 curpg
= cf
->lines_idx
/ cf
->page_sz
;
1202 "draw partition %zu [page_sz=%zu, "
1203 "line=%d, idx=%zu]",
1204 i
, cf
->page_sz
, ln
, cf
->lines_idx
));
1208 mvaddstr(ln
, 0, ARROW_CURSOR_STRING
);
1209 mvaddstr(ln
, cl
, cf
->lines
[i
+ 1]);
1214 if (colors_wanted() && is_freespace(cf
, i
)) {
1215 attron(COLOR_PAIR(CFDISK_CL_FREESPACE
));
1218 mvaddstr(ln
, 0, ARROW_CURSOR_DUMMY
);
1219 mvaddstr(ln
, cl
, cf
->lines
[i
+ 1]);
1221 attroff(COLOR_PAIR(CFDISK_CL_FREESPACE
));
1224 if ((size_t) ln
== MENU_START_LINE
- 1 &&
1225 cf
->page_sz
&& curpg
< cf
->nlines
/ cf
->page_sz
) {
1228 mvaddch(ln
, ui_cols
- 1, ACS_DARROW
);
1229 mvaddch(ln
, 0, ACS_DARROW
);
1235 static int ui_draw_table(struct cfdisk
*cf
)
1237 int cl
= ARROW_CURSOR_WIDTH
;
1238 size_t i
, nparts
= fdisk_table_get_nents(cf
->table
);
1239 size_t curpg
= cf
->page_sz
? cf
->lines_idx
/ cf
->page_sz
: 0;
1241 DBG(UI
, ul_debug("draw table"));
1243 for (i
= TABLE_START_LINE
; i
<= TABLE_START_LINE
+ cf
->page_sz
; i
++) {
1248 if ((size_t) cf
->lines_idx
> nparts
- 1)
1249 cf
->lines_idx
= nparts
? nparts
- 1 : 0;
1253 mvaddstr(TABLE_START_LINE
, cl
, cf
->lines
[0]);
1256 /* print partitions */
1257 for (i
= 0; i
< nparts
; i
++)
1258 ui_draw_partition(cf
, i
);
1261 mvaddch(TABLE_START_LINE
, ui_cols
- 1, ACS_UARROW
);
1262 mvaddch(TABLE_START_LINE
, 0, ACS_UARROW
);
1264 if (cf
->page_sz
&& curpg
< cf
->nlines
/ cf
->page_sz
) {
1265 mvaddch(MENU_START_LINE
- 1, ui_cols
- 1, ACS_DARROW
);
1266 mvaddch(MENU_START_LINE
- 1, 0, ACS_DARROW
);
1271 static int ui_table_goto(struct cfdisk
*cf
, int where
)
1274 size_t nparts
= fdisk_table_get_nents(cf
->table
);
1276 DBG(UI
, ul_debug("goto table %d", where
));
1280 else if ((size_t) where
> nparts
- 1)
1283 if ((size_t) where
== cf
->lines_idx
)
1286 old
= cf
->lines_idx
;
1287 cf
->lines_idx
= where
;
1289 if (!partition_on_page(cf
, old
) ||!partition_on_page(cf
, where
))
1292 ui_draw_partition(cf
, old
); /* cleanup old */
1293 ui_draw_partition(cf
, where
); /* draw new */
1301 static int ui_refresh(struct cfdisk
*cf
)
1303 struct fdisk_label
*lb
;
1305 uint64_t bytes
= fdisk_get_nsectors(cf
->cxt
) * fdisk_get_sector_size(cf
->cxt
);
1306 char *strsz
= size_to_human_string(SIZE_SUFFIX_SPACE
1307 | SIZE_SUFFIX_3LETTER
, bytes
);
1313 lb
= fdisk_get_label(cf
->cxt
, NULL
);
1318 ui_center(0, _("Disk: %s"), fdisk_get_devname(cf
->cxt
));
1320 ui_center(1, _("Size: %s, %ju bytes, %ju sectors"),
1321 strsz
, bytes
, (uintmax_t) fdisk_get_nsectors(cf
->cxt
));
1322 if (fdisk_get_disklabel_id(cf
->cxt
, &id
) == 0 && id
)
1323 ui_center(2, _("Label: %s, identifier: %s"),
1324 fdisk_label_get_name(lb
), id
);
1326 ui_center(2, _("Label: %s"), fdisk_label_get_name(lb
));
1335 static ssize_t
ui_get_string(struct cfdisk
*cf
, const char *prompt
,
1336 const char *hint
, char *buf
, size_t len
)
1339 ssize_t i
= 0, rc
= -1;
1340 int ln
= MENU_START_LINE
, cl
= 1;
1350 mvaddstr(ln
, cl
, (char *) prompt
);
1351 cl
+= mbs_safe_width(prompt
);
1357 cells
= mbs_safe_width(buf
);
1358 mvaddstr(ln
, cl
, buf
);
1366 move(ln
, cl
+ cells
);
1371 #if !defined(HAVE_SLCURSES_H) && !defined(HAVE_SLANG_SLCURSES_H) && \
1372 defined(HAVE_LIBNCURSESW) && defined(HAVE_WIDECHAR)
1374 if (get_wch(&c
) == ERR
) {
1377 if ((c
= getch()) == ERR
) {
1383 if (!isatty(STDIN_FILENO
))
1388 if (c
== '\r' || c
== '\n' || c
== KEY_ENTER
)
1393 rc
= -CFDISK_ERR_ESC
;
1400 i
= mbs_truncate(buf
, &cells
);
1403 mvaddch(ln
, cl
+ cells
, ' ');
1404 move(ln
, cl
+ cells
);
1409 #if defined(HAVE_LIBNCURSESW) && defined(HAVE_WIDECHAR)
1410 if (i
+ 1 < (ssize_t
) len
&& iswprint(c
)) {
1411 wchar_t wc
= (wchar_t) c
;
1412 char s
[MB_CUR_MAX
+ 1];
1413 int sz
= wctomb(s
, wc
);
1415 if (sz
> 0 && sz
+ i
< (ssize_t
) len
) {
1417 mvaddnstr(ln
, cl
+ cells
, s
, sz
);
1418 memcpy(buf
+ i
, s
, sz
);
1421 cells
+= wcwidth(wc
);
1426 if (i
+ 1 < (ssize_t
) len
&& isprint(c
)) {
1427 mvaddch(ln
, cl
+ cells
, c
);
1439 rc
= i
; /* success */
1449 /* @res is default value as well as result in bytes */
1450 static int ui_get_size(struct cfdisk
*cf
, const char *prompt
, uintmax_t *res
,
1451 uintmax_t low
, uintmax_t up
, int *expsize
)
1456 char *dflt
= size_to_human_string(0, *res
);
1458 DBG(UI
, ul_debug("get_size (default=%ju)", *res
));
1463 int pwr
= 0, insec
= 0;
1465 snprintf(buf
, sizeof(buf
), "%s", dflt
);
1466 rc
= ui_get_string(cf
, prompt
,
1467 _("May be followed by {M,B,G,T}iB "
1468 "(the \"iB\" is optional) or S for sectors."),
1471 ui_warnx(_("Please, specify size."));
1472 continue; /* nothing specified */
1473 } else if (rc
== -CFDISK_ERR_ESC
)
1474 break; /* cancel dialog */
1476 if (strcmp(buf
, dflt
) == 0)
1477 user
= *res
, rc
= 0; /* no change, use default */
1479 size_t len
= strlen(buf
);
1480 if (buf
[len
- 1] == 'S' || buf
[len
- 1] == 's') {
1482 buf
[len
- 1] = '\0';
1484 rc
= parse_size(buf
, &user
, &pwr
); /* parse */
1488 DBG(UI
, ul_debug("get_size user=%ju, power=%d, sectors=%s",
1489 user
, pwr
, insec
? "yes" : "no"));
1491 user
*= fdisk_get_sector_size(cf
->cxt
);
1493 ui_warnx(_("Minimal size is %ju"), low
);
1496 if (user
> up
&& pwr
&& user
< up
+ (1ULL << pwr
* 10))
1497 /* ignore when the user specified size overflow
1498 * with in range specified by suffix (e.g. MiB) */
1502 ui_warnx(_("Maximal size is %ju bytes."), up
);
1505 if (rc
== 0 && insec
&& expsize
)
1509 ui_warnx(_("Failed to parse size."));
1516 DBG(UI
, ul_debug("get_size (result=%ju, rc=%zd)", *res
, rc
));
1520 static struct fdisk_parttype
*ui_get_parttype(struct cfdisk
*cf
,
1521 struct fdisk_parttype
*cur
)
1523 struct cfdisk_menuitem
*d
, *cm
;
1524 size_t i
= 0, nitems
, idx
= 0;
1525 struct fdisk_parttype
*t
= NULL
;
1526 struct fdisk_label
*lb
;
1529 DBG(UI
, ul_debug("asking for parttype."));
1531 lb
= fdisk_get_label(cf
->cxt
, NULL
);
1533 /* create cfdisk menu according to label types, note that the
1534 * last cm[] item has to be empty -- so nitems + 1 */
1535 nitems
= fdisk_label_get_nparttypes(lb
);
1539 cm
= xcalloc(nitems
+ 1, sizeof(struct cfdisk_menuitem
));
1543 codetypes
= fdisk_label_has_code_parttypes(lb
);
1545 for (i
= 0; i
< nitems
; i
++) {
1546 const struct fdisk_parttype
*x
= fdisk_label_get_parttype(lb
, i
);
1549 cm
[i
].userdata
= (void *) x
;
1551 xasprintf(&name
, "%2x %s",
1552 fdisk_parttype_get_code(x
),
1553 _(fdisk_parttype_get_name(x
)));
1555 name
= (char *) _(fdisk_parttype_get_name(x
));
1556 cm
[i
].desc
= fdisk_parttype_get_string(x
);
1563 /* make the new menu active */
1565 cf
->menu
->vertical
= 1;
1566 cf
->menu
->idx
= idx
;
1567 menu_set_title(cf
->menu
, _("Select partition type"));
1576 if (ui_menu_move(cf
, key
) == 0)
1583 d
= menu_get_menuitem(cf
, cf
->menu
->idx
);
1585 t
= (struct fdisk_parttype
*) d
->userdata
;
1597 for (i
= 0; i
< nitems
; i
++)
1598 free((char *) cm
[i
].name
);
1601 DBG(UI
, ul_debug("get parrtype done [type=%s] ", t
?
1602 fdisk_parttype_get_name(t
) : NULL
));
1606 /* prints menu with libfdisk labels and waits for users response */
1607 static int ui_create_label(struct cfdisk
*cf
)
1609 struct cfdisk_menuitem
*d
, *cm
;
1611 size_t i
= 0, nitems
;
1612 struct fdisk_label
*lb
= NULL
;
1616 DBG(UI
, ul_debug("asking for new disklabe."));
1618 /* create cfdisk menu according to libfdisk labels, note that the
1619 * last cm[] item has to be empty -- so nitems + 1 */
1620 nitems
= fdisk_get_nlabels(cf
->cxt
);
1621 cm
= xcalloc(nitems
+ 1, sizeof(struct cfdisk_menuitem
));
1623 while (fdisk_next_label(cf
->cxt
, &lb
) == 0) {
1624 if (fdisk_label_is_disabled(lb
) ||
1625 fdisk_label_get_type(lb
) == FDISK_DISKLABEL_BSD
)
1627 cm
[i
++].name
= fdisk_label_get_name(lb
);
1631 ui_center(ui_lines
- 4,
1632 _("Device does not contain a recognized partition table."));
1633 ui_center(ui_lines
- 3,
1634 _("Please, select a type to create a new disk label."));
1636 /* make the new menu active */
1638 cf
->menu
->vertical
= 1;
1639 menu_set_title(cf
->menu
, _("Select label type"));
1648 if (ui_menu_move(cf
, key
) == 0)
1654 d
= menu_get_menuitem(cf
, cf
->menu
->idx
);
1656 rc
= fdisk_create_disklabel(cf
->cxt
, d
->name
);
1668 DBG(UI
, ul_debug("create label done [rc=%d] ", rc
));
1672 static int ui_help(void)
1675 static const char *help
[] = {
1676 N_("Help Screen for cfdisk"),
1678 N_("This is cfdisk, a curses based disk partitioning program, which"),
1679 N_("allows you to create, delete and modify partitions on your hard"),
1682 N_("Copyright (C) 2014 Karel Zak <kzak@redhat.com> "),
1683 N_("Based on the original cfdisk from Kevin E. Martin & aeb."),
1685 N_("Command Meaning"),
1686 N_("------- -------"),
1687 N_(" b Toggle bootable flag of the current partition"),
1688 N_(" d Delete the current partition"),
1689 N_(" h Print this screen"),
1690 N_(" n Create new partition from free space"),
1691 N_(" q Quit program without writing partition table"),
1692 N_(" t Change the partition type"),
1693 N_(" s Fix partitions order (only when in disarray)"),
1694 N_(" W Write partition table to disk (must enter upper case W)"),
1695 N_(" Since this might destroy data on the disk, you must"),
1696 N_(" either confirm or deny the write by entering `yes' or"),
1698 N_("Up Arrow Move cursor to the previous partition"),
1699 N_("Down Arrow Move cursor to the next partition"),
1700 N_("Left Arrow Move cursor to the previous menu item"),
1701 N_("Right Arrow Move cursor to the next menu item"),
1704 N_("Note: All of the commands can be entered with either upper or lower"),
1705 N_("case letters (except for Writes)."),
1707 N_("Use lsblk(8) or partx(8) to see more details about the device.")
1711 for (i
= 0; i
< ARRAY_SIZE(help
); i
++)
1712 mvaddstr(i
, 1, _(help
[i
]));
1714 ui_info(_("Press a key to continue."));
1720 /* TODO: use @sz, now 128bytes */
1721 static int main_menu_ignore_keys(struct cfdisk
*cf
, char *ignore
,
1722 size_t sz
__attribute__((__unused__
)))
1724 struct fdisk_partition
*pa
= get_current_partition(cf
);
1729 if (fdisk_partition_is_freespace(pa
)) {
1730 ignore
[i
++] = 'd'; /* delete */
1731 ignore
[i
++] = 't'; /* set type */
1732 ignore
[i
++] = 'b'; /* set bootable */
1735 if (!fdisk_is_label(cf
->cxt
, DOS
) &&
1736 !fdisk_is_label(cf
->cxt
, SGI
))
1740 if (!cf
->wrong_order
)
1742 if (fdisk_is_readonly(cf
->cxt
))
1748 /* returns: error: < 0, success: 0, quit: 1 */
1749 static int main_menu_action(struct cfdisk
*cf
, int key
)
1752 int ref
= 0, rc
, org_order
= cf
->wrong_order
;
1753 const char *info
= NULL
, *warn
= NULL
;
1754 struct fdisk_partition
*pa
;
1761 struct cfdisk_menuitem
*d
= menu_get_menuitem(cf
, cf
->menu
->idx
);
1766 } else if (key
!= 'w' && key
!= 'W')
1767 key
= tolower(key
); /* case insensitive except 'W'rite */
1769 DBG(MENU
, ul_debug("main menu action: key=%c", key
));
1771 if (cf
->menu
->ignore
&& strchr(cf
->menu
->ignore
, key
)) {
1772 DBG(MENU
, ul_debug(" ignore '%c'", key
));
1776 pa
= get_current_partition(cf
);
1777 n
= fdisk_partition_get_partno(pa
);
1779 DBG(MENU
, ul_debug("menu action on %p", pa
));
1784 case 'b': /* Bootable flag */
1786 int fl
= fdisk_is_label(cf
->cxt
, DOS
) ? DOS_FLAG_ACTIVE
:
1787 fdisk_is_label(cf
->cxt
, SGI
) ? SGI_FLAG_BOOT
: 0;
1789 if (fl
&& fdisk_partition_toggle_flag(cf
->cxt
, n
, fl
))
1790 warn
= _("Could not toggle the flag.");
1798 case 'd': /* Delete */
1799 if (fdisk_delete_partition(cf
->cxt
, n
) != 0)
1800 warn
= _("Could not delete partition %zu.");
1802 info
= _("Partition %zu has been deleted.");
1805 case 'h': /* help */
1811 uint64_t start
, size
, dflt_size
;
1812 struct fdisk_partition
*npa
; /* the new partition */
1813 int expsize
= 0; /* size specified explicitly in sectors */
1815 if (!pa
|| !fdisk_partition_is_freespace(pa
)
1816 || !fdisk_partition_has_start(pa
))
1818 npa
= fdisk_new_partition();
1821 /* free space range */
1822 start
= fdisk_partition_get_start(pa
);
1823 size
= dflt_size
= fdisk_partition_get_size(pa
) * fdisk_get_sector_size(cf
->cxt
);
1825 if (ui_get_size(cf
, _("Partition size: "), &size
, 1, size
, &expsize
)
1829 if (dflt_size
== size
) /* default is to fillin all free space */
1830 fdisk_partition_end_follow_default(npa
, 1);
1831 else /* set relative size of the partition */
1832 fdisk_partition_set_size(npa
, size
/ fdisk_get_sector_size(cf
->cxt
));
1835 fdisk_partition_size_explicit(pa
, 1);
1837 fdisk_partition_set_start(npa
, start
);
1838 fdisk_partition_partno_follow_default(npa
, 1);
1839 /* add to disk label -- libfdisk will ask for missing details */
1840 rc
= fdisk_add_partition(cf
->cxt
, npa
, NULL
);
1841 fdisk_unref_partition(npa
);
1846 case 'q': /* Quit */
1848 case 't': /* Type */
1850 struct fdisk_parttype
*t
;
1852 if (!pa
|| fdisk_partition_is_freespace(pa
))
1854 t
= (struct fdisk_parttype
*) fdisk_partition_get_type(pa
);
1855 t
= ui_get_parttype(cf
, t
);
1858 if (t
&& fdisk_set_partition_type(cf
->cxt
, n
, t
) == 0)
1859 info
= _("Changed type of partition %zu.");
1861 info
= _("The type of partition %zu is unchanged.");
1864 case 's': /* fix order */
1865 if (cf
->wrong_order
) {
1866 fdisk_reorder_partitions(cf
->cxt
);
1870 case 'W': /* Write */
1872 char buf
[64] = { 0 };
1875 if (fdisk_is_readonly(cf
->cxt
)) {
1876 warn
= _("Device open in read-only mode");
1880 rc
= ui_get_string(cf
,
1881 _("Are you sure you want to write the partition "
1883 _("Type \"yes\" or \"no\", or press ESC to leave this dialog."),
1887 if (rc
<= 0 || (strcasecmp(buf
, "yes") != 0 &&
1888 strcasecmp(buf
, _("yes")) != 0)) {
1889 info
= _("Did not write partition table to disk");
1892 rc
= fdisk_write_disklabel(cf
->cxt
);
1894 warn
= _("Failed to write disklabel");
1896 fdisk_reread_partition_table(cf
->cxt
);
1897 info
= _("The partition table has been altered.");
1913 ui_warnx(warn
, n
+ 1);
1915 ui_info(info
, n
+ 1);
1916 else if (key
== 'n' && cf
->wrong_order
&& org_order
== 0)
1917 ui_info(_("Note that partition table entries are not in disk order now."));
1922 static void ui_resize_refresh(struct cfdisk
*cf
)
1925 menu_refresh_size(cf
);
1930 static int ui_run(struct cfdisk
*cf
)
1936 DBG(UI
, ul_debug("start cols=%zu, lines=%zu", ui_cols
, ui_lines
));
1938 if (!fdisk_has_label(cf
->cxt
) || cf
->zero_start
) {
1939 rc
= ui_create_label(cf
);
1941 ui_errx(EXIT_FAILURE
,
1942 _("failed to create a new disklabel"));
1948 rc
= lines_refresh(cf
);
1950 ui_errx(EXIT_FAILURE
, _("failed to read partitions"));
1952 menu_push(cf
, main_menuitems
);
1953 cf
->menu
->ignore_cb
= main_menu_ignore_keys
;
1955 rc
= ui_refresh(cf
);
1959 if (fdisk_is_readonly(cf
->cxt
))
1960 ui_warnx(_("Device open in read-only mode."));
1963 int rc
= 0, key
= getch();
1966 /* Note that ncurses getch() returns ERR when interrupted
1967 * by signal, but SLang does not interrupt at all. */
1968 ui_resize_refresh(cf
);
1971 if (ui_menu_move(cf
, key
) == 0)
1974 DBG(UI
, ul_debug("main action key >%c<.", key
));
1978 case '\016': /* ^N */
1979 case 'j': /* Vi-like alternative */
1980 ui_table_goto(cf
, cf
->lines_idx
+ 1);
1983 case '\020': /* ^P */
1984 case 'k': /* Vi-like alternative */
1985 ui_table_goto(cf
, (int) cf
->lines_idx
- 1);
1989 ui_table_goto(cf
, (int) cf
->lines_idx
- cf
->page_sz
);
1993 ui_table_goto(cf
, 0);
1997 ui_table_goto(cf
, cf
->lines_idx
+ cf
->page_sz
);
2001 ui_table_goto(cf
, (int) cf
->nlines
- 1);
2006 rc
= main_menu_action(cf
, 0);
2009 rc
= main_menu_action(cf
, key
);
2021 DBG(UI
, ul_debug("end"));
2025 static void __attribute__ ((__noreturn__
)) usage(FILE *out
)
2027 fputs(USAGE_HEADER
, out
);
2030 _(" %1$s [options] <disk>\n"), program_invocation_short_name
);
2032 fputs(USAGE_OPTIONS
, out
);
2033 fputs(_(" -L --color[=<when>] colorize output (auto, always or never)\n"), out
);
2034 fputs(_(" -z --zero start with zeroed partition table\n"), out
);
2036 fputs(USAGE_SEPARATOR
, out
);
2037 fputs(USAGE_HELP
, out
);
2038 fputs(USAGE_VERSION
, out
);
2040 fprintf(out
, USAGE_MAN_TAIL("cfdisk(8)"));
2041 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
2044 int main(int argc
, char *argv
[])
2046 const char *diskpath
;
2047 int rc
, c
, colormode
= UL_COLORMODE_UNDEF
;
2048 struct cfdisk _cf
= { .lines_idx
= 0 },
2051 static const struct option longopts
[] = {
2052 { "color", optional_argument
, NULL
, 'L' },
2053 { "help", no_argument
, NULL
, 'h' },
2054 { "version", no_argument
, NULL
, 'V' },
2055 { "zero", no_argument
, NULL
, 'z' },
2059 setlocale(LC_ALL
, "");
2060 bindtextdomain(PACKAGE
, LOCALEDIR
);
2061 textdomain(PACKAGE
);
2062 atexit(close_stdout
);
2064 while((c
= getopt_long(argc
, argv
, "L::hVz", longopts
, NULL
)) != -1) {
2070 colormode
= UL_COLORMODE_AUTO
;
2072 colormode
= colormode_or_err(optarg
,
2073 _("unsupported color mode"));
2076 printf(UTIL_LINUX_VERSION
);
2077 return EXIT_SUCCESS
;
2084 colors_init(colormode
, "cfdisk");
2086 fdisk_init_debug(0);
2087 scols_init_debug(0);
2088 cfdisk_init_debug();
2089 cf
->cxt
= fdisk_new_context();
2091 err(EXIT_FAILURE
, _("failed to allocate libfdisk context"));
2093 fdisk_set_ask(cf
->cxt
, ask_callback
, (void *) cf
);
2096 diskpath
= access(DEFAULT_DEVICE
, F_OK
) == 0 ?
2097 DEFAULT_DEVICE
: ALTERNATE_DEVICE
;
2099 diskpath
= argv
[optind
];
2101 rc
= fdisk_assign_device(cf
->cxt
, diskpath
, 0);
2103 rc
= fdisk_assign_device(cf
->cxt
, diskpath
, 1);
2105 err(EXIT_FAILURE
, _("cannot open %s"),
2106 optind
== argc
? DEFAULT_DEVICE
: diskpath
);
2108 /* Don't use err(), warn() from this point */
2115 fdisk_unref_table(cf
->table
);
2117 rc
= fdisk_deassign_device(cf
->cxt
, cf
->nwrites
== 0);
2118 fdisk_unref_context(cf
->cxt
);
2119 DBG(MISC
, ul_debug("bye! [rc=%d]", rc
));
2120 return rc
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
;