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.
21 #include <libsmartcols.h>
22 #include <sys/ioctl.h>
26 #elif defined(HAVE_SLANG_SLANG_H)
27 # include <slang/slang.h>
30 #ifdef HAVE_SLCURSES_H
31 # include <slcurses.h>
32 #elif defined(HAVE_SLANG_SLCURSES_H)
33 # include <slang/slcurses.h>
34 #elif defined(HAVE_NCURSESW_NCURSES_H) && defined(HAVE_WIDECHAR)
35 # include <ncursesw/ncurses.h>
36 #elif defined(HAVE_NCURSES_H)
38 #elif defined(HAVE_NCURSES_NCURSES_H)
39 # include <ncurses/ncurses.h>
48 #include "closestream.h"
58 # define DEFAULT_DEVICE "/dev/hd0"
59 # define ALTERNATE_DEVICE "/dev/sd0"
60 #elif defined(__FreeBSD__)
61 # define DEFAULT_DEVICE "/dev/ad0"
62 # define ALTERNATE_DEVICE "/dev/da0"
64 # define DEFAULT_DEVICE "/dev/sda"
65 # define ALTERNATE_DEVICE "/dev/hda"
68 #define ARROW_CURSOR_STRING ">> "
69 #define ARROW_CURSOR_DUMMY " "
70 #define ARROW_CURSOR_WIDTH (sizeof(ARROW_CURSOR_STRING) - 1)
72 #define MENU_PADDING 2
73 #define TABLE_START_LINE 4
74 #define MENU_START_LINE ((size_t) LINES - 5)
75 #define INFO_LINE ((size_t) LINES - 2)
76 #define HINT_LINE ((size_t) LINES - 1)
78 #define CFDISK_ERR_ESC 5000
81 # define KEY_ESC '\033'
84 # define KEY_DELETE '\177'
93 static const int color_pairs
[][2] = {
94 /* color foreground, background */
95 [CFDISK_CL_WARNING
] = { COLOR_RED
, -1 },
96 [CFDISK_CL_FREESPACE
] = { COLOR_GREEN
, -1 }
101 static struct cfdisk_menuitem
*menu_get_menuitem(struct cfdisk
*cf
, size_t idx
);
102 static struct cfdisk_menuitem
*menu_get_menuitem_by_key(struct cfdisk
*cf
, int key
, size_t *idx
);
103 static struct cfdisk_menu
*menu_push(struct cfdisk
*cf
, struct cfdisk_menuitem
*item
);
104 static struct cfdisk_menu
*menu_pop(struct cfdisk
*cf
);
105 static void menu_refresh_size(struct cfdisk
*cf
);
107 static int ui_refresh(struct cfdisk
*cf
);
108 static void ui_warnx(const char *fmt
, ...);
109 static void ui_warn(const char *fmt
, ...);
110 static void ui_info(const char *fmt
, ...);
111 static void ui_draw_menu(struct cfdisk
*cf
);
112 static int ui_menu_move(struct cfdisk
*cf
, int key
);
113 static void ui_menu_resize(struct cfdisk
*cf
);
115 static int ui_get_size(struct cfdisk
*cf
, const char *prompt
, uintmax_t *res
,
116 uintmax_t low
, uintmax_t up
);
118 static int ui_enabled
;
119 static int ui_resize
;
122 struct cfdisk_menuitem
{
123 int key
; /* keyboard shortcut */
124 const char *name
; /* item name */
125 const char *desc
; /* item description (hint) */
131 char *title
; /* optional menu title */
132 struct cfdisk_menuitem
*items
; /* array with menu items */
133 char *ignore
;/* string with keys to ignore */
134 size_t width
; /* maximal width of the menu item */
135 size_t nitems
; /* number of the active menu items */
136 size_t page_sz
;/* when menu longer than screen */
137 size_t idx
; /* the current menu item */
138 struct cfdisk_menu
*prev
;
140 /* @ignore keys generator */
141 int (*ignore_cb
) (struct cfdisk
*, char *, size_t);
143 unsigned int vertical
: 1; /* enable vertical mode */
147 static struct cfdisk_menuitem main_menuitems
[] = {
148 { 'b', N_("Bootable"), N_("Toggle bootable flag of the current partition") },
149 { 'd', N_("Delete"), N_("Delete the current partition") },
150 { 'n', N_("New"), N_("Create new partition from free space") },
151 { 'q', N_("Quit"), N_("Quit program without writing partition table") },
152 { 't', N_("Type"), N_("Change the partition type") },
153 { 'h', N_("Help"), N_("Print help screen") },
154 { 's', N_("Sort"), N_("Fix partitions order") },
155 { 'W', N_("Write"), N_("Write partition table to disk (this might destroy data)") },
159 /* top level control struct */
161 struct fdisk_context
*cxt
; /* libfdisk context */
162 struct fdisk_table
*table
; /* partition table */
163 struct cfdisk_menu
*menu
; /* the current menu */
165 int *cols
; /* output columns */
166 size_t ncols
; /* number of columns */
168 char *linesbuf
; /* table as string */
169 size_t linesbufsz
; /* size of the tb_buf */
171 char **lines
; /* array with lines */
172 size_t nlines
; /* number of lines */
173 size_t lines_idx
; /* current line <0..N>, exclude header */
176 unsigned int nwrites
; /* fdisk_write_disklabel() counter */
178 unsigned int wrong_order
:1, /* PT not in right order */
179 zero_start
:1; /* ignore existing partition table */
182 /* Initialize output columns -- we follow libcfdisk columns (usually specific
185 static int cols_init(struct cfdisk
*cf
)
193 return fdisk_get_columns(cf
->cxt
, 0, &cf
->cols
, &cf
->ncols
);
196 static void resize(void)
200 if (ioctl(fileno(stdout
), TIOCGWINSZ
, &ws
) != -1
201 && ws
.ws_row
&& ws
.ws_col
) {
205 resizeterm(ws
.ws_row
, ws
.ws_col
);
207 clearok(stdscr
, TRUE
);
211 DBG(FRONTEND
, ul_debug("ui: resize refresh COLS=%d, LINES=%d", COLS
, LINES
));
215 /* Reads partition in tree-like order from scols
217 static int partition_from_scols(struct fdisk_table
*tb
,
218 struct libscols_line
*ln
)
220 struct fdisk_partition
*pa
= scols_line_get_userdata(ln
);
222 fdisk_table_add_partition(tb
, pa
);
223 fdisk_unref_partition(pa
);
225 if (scols_line_has_children(ln
)) {
226 struct libscols_line
*chln
;
227 struct libscols_iter
*itr
= scols_new_iter(SCOLS_ITER_FORWARD
);
231 while (scols_line_next_child(ln
, itr
, &chln
) == 0)
232 partition_from_scols(tb
, chln
);
233 scols_free_iter(itr
);
238 static char *table_to_string(struct cfdisk
*cf
, struct fdisk_table
*tb
)
240 const struct fdisk_column
*col
;
241 struct fdisk_partition
*pa
;
242 struct fdisk_label
*lb
;
243 struct fdisk_iter
*itr
= NULL
;
244 struct libscols_table
*table
= NULL
;
245 struct libscols_iter
*s_itr
= NULL
;
249 struct libscols_line
*ln
, *ln_cont
= NULL
;
251 DBG(FRONTEND
, ul_debug("table: convert to string"));
258 lb
= fdisk_context_get_label(cf
->cxt
, NULL
);
261 itr
= fdisk_new_iter(FDISK_ITER_FORWARD
);
265 /* get container (e.g. extended partition) */
266 while (fdisk_table_next_partition(tb
, itr
, &pa
) == 0) {
267 if (fdisk_partition_is_nested(pa
)) {
268 DBG(FRONTEND
, ul_debug("table: nested detected, using tree"));
269 tree
= SCOLS_FL_TREE
;
274 table
= scols_new_table();
277 scols_table_enable_maxout(table
, 1);
280 for (i
= 0; i
< cf
->ncols
; i
++) {
281 col
= fdisk_label_get_column(lb
, cf
->cols
[i
]);
285 if (fdisk_column_is_number(col
))
286 fl
|= SCOLS_FL_RIGHT
;
287 if (fdisk_column_get_id(col
) == FDISK_COL_TYPE
)
288 fl
|= SCOLS_FL_TRUNC
;
289 if (tree
&& fdisk_column_get_id(col
) == FDISK_COL_DEVICE
)
292 if (!scols_table_new_column(table
,
293 fdisk_column_get_name(col
),
294 fdisk_column_get_width(col
), fl
))
300 fdisk_reset_iter(itr
, FDISK_ITER_FORWARD
);
302 while (fdisk_table_next_partition(tb
, itr
, &pa
) == 0) {
303 struct libscols_line
*parent
= fdisk_partition_is_nested(pa
) ? ln_cont
: NULL
;
305 ln
= scols_table_new_line(table
, parent
);
308 for (i
= 0; i
< cf
->ncols
; i
++) {
310 col
= fdisk_label_get_column(lb
, cf
->cols
[i
]);
313 if (fdisk_partition_to_string(pa
, cf
->cxt
,
314 fdisk_column_get_id(col
), &cdata
))
316 scols_line_refer_data(ln
, i
, cdata
);
318 if (tree
&& fdisk_partition_is_container(pa
))
321 scols_line_set_userdata(ln
, (void *) pa
);
322 fdisk_ref_partition(pa
);
325 if (scols_table_is_empty(table
))
328 scols_table_reduce_termwidth(table
, ARROW_CURSOR_WIDTH
);
329 scols_print_table_to_string(table
, &res
);
331 /* scols_* code might reorder lines, let's reorder @tb according to the
332 * final output (it's no problem because partitions are addressed by
333 * parno stored within struct fdisk_partition) */
336 fdisk_reset_iter(itr
, FDISK_ITER_FORWARD
);
337 while (fdisk_table_next_partition(tb
, itr
, &pa
) == 0)
338 fdisk_table_remove_partition(tb
, pa
);
340 s_itr
= scols_new_iter(SCOLS_ITER_FORWARD
);
344 /* add all in the right order (don't forget the output is tree) */
345 while (scols_table_next_line(table
, s_itr
, &ln
) == 0) {
346 if (scols_line_get_parent(ln
))
348 if (partition_from_scols(tb
, ln
))
352 scols_unref_table(table
);
353 scols_free_iter(s_itr
);
354 fdisk_free_iter(itr
);
360 * Read data about partitions from libfdisk and prepare output lines.
362 static int lines_refresh(struct cfdisk
*cf
)
370 DBG(FRONTEND
, ul_debug("refreshing buffer"));
379 fdisk_unref_table(cf
->table
);
382 /* read partitions and free spaces into cf->table */
383 rc
= fdisk_get_partitions(cf
->cxt
, &cf
->table
);
385 rc
= fdisk_get_freespaces(cf
->cxt
, &cf
->table
);
389 cf
->linesbuf
= table_to_string(cf
, cf
->table
);
393 cf
->linesbufsz
= strlen(cf
->linesbuf
);
394 cf
->nlines
= fdisk_table_get_nents(cf
->table
) + 1; /* 1 for header line */
396 cf
->wrong_order
= fdisk_table_wrong_order(cf
->table
) ? 1 : 0;
398 if (MENU_START_LINE
- TABLE_START_LINE
< cf
->nlines
)
399 cf
->page_sz
= MENU_START_LINE
- TABLE_START_LINE
- 1;
401 cf
->lines
= xcalloc(cf
->nlines
, sizeof(char *));
403 for (p
= cf
->linesbuf
, i
= 0; p
&& i
< cf
->nlines
; i
++) {
415 static struct fdisk_partition
*get_current_partition(struct cfdisk
*cf
)
420 return fdisk_table_get_partition(cf
->table
, cf
->lines_idx
);
423 static int is_freespace(struct cfdisk
*cf
, size_t i
)
425 struct fdisk_partition
*pa
;
430 pa
= fdisk_table_get_partition(cf
->table
, i
);
431 return fdisk_partition_is_freespace(pa
);
434 /* converts libfdisk FDISK_ASKTYPE_MENU to cfdisk menu and returns user's
435 * responseback to libfdisk
437 static int ask_menu(struct fdisk_ask
*ask
, struct cfdisk
*cf
)
439 struct cfdisk_menuitem
*d
, *cm
;
441 size_t i
= 0, nitems
;
442 const char *name
, *desc
;
447 /* create cfdisk menu according to libfdisk ask-menu, note that the
448 * last cm[] item has to be empty -- so nitems + 1 */
449 nitems
= fdisk_ask_menu_get_nitems(ask
);
450 cm
= xcalloc(nitems
+ 1, sizeof(struct cfdisk_menuitem
));
452 for (i
= 0; i
< nitems
; i
++) {
453 if (fdisk_ask_menu_get_item(ask
, i
, &key
, &name
, &desc
))
460 /* make the new menu active */
471 if (ui_menu_move(cf
, key
) == 0)
478 d
= menu_get_menuitem(cf
, cf
->menu
->idx
);
480 fdisk_ask_menu_set_result(ask
, d
->key
);
494 static int ask_callback(struct fdisk_context
*cxt
, struct fdisk_ask
*ask
,
495 void *data
__attribute__((__unused__
)))
502 switch(fdisk_ask_get_type(ask
)) {
503 case FDISK_ASKTYPE_INFO
:
504 ui_info(fdisk_ask_print_get_mesg(ask
));
506 case FDISK_ASKTYPE_WARNX
:
507 ui_warnx(fdisk_ask_print_get_mesg(ask
));
509 case FDISK_ASKTYPE_WARN
:
510 ui_warn(fdisk_ask_print_get_mesg(ask
));
512 case FDISK_ASKTYPE_MENU
:
513 ask_menu(ask
, (struct cfdisk
*) data
);
516 ui_warnx(_("internal error: unsupported dialog type %d"),
517 fdisk_ask_get_type(ask
));
523 static int ui_end(void)
528 #if defined(HAVE_SLCURSES_H) || defined(HAVE_SLANG_SLCURSES_H)
529 SLsmg_gotorc(LINES
- 1, 0);
532 mvcur(0, COLS
- 1, LINES
-1, 0);
542 static void ui_vprint_center(int line
, int attrs
, const char *fmt
, va_list ap
)
550 xvasprintf(&buf
, fmt
, ap
);
552 width
= mbs_safe_width(buf
);
553 if (width
> (size_t) COLS
) {
554 char *p
= strrchr(buf
+ COLS
, ' ');
558 if (line
+ 1 >= LINES
)
561 mvaddstr(line
, 0, buf
);
562 mvaddstr(line
+ 1, 0, p
+1);
566 mvaddstr(line
, (COLS
- width
) / 2, buf
);
572 static void ui_center(int line
, const char *fmt
, ...)
576 ui_vprint_center(line
, 0, fmt
, ap
);
580 static void ui_warnx(const char *fmt
, ...)
585 ui_vprint_center(INFO_LINE
,
586 colors_wanted() ? COLOR_PAIR(CFDISK_CL_WARNING
) : 0,
589 vfprintf(stderr
, fmt
, ap
);
595 static void ui_warn(const char *fmt
, ...)
600 xasprintf(&fmt_m
, "%s: %m", fmt
);
604 ui_vprint_center(INFO_LINE
,
605 colors_wanted() ? COLOR_PAIR(CFDISK_CL_WARNING
) : 0,
608 vfprintf(stderr
, fmt_m
, ap
);
615 static int __attribute__((__noreturn__
)) ui_errx(int rc
, const char *fmt
, ...)
621 fprintf(stderr
, "%s: ", program_invocation_short_name
);
622 vfprintf(stderr
, fmt
, ap
);
629 static void ui_info(const char *fmt
, ...)
634 ui_vprint_center(INFO_LINE
, A_BOLD
, fmt
, ap
);
636 vfprintf(stdout
, fmt
, ap
);
642 static void ui_clean_info(void)
648 static void ui_hint(const char *fmt
, ...)
653 ui_vprint_center(HINT_LINE
, A_BOLD
, fmt
, ap
);
655 vfprintf(stdout
, fmt
, ap
);
661 static void ui_clean_hint(void)
667 static void die_on_signal(int dummy
__attribute__((__unused__
)))
669 DBG(FRONTEND
, ul_debug("die on signal."));
674 static void resize_on_signal(int dummy
__attribute__((__unused__
)))
676 DBG(FRONTEND
, ul_debug("resize on signal."));
680 static void menu_refresh_size(struct cfdisk
*cf
)
682 if (cf
->menu
&& cf
->menu
->nitems
)
683 cf
->menu
->page_sz
= (cf
->menu
->nitems
/ (LINES
- 4)) ? LINES
- 4 : 0;
686 static void menu_update_ignore(struct cfdisk
*cf
)
688 char ignore
[128] = { 0 };
690 struct cfdisk_menu
*m
;
691 struct cfdisk_menuitem
*d
, *org
;
696 assert(cf
->menu
->ignore_cb
);
699 org
= menu_get_menuitem(cf
, m
->idx
);
701 DBG(FRONTEND
, ul_debug("menu: update menu ignored keys"));
703 i
= m
->ignore_cb(cf
, ignore
, sizeof(ignore
));
706 /* return if no change */
707 if ( (!m
->ignore
&& !*ignore
)
708 || (m
->ignore
&& *ignore
&& strcmp(m
->ignore
, ignore
) == 0)) {
713 m
->ignore
= xstrdup(ignore
);
716 for (d
= m
->items
; d
->name
; d
++) {
717 if (m
->ignore
&& strchr(m
->ignore
, d
->key
))
722 /* refresh menu index to be at the same menuitem or go to the first */
723 if (org
&& menu_get_menuitem_by_key(cf
, org
->key
, &idx
))
728 menu_refresh_size(cf
);
731 static struct cfdisk_menu
*menu_push(
733 struct cfdisk_menuitem
*items
)
735 struct cfdisk_menu
*m
= xcalloc(1, sizeof(*m
));
736 struct cfdisk_menuitem
*d
;
740 DBG(FRONTEND
, ul_debug("menu: new menu"));
745 for (d
= m
->items
; d
->name
; d
++) {
746 const char *name
= _(d
->name
);
747 size_t len
= mbs_safe_width(name
);
754 menu_refresh_size(cf
);
758 static struct cfdisk_menu
*menu_pop(struct cfdisk
*cf
)
760 struct cfdisk_menu
*m
= NULL
;
764 DBG(FRONTEND
, ul_debug("menu: rem menu"));
768 free(cf
->menu
->ignore
);
769 free(cf
->menu
->title
);
776 static void menu_set_title(struct cfdisk_menu
*m
, const char *title
)
781 size_t len
= mbs_safe_width(title
);
782 if (len
+ 3 > m
->width
)
784 str
= xstrdup(title
);
790 static int ui_init(struct cfdisk
*cf
__attribute__((__unused__
)))
794 DBG(FRONTEND
, ul_debug("ui: init"));
796 /* setup SIGCHLD handler */
797 sigemptyset(&sa
.sa_mask
);
799 sa
.sa_handler
= die_on_signal
;
800 sigaction(SIGINT
, &sa
, NULL
);
801 sigaction(SIGTERM
, &sa
, NULL
);
803 sa
.sa_handler
= resize_on_signal
;
804 sigaction(SIGWINCH
, &sa
, NULL
);
809 #ifdef HAVE_USE_DEFAULT_COLORS
810 if (colors_wanted() && has_colors()) {
814 use_default_colors();
815 for (i
= 1; i
< ARRAY_SIZE(color_pairs
); i
++) /* yeah, start from 1! */
816 init_pair(i
, color_pairs
[i
][0], color_pairs
[i
][1]);
826 keypad(stdscr
, TRUE
);
831 static size_t menuitem_get_line(struct cfdisk
*cf
, size_t idx
)
833 struct cfdisk_menu
*m
= cf
->menu
;
836 if (!m
->page_sz
) /* small menu */
837 return (LINES
- (cf
->menu
->nitems
+ 1)) / 2 + idx
;
838 return (idx
% m
->page_sz
) + 1;
840 size_t len
= m
->width
+ 4 + MENU_PADDING
; /* item width */
841 size_t items
= COLS
/ len
; /* items per line */
846 return MENU_START_LINE
+ ((idx
/ items
));
850 static int menuitem_get_column(struct cfdisk
*cf
, size_t idx
)
852 if (cf
->menu
->vertical
) {
853 size_t nc
= cf
->menu
->width
+ MENU_PADDING
;
854 if ((size_t) COLS
<= nc
)
856 return (COLS
- nc
) / 2;
858 size_t len
= cf
->menu
->width
+ 4 + MENU_PADDING
; /* item width */
859 size_t items
= COLS
/ len
; /* items per line */
860 size_t extra
= items
< cf
->menu
->nitems
? /* extra space on line */
861 COLS
% len
: /* - multi-line menu */
862 COLS
- (cf
->menu
->nitems
* len
); /* - one line menu */
865 return 0; /* hmm... no space */
867 extra
+= MENU_PADDING
; /* add padding after last item to extra */
870 return (idx
* len
) + (extra
/ 2);
871 return ((idx
% items
) * len
) + (extra
/ 2);
875 static int menuitem_on_page(struct cfdisk
*cf
, size_t idx
)
877 struct cfdisk_menu
*m
= cf
->menu
;
879 if (m
->page_sz
== 0 ||
880 m
->idx
/ m
->page_sz
== idx
/ m
->page_sz
)
885 static struct cfdisk_menuitem
*menu_get_menuitem(struct cfdisk
*cf
, size_t idx
)
887 struct cfdisk_menuitem
*d
;
890 for (i
= 0, d
= cf
->menu
->items
; d
->name
; d
++) {
891 if (cf
->menu
->ignore
&& strchr(cf
->menu
->ignore
, d
->key
))
900 static struct cfdisk_menuitem
*menu_get_menuitem_by_key(struct cfdisk
*cf
,
901 int key
, size_t *idx
)
903 struct cfdisk_menuitem
*d
;
905 for (*idx
= 0, d
= cf
->menu
->items
; d
->name
; d
++) {
906 if (cf
->menu
->ignore
&& strchr(cf
->menu
->ignore
, d
->key
))
916 static void ui_draw_menuitem(struct cfdisk
*cf
,
917 struct cfdisk_menuitem
*d
,
920 char buf
[80 * MB_CUR_MAX
];
922 size_t width
= cf
->menu
->width
+ 2; /* 2 = blank around string */
923 int ln
, cl
, vert
= cf
->menu
->vertical
;
925 if (!menuitem_on_page(cf
, idx
))
926 return; /* no visible item */
927 ln
= menuitem_get_line(cf
, idx
);
928 cl
= menuitem_get_column(cf
, idx
);
931 mbsalign(name
, buf
, sizeof(buf
), &width
,
932 vert
? MBS_ALIGN_LEFT
: MBS_ALIGN_CENTER
,
935 DBG(FRONTEND
, ul_debug("ui: menuitem: cl=%d, ln=%d, item='%s'",
939 mvaddch(ln
, cl
- 1, ACS_VLINE
);
940 mvaddch(ln
, cl
+ cf
->menu
->width
+ 4, ACS_VLINE
);
943 if (cf
->menu
->idx
== idx
) {
945 mvprintw(ln
, cl
, vert
? " %s " : "[%s]", buf
);
950 mvprintw(ln
, cl
, vert
? " %s " : "[%s]", buf
);
953 static void ui_clean_menu(struct cfdisk
*cf
)
957 struct cfdisk_menu
*m
= cf
->menu
;
958 size_t ln
= menuitem_get_line(cf
, 0);
961 nlines
= m
->page_sz
? m
->page_sz
: m
->nitems
;
963 nlines
= menuitem_get_line(cf
, m
->nitems
);
965 for (i
= ln
; i
<= ln
+ nlines
; i
++) {
976 static void ui_draw_menu(struct cfdisk
*cf
)
978 struct cfdisk_menuitem
*d
;
979 struct cfdisk_menu
*m
;
981 size_t ln
= menuitem_get_line(cf
, 0);
987 DBG(FRONTEND
, ul_debug("ui: menu: draw start"));
993 nlines
= m
->page_sz
? m
->page_sz
: m
->nitems
;
995 nlines
= menuitem_get_line(cf
, m
->nitems
);
998 menu_update_ignore(cf
);
1000 while ((d
= menu_get_menuitem(cf
, i
)))
1001 ui_draw_menuitem(cf
, d
, i
++);
1004 size_t cl
= menuitem_get_column(cf
, 0);
1005 size_t curpg
= m
->page_sz
? m
->idx
/ m
->page_sz
: 0;
1007 /* corners and horizontal lines */
1008 mvaddch(ln
- 1, cl
- 1, ACS_ULCORNER
);
1009 mvaddch(ln
+ nlines
, cl
- 1, ACS_LLCORNER
);
1011 for (i
= 0; i
< m
->width
+ 4; i
++) {
1012 mvaddch(ln
- 1, cl
+ i
, ACS_HLINE
);
1013 mvaddch(ln
+ nlines
, cl
+ i
, ACS_HLINE
);
1016 mvaddch(ln
- 1, cl
+ i
, ACS_URCORNER
);
1017 mvaddch(ln
+ nlines
, cl
+ i
, ACS_LRCORNER
);
1019 /* draw also lines around empty lines on last page */
1021 m
->nitems
/ m
->page_sz
== m
->idx
/ m
->page_sz
) {
1022 for (i
= m
->nitems
% m
->page_sz
+ 1; i
<= m
->page_sz
; i
++) {
1023 mvaddch(i
, cl
- 1, ACS_VLINE
);
1024 mvaddch(i
, cl
+ cf
->menu
->width
+ 4, ACS_VLINE
);
1029 mvprintw(ln
- 1, cl
, " %s ", m
->title
);
1033 mvaddch(ln
- 1, cl
+ m
->width
+ 3, ACS_UARROW
);
1034 if (m
->page_sz
&& curpg
< m
->nitems
/ m
->page_sz
)
1035 mvaddch(ln
+ nlines
, cl
+ m
->width
+ 3, ACS_DARROW
);
1038 DBG(FRONTEND
, ul_debug("ui: menu: draw end."));
1041 static void ui_menu_goto(struct cfdisk
*cf
, int where
)
1043 struct cfdisk_menuitem
*d
;
1046 /* stop and begin/end for vertical menus */
1047 if (cf
->menu
->vertical
) {
1050 else if (where
> (int) cf
->menu
->nitems
- 1)
1051 where
= cf
->menu
->nitems
- 1;
1053 /* continue from begin/end */
1055 where
= cf
->menu
->nitems
- 1;
1056 else if ((size_t) where
> cf
->menu
->nitems
- 1)
1059 if ((size_t) where
== cf
->menu
->idx
)
1064 old
= cf
->menu
->idx
;
1065 cf
->menu
->idx
= where
;
1067 if (!menuitem_on_page(cf
, old
)) {
1072 d
= menu_get_menuitem(cf
, old
);
1073 ui_draw_menuitem(cf
, d
, old
);
1075 d
= menu_get_menuitem(cf
, where
);
1076 ui_draw_menuitem(cf
, d
, where
);
1079 static int ui_menu_move(struct cfdisk
*cf
, int key
)
1081 struct cfdisk_menu
*m
;
1087 return 0; /* ignore errors */
1091 DBG(FRONTEND
, ul_debug("ui: menu move key >%c<.", key
));
1097 case '\016': /* ^N */
1098 case 'j': /* Vi-like alternative */
1099 ui_menu_goto(cf
, m
->idx
+ 1);
1102 case '\020': /* ^P */
1103 case 'k': /* Vi-like alternative */
1104 ui_menu_goto(cf
, (int) m
->idx
- 1);
1108 ui_menu_goto(cf
, (int) m
->idx
- m
->page_sz
);
1113 ui_menu_goto(cf
, 0);
1117 ui_menu_goto(cf
, m
->idx
+ m
->page_sz
);
1122 ui_menu_goto(cf
, m
->nitems
);
1129 ui_menu_goto(cf
, m
->idx
+ 1);
1135 ui_menu_goto(cf
, (int) m
->idx
- 1);
1140 return 1; /* key irrelevant for menu move */
1143 /* but don't call me from ui_run(), this is for pop-up menus only */
1144 static void ui_menu_resize(struct cfdisk
*cf
)
1148 menu_refresh_size(cf
);
1153 static int partition_on_page(struct cfdisk
*cf
, size_t i
)
1155 if (cf
->page_sz
== 0 ||
1156 cf
->lines_idx
/ cf
->page_sz
== i
/ cf
->page_sz
)
1161 static void ui_draw_partition(struct cfdisk
*cf
, size_t i
)
1163 int ln
= TABLE_START_LINE
+ 1 + i
; /* skip table header */
1164 int cl
= ARROW_CURSOR_WIDTH
; /* we need extra space for cursor */
1165 int cur
= cf
->lines_idx
== i
;
1169 if (!partition_on_page(cf
, i
))
1171 ln
= TABLE_START_LINE
+ (i
% cf
->page_sz
) + 1;
1172 curpg
= cf
->lines_idx
/ cf
->page_sz
;
1175 DBG(FRONTEND
, ul_debug(
1176 "ui: draw partition %zu [page_sz=%zu, "
1177 "line=%d, idx=%zu]",
1178 i
, cf
->page_sz
, ln
, cf
->lines_idx
));
1182 mvaddstr(ln
, 0, ARROW_CURSOR_STRING
);
1183 mvaddstr(ln
, cl
, cf
->lines
[i
+ 1]);
1188 if (colors_wanted() && is_freespace(cf
, i
)) {
1189 attron(COLOR_PAIR(CFDISK_CL_FREESPACE
));
1192 mvaddstr(ln
, 0, ARROW_CURSOR_DUMMY
);
1193 mvaddstr(ln
, cl
, cf
->lines
[i
+ 1]);
1195 attroff(COLOR_PAIR(CFDISK_CL_FREESPACE
));
1198 if ((size_t) ln
== MENU_START_LINE
- 1 &&
1199 cf
->page_sz
&& curpg
< cf
->nlines
/ cf
->page_sz
) {
1202 mvaddch(ln
, COLS
- 1, ACS_DARROW
);
1203 mvaddch(ln
, 0, ACS_DARROW
);
1209 static int ui_draw_table(struct cfdisk
*cf
)
1211 int cl
= ARROW_CURSOR_WIDTH
;
1212 size_t i
, nparts
= fdisk_table_get_nents(cf
->table
);
1213 size_t curpg
= cf
->page_sz
? cf
->lines_idx
/ cf
->page_sz
: 0;
1215 DBG(FRONTEND
, ul_debug("ui: draw table"));
1217 for (i
= TABLE_START_LINE
; i
<= TABLE_START_LINE
+ cf
->page_sz
; i
++) {
1222 if ((size_t) cf
->lines_idx
> nparts
- 1)
1223 cf
->lines_idx
= nparts
? nparts
- 1 : 0;
1227 mvaddstr(TABLE_START_LINE
, cl
, cf
->lines
[0]);
1230 /* print partitions */
1231 for (i
= 0; i
< nparts
; i
++)
1232 ui_draw_partition(cf
, i
);
1235 mvaddch(TABLE_START_LINE
, COLS
- 1, ACS_UARROW
);
1236 mvaddch(TABLE_START_LINE
, 0, ACS_UARROW
);
1238 if (cf
->page_sz
&& curpg
< cf
->nlines
/ cf
->page_sz
) {
1239 mvaddch(MENU_START_LINE
- 1, COLS
- 1, ACS_DARROW
);
1240 mvaddch(MENU_START_LINE
- 1, 0, ACS_DARROW
);
1245 static int ui_table_goto(struct cfdisk
*cf
, int where
)
1248 size_t nparts
= fdisk_table_get_nents(cf
->table
);
1250 DBG(FRONTEND
, ul_debug("ui: goto table %d", where
));
1254 else if ((size_t) where
> nparts
- 1)
1257 if ((size_t) where
== cf
->lines_idx
)
1260 old
= cf
->lines_idx
;
1261 cf
->lines_idx
= where
;
1263 if (!partition_on_page(cf
, old
) ||!partition_on_page(cf
, where
))
1266 ui_draw_partition(cf
, old
); /* cleanup old */
1267 ui_draw_partition(cf
, where
); /* draw new */
1275 static int ui_refresh(struct cfdisk
*cf
)
1278 uint64_t bytes
= cf
->cxt
->total_sectors
* cf
->cxt
->sector_size
;
1279 char *strsz
= size_to_human_string(SIZE_SUFFIX_SPACE
1280 | SIZE_SUFFIX_3LETTER
, bytes
);
1288 ui_center(0, _("Disk: %s"), cf
->cxt
->dev_path
);
1290 ui_center(1, _("Size: %s, %ju bytes, %ju sectors"),
1291 strsz
, bytes
, (uintmax_t) cf
->cxt
->total_sectors
);
1292 if (fdisk_get_disklabel_id(cf
->cxt
, &id
) == 0 && id
)
1293 ui_center(2, _("Label: %s, identifier: %s"),
1294 cf
->cxt
->label
->name
, id
);
1296 ui_center(2, _("Label: %s"), cf
->cxt
->label
->name
);
1305 static ssize_t
ui_get_string(struct cfdisk
*cf
, const char *prompt
,
1306 const char *hint
, char *buf
, size_t len
)
1309 ssize_t i
= 0, rc
= -1;
1310 int ln
= MENU_START_LINE
, cl
= 1;
1320 mvaddstr(ln
, cl
, (char *) prompt
);
1321 cl
+= mbs_safe_width(prompt
);
1327 cells
= mbs_safe_width(buf
);
1328 mvaddstr(ln
, cl
, buf
);
1336 move(ln
, cl
+ cells
);
1341 #if !defined(HAVE_SLCURSES_H) && !defined(HAVE_SLANG_SLCURSES_H) && \
1342 defined(HAVE_LIBNCURSESW) && defined(HAVE_WIDECHAR)
1344 if (get_wch(&c
) == ERR
) {
1347 if ((c
= getch()) == ERR
) {
1353 if (!isatty(STDIN_FILENO
))
1358 if (c
== '\r' || c
== '\n' || c
== KEY_ENTER
)
1363 rc
= -CFDISK_ERR_ESC
;
1370 i
= mbs_truncate(buf
, &cells
);
1373 mvaddch(ln
, cl
+ cells
, ' ');
1374 move(ln
, cl
+ cells
);
1379 #if defined(HAVE_LIBNCURSESW) && defined(HAVE_WIDECHAR)
1380 if (i
+ 1 < (ssize_t
) len
&& iswprint(c
)) {
1381 wchar_t wc
= (wchar_t) c
;
1382 char s
[MB_CUR_MAX
+ 1];
1383 int sz
= wctomb(s
, wc
);
1385 if (sz
> 0 && sz
+ i
< (ssize_t
) len
) {
1387 mvaddnstr(ln
, cl
+ cells
, s
, sz
);
1388 memcpy(buf
+ i
, s
, sz
);
1391 cells
+= wcwidth(wc
);
1396 if (i
+ 1 < (ssize_t
) len
&& isprint(c
)) {
1397 mvaddch(ln
, cl
+ cells
, c
);
1409 rc
= i
; /* success */
1419 /* @res is default value as well as result in bytes */
1420 static int ui_get_size(struct cfdisk
*cf
, const char *prompt
, uintmax_t *res
,
1421 uintmax_t low
, uintmax_t up
)
1426 char *dflt
= size_to_human_string(0, *res
);
1428 DBG(FRONTEND
, ul_debug("ui: get_size (default=%ju)", *res
));
1433 int pwr
= 0, insec
= 0;
1435 snprintf(buf
, sizeof(buf
), "%s", dflt
);
1436 rc
= ui_get_string(cf
, prompt
,
1437 _("May be followed by {M,B,G,T}iB "
1438 "(the \"iB\" is optional) or S for sectors."),
1441 ui_warnx(_("Please, specify size."));
1442 continue; /* nothing specified */
1443 } else if (rc
== -CFDISK_ERR_ESC
)
1444 break; /* cancel dialog */
1446 if (strcmp(buf
, dflt
) == 0)
1447 user
= *res
, rc
= 0; /* no change, use default */
1449 size_t len
= strlen(buf
);
1450 if (buf
[len
- 1] == 'S' || buf
[len
- 1] == 's') {
1452 buf
[len
- 1] = '\0';
1454 rc
= parse_size(buf
, &user
, &pwr
); /* parse */
1458 DBG(FRONTEND
, ul_debug("ui: get_size user=%ju, power=%d, sectors=%s",
1459 user
, pwr
, insec
? "yes" : "no"));
1461 user
*= cf
->cxt
->sector_size
;
1463 ui_warnx(_("Minimal size is %ju"), low
);
1466 if (user
> up
&& pwr
&& user
< up
+ (1ULL << pwr
* 10))
1467 /* ignore when the user specified size overflow
1468 * with in range specified by suffix (e.g. MiB) */
1472 ui_warnx(_("Maximal size is %ju bytes."), up
);
1476 ui_warnx(_("Failed to parse size."));
1483 DBG(FRONTEND
, ul_debug("ui: get_size (result=%ju, rc=%zd)", *res
, rc
));
1487 static struct fdisk_parttype
*ui_get_parttype(struct cfdisk
*cf
,
1488 struct fdisk_parttype
*cur
)
1490 struct cfdisk_menuitem
*d
, *cm
;
1491 size_t i
= 0, nitems
, idx
= 0;
1492 struct fdisk_parttype
*t
= NULL
;
1493 int has_typestr
= 0;
1495 DBG(FRONTEND
, ul_debug("ui: asking for parttype."));
1497 /* create cfdisk menu according to label types, note that the
1498 * last cm[] item has to be empty -- so nitems + 1 */
1499 nitems
= cf
->cxt
->label
->nparttypes
;
1502 cm
= xcalloc(nitems
+ 1, sizeof(struct cfdisk_menuitem
));
1506 has_typestr
= cf
->cxt
->label
->parttypes
[0].typestr
&&
1507 *cf
->cxt
->label
->parttypes
[0].typestr
;
1509 for (i
= 0; i
< nitems
; i
++) {
1510 struct fdisk_parttype
*x
= &cf
->cxt
->label
->parttypes
[i
];
1517 xasprintf(&name
, "%2x %s", x
->type
, _(x
->name
));
1519 name
= (char *) _(x
->name
);
1520 cm
[i
].desc
= x
->typestr
;
1527 /* make the new menu active */
1529 cf
->menu
->vertical
= 1;
1530 cf
->menu
->idx
= idx
;
1531 menu_set_title(cf
->menu
, _("Select partition type"));
1540 if (ui_menu_move(cf
, key
) == 0)
1547 d
= menu_get_menuitem(cf
, cf
->menu
->idx
);
1549 t
= (struct fdisk_parttype
*) d
->userdata
;
1561 for (i
= 0; i
< nitems
; i
++)
1562 free((char *) cm
[i
].name
);
1565 DBG(FRONTEND
, ul_debug("ui: get parrtype done [type=%s] ", t
? t
->name
: NULL
));
1569 /* prints menu with libfdisk labels and waits for users response */
1570 static int ui_create_label(struct cfdisk
*cf
)
1572 struct cfdisk_menuitem
*d
, *cm
;
1574 size_t i
= 0, nitems
;
1575 struct fdisk_label
*lb
= NULL
;
1579 DBG(FRONTEND
, ul_debug("ui: asking for new disklabe."));
1581 /* create cfdisk menu according to libfdisk labels, note that the
1582 * last cm[] item has to be empty -- so nitems + 1 */
1583 nitems
= fdisk_context_get_nlabels(cf
->cxt
);
1584 cm
= xcalloc(nitems
+ 1, sizeof(struct cfdisk_menuitem
));
1586 while (fdisk_context_next_label(cf
->cxt
, &lb
) == 0) {
1587 if (fdisk_label_is_disabled(lb
) || strcmp(lb
->name
, "bsd") == 0)
1589 cm
[i
++].name
= lb
->name
;
1593 ui_center(LINES
- 4,
1594 _("Device does not contain a recognized partition table."));
1595 ui_center(LINES
- 3,
1596 _("Please, select a type to create a new disk label."));
1598 /* make the new menu active */
1600 cf
->menu
->vertical
= 1;
1601 menu_set_title(cf
->menu
, _("Select label type"));
1610 if (ui_menu_move(cf
, key
) == 0)
1616 d
= menu_get_menuitem(cf
, cf
->menu
->idx
);
1618 rc
= fdisk_create_disklabel(cf
->cxt
, d
->name
);
1630 DBG(FRONTEND
, ul_debug("ui: create label done [rc=%d] ", rc
));
1634 static int ui_help(void)
1637 static const char *help
[] = {
1638 N_("Help Screen for cfdisk"),
1640 N_("This is cfdisk, a curses based disk partitioning program, which"),
1641 N_("allows you to create, delete and modify partitions on your hard"),
1644 N_("Copyright (C) 2014 Karel Zak <kzak@redhat.com> "),
1645 N_("Based on the original cfdisk from Kevin E. Martin & aeb."),
1647 N_("Command Meaning"),
1648 N_("------- -------"),
1649 N_(" b Toggle bootable flag of the current partition"),
1650 N_(" d Delete the current partition"),
1651 N_(" h Print this screen"),
1652 N_(" n Create new partition from free space"),
1653 N_(" q Quit program without writing partition table"),
1654 N_(" t Change the partition type"),
1655 N_(" s Fix partitions order (only when in disarray)"),
1656 N_(" W Write partition table to disk (must enter upper case W)"),
1657 N_(" Since this might destroy data on the disk, you must"),
1658 N_(" either confirm or deny the write by entering `yes' or"),
1660 N_("Up Arrow Move cursor to the previous partition"),
1661 N_("Down Arrow Move cursor to the next partition"),
1662 N_("Left Arrow Move cursor to the previous menu item"),
1663 N_("Right Arrow Move cursor to the next menu item"),
1666 N_("Note: All of the commands can be entered with either upper or lower"),
1667 N_("case letters (except for Writes)."),
1669 N_("Use lsblk(8) or partx(8) to see more details about the device.")
1673 for (i
= 0; i
< ARRAY_SIZE(help
); i
++)
1674 mvaddstr(i
, 1, _(help
[i
]));
1676 ui_info(_("Press a key to continue."));
1682 /* TODO: use @sz, now 128bytes */
1683 static int main_menu_ignore_keys(struct cfdisk
*cf
, char *ignore
,
1684 size_t sz
__attribute__((__unused__
)))
1686 struct fdisk_partition
*pa
= get_current_partition(cf
);
1691 if (fdisk_partition_is_freespace(pa
)) {
1692 ignore
[i
++] = 'd'; /* delete */
1693 ignore
[i
++] = 't'; /* set type */
1694 ignore
[i
++] = 'b'; /* set bootable */
1697 if (!fdisk_is_disklabel(cf
->cxt
, DOS
) &&
1698 !fdisk_is_disklabel(cf
->cxt
, SGI
))
1702 if (!cf
->wrong_order
)
1704 if (fdisk_context_is_readonly(cf
->cxt
))
1710 /* returns: error: < 0, success: 0, quit: 1 */
1711 static int main_menu_action(struct cfdisk
*cf
, int key
)
1714 int ref
= 0, rc
, org_order
= cf
->wrong_order
;
1715 const char *info
= NULL
, *warn
= NULL
;
1716 struct fdisk_partition
*pa
;
1723 struct cfdisk_menuitem
*d
= menu_get_menuitem(cf
, cf
->menu
->idx
);
1728 } else if (key
!= 'w' && key
!= 'W')
1729 key
= tolower(key
); /* case insensitive except 'W'rite */
1731 DBG(FRONTEND
, ul_debug("ui: main menu action: key=%c", key
));
1733 if (cf
->menu
->ignore
&& strchr(cf
->menu
->ignore
, key
)) {
1734 DBG(FRONTEND
, ul_debug(" ignore '%c'", key
));
1738 pa
= get_current_partition(cf
);
1739 n
= fdisk_partition_get_partno(pa
);
1741 DBG(FRONTEND
, ul_debug("menu action on %p", pa
));
1746 case 'b': /* Bootable flag */
1748 int fl
= fdisk_is_disklabel(cf
->cxt
, DOS
) ? DOS_FLAG_ACTIVE
:
1749 fdisk_is_disklabel(cf
->cxt
, SGI
) ? SGI_FLAG_BOOT
: 0;
1751 if (fl
&& fdisk_partition_toggle_flag(cf
->cxt
, n
, fl
))
1752 warn
= _("Could not toggle the flag.");
1760 case 'd': /* Delete */
1761 if (fdisk_delete_partition(cf
->cxt
, n
) != 0)
1762 warn
= _("Could not delete partition %zu.");
1764 info
= _("Partition %zu has been deleted.");
1767 case 'h': /* help */
1773 uint64_t start
, size
, dflt_size
;
1774 struct fdisk_partition
*npa
; /* the new partition */
1776 if (!pa
|| !fdisk_partition_is_freespace(pa
))
1778 npa
= fdisk_new_partition();
1781 /* free space range */
1782 start
= fdisk_partition_get_start(pa
);
1783 size
= dflt_size
= fdisk_partition_get_size(pa
) * cf
->cxt
->sector_size
;
1785 if (ui_get_size(cf
, _("Partition size: "), &size
, 1, size
)
1789 if (dflt_size
== size
) /* default is to fillin all free space */
1790 fdisk_partition_end_follow_default(npa
, 1);
1791 else /* set relative size of the partition */
1792 fdisk_partition_set_size(npa
, size
/ cf
->cxt
->sector_size
);
1794 fdisk_partition_set_start(npa
, start
);
1795 fdisk_partition_partno_follow_default(npa
, 1);
1796 /* add to disk label -- libfdisk will ask for missing details */
1797 rc
= fdisk_add_partition(cf
->cxt
, npa
);
1798 fdisk_unref_partition(npa
);
1803 case 'q': /* Quit */
1805 case 't': /* Type */
1807 struct fdisk_parttype
*t
;
1809 if (!pa
|| fdisk_partition_is_freespace(pa
))
1811 t
= (struct fdisk_parttype
*) fdisk_partition_get_type(pa
);
1812 t
= ui_get_parttype(cf
, t
);
1815 if (t
&& fdisk_set_partition_type(cf
->cxt
, n
, t
) == 0)
1816 info
= _("Changed type of partition %zu.");
1818 info
= _("The type of partition %zu is unchanged.");
1821 case 's': /* fix order */
1822 if (cf
->wrong_order
) {
1823 fdisk_reorder_partitions(cf
->cxt
);
1827 case 'W': /* Write */
1829 char buf
[64] = { 0 };
1832 if (fdisk_context_is_readonly(cf
->cxt
)) {
1833 warn
= _("Device open in read-only mode");
1837 rc
= ui_get_string(cf
,
1838 _("Are you sure you want to write the partition "
1840 _("Type \"yes\" or \"no\", or press ESC to leave this dialog."),
1844 if (rc
<= 0 || strcasecmp(buf
, "yes") != 0
1845 || strcasecmp(buf
, _("yes")) != 0) {
1846 info
= _("Did not write partition table to disk");
1849 rc
= fdisk_write_disklabel(cf
->cxt
);
1851 warn
= _("Failed to write disklabel");
1853 fdisk_reread_partition_table(cf
->cxt
);
1854 info
= _("The partition table has been altered.");
1870 ui_warnx(warn
, n
+ 1);
1872 ui_info(info
, n
+ 1);
1873 else if (key
== 'n' && cf
->wrong_order
&& org_order
== 0)
1874 ui_info(_("Note that partition table entries are not in disk order now."));
1879 static void ui_resize_refresh(struct cfdisk
*cf
)
1882 menu_refresh_size(cf
);
1887 static int ui_run(struct cfdisk
*cf
)
1891 DBG(FRONTEND
, ul_debug("ui: start COLS=%d, LINES=%d", COLS
, LINES
));
1893 if (!fdisk_dev_has_disklabel(cf
->cxt
) || cf
->zero_start
) {
1894 rc
= ui_create_label(cf
);
1896 ui_errx(EXIT_FAILURE
,
1897 _("failed to create a new disklabel"));
1903 rc
= lines_refresh(cf
);
1905 ui_errx(EXIT_FAILURE
, _("failed to read partitions"));
1907 menu_push(cf
, main_menuitems
);
1908 cf
->menu
->ignore_cb
= main_menu_ignore_keys
;
1910 rc
= ui_refresh(cf
);
1914 if (fdisk_context_is_readonly(cf
->cxt
))
1915 ui_warnx(_("Device open in read-only mode."));
1918 int rc
= 0, key
= getch();
1921 /* Note that ncurses getch() returns ERR when interrupted
1922 * by signal, but SLang does not interrupt at all. */
1923 ui_resize_refresh(cf
);
1926 if (ui_menu_move(cf
, key
) == 0)
1929 DBG(FRONTEND
, ul_debug("ui: main action key >%c<.", key
));
1933 case '\016': /* ^N */
1934 case 'j': /* Vi-like alternative */
1935 ui_table_goto(cf
, cf
->lines_idx
+ 1);
1938 case '\020': /* ^P */
1939 case 'k': /* Vi-like alternative */
1940 ui_table_goto(cf
, (int) cf
->lines_idx
- 1);
1944 ui_table_goto(cf
, (int) cf
->lines_idx
- cf
->page_sz
);
1948 ui_table_goto(cf
, 0);
1952 ui_table_goto(cf
, cf
->lines_idx
+ cf
->page_sz
);
1956 ui_table_goto(cf
, (int) cf
->nlines
- 1);
1961 rc
= main_menu_action(cf
, 0);
1964 rc
= main_menu_action(cf
, key
);
1976 DBG(FRONTEND
, ul_debug("ui: end"));
1980 static void __attribute__ ((__noreturn__
)) usage(FILE *out
)
1982 fputs(USAGE_HEADER
, out
);
1985 _(" %1$s [options] <disk>\n"), program_invocation_short_name
);
1987 fputs(USAGE_OPTIONS
, out
);
1988 fputs(_(" -L --color[=<when>] colorize output (auto, always or never)\n"), out
);
1989 fputs(_(" -z --zero start with zeroed partition table\n"), out
);
1991 fputs(USAGE_SEPARATOR
, out
);
1992 fputs(USAGE_HELP
, out
);
1993 fputs(USAGE_VERSION
, out
);
1995 fprintf(out
, USAGE_MAN_TAIL("cfdisk(8)"));
1996 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
1999 int main(int argc
, char *argv
[])
2001 const char *diskpath
;
2002 int rc
, c
, colormode
= UL_COLORMODE_UNDEF
;
2003 struct cfdisk _cf
= { .lines_idx
= 0 },
2006 static const struct option longopts
[] = {
2007 { "color", optional_argument
, NULL
, 'L' },
2008 { "help", no_argument
, NULL
, 'h' },
2009 { "version", no_argument
, NULL
, 'V' },
2010 { "zero", no_argument
, NULL
, 'z' },
2014 setlocale(LC_ALL
, "");
2015 bindtextdomain(PACKAGE
, LOCALEDIR
);
2016 textdomain(PACKAGE
);
2017 atexit(close_stdout
);
2019 while((c
= getopt_long(argc
, argv
, "L::hVz", longopts
, NULL
)) != -1) {
2025 colormode
= UL_COLORMODE_AUTO
;
2027 colormode
= colormode_or_err(optarg
,
2028 _("unsupported color mode"));
2031 printf(_("%s from %s\n"), program_invocation_short_name
,
2033 return EXIT_SUCCESS
;
2040 colors_init(colormode
, "cfdisk");
2042 fdisk_init_debug(0);
2043 scols_init_debug(0);
2045 cf
->cxt
= fdisk_new_context();
2047 err(EXIT_FAILURE
, _("failed to allocate libfdisk context"));
2049 fdisk_context_set_ask(cf
->cxt
, ask_callback
, (void *) cf
);
2052 diskpath
= access(DEFAULT_DEVICE
, F_OK
) == 0 ?
2053 DEFAULT_DEVICE
: ALTERNATE_DEVICE
;
2055 diskpath
= argv
[optind
];
2057 rc
= fdisk_context_assign_device(cf
->cxt
, diskpath
, 0);
2059 rc
= fdisk_context_assign_device(cf
->cxt
, diskpath
, 1);
2061 err(EXIT_FAILURE
, _("cannot open %s"),
2062 optind
== argc
? DEFAULT_DEVICE
: diskpath
);
2064 /* Don't use err(), warn() from this point */
2071 fdisk_unref_table(cf
->table
);
2073 rc
= fdisk_context_deassign_device(cf
->cxt
, cf
->nwrites
== 0);
2074 fdisk_free_context(cf
->cxt
);
2075 DBG(FRONTEND
, ul_debug("bye! [rc=%d]", rc
));
2076 return rc
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
;