]> git.ipfire.org Git - thirdparty/util-linux.git/blame - disk-utils/cfdisk.c
more: Use ul_strtou16() in a robust way
[thirdparty/util-linux.git] / disk-utils / cfdisk.c
CommitLineData
2ddfd9e4 1/*
9e95aa12
KZ
2 * SPDX-License-Identifier: GPL-2.0-or-later
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
2ddfd9e4
BS
9 * cfdisk.c - Display or manipulate a disk partition table.
10 *
793e8d2a 11 * Copyright (C) 2014-2023 Karel Zak <kzak@redhat.com>
2ddfd9e4
BS
12 * Copyright (C) 1994 Kevin E. Martin (martin@cs.unc.edu)
13 *
14 * The original cfdisk was inspired by the fdisk program
15 * by A. V. Le Blanc (leblanc@mcc.ac.uk.
2ddfd9e4 16 */
6dbe3af9
KZ
17#include <stdlib.h>
18#include <stdio.h>
6dbe3af9 19#include <errno.h>
8c3a5a44 20#include <signal.h>
00b4f26a 21#include <ctype.h>
04915c3f 22#include <getopt.h>
57c83d63 23#include <assert.h>
baa3b270 24#include <libsmartcols.h>
7556c944 25#include <sys/ioctl.h>
ad6d9fd5 26#include <rpmatch.h>
57c83d63 27#include <libfdisk.h>
541e6934 28
dda7fe12
OO
29#ifdef HAVE_LIBMOUNT
30# include <libmount.h> /* keep it optional for non-linux systems */
31#endif
32
541e6934 33#ifdef HAVE_SLANG_H
3b411726 34# include <slang.h>
541e6934 35#elif defined(HAVE_SLANG_SLANG_H)
3b411726 36# include <slang/slang.h>
541e6934
KZ
37#endif
38
cc6ab5ef
PS
39#ifndef _XOPEN_SOURCE
40# define _XOPEN_SOURCE 500 /* for inclusion of get_wch */
41#endif
42
48d7b13a 43#ifdef HAVE_SLCURSES_H
3b411726 44# include <slcurses.h>
48d7b13a 45#elif defined(HAVE_SLANG_SLCURSES_H)
3b411726 46# include <slang/slcurses.h>
30c97bb8 47#elif defined(HAVE_NCURSESW_NCURSES_H) && defined(HAVE_WIDECHAR)
3b411726 48# include <ncursesw/ncurses.h>
48d7b13a 49#elif defined(HAVE_NCURSES_H)
3b411726 50# include <ncurses.h>
48d7b13a 51#elif defined(HAVE_NCURSES_NCURSES_H)
3b411726 52# include <ncurses/ncurses.h>
2b6fc908 53#endif
541e6934 54
8c3a5a44 55#include "c.h"
7efdcd75 56#include "cctype.h"
b2d28533 57#include "closestream.h"
7eda085c 58#include "nls.h"
5c0888dd 59#include "widechar.h"
8abcf290 60#include "strutils.h"
8c3a5a44 61#include "xalloc.h"
5f94ca33 62#include "mbsalign.h"
1b6d02fe 63#include "mbsedit.h"
04915c3f 64#include "colors.h"
57c83d63 65#include "debug.h"
dda7fe12 66#include "list.h"
ec8f7121 67#include "blkdev.h"
8c3a5a44 68
7efb0dc4 69static const char *const default_disks[] = {
a4f0a42d 70#ifdef __GNU__
6e51ab0c
KZ
71 "/dev/hd0",
72 "/dev/sd0",
a4f0a42d 73#elif defined(__FreeBSD__)
6e51ab0c
KZ
74 "/dev/ad0",
75 "/dev/da0",
a4f0a42d 76#else
6e51ab0c
KZ
77 "/dev/sda",
78 "/dev/vda",
79 "/dev/hda",
a4f0a42d 80#endif
6e51ab0c 81};
a4f0a42d 82
c743bf74 83#define ARROW_CURSOR_STRING ">> "
8c3a5a44
KZ
84#define ARROW_CURSOR_DUMMY " "
85#define ARROW_CURSOR_WIDTH (sizeof(ARROW_CURSOR_STRING) - 1)
86
5c04b408
KZ
87/* vertical menu */
88#define MENU_V_SPADDING 1 /* space around menu item string */
89
90/* horizontal menu */
91#define MENU_H_SPADDING 0 /* space around menu item string */
92#define MENU_H_BETWEEN 2 /* space between menu items */
93#define MENU_H_PRESTR "["
94#define MENU_H_POSTSTR "]"
95
96#define MENU_TITLE_PADDING 3
97
98#define MENU_H_PRESTR_SZ (sizeof(MENU_H_PRESTR) - 1)
99#define MENU_H_POSTSTR_SZ (sizeof(MENU_H_POSTSTR) - 1)
100
8c3a5a44 101#define TABLE_START_LINE 4
6e07b904 102#define MENU_START_LINE (ui_lines - 4) /* The menu maybe use two lines */
eef63970 103#define INFO_LINE (ui_lines - 2)
702d0e1c 104#define WARN_LINE INFO_LINE
eef63970 105#define HINT_LINE (ui_lines - 1)
b1f58330
KZ
106
107#define CFDISK_ERR_ESC 5000
108
109#ifndef KEY_ESC
110# define KEY_ESC '\033'
111#endif
112#ifndef KEY_DELETE
113# define KEY_DELETE '\177'
114#endif
1b6d02fe
KZ
115#ifndef KEY_DC
116# define KEY_DC 0423
117#endif
118
1af8003b
KZ
119
120/* colors */
121enum {
122 CFDISK_CL_NONE = 0,
45333e9d
KZ
123 CFDISK_CL_WARNING,
124 CFDISK_CL_FREESPACE,
f9fb8290 125 CFDISK_CL_INFO
1af8003b 126};
061282b2 127#ifdef HAVE_USE_DEFAULT_COLORS
1af8003b
KZ
128static const int color_pairs[][2] = {
129 /* color foreground, background */
45333e9d 130 [CFDISK_CL_WARNING] = { COLOR_RED, -1 },
f9fb8290
KZ
131 [CFDISK_CL_FREESPACE] = { COLOR_GREEN, -1 },
132 [CFDISK_CL_INFO] = { COLOR_BLUE, -1 }
1af8003b 133};
061282b2 134#endif
8c3a5a44 135
8460875d 136struct cfdisk;
8460875d 137
3b411726
KZ
138static struct cfdisk_menuitem *menu_get_menuitem(struct cfdisk *cf, size_t idx);
139static struct cfdisk_menuitem *menu_get_menuitem_by_key(struct cfdisk *cf, int key, size_t *idx);
140static struct cfdisk_menu *menu_push(struct cfdisk *cf, struct cfdisk_menuitem *item);
b1f58330 141static struct cfdisk_menu *menu_pop(struct cfdisk *cf);
7556c944 142static void menu_refresh_size(struct cfdisk *cf);
83fa0f80 143
502c5186 144static int ui_end(void);
1af8003b 145static int ui_refresh(struct cfdisk *cf);
fbae1442
KZ
146
147static void ui_warnx(const char *fmt, ...)
148 __attribute__((__format__ (__printf__, 1, 2)));
149static void ui_warn(const char *fmt, ...)
150 __attribute__((__format__ (__printf__, 1, 2)));
151static void ui_info(const char *fmt, ...)
152 __attribute__((__format__ (__printf__, 1, 2)));
153
b1f58330 154static void ui_draw_menu(struct cfdisk *cf);
ac27ea5c 155static int ui_menu_move(struct cfdisk *cf, int key);
7556c944 156static void ui_menu_resize(struct cfdisk *cf);
ac27ea5c 157
7085f1e4
KZ
158static int ui_get_size(struct cfdisk *cf, const char *prompt, uint64_t *res,
159 uint64_t low, uint64_t up, int *expsize);
8460875d 160
83fa0f80 161static int ui_enabled;
502c5186
KZ
162static volatile sig_atomic_t sig_resize;
163static volatile sig_atomic_t sig_die;
8c3a5a44 164
eef63970
KZ
165/* ncurses LINES and COLS may be actual variables or *macros*, but we need
166 * something portable and writable */
2ba641e5
SK
167static size_t ui_lines;
168static size_t ui_cols;
eef63970 169
88942179 170/* menu item */
3b411726 171struct cfdisk_menuitem {
8c3a5a44
KZ
172 int key; /* keyboard shortcut */
173 const char *name; /* item name */
88942179 174 const char *desc; /* item description (hint) */
6fed9601 175 void *userdata;
749af4b6
KZ
176};
177
88942179 178/* menu */
8c3a5a44 179struct cfdisk_menu {
88942179
KZ
180 char *title; /* optional menu title */
181 struct cfdisk_menuitem *items; /* array with menu items */
182 char *ignore;/* string with keys to ignore */
183 size_t width; /* maximal width of the menu item */
184 size_t nitems; /* number of the active menu items */
185 size_t page_sz;/* when menu longer than screen */
186 size_t idx; /* the current menu item */
b5ef65a9 187 int prefkey;/* preferred menu item */
8c3a5a44 188 struct cfdisk_menu *prev;
88942179
KZ
189
190 /* @ignore keys generator */
63128bbf 191 int (*ignore_cb) (struct cfdisk *, char *, size_t);
8a726114 192
88942179 193 unsigned int vertical : 1; /* enable vertical mode */
749af4b6
KZ
194};
195
88942179 196/* main menu */
3b411726 197static struct cfdisk_menuitem main_menuitems[] = {
8c3a5a44
KZ
198 { 'b', N_("Bootable"), N_("Toggle bootable flag of the current partition") },
199 { 'd', N_("Delete"), N_("Delete the current partition") },
a351f2e5 200 { 'r', N_("Resize"), N_("Reduce or enlarge the current partition") },
8c3a5a44 201 { 'n', N_("New"), N_("Create new partition from free space") },
3d6db3fd 202 { 'q', N_("Quit"), N_("Quit program without writing changes") },
8c3a5a44 203 { 't', N_("Type"), N_("Change the partition type") },
314a0dd8 204 { 'h', N_("Help"), N_("Print help screen") },
d5314bf5 205 { 's', N_("Sort"), N_("Fix partitions order") },
8c3a5a44 206 { 'W', N_("Write"), N_("Write partition table to disk (this might destroy data)") },
a89eafed 207 { 'u', N_("Dump"), N_("Dump partition table to sfdisk compatible script file") },
8c3a5a44
KZ
208 { 0, NULL, NULL }
209};
6dbe3af9 210
dda7fe12
OO
211/* line and extra partinfo list_head */
212struct cfdisk_line {
213 char *data; /* line data */
214 struct libscols_table *extra; /* extra info ('X') */
215 WINDOW *w; /* window with extra info */
216};
217
88942179 218/* top level control struct */
8c3a5a44
KZ
219struct cfdisk {
220 struct fdisk_context *cxt; /* libfdisk context */
221 struct fdisk_table *table; /* partition table */
044ebc0c 222 struct fdisk_table *original_layout; /* original on-disk PT */
dda7fe12 223
a6f69126 224 struct cfdisk_menu *menu; /* the current menu */
7eda085c 225
bd85d11f
KZ
226 int *fields; /* output columns IDs */
227 size_t nfields; /* number of columns IDs */
7eda085c 228
8c3a5a44
KZ
229 char *linesbuf; /* table as string */
230 size_t linesbufsz; /* size of the tb_buf */
7eda085c 231
dda7fe12
OO
232 struct cfdisk_line *lines; /* list of lines */
233
8c3a5a44 234 size_t nlines; /* number of lines */
a6f69126
KZ
235 size_t lines_idx; /* current line <0..N>, exclude header */
236 size_t page_sz;
d5314bf5 237
bc787727
KZ
238 unsigned int nwrites; /* fdisk_write_disklabel() counter */
239
dda7fe12
OO
240 WINDOW *act_win; /* the window currently on the screen */
241
242#ifdef HAVE_LIBMOUNT
243 struct libmnt_table *mtab;
244 struct libmnt_table *fstab;
245 struct libmnt_cache *mntcache;
246#endif
63d79371
ZJS
247 bool wrong_order, /* PT not in right order */
248 zero_start, /* ignore existing partition table */
249 device_is_used, /* don't use re-read ioctl */
250 show_extra; /* show extra partinfo */
8c3a5a44 251};
5c36a0eb 252
14620822
KZ
253
254/*
255 * let's use include/debug.h stuff for cfdisk too
256 */
2ba641e5 257static UL_DEBUG_DEFINE_MASK(cfdisk);
819d9a29 258UL_DEBUG_DEFINE_MASKNAMES(cfdisk) = UL_DEBUG_EMPTY_MASKNAMES;
14620822
KZ
259
260#define CFDISK_DEBUG_INIT (1 << 1)
261#define CFDISK_DEBUG_UI (1 << 2)
262#define CFDISK_DEBUG_MENU (1 << 3)
263#define CFDISK_DEBUG_MISC (1 << 4)
264#define CFDISK_DEBUG_TABLE (1 << 5)
265#define CFDISK_DEBUG_ALL 0xFFFF
266
14620822
KZ
267#define DBG(m, x) __UL_DBG(cfdisk, CFDISK_DEBUG_, m, x)
268
269static void cfdisk_init_debug(void)
270{
a15dca2f 271 __UL_INIT_DEBUG_FROM_ENV(cfdisk, CFDISK_DEBUG_, 0, CFDISK_DEBUG);
14620822
KZ
272}
273
bd85d11f 274/* Initialize output columns -- we follow libfdisk fields (usually specific
88942179
KZ
275 * to the label type.
276 */
8c3a5a44
KZ
277static int cols_init(struct cfdisk *cf)
278{
279 assert(cf);
5c36a0eb 280
bd85d11f
KZ
281 free(cf->fields);
282 cf->fields = NULL;
283 cf->nfields = 0;
5c36a0eb 284
11ee1177 285 return fdisk_label_get_fields_ids(NULL, cf->cxt, &cf->fields, &cf->nfields);
5c36a0eb
KZ
286}
287
502c5186
KZ
288static void die_on_signal(void)
289{
290 DBG(MISC, ul_debug("die on signal."));
291 ui_end();
292 exit(EXIT_FAILURE);
293}
294
7556c944
KZ
295static void resize(void)
296{
297 struct winsize ws;
298
299 if (ioctl(fileno(stdout), TIOCGWINSZ, &ws) != -1
300 && ws.ws_row && ws.ws_col) {
eef63970
KZ
301 ui_lines = ws.ws_row;
302 ui_cols = ws.ws_col;
7556c944
KZ
303#if HAVE_RESIZETERM
304 resizeterm(ws.ws_row, ws.ws_col);
305#endif
306 clearok(stdscr, TRUE);
307 }
308 touchwin(stdscr);
309
eef63970
KZ
310 DBG(UI, ul_debug("ui: resize refresh ui_cols=%zu, ui_lines=%zu",
311 ui_cols, ui_lines));
c431d901 312 sig_resize = 0;
7556c944
KZ
313}
314
c743bf74
KZ
315/* Reads partition in tree-like order from scols
316 */
317static int partition_from_scols(struct fdisk_table *tb,
318 struct libscols_line *ln)
319{
320 struct fdisk_partition *pa = scols_line_get_userdata(ln);
321
322 fdisk_table_add_partition(tb, pa);
323 fdisk_unref_partition(pa);
324
325 if (scols_line_has_children(ln)) {
326 struct libscols_line *chln;
327 struct libscols_iter *itr = scols_new_iter(SCOLS_ITER_FORWARD);
328
329 if (!itr)
330 return -EINVAL;
331 while (scols_line_next_child(ln, itr, &chln) == 0)
332 partition_from_scols(tb, chln);
a9408274 333 scols_free_iter(itr);
c743bf74
KZ
334 }
335 return 0;
336}
337
8c3a5a44
KZ
338static char *table_to_string(struct cfdisk *cf, struct fdisk_table *tb)
339{
8903f7df 340 struct fdisk_partition *pa;
8c3a5a44 341 struct fdisk_label *lb;
1b504263 342 struct fdisk_iter *itr;
baa3b270
OO
343 struct libscols_table *table = NULL;
344 struct libscols_iter *s_itr = NULL;
8c3a5a44 345 char *res = NULL;
8903f7df 346 size_t i;
45333e9d 347 int tree = 0;
baa3b270 348 struct libscols_line *ln, *ln_cont = NULL;
8c3a5a44 349
14620822 350 DBG(TABLE, ul_debug("convert to string"));
8c3a5a44
KZ
351
352 assert(cf);
353 assert(cf->cxt);
bd85d11f 354 assert(cf->fields);
8c3a5a44
KZ
355 assert(tb);
356
6a632136 357 lb = fdisk_get_label(cf->cxt, NULL);
8c3a5a44
KZ
358 assert(lb);
359
8c3a5a44
KZ
360 itr = fdisk_new_iter(FDISK_ITER_FORWARD);
361 if (!itr)
362 goto done;
5c36a0eb 363
45333e9d
KZ
364 /* get container (e.g. extended partition) */
365 while (fdisk_table_next_partition(tb, itr, &pa) == 0) {
366 if (fdisk_partition_is_nested(pa)) {
14620822 367 DBG(TABLE, ul_debug("nested detected, using tree"));
baa3b270 368 tree = SCOLS_FL_TREE;
45333e9d
KZ
369 break;
370 }
371 }
45333e9d 372
0925a9dd 373 table = scols_new_table();
baa3b270 374 if (!table)
45333e9d 375 goto done;
0925a9dd 376 scols_table_enable_maxout(table, 1);
717fff86 377 scols_table_enable_nowrap(table, 1);
45333e9d 378
9907a95a
KZ
379#if !defined(HAVE_LIBNCURSESW) || !defined(HAVE_WIDECHAR)
380 scols_table_enable_ascii(table, 1);
381#endif
382
8c3a5a44 383 /* headers */
bd85d11f
KZ
384 for (i = 0; i < cf->nfields; i++) {
385 int fl = 0;
386 const struct fdisk_field *field =
387 fdisk_label_get_field(lb, cf->fields[i]);
388 if (!field)
389 continue;
390
391 if (fdisk_field_is_number(field))
392 fl |= SCOLS_FL_RIGHT;
393 if (fdisk_field_get_id(field) == FDISK_FIELD_TYPE)
394 fl |= SCOLS_FL_TRUNC;
395 if (tree && fdisk_field_get_id(field) == FDISK_FIELD_DEVICE)
396 fl |= SCOLS_FL_TREE;
397
398 if (!scols_table_new_column(table,
b6e3c614 399 _(fdisk_field_get_name(field)),
bd85d11f
KZ
400 fdisk_field_get_width(field), fl))
401 goto done;
8c3a5a44
KZ
402 }
403
404 /* data */
8903f7df
KZ
405 fdisk_reset_iter(itr, FDISK_ITER_FORWARD);
406
8c3a5a44 407 while (fdisk_table_next_partition(tb, itr, &pa) == 0) {
baa3b270 408 struct libscols_line *parent = fdisk_partition_is_nested(pa) ? ln_cont : NULL;
8903f7df 409
baa3b270 410 ln = scols_table_new_line(table, parent);
8c3a5a44
KZ
411 if (!ln)
412 goto done;
bd85d11f 413 for (i = 0; i < cf->nfields; i++) {
8c3a5a44 414 char *cdata = NULL;
bd85d11f 415
d44115f3 416 if (fdisk_partition_to_string(pa, cf->cxt,
bd85d11f 417 cf->fields[i], &cdata))
8c3a5a44 418 continue;
c743bf74 419 scols_line_refer_data(ln, i, cdata);
8c3a5a44 420 }
8903f7df 421 if (tree && fdisk_partition_is_container(pa))
45333e9d 422 ln_cont = ln;
d051ea93 423
baa3b270 424 scols_line_set_userdata(ln, (void *) pa);
d051ea93 425 fdisk_ref_partition(pa);
8c3a5a44 426 }
6dbe3af9 427
baa3b270 428 if (scols_table_is_empty(table))
d051ea93
KZ
429 goto done;
430
baa3b270
OO
431 scols_table_reduce_termwidth(table, ARROW_CURSOR_WIDTH);
432 scols_print_table_to_string(table, &res);
d051ea93 433
baa3b270 434 /* scols_* code might reorder lines, let's reorder @tb according to the
d051ea93
KZ
435 * final output (it's no problem because partitions are addressed by
436 * parno stored within struct fdisk_partition) */
437
438 /* remove all */
49e1a333 439 fdisk_reset_table(tb);
d051ea93 440
baa3b270
OO
441 s_itr = scols_new_iter(SCOLS_ITER_FORWARD);
442 if (!s_itr)
443 goto done;
444
c743bf74 445 /* add all in the right order (don't forget the output is tree) */
baa3b270 446 while (scols_table_next_line(table, s_itr, &ln) == 0) {
c743bf74
KZ
447 if (scols_line_get_parent(ln))
448 continue;
449 if (partition_from_scols(tb, ln))
450 break;
8c3a5a44
KZ
451 }
452done:
baa3b270
OO
453 scols_unref_table(table);
454 scols_free_iter(s_itr);
8c3a5a44 455 fdisk_free_iter(itr);
6dbe3af9 456
8c3a5a44 457 return res;
2b6fc908
KZ
458}
459
dda7fe12
OO
460static void cfdisk_free_lines(struct cfdisk *cf)
461{
462 size_t i = 0;
463 while(i < cf->nlines) {
464 scols_unref_table(cf->lines[i].extra);
465
466 DBG(UI, ul_debug("delete window: %p",
467 cf->lines[i].w));
468
a2d0dbb4
KZ
469 if (cf->lines[i].w)
470 delwin(cf->lines[i].w);
471 cf->lines[i].w = NULL;
dda7fe12
OO
472 ++i;
473 }
474 cf->act_win = NULL;
475 free(cf->lines);
a2d0dbb4 476 cf->lines = NULL;
dda7fe12 477}
88942179
KZ
478/*
479 * Read data about partitions from libfdisk and prepare output lines.
480 */
8460875d 481static int lines_refresh(struct cfdisk *cf)
8c3a5a44
KZ
482{
483 int rc;
484 char *p;
485 size_t i;
7eda085c 486
8c3a5a44 487 assert(cf);
2b6fc908 488
14620822 489 DBG(TABLE, ul_debug("refreshing buffer"));
c64061c9 490
8c3a5a44 491 free(cf->linesbuf);
dda7fe12 492 cfdisk_free_lines(cf);
8c3a5a44
KZ
493 cf->linesbuf = NULL;
494 cf->linesbufsz = 0;
495 cf->lines = NULL;
496 cf->nlines = 0;
6dbe3af9 497
8c3a5a44 498 fdisk_unref_table(cf->table);
1af8003b 499 cf->table = NULL;
6dbe3af9 500
2cec7949
KZ
501 /* read partitions and free spaces into cf->table */
502 rc = fdisk_get_partitions(cf->cxt, &cf->table);
503 if (!rc)
504 rc = fdisk_get_freespaces(cf->cxt, &cf->table);
8c3a5a44
KZ
505 if (rc)
506 return rc;
0d8589c5 507
8c3a5a44
KZ
508 cf->linesbuf = table_to_string(cf, cf->table);
509 if (!cf->linesbuf)
510 return -ENOMEM;
6dbe3af9 511
8c3a5a44
KZ
512 cf->linesbufsz = strlen(cf->linesbuf);
513 cf->nlines = fdisk_table_get_nents(cf->table) + 1; /* 1 for header line */
a6f69126 514 cf->page_sz = 0;
d5314bf5 515 cf->wrong_order = fdisk_table_wrong_order(cf->table) ? 1 : 0;
a6f69126
KZ
516
517 if (MENU_START_LINE - TABLE_START_LINE < cf->nlines)
518 cf->page_sz = MENU_START_LINE - TABLE_START_LINE - 1;
6dbe3af9 519
dda7fe12 520 cf->lines = xcalloc(cf->nlines, sizeof(struct cfdisk_line));
6dbe3af9 521
8c3a5a44 522 for (p = cf->linesbuf, i = 0; p && i < cf->nlines; i++) {
86688c37
KZ
523 cf->lines[i].data = p;
524 p = strchr(p, '\n');
8c3a5a44
KZ
525 if (p) {
526 *p = '\0';
527 p++;
528 }
dda7fe12
OO
529 cf->lines[i].extra = scols_new_table();
530 scols_table_enable_noheadings(cf->lines[i].extra, 1);
531 scols_table_new_column(cf->lines[i].extra, NULL, 0, SCOLS_FL_RIGHT);
532 scols_table_new_column(cf->lines[i].extra, NULL, 0, SCOLS_FL_TRUNC);
8c3a5a44 533 }
6dbe3af9 534
8c3a5a44 535 return 0;
6dbe3af9
KZ
536}
537
00b4f26a
KZ
538static struct fdisk_partition *get_current_partition(struct cfdisk *cf)
539{
540 assert(cf);
541 assert(cf->table);
542
543 return fdisk_table_get_partition(cf->table, cf->lines_idx);
544}
545
45333e9d
KZ
546static int is_freespace(struct cfdisk *cf, size_t i)
547{
548 struct fdisk_partition *pa;
549
550 assert(cf);
551 assert(cf->table);
552
553 pa = fdisk_table_get_partition(cf->table, i);
554 return fdisk_partition_is_freespace(pa);
555}
556
b1f58330 557/* converts libfdisk FDISK_ASKTYPE_MENU to cfdisk menu and returns user's
9e930041 558 * response back to libfdisk
b1f58330
KZ
559 */
560static int ask_menu(struct fdisk_ask *ask, struct cfdisk *cf)
561{
3b411726 562 struct cfdisk_menuitem *d, *cm;
b1f58330
KZ
563 int key;
564 size_t i = 0, nitems;
565 const char *name, *desc;
566
567 assert(ask);
568 assert(cf);
569
570 /* create cfdisk menu according to libfdisk ask-menu, note that the
571 * last cm[] item has to be empty -- so nitems + 1 */
572 nitems = fdisk_ask_menu_get_nitems(ask);
3b411726 573 cm = xcalloc(nitems + 1, sizeof(struct cfdisk_menuitem));
b1f58330
KZ
574
575 for (i = 0; i < nitems; i++) {
576 if (fdisk_ask_menu_get_item(ask, i, &key, &name, &desc))
577 break;
578 cm[i].key = key;
579 cm[i].desc = desc;
580 cm[i].name = name;
581 }
582
583 /* make the new menu active */
63128bbf 584 menu_push(cf, cm);
b1f58330
KZ
585 ui_draw_menu(cf);
586 refresh();
587
588 /* wait for keys */
502c5186 589 while (!sig_die) {
7ee26cbf 590 key = getch();
ac27ea5c 591
502c5186
KZ
592 if (sig_die)
593 break;
c431d901 594 if (sig_resize)
7556c944 595 ui_menu_resize(cf);
ac27ea5c
KZ
596 if (ui_menu_move(cf, key) == 0)
597 continue;
598
599 switch (key) {
b1f58330
KZ
600 case KEY_ENTER:
601 case '\n':
602 case '\r':
6fed9601 603 d = menu_get_menuitem(cf, cf->menu->idx);
b1f58330
KZ
604 if (d)
605 fdisk_ask_menu_set_result(ask, d->key);
606 menu_pop(cf);
607 free(cm);
608 return 0;
609 }
502c5186
KZ
610 }
611
612 if (sig_die)
613 die_on_signal();
b1f58330
KZ
614
615 menu_pop(cf);
616 free(cm);
617 return -1;
618}
619
88942179
KZ
620/* libfdisk callback
621 */
ffab025d
KZ
622static int ask_callback(struct fdisk_context *cxt __attribute__((__unused__)),
623 struct fdisk_ask *ask,
624 void *data __attribute__((__unused__)))
8c3a5a44
KZ
625{
626 int rc = 0;
6dbe3af9 627
8c3a5a44 628 assert(ask);
6dbe3af9 629
8c3a5a44
KZ
630 switch(fdisk_ask_get_type(ask)) {
631 case FDISK_ASKTYPE_INFO:
fbae1442 632 ui_info("%s", fdisk_ask_print_get_mesg(ask));
8c3a5a44
KZ
633 break;
634 case FDISK_ASKTYPE_WARNX:
fbae1442 635 ui_warnx("%s", fdisk_ask_print_get_mesg(ask));
8c3a5a44
KZ
636 break;
637 case FDISK_ASKTYPE_WARN:
fbae1442 638 ui_warn("%s", fdisk_ask_print_get_mesg(ask));
8c3a5a44 639 break;
b1f58330
KZ
640 case FDISK_ASKTYPE_MENU:
641 ask_menu(ask, (struct cfdisk *) data);
642 break;
8c3a5a44 643 default:
83fa0f80
KZ
644 ui_warnx(_("internal error: unsupported dialog type %d"),
645 fdisk_ask_get_type(ask));
8c3a5a44
KZ
646 return -EINVAL;
647 }
648 return rc;
fd6b7a7f 649}
6dbe3af9 650
7aa0d529 651static int ui_end(void)
8c3a5a44 652{
7aa0d529 653 if (!ui_enabled)
8c3a5a44
KZ
654 return -EINVAL;
655
48d7b13a 656#if defined(HAVE_SLCURSES_H) || defined(HAVE_SLANG_SLCURSES_H)
eef63970 657 SLsmg_gotorc(ui_lines - 1, 0);
8c3a5a44 658 SLsmg_refresh();
2b6fc908 659#else
eef63970 660 mvcur(0, ui_cols - 1, ui_lines-1, 0);
2b6fc908 661#endif
cd8414f7 662 curs_set(1);
8c3a5a44
KZ
663 nl();
664 endwin();
665 printf("\n");
7aa0d529 666 ui_enabled = 0;
8c3a5a44 667 return 0;
6dbe3af9
KZ
668}
669
fbae1442
KZ
670static void __attribute__((__format__ (__printf__, 3, 0)))
671 ui_vprint_center(size_t line, int attrs, const char *fmt, va_list ap)
f609e92e 672{
8c3a5a44 673 size_t width;
8c3a5a44 674 char *buf = NULL;
f609e92e 675
8c3a5a44
KZ
676 move(line, 0);
677 clrtoeol();
f609e92e 678
8c3a5a44 679 xvasprintf(&buf, fmt, ap);
fd6b7a7f 680
b1f58330 681 width = mbs_safe_width(buf);
eef63970
KZ
682 if (width > (size_t) ui_cols) {
683 char *p = strrchr(buf + ui_cols, ' ');
91ba41ca 684 if (!p)
eef63970 685 p = buf + ui_cols;
91ba41ca 686 *p = '\0';
eef63970 687 if (line + 1 >= ui_lines)
91ba41ca
KZ
688 line--;
689 attron(attrs);
690 mvaddstr(line, 0, buf);
691 mvaddstr(line + 1, 0, p+1);
692 attroff(attrs);
693 } else {
694 attron(attrs);
eef63970 695 mvaddstr(line, (ui_cols - width) / 2, buf);
91ba41ca
KZ
696 attroff(attrs);
697 }
8c3a5a44 698 free(buf);
6dbe3af9
KZ
699}
700
fbae1442
KZ
701static void __attribute__((__format__ (__printf__, 2, 3)))
702 ui_center(size_t line, const char *fmt, ...)
1af8003b
KZ
703{
704 va_list ap;
705 va_start(ap, fmt);
706 ui_vprint_center(line, 0, fmt, ap);
707 va_end(ap);
708}
709
fbae1442
KZ
710static void __attribute__((__format__ (__printf__, 1, 2)))
711 ui_warnx(const char *fmt, ...)
83fa0f80
KZ
712{
713 va_list ap;
714 va_start(ap, fmt);
715 if (ui_enabled)
702d0e1c 716 ui_vprint_center(WARN_LINE,
f1512be8
KZ
717 colors_wanted() ? COLOR_PAIR(CFDISK_CL_WARNING) : 0,
718 fmt, ap);
8fc37981 719 else {
83fa0f80 720 vfprintf(stderr, fmt, ap);
8fc37981
KZ
721 fputc('\n', stderr);
722 }
83fa0f80
KZ
723 va_end(ap);
724}
725
fbae1442
KZ
726static void __attribute__((__format__ (__printf__, 1, 2)))
727 ui_warn(const char *fmt, ...)
1af8003b 728{
83fa0f80 729 char *fmt_m;
1af8003b 730 va_list ap;
83fa0f80
KZ
731
732 xasprintf(&fmt_m, "%s: %m", fmt);
733
1af8003b 734 va_start(ap, fmt);
83fa0f80 735 if (ui_enabled)
702d0e1c 736 ui_vprint_center(WARN_LINE,
f1512be8
KZ
737 colors_wanted() ? COLOR_PAIR(CFDISK_CL_WARNING) : 0,
738 fmt_m, ap);
8fc37981 739 else {
83fa0f80 740 vfprintf(stderr, fmt_m, ap);
8fc37981
KZ
741 fputc('\n', stderr);
742 }
1af8003b 743 va_end(ap);
83fa0f80 744 free(fmt_m);
1af8003b
KZ
745}
746
29e9b787
KZ
747static void ui_clean_warn(void)
748{
749 move(WARN_LINE, 0);
750 clrtoeol();
751}
752
fbae1442
KZ
753static int __attribute__((__noreturn__))
754 __attribute__((__format__ (__printf__, 2, 3)))
755 ui_err(int rc, const char *fmt, ...)
756{
134b6296
KZ
757 va_list ap;
758 ui_end();
759
760 va_start(ap, fmt);
761 fprintf(stderr, "%s: ", program_invocation_short_name);
762 vfprintf(stderr, fmt, ap);
763 fprintf(stderr, ": %s\n", strerror(errno));
764 va_end(ap);
765
766 exit(rc);
767}
768
fbae1442
KZ
769static int __attribute__((__noreturn__))
770 __attribute__((__format__ (__printf__, 2, 3)))
771 ui_errx(int rc, const char *fmt, ...)
7aa0d529
KZ
772 {
773 va_list ap;
774 ui_end();
775
776 va_start(ap, fmt);
777 fprintf(stderr, "%s: ", program_invocation_short_name);
778 vfprintf(stderr, fmt, ap);
8fc37981 779 fputc('\n', stderr);
7aa0d529
KZ
780 va_end(ap);
781
782 exit(rc);
783}
784
fbae1442
KZ
785static void __attribute__((__format__ (__printf__, 1, 2)))
786 ui_info(const char *fmt, ...)
1af8003b
KZ
787{
788 va_list ap;
789 va_start(ap, fmt);
83fa0f80 790 if (ui_enabled)
f9fb8290
KZ
791 ui_vprint_center(INFO_LINE,
792 colors_wanted() ? COLOR_PAIR(CFDISK_CL_INFO) : 0,
793 fmt, ap);
8fc37981 794 else {
83fa0f80 795 vfprintf(stdout, fmt, ap);
8fc37981
KZ
796 fputc('\n', stdout);
797 }
1af8003b
KZ
798 va_end(ap);
799}
800
801static void ui_clean_info(void)
802{
803 move(INFO_LINE, 0);
804 clrtoeol();
805}
6dbe3af9 806
fbae1442
KZ
807static void __attribute__((__format__ (__printf__, 1, 2)))
808 ui_hint(const char *fmt, ...)
b1f58330
KZ
809{
810 va_list ap;
811 va_start(ap, fmt);
812 if (ui_enabled)
813 ui_vprint_center(HINT_LINE, A_BOLD, fmt, ap);
8fc37981 814 else {
b1f58330 815 vfprintf(stdout, fmt, ap);
8fc37981
KZ
816 fputc('\n', stdout);
817 }
b1f58330
KZ
818 va_end(ap);
819}
820
821static void ui_clean_hint(void)
822{
823 move(HINT_LINE, 0);
824 clrtoeol();
825}
826
502c5186
KZ
827
828static void sig_handler_die(int dummy __attribute__((__unused__)))
8c3a5a44 829{
502c5186 830 sig_die = 1;
6dbe3af9
KZ
831}
832
c431d901 833static void sig_handler_resize(int dummy __attribute__((__unused__)))
7556c944 834{
c431d901 835 sig_resize = 1;
7556c944
KZ
836}
837
838static void menu_refresh_size(struct cfdisk *cf)
839{
840 if (cf->menu && cf->menu->nitems)
eef63970 841 cf->menu->page_sz = (cf->menu->nitems / (ui_lines - 4)) ? ui_lines - 4 : 0;
7556c944
KZ
842}
843
8c3a5a44
KZ
844static void menu_update_ignore(struct cfdisk *cf)
845{
00b4f26a
KZ
846 char ignore[128] = { 0 };
847 int i = 0;
8c3a5a44 848 struct cfdisk_menu *m;
b5ef65a9 849 struct cfdisk_menuitem *d, *org = NULL;
83fa0f80 850 size_t idx;
6dbe3af9 851
8c3a5a44 852 assert(cf);
63128bbf
KZ
853 assert(cf->menu);
854 assert(cf->menu->ignore_cb);
6dbe3af9 855
8c3a5a44 856 m = cf->menu;
14620822 857 DBG(MENU, ul_debug("update menu ignored keys"));
6dbe3af9 858
63128bbf 859 i = m->ignore_cb(cf, ignore, sizeof(ignore));
00b4f26a
KZ
860 ignore[i] = '\0';
861
8c3a5a44 862 /* return if no change */
b5ef65a9 863 if ((!m->ignore && !*ignore)
00b4f26a 864 || (m->ignore && *ignore && strcmp(m->ignore, ignore) == 0)) {
8c3a5a44 865 return;
fd6b7a7f 866 }
6dbe3af9 867
b5ef65a9
KZ
868 if (!m->prefkey)
869 org = menu_get_menuitem(cf, m->idx);
870
8c3a5a44 871 free(m->ignore);
8460875d 872 m->ignore = xstrdup(ignore);
8c3a5a44 873 m->nitems = 0;
6dbe3af9 874
3b411726 875 for (d = m->items; d->name; d++) {
8c3a5a44 876 if (m->ignore && strchr(m->ignore, d->key))
8460875d
KZ
877 continue;
878 m->nitems++;
8c3a5a44 879 }
83fa0f80 880
b5ef65a9
KZ
881 DBG(MENU, ul_debug("update menu preferred keys"));
882
83fa0f80
KZ
883 /* refresh menu index to be at the same menuitem or go to the first */
884 if (org && menu_get_menuitem_by_key(cf, org->key, &idx))
6fed9601 885 m->idx = idx;
b5ef65a9
KZ
886 else if (m->prefkey && menu_get_menuitem_by_key(cf, m->prefkey, &idx))
887 m->idx = idx;
83fa0f80 888 else
6fed9601
KZ
889 m->idx = 0;
890
7556c944 891 menu_refresh_size(cf);
6dbe3af9
KZ
892}
893
b1f58330
KZ
894static struct cfdisk_menu *menu_push(
895 struct cfdisk *cf,
3b411726 896 struct cfdisk_menuitem *items)
8c3a5a44
KZ
897{
898 struct cfdisk_menu *m = xcalloc(1, sizeof(*m));
3b411726 899 struct cfdisk_menuitem *d;
6dbe3af9 900
8c3a5a44 901 assert(cf);
6dbe3af9 902
14620822 903 DBG(MENU, ul_debug("new menu"));
6dbe3af9 904
8c3a5a44 905 m->prev = cf->menu;
3b411726 906 m->items = items;
6dbe3af9 907
3b411726 908 for (d = m->items; d->name; d++) {
8c3a5a44 909 const char *name = _(d->name);
b1f58330 910 size_t len = mbs_safe_width(name);
8c3a5a44
KZ
911 if (len > m->width)
912 m->width = len;
913 m->nitems++;
914 }
6dbe3af9 915
8c3a5a44 916 cf->menu = m;
b5ef65a9 917
7556c944 918 menu_refresh_size(cf);
8c3a5a44 919 return m;
6dbe3af9
KZ
920}
921
8c3a5a44 922static struct cfdisk_menu *menu_pop(struct cfdisk *cf)
6dbe3af9 923{
8c3a5a44 924 struct cfdisk_menu *m = NULL;
6dbe3af9 925
8c3a5a44 926 assert(cf);
7eda085c 927
14620822 928 DBG(MENU, ul_debug("pop menu"));
7eda085c 929
8c3a5a44
KZ
930 if (cf->menu) {
931 m = cf->menu->prev;
932 free(cf->menu->ignore);
8a726114 933 free(cf->menu->title);
8c3a5a44 934 free(cf->menu);
c07ebfa1 935 }
8c3a5a44
KZ
936 cf->menu = m;
937 return cf->menu;
6dbe3af9
KZ
938}
939
8a726114
KZ
940static void menu_set_title(struct cfdisk_menu *m, const char *title)
941{
942 char *str = NULL;
943
944 if (title) {
945 size_t len = mbs_safe_width(title);
5c04b408
KZ
946 if (len + MENU_TITLE_PADDING > m->width)
947 m->width = len + MENU_TITLE_PADDING;
8a726114
KZ
948 str = xstrdup(title);
949 }
33ca468b 950 free(m->title);
8a726114
KZ
951 m->title = str;
952}
953
954
b1f58330 955static int ui_init(struct cfdisk *cf __attribute__((__unused__)))
8c3a5a44
KZ
956{
957 struct sigaction sa;
6dbe3af9 958
14620822 959 DBG(UI, ul_debug("init"));
6dbe3af9 960
8c3a5a44
KZ
961 /* setup SIGCHLD handler */
962 sigemptyset(&sa.sa_mask);
963 sa.sa_flags = 0;
502c5186 964 sa.sa_handler = sig_handler_die;
8c3a5a44
KZ
965 sigaction(SIGINT, &sa, NULL);
966 sigaction(SIGTERM, &sa, NULL);
6dbe3af9 967
c431d901 968 sa.sa_handler = sig_handler_resize;
7556c944
KZ
969 sigaction(SIGWINCH, &sa, NULL);
970
83fa0f80 971 ui_enabled = 1;
8c3a5a44 972 initscr();
6dbe3af9 973
f1512be8 974#ifdef HAVE_USE_DEFAULT_COLORS
04915c3f 975 if (colors_wanted() && has_colors()) {
1af8003b
KZ
976 size_t i;
977
978 start_color();
979 use_default_colors();
1af8003b
KZ
980 for (i = 1; i < ARRAY_SIZE(color_pairs); i++) /* yeah, start from 1! */
981 init_pair(i, color_pairs[i][0], color_pairs[i][1]);
982 }
f1512be8 983#else
e66a6627 984 colors_off();
f1512be8 985#endif
1af8003b 986
8c3a5a44
KZ
987 cbreak();
988 noecho();
989 nonl();
990 curs_set(0);
991 keypad(stdscr, TRUE);
6dbe3af9 992
8c3a5a44 993 return 0;
7eda085c
KZ
994}
995
5c04b408
KZ
996/* "[ string ]" */
997#define MENU_H_ITEMWIDTH(m) ( MENU_H_PRESTR_SZ \
998 + MENU_H_SPADDING \
999 + (m)->width \
1000 + MENU_H_SPADDING \
1001 + MENU_H_POSTSTR_SZ)
1002
1003#define MENU_V_ITEMWIDTH(m) (MENU_V_SPADDING + (m)->width + MENU_V_SPADDING)
1004
1005
8c3a5a44
KZ
1006static size_t menuitem_get_line(struct cfdisk *cf, size_t idx)
1007{
6fed9601
KZ
1008 struct cfdisk_menu *m = cf->menu;
1009
1010 if (m->vertical) {
1011 if (!m->page_sz) /* small menu */
eef63970 1012 return (ui_lines - (cf->menu->nitems + 1)) / 2 + idx;
6fed9601 1013 return (idx % m->page_sz) + 1;
042f62df
RP
1014 }
1015
1016 {
5c04b408 1017 size_t len = MENU_H_ITEMWIDTH(m) + MENU_H_BETWEEN; /** item width */
eef63970 1018 size_t items = ui_cols / len; /* items per line */
8a726114 1019
a1da27a8
KZ
1020 if (items == 0)
1021 return 0;
8a726114
KZ
1022 return MENU_START_LINE + ((idx / items));
1023 }
7eda085c
KZ
1024}
1025
8c3a5a44
KZ
1026static int menuitem_get_column(struct cfdisk *cf, size_t idx)
1027{
8a726114 1028 if (cf->menu->vertical) {
5c04b408 1029 size_t nc = MENU_V_ITEMWIDTH(cf->menu);
eef63970 1030 if ((size_t) ui_cols <= nc)
7aa0d529 1031 return 0;
eef63970 1032 return (ui_cols - nc) / 2;
042f62df
RP
1033 }
1034
1035 {
5c04b408 1036 size_t len = MENU_H_ITEMWIDTH(cf->menu) + MENU_H_BETWEEN; /* item width */
eef63970 1037 size_t items = ui_cols / len; /* items per line */
8a726114 1038 size_t extra = items < cf->menu->nitems ? /* extra space on line */
eef63970
KZ
1039 ui_cols % len : /* - multi-line menu */
1040 ui_cols - (cf->menu->nitems * len); /* - one line menu */
8a726114 1041
a1da27a8
KZ
1042 if (items == 0)
1043 return 0; /* hmm... no space */
1044
5c04b408 1045 extra += MENU_H_BETWEEN; /* add padding after last item to extra */
e66ac5d3 1046
8a726114
KZ
1047 if (idx < items)
1048 return (idx * len) + (extra / 2);
1049 return ((idx % items) * len) + (extra / 2);
1050 }
df1dddf9
KZ
1051}
1052
6fed9601
KZ
1053static int menuitem_on_page(struct cfdisk *cf, size_t idx)
1054{
1055 struct cfdisk_menu *m = cf->menu;
1056
1057 if (m->page_sz == 0 ||
1058 m->idx / m->page_sz == idx / m->page_sz)
1059 return 1;
1060 return 0;
1061}
1062
3b411726 1063static struct cfdisk_menuitem *menu_get_menuitem(struct cfdisk *cf, size_t idx)
8c3a5a44 1064{
3b411726 1065 struct cfdisk_menuitem *d;
8c3a5a44 1066 size_t i;
7eda085c 1067
3b411726 1068 for (i = 0, d = cf->menu->items; d->name; d++) {
8c3a5a44
KZ
1069 if (cf->menu->ignore && strchr(cf->menu->ignore, d->key))
1070 continue;
1071 if (i++ == idx)
1072 return d;
d26aa358 1073 }
7eda085c 1074
8c3a5a44 1075 return NULL;
6dbe3af9
KZ
1076}
1077
3b411726 1078static struct cfdisk_menuitem *menu_get_menuitem_by_key(struct cfdisk *cf,
83fa0f80
KZ
1079 int key, size_t *idx)
1080{
3b411726 1081 struct cfdisk_menuitem *d;
83fa0f80 1082
3b411726 1083 for (*idx = 0, d = cf->menu->items; d->name; d++) {
83fa0f80
KZ
1084 if (cf->menu->ignore && strchr(cf->menu->ignore, d->key))
1085 continue;
1086 if (key == d->key)
1087 return d;
1088 (*idx)++;
1089 }
1090
1091 return NULL;
1092}
1093
8c3a5a44 1094static void ui_draw_menuitem(struct cfdisk *cf,
3b411726 1095 struct cfdisk_menuitem *d,
8c3a5a44
KZ
1096 size_t idx)
1097{
e948f4b6 1098 char *buf, *ptr;
8c3a5a44 1099 const char *name;
5c04b408 1100 size_t width;
e948f4b6 1101 const size_t buf_sz = 80 * MB_CUR_MAX;
8a726114 1102 int ln, cl, vert = cf->menu->vertical;
8c3a5a44 1103
6fed9601
KZ
1104 if (!menuitem_on_page(cf, idx))
1105 return; /* no visible item */
1106 ln = menuitem_get_line(cf, idx);
1107 cl = menuitem_get_column(cf, idx);
1108
e948f4b6 1109 ptr = buf = xmalloc(buf_sz);
5c04b408
KZ
1110 /* string width */
1111 if (vert) {
1112 width = cf->menu->width + MENU_V_SPADDING;
1113 memset(ptr, ' ', MENU_V_SPADDING);
1114 ptr += MENU_V_SPADDING;
1115 } else
1116 width = MENU_H_SPADDING + cf->menu->width + MENU_H_SPADDING;
1117
8c3a5a44 1118 name = _(d->name);
e948f4b6 1119 mbsalign(name, ptr, buf_sz, &width,
8a726114
KZ
1120 vert ? MBS_ALIGN_LEFT : MBS_ALIGN_CENTER,
1121 0);
8c3a5a44 1122
14620822 1123 DBG(MENU, ul_debug("menuitem: cl=%d, ln=%d, item='%s'",
8c3a5a44
KZ
1124 cl, ln, buf));
1125
8a726114
KZ
1126 if (vert) {
1127 mvaddch(ln, cl - 1, ACS_VLINE);
5c04b408 1128 mvaddch(ln, cl + MENU_V_ITEMWIDTH(cf->menu), ACS_VLINE);
8a726114
KZ
1129 }
1130
5c04b408 1131 if (cf->menu->idx == idx)
8c3a5a44 1132 standout();
5c04b408
KZ
1133
1134 if (vert)
1135 mvprintw(ln, cl, "%s", buf);
1136 else
1137 mvprintw(ln, cl, "%s%s%s", MENU_H_PRESTR, buf, MENU_H_POSTSTR);
e948f4b6 1138 free(buf);
5c04b408
KZ
1139
1140 if (cf->menu->idx == idx) {
8c3a5a44
KZ
1141 standend();
1142 if (d->desc)
fbae1442 1143 ui_hint("%s", _(d->desc));
5c04b408 1144 }
6dbe3af9
KZ
1145}
1146
7556c944
KZ
1147static void ui_clean_menu(struct cfdisk *cf)
1148{
1149 size_t i;
702d0e1c 1150 size_t lastline;
7556c944
KZ
1151 struct cfdisk_menu *m = cf->menu;
1152 size_t ln = menuitem_get_line(cf, 0);
1153
1154 if (m->vertical)
702d0e1c 1155 lastline = ln + (m->page_sz ? m->page_sz : m->nitems);
7556c944 1156 else
702d0e1c 1157 lastline = menuitem_get_line(cf, m->nitems);
7556c944 1158
702d0e1c 1159 for (i = ln; i <= lastline; i++) {
7556c944
KZ
1160 move(i, 0);
1161 clrtoeol();
702d0e1c 1162 DBG(MENU, ul_debug("clean_menu: line %zu", i));
7556c944
KZ
1163 }
1164 if (m->vertical) {
1165 move(ln - 1, 0);
1166 clrtoeol();
1167 }
1168 ui_clean_hint();
1169}
1170
8c3a5a44
KZ
1171static void ui_draw_menu(struct cfdisk *cf)
1172{
3b411726 1173 struct cfdisk_menuitem *d;
8a726114 1174 struct cfdisk_menu *m;
6fed9601
KZ
1175 size_t i = 0;
1176 size_t ln = menuitem_get_line(cf, 0);
1177 size_t nlines;
7eda085c 1178
8c3a5a44
KZ
1179 assert(cf);
1180 assert(cf->menu);
fd6b7a7f 1181
14620822 1182 DBG(MENU, ul_debug("draw start"));
6dbe3af9 1183
7556c944 1184 ui_clean_menu(cf);
8a726114
KZ
1185 m = cf->menu;
1186
6fed9601
KZ
1187 if (m->vertical)
1188 nlines = m->page_sz ? m->page_sz : m->nitems;
1189 else
1190 nlines = menuitem_get_line(cf, m->nitems);
1191
63128bbf
KZ
1192 if (m->ignore_cb)
1193 menu_update_ignore(cf);
00b4f26a 1194 i = 0;
8c3a5a44
KZ
1195 while ((d = menu_get_menuitem(cf, i)))
1196 ui_draw_menuitem(cf, d, i++);
6dbe3af9 1197
8a726114 1198 if (m->vertical) {
8a726114 1199 size_t cl = menuitem_get_column(cf, 0);
6fed9601 1200 size_t curpg = m->page_sz ? m->idx / m->page_sz : 0;
8a726114 1201
6fed9601 1202 /* corners and horizontal lines */
8a726114
KZ
1203 mvaddch(ln - 1, cl - 1, ACS_ULCORNER);
1204 mvaddch(ln + nlines, cl - 1, ACS_LLCORNER);
1205
5c04b408 1206 for (i = 0; i < MENU_V_ITEMWIDTH(m); i++) {
8a726114
KZ
1207 mvaddch(ln - 1, cl + i, ACS_HLINE);
1208 mvaddch(ln + nlines, cl + i, ACS_HLINE);
1209 }
6fed9601 1210
8a726114
KZ
1211 mvaddch(ln - 1, cl + i, ACS_URCORNER);
1212 mvaddch(ln + nlines, cl + i, ACS_LRCORNER);
1213
6fed9601
KZ
1214 /* draw also lines around empty lines on last page */
1215 if (m->page_sz &&
1216 m->nitems / m->page_sz == m->idx / m->page_sz) {
1217 for (i = m->nitems % m->page_sz + 1; i <= m->page_sz; i++) {
1218 mvaddch(i, cl - 1, ACS_VLINE);
5c04b408 1219 mvaddch(i, cl + MENU_V_ITEMWIDTH(m), ACS_VLINE);
6fed9601
KZ
1220 }
1221 }
8a726114
KZ
1222 if (m->title) {
1223 attron(A_BOLD);
1224 mvprintw(ln - 1, cl, " %s ", m->title);
1225 attroff(A_BOLD);
1226 }
6fed9601 1227 if (curpg != 0)
5c04b408 1228 mvaddch(ln - 1, cl + MENU_V_ITEMWIDTH(m) - 2, ACS_UARROW);
6fed9601 1229 if (m->page_sz && curpg < m->nitems / m->page_sz)
5c04b408 1230 mvaddch(ln + nlines, cl + MENU_V_ITEMWIDTH(m) - 2, ACS_DARROW);
8a726114
KZ
1231 }
1232
14620822 1233 DBG(MENU, ul_debug("draw end."));
6dbe3af9
KZ
1234}
1235
dda7fe12
OO
1236inline static int extra_insert_pair(struct cfdisk_line *l, const char *name, const char *data)
1237{
1238 struct libscols_line *lsl;
4c166c22 1239 int rc;
dda7fe12
OO
1240
1241 assert(l);
a2d0dbb4 1242 assert(l->extra);
dda7fe12 1243
13c551f2 1244 if (!data || !*data)
dda7fe12
OO
1245 return 0;
1246
1247 lsl = scols_table_new_line(l->extra, NULL);
1248 if (!lsl)
1249 return -ENOMEM;
1250
4c166c22
KZ
1251 rc = scols_line_set_data(lsl, 0, name);
1252 if (!rc)
1253 rc = scols_line_set_data(lsl, 1, data);
dda7fe12 1254
4c166c22 1255 return rc;
dda7fe12
OO
1256}
1257
cf9b302d
KZ
1258#ifndef HAVE_LIBMOUNT
1259static char *get_mountpoint( struct cfdisk *cf __attribute__((unused)),
1260 const char *tagname __attribute__((unused)),
1261 const char *tagdata __attribute__((unused)))
1262{
1263 return NULL;
1264}
1265#else
1266static char *get_mountpoint(struct cfdisk *cf, const char *tagname, const char *tagdata)
dda7fe12
OO
1267{
1268 struct libmnt_fs *fs = NULL;
1269 char *target = NULL;
1270 int mounted = 0;
1271
cf9b302d
KZ
1272 assert(tagname);
1273 assert(tagdata);
dda7fe12 1274
cf9b302d 1275 DBG(UI, ul_debug("asking for mountpoint [%s=%s]", tagname, tagdata));
dda7fe12
OO
1276
1277 if (!cf->mntcache)
1278 cf->mntcache = mnt_new_cache();
1279
1280 /* 1st try between mounted filesystems */
1281 if (!cf->mtab) {
1282 cf->mtab = mnt_new_table();
1283 if (cf->mtab) {
1284 mnt_table_set_cache(cf->mtab, cf->mntcache);
1285 mnt_table_parse_mtab(cf->mtab, NULL);
1286 }
1287 }
1288
1289 if (cf->mtab)
cf9b302d 1290 fs = mnt_table_find_tag(cf->mtab, tagname, tagdata, MNT_ITER_FORWARD);
dda7fe12
OO
1291
1292 /* 2nd try fstab */
1293 if (!fs) {
1294 if (!cf->fstab) {
1295 cf->fstab = mnt_new_table();
1296 if (cf->fstab) {
1297 mnt_table_set_cache(cf->fstab, cf->mntcache);
a8955bf0
KZ
1298 if (mnt_table_parse_fstab(cf->fstab, NULL) != 0) {
1299 mnt_unref_table(cf->fstab);
1300 cf->fstab = NULL;
1301 }
dda7fe12
OO
1302 }
1303 }
1304 if (cf->fstab)
cf9b302d 1305 fs = mnt_table_find_tag(cf->fstab, tagname, tagdata, MNT_ITER_FORWARD);
dda7fe12
OO
1306 } else
1307 mounted = 1;
1308
1309 if (fs) {
1310 if (mounted)
1311 xasprintf(&target, _("%s (mounted)"), mnt_fs_get_target(fs));
1312 else
1313 target = xstrdup(mnt_fs_get_target(fs));
1314 }
1315
1316 return target;
1317}
1318#endif /* HAVE_LIBMOUNT */
1319
94a5b2e8
KZ
1320static inline int iszero(const char *str)
1321{
1322 const char *p;
1323
1324 for (p = str; p && *p == '0'; p++);
1325
1326 return !p || *p == '\0';
1327}
1328
5efc31f9
KZ
1329static int has_uuid(struct fdisk_table *tb, const char *uuid)
1330{
1331 struct fdisk_partition *pa;
1332 struct fdisk_iter *itr;
1333 int rc = 0;
1334
1335 if (!tb || !uuid || fdisk_table_is_empty(tb))
1336 return 0;
1337
1338 itr = fdisk_new_iter(FDISK_ITER_FORWARD);
1339 while (rc == 0 && fdisk_table_next_partition(tb, itr, &pa) == 0) {
1340 const char *x = fdisk_partition_get_uuid(pa);
1341 if (x)
1342 rc = strcmp(x, uuid) == 0;
1343 }
1344 fdisk_free_iter(itr);
1345 return rc;
1346}
1347
dda7fe12
OO
1348static void extra_prepare_data(struct cfdisk *cf)
1349{
1350 struct fdisk_partition *pa = get_current_partition(cf);
1351 struct cfdisk_line *l = &cf->lines[cf->lines_idx];
1352 char *data = NULL;
cf9b302d 1353 char *mountpoint = NULL;
dda7fe12
OO
1354
1355 DBG(UI, ul_debug("preparing extra data"));
1356
1357 /* string data should not equal an empty string */
1358 if (!fdisk_partition_to_string(pa, cf->cxt, FDISK_FIELD_NAME, &data) && data) {
1359 extra_insert_pair(l, _("Partition name:"), data);
cf9b302d
KZ
1360 if (!mountpoint)
1361 mountpoint = get_mountpoint(cf, "PARTLABEL", data);
dda7fe12
OO
1362 free(data);
1363 }
1364
1365 if (!fdisk_partition_to_string(pa, cf->cxt, FDISK_FIELD_UUID, &data) && data) {
1366 extra_insert_pair(l, _("Partition UUID:"), data);
5efc31f9
KZ
1367
1368 /* Search for mountpoint by PARTUUID= means that we need to
1369 * check fstab and convert PARTUUID to the device name. This is
1370 * unnecessary and overkill for newly created partitions. Let's
1371 * check if the UUID already exist in the old layout, otherwise
1372 * ignore it.
1373 */
1374 if (!mountpoint && has_uuid(cf->original_layout, data))
cf9b302d 1375 mountpoint = get_mountpoint(cf, "PARTUUID", data);
dda7fe12
OO
1376 free(data);
1377 }
1378
1379 if (!fdisk_partition_to_string(pa, cf->cxt, FDISK_FIELD_TYPE, &data) && data) {
1380 char *code = NULL, *type = NULL;
1381
1382 fdisk_partition_to_string(pa, cf->cxt, FDISK_FIELD_TYPEID, &code);
1383 xasprintf(&type, "%s (%s)", data, code);
1384
1385 extra_insert_pair(l, _("Partition type:"), type);
1386 free(data);
1387 free(code);
1388 free(type);
1389 }
1390
1391 if (!fdisk_partition_to_string(pa, cf->cxt, FDISK_FIELD_ATTR, &data) && data) {
1392 extra_insert_pair(l, _("Attributes:"), data);
1393 free(data);
1394 }
1395
1396 /* for numeric data, only show non-zero rows */
1397 if (!fdisk_partition_to_string(pa, cf->cxt, FDISK_FIELD_BSIZE, &data) && data) {
94a5b2e8 1398 if (!iszero(data))
dda7fe12
OO
1399 extra_insert_pair(l, "BSIZE:", data);
1400 free(data);
1401 }
1402
1403 if (!fdisk_partition_to_string(pa, cf->cxt, FDISK_FIELD_CPG, &data) && data) {
94a5b2e8 1404 if (!iszero(data))
dda7fe12
OO
1405 extra_insert_pair(l, "CPG:", data);
1406 free(data);
1407 }
1408
1409 if (!fdisk_partition_to_string(pa, cf->cxt, FDISK_FIELD_FSIZE, &data) && data) {
94a5b2e8 1410 if (!iszero(data))
dda7fe12
OO
1411 extra_insert_pair(l, "FSIZE:", data);
1412 free(data);
1413 }
1414
3d6db3fd
KZ
1415 if (!fdisk_partition_to_string(pa, cf->cxt, FDISK_FIELD_FSUUID, &data) && data) {
1416 extra_insert_pair(l, _("Filesystem UUID:"), data);
cf9b302d
KZ
1417 if (!mountpoint)
1418 mountpoint = get_mountpoint(cf, "UUID", data);
3d6db3fd
KZ
1419 free(data);
1420 }
dda7fe12 1421
3d6db3fd
KZ
1422 if (!fdisk_partition_to_string(pa, cf->cxt, FDISK_FIELD_FSLABEL, &data) && data) {
1423 extra_insert_pair(l, _("Filesystem LABEL:"), data);
cf9b302d
KZ
1424 if (!mountpoint)
1425 mountpoint = get_mountpoint(cf, "LABEL", data);
3d6db3fd
KZ
1426 free(data);
1427 }
1428 if (!fdisk_partition_to_string(pa, cf->cxt, FDISK_FIELD_FSTYPE, &data) && data) {
1429 extra_insert_pair(l, _("Filesystem:"), data);
1430 free(data);
dda7fe12 1431 }
dda7fe12 1432
cf9b302d
KZ
1433 if (mountpoint) {
1434 extra_insert_pair(l, _("Mountpoint:"), mountpoint);
1435 free(mountpoint);
dda7fe12 1436 }
dda7fe12
OO
1437}
1438
dda7fe12
OO
1439static int ui_draw_extra(struct cfdisk *cf)
1440{
1441 WINDOW *win_ex;
1442 int wline = 1;
1443 struct cfdisk_line *ln = &cf->lines[cf->lines_idx];
1444 char *tbstr = NULL, *end;
1445 int win_ex_start_line, win_height, tblen;
1446 int ndatalines;
1447
3542fc51
KZ
1448 if (!cf->show_extra)
1449 return 0;
1450
1451 DBG(UI, ul_debug("draw extra"));
1452
a2d0dbb4
KZ
1453 assert(ln->extra);
1454
cbcafba2 1455 if (cf->act_win) {
3542fc51 1456 wclear(cf->act_win);
cbcafba2
KZ
1457 touchwin(stdscr);
1458 }
3542fc51 1459
a2d0dbb4 1460 if (scols_table_is_empty(ln->extra)) {
dda7fe12 1461 extra_prepare_data(cf);
a2d0dbb4
KZ
1462 if (scols_table_is_empty(ln->extra))
1463 return 0;
1464 }
dda7fe12
OO
1465
1466 ndatalines = fdisk_table_get_nents(cf->table) + 1;
1467
1468 /* nents + header + one free line */
1469 win_ex_start_line = TABLE_START_LINE + ndatalines;
1470 win_height = MENU_START_LINE - win_ex_start_line;
1471 tblen = scols_table_get_nlines(ln->extra);
1472
1473 /* we can't get a single line of data under the partlist*/
1474 if (win_height < 3)
1475 return 1;
1476
1477 /* number of data lines + 2 for top/bottom lines */
1478 win_height = win_height < tblen + 2 ? win_height : tblen + 2;
1479
1480 if ((size_t) win_ex_start_line + win_height + 1 < MENU_START_LINE)
1481 win_ex_start_line = MENU_START_LINE - win_height;
1482
3542fc51 1483 win_ex = subwin(stdscr, win_height, ui_cols - 2, win_ex_start_line, 1);
dda7fe12
OO
1484
1485 scols_table_reduce_termwidth(ln->extra, 4);
1486 scols_print_table_to_string(ln->extra, &tbstr);
1487
1488 end = tbstr;
1489 while ((end = strchr(end, '\n')))
1490 *end++ = '\0';
1491
1492 box(win_ex, 0, 0);
1493
1494 end = tbstr;
1495 while (--win_height > 1) {
1496 mvwaddstr(win_ex, wline++, 1 /* window column*/, tbstr);
1497 tbstr += strlen(tbstr) + 1;
1498 }
1499 free(end);
1500
a2d0dbb4
KZ
1501 if (ln->w)
1502 delwin(ln->w);
dda7fe12
OO
1503
1504 DBG(UI, ul_debug("draw window: %p", win_ex));
3542fc51 1505 touchwin(stdscr);
dda7fe12
OO
1506 wrefresh(win_ex);
1507
1508 cf->act_win = ln->w = win_ex;
1509 return 0;
1510}
1511
8c3a5a44
KZ
1512static void ui_menu_goto(struct cfdisk *cf, int where)
1513{
3b411726 1514 struct cfdisk_menuitem *d;
8c3a5a44
KZ
1515 size_t old;
1516
f019ce1f
KZ
1517 /* stop and begin/end for vertical menus */
1518 if (cf->menu->vertical) {
1519 if (where < 0)
1520 where = 0;
1521 else if (where > (int) cf->menu->nitems - 1)
1522 where = cf->menu->nitems - 1;
1523 } else {
1524 /* continue from begin/end */
1525 if (where < 0)
1526 where = cf->menu->nitems - 1;
1527 else if ((size_t) where > cf->menu->nitems - 1)
1528 where = 0;
1529 }
6fed9601 1530 if ((size_t) where == cf->menu->idx)
8c3a5a44 1531 return;
6dbe3af9 1532
1af8003b
KZ
1533 ui_clean_info();
1534
6fed9601
KZ
1535 old = cf->menu->idx;
1536 cf->menu->idx = where;
1537
1538 if (!menuitem_on_page(cf, old)) {
1539 ui_draw_menu(cf);
1540 return;
1541 }
6dbe3af9 1542
8c3a5a44
KZ
1543 d = menu_get_menuitem(cf, old);
1544 ui_draw_menuitem(cf, d, old);
6dbe3af9 1545
8c3a5a44
KZ
1546 d = menu_get_menuitem(cf, where);
1547 ui_draw_menuitem(cf, d, where);
dda7fe12 1548
6dbe3af9
KZ
1549}
1550
ac27ea5c
KZ
1551static int ui_menu_move(struct cfdisk *cf, int key)
1552{
f019ce1f
KZ
1553 struct cfdisk_menu *m;
1554
ac27ea5c
KZ
1555 assert(cf);
1556 assert(cf->menu);
1557
c71f5a56 1558 if (key == (int) ERR)
7556c944
KZ
1559 return 0; /* ignore errors */
1560
f019ce1f
KZ
1561 m = cf->menu;
1562
14620822 1563 DBG(MENU, ul_debug("menu move key >%c<.", key));
f019ce1f
KZ
1564
1565 if (m->vertical)
ac27ea5c
KZ
1566 {
1567 switch (key) {
1568 case KEY_DOWN:
1569 case '\016': /* ^N */
1570 case 'j': /* Vi-like alternative */
f019ce1f 1571 ui_menu_goto(cf, m->idx + 1);
ac27ea5c
KZ
1572 return 0;
1573 case KEY_UP:
1574 case '\020': /* ^P */
1575 case 'k': /* Vi-like alternative */
f019ce1f 1576 ui_menu_goto(cf, (int) m->idx - 1);
ac27ea5c 1577 return 0;
f019ce1f
KZ
1578 case KEY_PPAGE:
1579 if (m->page_sz) {
1580 ui_menu_goto(cf, (int) m->idx - m->page_sz);
1581 return 0;
1582 }
b77025b6 1583 FALLTHROUGH;
ac27ea5c
KZ
1584 case KEY_HOME:
1585 ui_menu_goto(cf, 0);
1586 return 0;
f019ce1f
KZ
1587 case KEY_NPAGE:
1588 if (m->page_sz) {
1589 ui_menu_goto(cf, m->idx + m->page_sz);
1590 return 0;
1591 }
b77025b6 1592 FALLTHROUGH;
ac27ea5c 1593 case KEY_END:
f019ce1f 1594 ui_menu_goto(cf, m->nitems);
ac27ea5c
KZ
1595 return 0;
1596 }
1597 } else {
1598 switch (key) {
1599 case KEY_RIGHT:
1600 case '\t':
f019ce1f 1601 ui_menu_goto(cf, m->idx + 1);
ac27ea5c
KZ
1602 return 0;
1603 case KEY_LEFT:
1604#ifdef KEY_BTAB
1605 case KEY_BTAB:
1606#endif
f019ce1f 1607 ui_menu_goto(cf, (int) m->idx - 1);
ac27ea5c
KZ
1608 return 0;
1609 }
1610 }
1611
2f4eb047
KZ
1612 if (key == '\014') { /* ^L refresh */
1613 ui_menu_resize(cf);
1614 return 0;
1615 }
1616
b607e5fa 1617 DBG(MENU, ul_debug(" no menu move key"));
2f4eb047 1618 return 1;
ac27ea5c
KZ
1619}
1620
7556c944
KZ
1621/* but don't call me from ui_run(), this is for pop-up menus only */
1622static void ui_menu_resize(struct cfdisk *cf)
1623{
b607e5fa 1624 DBG(MENU, ul_debug("menu resize/refresh"));
7556c944
KZ
1625 resize();
1626 ui_clean_menu(cf);
1627 menu_refresh_size(cf);
1628 ui_draw_menu(cf);
1629 refresh();
1630}
1631
a6f69126
KZ
1632static int partition_on_page(struct cfdisk *cf, size_t i)
1633{
1634 if (cf->page_sz == 0 ||
1635 cf->lines_idx / cf->page_sz == i / cf->page_sz)
1636 return 1;
1637 return 0;
1638}
1639
8c3a5a44
KZ
1640static void ui_draw_partition(struct cfdisk *cf, size_t i)
1641{
1642 int ln = TABLE_START_LINE + 1 + i; /* skip table header */
1643 int cl = ARROW_CURSOR_WIDTH; /* we need extra space for cursor */
a6f69126
KZ
1644 int cur = cf->lines_idx == i;
1645 size_t curpg = 0;
1646
1647 if (cf->page_sz) {
1648 if (!partition_on_page(cf, i))
1649 return;
1650 ln = TABLE_START_LINE + (i % cf->page_sz) + 1;
1651 curpg = cf->lines_idx / cf->page_sz;
1652 }
6dbe3af9 1653
14620822
KZ
1654 DBG(UI, ul_debug(
1655 "draw partition %zu [page_sz=%zu, "
a6f69126
KZ
1656 "line=%d, idx=%zu]",
1657 i, cf->page_sz, ln, cf->lines_idx));
6dbe3af9 1658
a6f69126
KZ
1659 if (cur) {
1660 attron(A_REVERSE);
8c3a5a44 1661 mvaddstr(ln, 0, ARROW_CURSOR_STRING);
dda7fe12 1662 mvaddstr(ln, cl, cf->lines[i + 1].data);
a6f69126 1663 attroff(A_REVERSE);
6dbe3af9 1664 } else {
45333e9d
KZ
1665 int at = 0;
1666
f1512be8 1667 if (colors_wanted() && is_freespace(cf, i)) {
45333e9d
KZ
1668 attron(COLOR_PAIR(CFDISK_CL_FREESPACE));
1669 at = 1;
1670 }
8c3a5a44 1671 mvaddstr(ln, 0, ARROW_CURSOR_DUMMY);
dda7fe12 1672 mvaddstr(ln, cl, cf->lines[i + 1].data);
45333e9d 1673 if (at)
dda7fe12 1674 attroff(COLOR_PAIR(CFDISK_CL_FREESPACE));
6dbe3af9
KZ
1675 }
1676
a6f69126
KZ
1677 if ((size_t) ln == MENU_START_LINE - 1 &&
1678 cf->page_sz && curpg < cf->nlines / cf->page_sz) {
1679 if (cur)
1680 attron(A_REVERSE);
eef63970 1681 mvaddch(ln, ui_cols - 1, ACS_DARROW);
a6f69126
KZ
1682 mvaddch(ln, 0, ACS_DARROW);
1683 if (cur)
1684 attroff(A_REVERSE);
1685 }
dda7fe12 1686
6dbe3af9
KZ
1687}
1688
8c3a5a44
KZ
1689static int ui_draw_table(struct cfdisk *cf)
1690{
1691 int cl = ARROW_CURSOR_WIDTH;
1692 size_t i, nparts = fdisk_table_get_nents(cf->table);
a6f69126 1693 size_t curpg = cf->page_sz ? cf->lines_idx / cf->page_sz : 0;
7eda085c 1694
14620822 1695 DBG(UI, ul_debug("draw table"));
6dbe3af9 1696
a6f69126
KZ
1697 for (i = TABLE_START_LINE; i <= TABLE_START_LINE + cf->page_sz; i++) {
1698 move(i, 0);
1699 clrtoeol();
1700 }
6dbe3af9 1701
a2d0dbb4 1702 if (nparts == 0 || (size_t) cf->lines_idx > nparts - 1)
b2301179
KZ
1703 cf->lines_idx = nparts ? nparts - 1 : 0;
1704
8c3a5a44
KZ
1705 /* print header */
1706 attron(A_BOLD);
dda7fe12 1707 mvaddstr(TABLE_START_LINE, cl, cf->lines[0].data);
8c3a5a44 1708 attroff(A_BOLD);
6dbe3af9 1709
8c3a5a44
KZ
1710 /* print partitions */
1711 for (i = 0; i < nparts; i++)
1712 ui_draw_partition(cf, i);
6dbe3af9 1713
a6f69126 1714 if (curpg != 0) {
eef63970 1715 mvaddch(TABLE_START_LINE, ui_cols - 1, ACS_UARROW);
a6f69126
KZ
1716 mvaddch(TABLE_START_LINE, 0, ACS_UARROW);
1717 }
1718 if (cf->page_sz && curpg < cf->nlines / cf->page_sz) {
eef63970 1719 mvaddch(MENU_START_LINE - 1, ui_cols - 1, ACS_DARROW);
a6f69126
KZ
1720 mvaddch(MENU_START_LINE - 1, 0, ACS_DARROW);
1721 }
8c3a5a44 1722 return 0;
6dbe3af9
KZ
1723}
1724
8c3a5a44
KZ
1725static int ui_table_goto(struct cfdisk *cf, int where)
1726{
1727 size_t old;
1728 size_t nparts = fdisk_table_get_nents(cf->table);
6dbe3af9 1729
14620822 1730 DBG(UI, ul_debug("goto table %d", where));
6dbe3af9 1731
8c3a5a44
KZ
1732 if (where < 0)
1733 where = 0;
72f801db
KZ
1734 if (!nparts)
1735 where = 0;
8c3a5a44
KZ
1736 else if ((size_t) where > nparts - 1)
1737 where = nparts - 1;
6dbe3af9 1738
8c3a5a44
KZ
1739 if ((size_t) where == cf->lines_idx)
1740 return 0;
6dbe3af9 1741
8c3a5a44
KZ
1742 old = cf->lines_idx;
1743 cf->lines_idx = where;
6dbe3af9 1744
a6f69126
KZ
1745 if (!partition_on_page(cf, old) ||!partition_on_page(cf, where))
1746 ui_draw_table(cf);
1747 else {
1748 ui_draw_partition(cf, old); /* cleanup old */
1749 ui_draw_partition(cf, where); /* draw new */
1750 }
1af8003b 1751 ui_clean_info();
8c3a5a44 1752 ui_draw_menu(cf);
3542fc51 1753 ui_draw_extra(cf);
8c3a5a44 1754 refresh();
dda7fe12 1755
8c3a5a44 1756 return 0;
6dbe3af9
KZ
1757}
1758
8c3a5a44
KZ
1759static int ui_refresh(struct cfdisk *cf)
1760{
57c83d63 1761 struct fdisk_label *lb;
8c3a5a44 1762 char *id = NULL;
57c83d63 1763 uint64_t bytes = fdisk_get_nsectors(cf->cxt) * fdisk_get_sector_size(cf->cxt);
ec8a728a 1764 char *strsz;
8c3a5a44 1765
83fa0f80 1766 if (!ui_enabled)
8c3a5a44
KZ
1767 return -EINVAL;
1768
39f37044
KZ
1769 strsz = size_to_human_string(SIZE_DECIMAL_2DIGITS
1770 | SIZE_SUFFIX_SPACE
ec8a728a
KZ
1771 | SIZE_SUFFIX_3LETTER, bytes);
1772
57c83d63
KZ
1773 lb = fdisk_get_label(cf->cxt, NULL);
1774 assert(lb);
1775
a2d0dbb4
KZ
1776 clear();
1777
8c3a5a44
KZ
1778 /* header */
1779 attron(A_BOLD);
57c83d63 1780 ui_center(0, _("Disk: %s"), fdisk_get_devname(cf->cxt));
8c3a5a44 1781 attroff(A_BOLD);
7085f1e4 1782 ui_center(1, _("Size: %s, %"PRIu64" bytes, %ju sectors"),
57c83d63 1783 strsz, bytes, (uintmax_t) fdisk_get_nsectors(cf->cxt));
8c3a5a44 1784 if (fdisk_get_disklabel_id(cf->cxt, &id) == 0 && id)
1af8003b 1785 ui_center(2, _("Label: %s, identifier: %s"),
57c83d63 1786 fdisk_label_get_name(lb), id);
7eda085c 1787 else
57c83d63 1788 ui_center(2, _("Label: %s"), fdisk_label_get_name(lb));
8c3a5a44 1789 free(strsz);
ec8f7121 1790 free(id);
6dbe3af9 1791
8c3a5a44
KZ
1792 ui_draw_table(cf);
1793 ui_draw_menu(cf);
6dbe3af9 1794 refresh();
8c3a5a44 1795 return 0;
6dbe3af9
KZ
1796}
1797
ffab025d 1798static ssize_t ui_get_string(const char *prompt,
b1f58330
KZ
1799 const char *hint, char *buf, size_t len)
1800{
b1f58330 1801 int ln = MENU_START_LINE, cl = 1;
1b6d02fe
KZ
1802 ssize_t rc = -1;
1803 struct mbs_editor *edit;
1804
1805 DBG(UI, ul_debug("ui get string"));
b1f58330 1806
b1f58330
KZ
1807 assert(buf);
1808 assert(len);
1809
1810 move(ln, 0);
1811 clrtoeol();
1812
a89eafed
KZ
1813 move(ln + 1, 0);
1814 clrtoeol();
1815
b1f58330 1816 if (prompt) {
f2cfb235 1817 mvaddstr(ln, cl, prompt);
b1f58330
KZ
1818 cl += mbs_safe_width(prompt);
1819 }
1820
1b6d02fe
KZ
1821 edit = mbs_new_edit(buf, len, ui_cols - cl);
1822 if (!edit)
1823 goto done;
1824
1825 mbs_edit_goto(edit, MBS_EDIT_END);
b1f58330
KZ
1826
1827 if (hint)
fbae1442 1828 ui_hint("%s", hint);
b1f58330
KZ
1829 else
1830 ui_clean_hint();
1831
b1f58330 1832 curs_set(1);
b1f58330 1833
502c5186 1834 while (!sig_die) {
1b6d02fe
KZ
1835 wint_t c; /* we have fallback in widechar.h */
1836
1837 move(ln, cl);
1838 clrtoeol();
1839 mvaddstr(ln, cl, edit->buf);
1840 move(ln, cl + edit->cursor_cells);
1841 refresh();
1842
b1f58330
KZ
1843#if !defined(HAVE_SLCURSES_H) && !defined(HAVE_SLANG_SLCURSES_H) && \
1844 defined(HAVE_LIBNCURSESW) && defined(HAVE_WIDECHAR)
1845 if (get_wch(&c) == ERR) {
1846#else
c71f5a56 1847 if ((c = getch()) == (wint_t) ERR) {
b1f58330 1848#endif
502c5186
KZ
1849 if (sig_die)
1850 break;
c431d901 1851 if (sig_resize) {
7556c944
KZ
1852 resize();
1853 continue;
1854 }
b1f58330
KZ
1855 if (!isatty(STDIN_FILENO))
1856 exit(2);
1857 else
1858 goto done;
1859 }
1b6d02fe
KZ
1860
1861 DBG(UI, ul_debug("ui get string: key=%lc", c));
1862
b1f58330
KZ
1863 if (c == '\r' || c == '\n' || c == KEY_ENTER)
1864 break;
1865
1b6d02fe
KZ
1866 rc = 1;
1867
b1f58330
KZ
1868 switch (c) {
1869 case KEY_ESC:
1870 rc = -CFDISK_ERR_ESC;
1871 goto done;
1b6d02fe
KZ
1872 case KEY_LEFT:
1873 rc = mbs_edit_goto(edit, MBS_EDIT_LEFT);
1874 break;
635a8151 1875 case KEY_RIGHT:
1b6d02fe
KZ
1876 rc = mbs_edit_goto(edit, MBS_EDIT_RIGHT);
1877 break;
635a8151 1878 case KEY_END:
1b6d02fe
KZ
1879 rc = mbs_edit_goto(edit, MBS_EDIT_END);
1880 break;
635a8151 1881 case KEY_HOME:
1b6d02fe
KZ
1882 rc = mbs_edit_goto(edit, MBS_EDIT_HOME);
1883 break;
635a8151
KZ
1884 case KEY_UP:
1885 case KEY_DOWN:
635a8151 1886 break;
1b6d02fe
KZ
1887 case KEY_DC:
1888 rc = mbs_edit_delete(edit);
1889 break;
b1f58330 1890 case '\b':
6b1e439a 1891 case KEY_DELETE:
b1f58330 1892 case KEY_BACKSPACE:
1b6d02fe 1893 rc = mbs_edit_backspace(edit);
b1f58330
KZ
1894 break;
1895 default:
1b6d02fe
KZ
1896 rc = mbs_edit_insert(edit, c);
1897 break;
b1f58330 1898 }
1b6d02fe
KZ
1899 if (rc == 1)
1900 beep();
b1f58330
KZ
1901 }
1902
502c5186
KZ
1903 if (sig_die)
1904 die_on_signal();
1905
1b6d02fe 1906 rc = strlen(edit->buf); /* success */
b1f58330
KZ
1907done:
1908 move(ln, 0);
1909 clrtoeol();
1910 curs_set(0);
1911 refresh();
1b6d02fe 1912 mbs_free_edit(edit);
b1f58330
KZ
1913
1914 return rc;
1915}
1916
a351f2e5
KZ
1917static int ui_get_size(struct cfdisk *cf, /* context */
1918 const char *prompt, /* UI dialog string */
7085f1e4
KZ
1919 uint64_t *res, /* result in bytes */
1920 uint64_t low, /* minimal size */
1921 uint64_t up, /* maximal size */
a351f2e5 1922 int *expsize) /* explicitly specified size */
b1f58330
KZ
1923{
1924 char buf[128];
7085f1e4 1925 uint64_t user = 0;
b1f58330
KZ
1926 ssize_t rc;
1927 char *dflt = size_to_human_string(0, *res);
1928
33342316 1929 DBG(UI, ul_debug("get_size (default=%"PRIu64")", *res));
b1f58330
KZ
1930
1931 ui_clean_info();
1932
29e9b787
KZ
1933 snprintf(buf, sizeof(buf), "%s", dflt);
1934
b1f58330 1935 do {
91ba41ca 1936 int pwr = 0, insec = 0;
b1f58330 1937
ffab025d 1938 rc = ui_get_string(prompt,
a4fbbbd4
BS
1939 _("May be followed by M for MiB, G for GiB, "
1940 "T for TiB, or S for sectors."),
b1f58330 1941 buf, sizeof(buf));
29e9b787
KZ
1942 ui_clean_warn();
1943
b1f58330
KZ
1944 if (rc == 0) {
1945 ui_warnx(_("Please, specify size."));
1946 continue; /* nothing specified */
042f62df 1947 } if (rc == -CFDISK_ERR_ESC)
b1f58330
KZ
1948 break; /* cancel dialog */
1949
91ba41ca
KZ
1950 if (strcmp(buf, dflt) == 0)
1951 user = *res, rc = 0; /* no change, use default */
1952 else {
1953 size_t len = strlen(buf);
bfdb3244 1954 if (buf[len - 1] == 'S' || buf[len - 1] == 's') {
91ba41ca
KZ
1955 insec = 1;
1956 buf[len - 1] = '\0';
1957 }
c6b3e988 1958 rc = ul_parse_size(buf, (uintmax_t *)&user, &pwr); /* parse */
91ba41ca
KZ
1959 }
1960
b1f58330 1961 if (rc == 0) {
7085f1e4 1962 DBG(UI, ul_debug("get_size user=%"PRIu64", power=%d, in-sectors=%s",
91ba41ca
KZ
1963 user, pwr, insec ? "yes" : "no"));
1964 if (insec)
57c83d63 1965 user *= fdisk_get_sector_size(cf->cxt);
b1f58330 1966 if (user < low) {
7085f1e4 1967 ui_warnx(_("Minimum size is %"PRIu64" bytes."), low);
b1f58330
KZ
1968 rc = -ERANGE;
1969 }
1970 if (user > up && pwr && user < up + (1ULL << pwr * 10))
1971 /* ignore when the user specified size overflow
1972 * with in range specified by suffix (e.g. MiB) */
1973 user = up;
1974
1975 if (user > up) {
7085f1e4 1976 ui_warnx(_("Maximum size is %"PRIu64" bytes."), up);
b1f58330
KZ
1977 rc = -ERANGE;
1978 }
9b0b9fb1
KZ
1979 if (rc == 0 && insec && expsize)
1980 *expsize = 1;
1981
2cec7949
KZ
1982 } else
1983 ui_warnx(_("Failed to parse size."));
b1f58330
KZ
1984 } while (rc != 0);
1985
1986 if (rc == 0)
1987 *res = user;
1988 free(dflt);
1989
7085f1e4 1990 DBG(UI, ul_debug("get_size (result=%"PRIu64", rc=%zd)", *res, rc));
b1f58330
KZ
1991 return rc;
1992}
1993
6d6d9c1a
KZ
1994static struct fdisk_parttype *ui_get_parttype(struct cfdisk *cf,
1995 struct fdisk_parttype *cur)
6fed9601 1996{
3b411726 1997 struct cfdisk_menuitem *d, *cm;
6d6d9c1a 1998 size_t i = 0, nitems, idx = 0;
a745611d 1999 struct fdisk_parttype *t = NULL;
5ab37600 2000 struct fdisk_label *lb;
a745611d 2001 int codetypes = 0;
6fed9601 2002
14620822 2003 DBG(UI, ul_debug("asking for parttype."));
6fed9601 2004
5ab37600
KZ
2005 lb = fdisk_get_label(cf->cxt, NULL);
2006
6fed9601
KZ
2007 /* create cfdisk menu according to label types, note that the
2008 * last cm[] item has to be empty -- so nitems + 1 */
a745611d
KZ
2009 nitems = fdisk_label_get_nparttypes(lb);
2010 if (!nitems)
6d6d9c1a 2011 return NULL;
5ab37600 2012
3b411726 2013 cm = xcalloc(nitems + 1, sizeof(struct cfdisk_menuitem));
6fed9601
KZ
2014 if (!cm)
2015 return NULL;
2016
a745611d 2017 codetypes = fdisk_label_has_code_parttypes(lb);
6d6d9c1a 2018
6fed9601 2019 for (i = 0; i < nitems; i++) {
a745611d 2020 const struct fdisk_parttype *x = fdisk_label_get_parttype(lb, i);
6d6d9c1a 2021 char *name;
6fed9601 2022
a745611d
KZ
2023 cm[i].userdata = (void *) x;
2024 if (codetypes)
2025 xasprintf(&name, "%2x %s",
2026 fdisk_parttype_get_code(x),
2027 _(fdisk_parttype_get_name(x)));
6d6d9c1a 2028 else {
a745611d
KZ
2029 name = (char *) _(fdisk_parttype_get_name(x));
2030 cm[i].desc = fdisk_parttype_get_string(x);
6d6d9c1a
KZ
2031 }
2032 cm[i].name = name;
2033 if (x == cur)
2034 idx = i;
6fed9601
KZ
2035 }
2036
2037 /* make the new menu active */
2038 menu_push(cf, cm);
2039 cf->menu->vertical = 1;
6d6d9c1a 2040 cf->menu->idx = idx;
6fed9601
KZ
2041 menu_set_title(cf->menu, _("Select partition type"));
2042 ui_draw_menu(cf);
2043 refresh();
2044
502c5186 2045 while (!sig_die) {
6fed9601 2046 int key = getch();
f019ce1f 2047
502c5186
KZ
2048 if (sig_die)
2049 break;
c431d901 2050 if (sig_resize)
7556c944 2051 ui_menu_resize(cf);
6fed9601
KZ
2052 if (ui_menu_move(cf, key) == 0)
2053 continue;
f019ce1f 2054
6fed9601
KZ
2055 switch (key) {
2056 case KEY_ENTER:
2057 case '\n':
2058 case '\r':
2059 d = menu_get_menuitem(cf, cf->menu->idx);
2060 if (d)
2061 t = (struct fdisk_parttype *) d->userdata;
2062 goto done;
ad5d45c0 2063 case KEY_ESC:
6fed9601
KZ
2064 case 'q':
2065 case 'Q':
2066 goto done;
2067 }
502c5186 2068 }
6fed9601 2069
502c5186
KZ
2070 if (sig_die)
2071 die_on_signal();
6fed9601
KZ
2072done:
2073 menu_pop(cf);
a745611d 2074 if (codetypes) {
6d6d9c1a
KZ
2075 for (i = 0; i < nitems; i++)
2076 free((char *) cm[i].name);
2077 }
6fed9601 2078 free(cm);
57c83d63 2079 DBG(UI, ul_debug("get parrtype done [type=%s] ", t ?
30cc5f57 2080 fdisk_parttype_get_name(t) : ""));
6fed9601
KZ
2081 return t;
2082}
2083
a89eafed
KZ
2084static int ui_script_read(struct cfdisk *cf)
2085{
2086 struct fdisk_script *sc = NULL;
2087 char buf[PATH_MAX] = { 0 };
2088 int rc;
2089
941409c0 2090 erase();
ffab025d 2091 rc = ui_get_string( _("Enter script file name: "),
a89eafed
KZ
2092 _("The script file will be applied to in-memory partition table."),
2093 buf, sizeof(buf));
2094 if (rc <= 0)
2095 return rc;
2096
2097 rc = -1;
2098 errno = 0;
2099 sc = fdisk_new_script_from_file(cf->cxt, buf);
2100 if (!sc && errno)
54fefa07 2101 ui_warn(_("Cannot open %s"), buf);
a89eafed
KZ
2102 else if (!sc)
2103 ui_warnx(_("Failed to parse script file %s"), buf);
2104 else if (fdisk_apply_script(cf->cxt, sc) != 0)
2105 ui_warnx(_("Failed to apply script %s"), buf);
2106 else
2107 rc = 0;
2108
941409c0 2109 ui_clean_hint();
a89eafed
KZ
2110 fdisk_unref_script(sc);
2111 return rc;
2112}
2113
2114static int ui_script_write(struct cfdisk *cf)
2115{
2116 struct fdisk_script *sc = NULL;
2117 char buf[PATH_MAX] = { 0 };
2118 FILE *f = NULL;
2119 int rc;
2120
ffab025d 2121 rc = ui_get_string( _("Enter script file name: "),
a89eafed
KZ
2122 _("The current in-memory partition table will be dumped to the file."),
2123 buf, sizeof(buf));
2124 if (rc <= 0)
2125 return rc;
2126
2127 rc = 0;
2128 sc = fdisk_new_script(cf->cxt);
2129 if (!sc) {
2130 ui_warn(_("Failed to allocate script handler"));
2131 goto done;
2132 }
2133
2134 rc = fdisk_script_read_context(sc, NULL);
2135 if (rc) {
2136 ui_warnx(_("Failed to read disk layout into script."));
2137 goto done;
2138 }
2139
8c9615a9 2140 DBG(UI, ul_debug("writing dump into: '%s'", buf));
a89eafed
KZ
2141 f = fopen(buf, "w");
2142 if (!f) {
54fefa07 2143 ui_warn(_("Cannot open %s"), buf);
8c9615a9 2144 rc = -errno;
a89eafed
KZ
2145 goto done;
2146 }
2147
2148 rc = fdisk_script_write_file(sc, f);
8c9615a9
KZ
2149 if (!rc)
2150 ui_info(_("Disk layout successfully dumped."));
2151done:
a89eafed
KZ
2152 if (rc)
2153 ui_warn(_("Failed to write script %s"), buf);
a89eafed
KZ
2154 if (f)
2155 fclose(f);
2156 fdisk_unref_script(sc);
2157 return rc;
2158}
2159
7aa0d529
KZ
2160/* prints menu with libfdisk labels and waits for users response */
2161static int ui_create_label(struct cfdisk *cf)
2162{
3b411726 2163 struct cfdisk_menuitem *d, *cm;
941409c0 2164 int rc = 1, refresh_menu = 1;
7aa0d529
KZ
2165 size_t i = 0, nitems;
2166 struct fdisk_label *lb = NULL;
2167
2168 assert(cf);
2169
14620822 2170 DBG(UI, ul_debug("asking for new disklabe."));
7aa0d529
KZ
2171
2172 /* create cfdisk menu according to libfdisk labels, note that the
2173 * last cm[] item has to be empty -- so nitems + 1 */
6a632136 2174 nitems = fdisk_get_nlabels(cf->cxt);
3b411726 2175 cm = xcalloc(nitems + 1, sizeof(struct cfdisk_menuitem));
7aa0d529 2176
33ca468b
KZ
2177 while (fdisk_next_label(cf->cxt, &lb) == 0 && i < nitems) {
2178
57c83d63 2179 if (fdisk_label_is_disabled(lb) ||
8eccde20 2180 fdisk_label_get_type(lb) == FDISK_DISKLABEL_BSD)
dd626abd 2181 continue;
57c83d63 2182 cm[i++].name = fdisk_label_get_name(lb);
7aa0d529
KZ
2183 }
2184
2185 erase();
7aa0d529
KZ
2186
2187 /* make the new menu active */
63128bbf 2188 menu_push(cf, cm);
7aa0d529
KZ
2189 cf->menu->vertical = 1;
2190 menu_set_title(cf->menu, _("Select label type"));
941409c0
KZ
2191
2192 if (!cf->zero_start)
2193 ui_info(_("Device does not contain a recognized partition table."));
2194
7aa0d529 2195
502c5186 2196 while (!sig_die) {
c308e205
SK
2197 int key;
2198
941409c0
KZ
2199 if (refresh_menu) {
2200 ui_draw_menu(cf);
45be288c 2201 ui_hint(_("Select a type to create a new label, press 'L' to load script file, 'Q' quits."));
941409c0
KZ
2202 refresh();
2203 refresh_menu = 0;
2204 }
2205
c308e205 2206 key = getch();
7556c944 2207
502c5186
KZ
2208 if (sig_die)
2209 break;
c431d901 2210 if (sig_resize)
7556c944 2211 ui_menu_resize(cf);
7aa0d529
KZ
2212 if (ui_menu_move(cf, key) == 0)
2213 continue;
2214 switch (key) {
2215 case KEY_ENTER:
2216 case '\n':
2217 case '\r':
6fed9601 2218 d = menu_get_menuitem(cf, cf->menu->idx);
7aa0d529
KZ
2219 if (d)
2220 rc = fdisk_create_disklabel(cf->cxt, d->name);
2221 goto done;
ad5d45c0 2222 case KEY_ESC:
7aa0d529
KZ
2223 case 'q':
2224 case 'Q':
2225 goto done;
f16c37f4 2226 case 'l':
a89eafed 2227 case 'L':
a89eafed
KZ
2228 rc = ui_script_read(cf);
2229 if (rc == 0)
2230 goto done;
941409c0 2231 refresh_menu = 1;
a89eafed 2232 break;
7aa0d529 2233 }
502c5186 2234 }
7aa0d529 2235
502c5186
KZ
2236 if (sig_die)
2237 die_on_signal();
7aa0d529
KZ
2238done:
2239 menu_pop(cf);
2240 free(cm);
14620822 2241 DBG(UI, ul_debug("create label done [rc=%d] ", rc));
7aa0d529
KZ
2242 return rc;
2243}
2244
a89eafed 2245
314a0dd8
KZ
2246static int ui_help(void)
2247{
2248 size_t i;
7efb0dc4 2249 static const char *const help[] = {
e993b9c8
BS
2250 N_("This is cfdisk, a curses-based disk partitioning program."),
2251 N_("It lets you create, delete, and modify partitions on a block device."),
fd4484a7 2252 " ",
314a0dd8
KZ
2253 N_("Command Meaning"),
2254 N_("------- -------"),
793e8d2a
KZ
2255 N_(" b Toggle bootable flag of the current partition;"),
2256 N_(" implemented for DOS (MBR) and SGI labels only"),
314a0dd8
KZ
2257 N_(" d Delete the current partition"),
2258 N_(" h Print this screen"),
2259 N_(" n Create new partition from free space"),
2260 N_(" q Quit program without writing partition table"),
c186e148 2261 N_(" r Reduce or enlarge the current partition"),
33f7c502 2262 N_(" s Fix partitions order (only when in disarray)"),
e993b9c8 2263 N_(" t Change the partition type"),
a89eafed 2264 N_(" u Dump disk layout to sfdisk compatible script file"),
e993b9c8
BS
2265 N_(" W Write partition table to disk (you must enter uppercase W);"),
2266 N_(" since this might destroy data on the disk, you must either"),
2267 N_(" confirm or deny the write by entering 'yes' or 'no'"),
dda7fe12 2268 N_(" x Display/hide extra information about a partition"),
314a0dd8
KZ
2269 N_("Up Arrow Move cursor to the previous partition"),
2270 N_("Down Arrow Move cursor to the next partition"),
2271 N_("Left Arrow Move cursor to the previous menu item"),
2272 N_("Right Arrow Move cursor to the next menu item"),
fd4484a7 2273 " ",
314a0dd8 2274 N_("Note: All of the commands can be entered with either upper or lower"),
e993b9c8 2275 N_("case letters (except for Write)."),
fd4484a7 2276 " ",
e7e76976
KZ
2277 N_("Use lsblk(8) or partx(8) to see more details about the device."),
2278 " ",
2279 " ",
793e8d2a 2280 "Copyright (C) 2014-2023 Karel Zak <kzak@redhat.com>"
314a0dd8
KZ
2281 };
2282
2283 erase();
314a0dd8
KZ
2284 for (i = 0; i < ARRAY_SIZE(help); i++)
2285 mvaddstr(i, 1, _(help[i]));
2286
2287 ui_info(_("Press a key to continue."));
7556c944 2288
314a0dd8 2289 getch();
502c5186
KZ
2290
2291 if (sig_die)
2292 die_on_signal();
314a0dd8
KZ
2293 return 0;
2294}
2295
63128bbf
KZ
2296/* TODO: use @sz, now 128bytes */
2297static int main_menu_ignore_keys(struct cfdisk *cf, char *ignore,
2298 size_t sz __attribute__((__unused__)))
2299{
2300 struct fdisk_partition *pa = get_current_partition(cf);
2301 size_t i = 0;
2302
2303 if (!pa)
2304 return 0;
2305 if (fdisk_partition_is_freespace(pa)) {
2306 ignore[i++] = 'd'; /* delete */
2307 ignore[i++] = 't'; /* set type */
2308 ignore[i++] = 'b'; /* set bootable */
a351f2e5 2309 ignore[i++] = 'r'; /* resize */
b5ef65a9 2310 cf->menu->prefkey = 'n';
63128bbf 2311 } else {
b5ef65a9 2312 cf->menu->prefkey = 'q';
63128bbf 2313 ignore[i++] = 'n';
aa36c2cf
KZ
2314 if (!fdisk_is_label(cf->cxt, DOS) &&
2315 !fdisk_is_label(cf->cxt, SGI))
63128bbf
KZ
2316 ignore[i++] = 'b';
2317 }
e146ae4e 2318
d5314bf5
KZ
2319 if (!cf->wrong_order)
2320 ignore[i++] = 's';
f9fb8290 2321
6a632136 2322 if (fdisk_is_readonly(cf->cxt))
e146ae4e 2323 ignore[i++] = 'W';
dda7fe12 2324
63128bbf
KZ
2325 return i;
2326}
2327
2328
ed8a13e6
KZ
2329/* returns: error: < 0, success: 0, quit: 1 */
2330static int main_menu_action(struct cfdisk *cf, int key)
2331{
2332 size_t n;
d5314bf5 2333 int ref = 0, rc, org_order = cf->wrong_order;
ed8a13e6
KZ
2334 const char *info = NULL, *warn = NULL;
2335 struct fdisk_partition *pa;
2336
2337 assert(cf);
2338 assert(cf->cxt);
2339 assert(cf->menu);
2340
2341 if (key == 0) {
3b411726 2342 struct cfdisk_menuitem *d = menu_get_menuitem(cf, cf->menu->idx);
ed8a13e6
KZ
2343 if (!d)
2344 return 0;
2345 key = d->key;
2346
2347 } else if (key != 'w' && key != 'W')
2348 key = tolower(key); /* case insensitive except 'W'rite */
2349
14620822 2350 DBG(MENU, ul_debug("main menu action: key=%c", key));
ed8a13e6
KZ
2351
2352 if (cf->menu->ignore && strchr(cf->menu->ignore, key)) {
14620822 2353 DBG(MENU, ul_debug(" ignore '%c'", key));
ed8a13e6
KZ
2354 return 0;
2355 }
2356
2357 pa = get_current_partition(cf);
ec8a728a
KZ
2358 if (!pa)
2359 return -EINVAL;
ed8a13e6
KZ
2360 n = fdisk_partition_get_partno(pa);
2361
14620822 2362 DBG(MENU, ul_debug("menu action on %p", pa));
7b1ffbc4
KZ
2363 ui_clean_hint();
2364 ui_clean_info();
ed8a13e6
KZ
2365
2366 switch (key) {
2367 case 'b': /* Bootable flag */
2368 {
aa36c2cf
KZ
2369 int fl = fdisk_is_label(cf->cxt, DOS) ? DOS_FLAG_ACTIVE :
2370 fdisk_is_label(cf->cxt, SGI) ? SGI_FLAG_BOOT : 0;
ed8a13e6 2371
a1ef792f 2372 if (fl && fdisk_toggle_partition_flag(cf->cxt, n, fl))
ed8a13e6
KZ
2373 warn = _("Could not toggle the flag.");
2374 else if (fl)
2375 ref = 1;
2376 break;
2377 }
f1512be8 2378#ifdef KEY_DC
f019ce1f 2379 case KEY_DC:
f1512be8 2380#endif
ed8a13e6
KZ
2381 case 'd': /* Delete */
2382 if (fdisk_delete_partition(cf->cxt, n) != 0)
2383 warn = _("Could not delete partition %zu.");
2384 else
2385 info = _("Partition %zu has been deleted.");
2386 ref = 1;
2387 break;
d4640320 2388 case 'h': /* Help */
2a0ffa31 2389 case '?':
314a0dd8
KZ
2390 ui_help();
2391 ref = 1;
2392 break;
ed8a13e6
KZ
2393 case 'n': /* New */
2394 {
a351f2e5 2395 uint64_t start, size, dflt_size, secs, max_size;
ed8a13e6 2396 struct fdisk_partition *npa; /* the new partition */
9b0b9fb1 2397 int expsize = 0; /* size specified explicitly in sectors */
ed8a13e6 2398
ec8a728a 2399 if (!fdisk_partition_is_freespace(pa) || !fdisk_partition_has_start(pa))
ed8a13e6 2400 return -EINVAL;
75090201 2401
ed8a13e6
KZ
2402 /* free space range */
2403 start = fdisk_partition_get_start(pa);
a351f2e5 2404 size = max_size = dflt_size = fdisk_partition_get_size(pa) * fdisk_get_sector_size(cf->cxt);
ed8a13e6 2405
1bb387bd
KZ
2406 if (ui_get_size(cf, _("Partition size: "), &size,
2407 fdisk_get_sector_size(cf->cxt),
a351f2e5 2408 max_size, &expsize) == -CFDISK_ERR_ESC)
ed8a13e6
KZ
2409 break;
2410
75090201 2411 secs = size / fdisk_get_sector_size(cf->cxt);
75090201
KZ
2412
2413 npa = fdisk_new_partition();
2414 if (!npa)
2415 return -ENOMEM;
2416
ed8a13e6
KZ
2417 if (dflt_size == size) /* default is to fillin all free space */
2418 fdisk_partition_end_follow_default(npa, 1);
75090201
KZ
2419 else
2420 fdisk_partition_set_size(npa, secs);
ed8a13e6 2421
9b0b9fb1
KZ
2422 if (expsize)
2423 fdisk_partition_size_explicit(pa, 1);
2424
ed8a13e6 2425 fdisk_partition_set_start(npa, start);
9b0b9fb1 2426 fdisk_partition_partno_follow_default(npa, 1);
ed8a13e6 2427 /* add to disk label -- libfdisk will ask for missing details */
c3bc7483 2428 rc = fdisk_add_partition(cf->cxt, npa, NULL);
ed8a13e6
KZ
2429 fdisk_unref_partition(npa);
2430 if (rc == 0)
2431 ref = 1;
2432 break;
2433 }
2434 case 'q': /* Quit */
2435 return 1;
2436 case 't': /* Type */
6fed9601 2437 {
6fed9601
KZ
2438 struct fdisk_parttype *t;
2439
ec8a728a 2440 if (fdisk_partition_is_freespace(pa))
6fed9601
KZ
2441 return -EINVAL;
2442 t = (struct fdisk_parttype *) fdisk_partition_get_type(pa);
6d6d9c1a 2443 t = ui_get_parttype(cf, t);
6fed9601
KZ
2444 ref = 1;
2445
2446 if (t && fdisk_set_partition_type(cf->cxt, n, t) == 0)
09af3db4 2447 info = _("Changed type of partition %zu.");
6fed9601 2448 else
09af3db4 2449 info = _("The type of partition %zu is unchanged.");
ed8a13e6 2450 break;
6fed9601 2451 }
a351f2e5
KZ
2452 case 'r': /* resize */
2453 {
a351f2e5 2454 uint64_t size, max_size, secs;
78fd7300 2455 struct fdisk_partition *npa;
a351f2e5
KZ
2456
2457 if (fdisk_partition_is_freespace(pa) || !fdisk_partition_has_start(pa))
2458 return -EINVAL;
2459
78fd7300
TW
2460 rc = fdisk_partition_get_max_size(cf->cxt,
2461 fdisk_partition_get_partno(pa),
2462 &size);
2463 if (rc)
2464 return rc;
a351f2e5
KZ
2465
2466 size *= fdisk_get_sector_size(cf->cxt);
2467 max_size = size;
2468
2469 if (ui_get_size(cf, _("New size: "), &size,
2470 fdisk_get_sector_size(cf->cxt),
2471 max_size, NULL) == -CFDISK_ERR_ESC)
2472 break;
2473 secs = size / fdisk_get_sector_size(cf->cxt);
2474 npa = fdisk_new_partition();
2475 if (!npa)
2476 return -ENOMEM;
2477
2478 fdisk_partition_set_size(npa, secs);
2479
2480 rc = fdisk_set_partition(cf->cxt, n, npa);
2481 fdisk_unref_partition(npa);
2482 if (rc == 0) {
2483 ref = 1;
2484 info = _("Partition %zu resized.");
2485 }
2486 break;
2487 }
d4640320 2488 case 's': /* Sort */
d5314bf5
KZ
2489 if (cf->wrong_order) {
2490 fdisk_reorder_partitions(cf->cxt);
2491 ref = 1;
2492 }
2493 break;
d4640320 2494 case 'u': /* dUmp */
8c9615a9 2495 ui_script_write(cf);
a89eafed 2496 break;
ed8a13e6 2497 case 'W': /* Write */
7b1ffbc4
KZ
2498 {
2499 char buf[64] = { 0 };
e146ae4e 2500
6a632136 2501 if (fdisk_is_readonly(cf->cxt)) {
d4640320 2502 warn = _("Device is open in read-only mode.");
e146ae4e
KZ
2503 break;
2504 }
2505
ffab025d 2506 rc = ui_get_string(
7b1ffbc4
KZ
2507 _("Are you sure you want to write the partition "
2508 "table to disk? "),
09af3db4 2509 _("Type \"yes\" or \"no\", or press ESC to leave this dialog."),
7b1ffbc4
KZ
2510 buf, sizeof(buf));
2511
2512 ref = 1;
7efdcd75 2513 if (rc <= 0 || (c_strcasecmp(buf, "yes") != 0 &&
59af21c3 2514 strcasecmp(buf, _("yes")) != 0)) {
d4640320 2515 info = _("Did not write partition table to disk.");
7b1ffbc4
KZ
2516 break;
2517 }
ed8a13e6
KZ
2518 rc = fdisk_write_disklabel(cf->cxt);
2519 if (rc)
d4640320 2520 warn = _("Failed to write disklabel.");
7b1ffbc4 2521 else {
b0d4d093
KZ
2522 size_t q_idx = 0;
2523
044ebc0c
KZ
2524 if (cf->device_is_used)
2525 fdisk_reread_changes(cf->cxt, cf->original_layout);
2526 else
2527 fdisk_reread_partition_table(cf->cxt);
7b1ffbc4 2528 info = _("The partition table has been altered.");
b0d4d093
KZ
2529 if (menu_get_menuitem_by_key(cf, 'q', &q_idx))
2530 ui_menu_goto(cf, q_idx);
7b1ffbc4 2531 }
bc787727 2532 cf->nwrites++;
7b1ffbc4
KZ
2533 break;
2534 }
2535 default:
ed8a13e6
KZ
2536 break;
2537 }
2538
2539 if (ref) {
2540 lines_refresh(cf);
2541 ui_refresh(cf);
3542fc51 2542 ui_draw_extra(cf);
a89eafed
KZ
2543 } else
2544 ui_draw_menu(cf);
7b1ffbc4
KZ
2545
2546 ui_clean_hint();
75090201 2547
ed8a13e6 2548 if (warn)
7b1ffbc4 2549 ui_warnx(warn, n + 1);
ed8a13e6 2550 else if (info)
7b1ffbc4 2551 ui_info(info, n + 1);
d5314bf5
KZ
2552 else if (key == 'n' && cf->wrong_order && org_order == 0)
2553 ui_info(_("Note that partition table entries are not in disk order now."));
ed8a13e6
KZ
2554
2555 return 0;
2556}
2557
7556c944
KZ
2558static void ui_resize_refresh(struct cfdisk *cf)
2559{
2f4eb047 2560 DBG(UI, ul_debug("ui resize/refresh"));
7556c944
KZ
2561 resize();
2562 menu_refresh_size(cf);
2563 lines_refresh(cf);
2564 ui_refresh(cf);
3542fc51 2565 ui_draw_extra(cf);
7556c944
KZ
2566}
2567
a2d0dbb4
KZ
2568static void toggle_show_extra(struct cfdisk *cf)
2569{
2570 if (cf->show_extra && cf->act_win) {
2571 wclear(cf->act_win);
2572 touchwin(stdscr);
2573 }
2574 cf->show_extra = cf->show_extra ? 0 : 1;
2575
2576 if (cf->show_extra)
2577 ui_draw_extra(cf);
2578 DBG(MENU, ul_debug("extra: %s", cf->show_extra ? "ENABLED" : "DISABLED" ));
2579}
2580
8c3a5a44
KZ
2581static int ui_run(struct cfdisk *cf)
2582{
7aa0d529 2583 int rc = 0;
6dbe3af9 2584
eef63970
KZ
2585 ui_lines = LINES;
2586 ui_cols = COLS;
2587 DBG(UI, ul_debug("start cols=%zu, lines=%zu", ui_cols, ui_lines));
6dbe3af9 2588
97ffbd43 2589 if (fdisk_get_collision(cf->cxt)) {
ad6d9fd5
KZ
2590 ui_warnx(_("Device already contains a %s signature."), fdisk_get_collision(cf->cxt));
2591 if (fdisk_is_readonly(cf->cxt)) {
2592 ui_hint(_("Press a key to continue."));
2593 getch();
2594 } else {
2595 char buf[64] = { 0 };
2596 rc = ui_get_string(_("Do you want to remove it? [Y]es/[N]o: "), NULL,
2597 buf, sizeof(buf));
2598 fdisk_enable_wipe(cf->cxt,
2599 rc > 0 && rpmatch(buf) == RPMATCH_YES ? 1 : 0);
2600 }
97ffbd43
KZ
2601 }
2602
aa36c2cf 2603 if (!fdisk_has_label(cf->cxt) || cf->zero_start) {
7aa0d529 2604 rc = ui_create_label(cf);
134b6296
KZ
2605 if (rc < 0) {
2606 errno = -rc;
2607 ui_err(EXIT_FAILURE,
7aa0d529 2608 _("failed to create a new disklabel"));
134b6296 2609 }
7aa0d529
KZ
2610 if (rc)
2611 return rc;
2612 }
2613
2614 cols_init(cf);
2615 rc = lines_refresh(cf);
2616 if (rc)
2617 ui_errx(EXIT_FAILURE, _("failed to read partitions"));
6dbe3af9 2618
3b411726 2619 menu_push(cf, main_menuitems);
63128bbf
KZ
2620 cf->menu->ignore_cb = main_menu_ignore_keys;
2621
8c3a5a44
KZ
2622 rc = ui_refresh(cf);
2623 if (rc)
2624 return rc;
6dbe3af9 2625
dda7fe12 2626 cf->show_extra = 1;
3542fc51 2627 ui_draw_extra(cf);
dda7fe12 2628
6a632136 2629 if (fdisk_is_readonly(cf->cxt))
0d182490 2630 ui_warnx(_("Device is open in read-only mode. Changes will remain in memory only."));
d527e8cf
KZ
2631 else if (cf->device_is_used)
2632 ui_warnx(_("Device is currently in use, repartitioning is probably a bad idea."));
f9fb8290 2633 else if (cf->wrong_order)
a2d0dbb4 2634 ui_info(_("Note that partition table entries are not in disk order now."));
e146ae4e 2635
502c5186 2636 while (!sig_die) {
7ee26cbf 2637 int key = getch();
6dbe3af9 2638
7ee26cbf 2639 rc = 0;
502c5186
KZ
2640
2641 if (sig_die)
2642 break;
c431d901 2643 if (sig_resize)
7556c944
KZ
2644 /* Note that ncurses getch() returns ERR when interrupted
2645 * by signal, but SLang does not interrupt at all. */
2646 ui_resize_refresh(cf);
2647 if (key == ERR)
2648 continue;
2f4eb047
KZ
2649 if (key == '\014') { /* ^L refresh */
2650 ui_resize_refresh(cf);
2651 continue;
2652 }
ac27ea5c
KZ
2653 if (ui_menu_move(cf, key) == 0)
2654 continue;
2655
2f4eb047 2656 DBG(UI, ul_debug("main action key >%1$c< [\\0%1$o].", key));
f019ce1f 2657
8c3a5a44
KZ
2658 switch (key) {
2659 case KEY_DOWN:
2660 case '\016': /* ^N */
2661 case 'j': /* Vi-like alternative */
2662 ui_table_goto(cf, cf->lines_idx + 1);
2663 break;
2664 case KEY_UP:
2665 case '\020': /* ^P */
2666 case 'k': /* Vi-like alternative */
f019ce1f 2667 ui_table_goto(cf, (int) cf->lines_idx - 1);
8c3a5a44 2668 break;
f019ce1f
KZ
2669 case KEY_PPAGE:
2670 if (cf->page_sz) {
2671 ui_table_goto(cf, (int) cf->lines_idx - cf->page_sz);
2672 break;
2673 }
b77025b6 2674 FALLTHROUGH;
8c3a5a44
KZ
2675 case KEY_HOME:
2676 ui_table_goto(cf, 0);
2677 break;
f019ce1f
KZ
2678 case KEY_NPAGE:
2679 if (cf->page_sz) {
2680 ui_table_goto(cf, cf->lines_idx + cf->page_sz);
2681 break;
2682 }
b77025b6 2683 FALLTHROUGH;
8c3a5a44 2684 case KEY_END:
f019ce1f 2685 ui_table_goto(cf, (int) cf->nlines - 1);
8c3a5a44 2686 break;
8c3a5a44
KZ
2687 case KEY_ENTER:
2688 case '\n':
2689 case '\r':
ed8a13e6 2690 rc = main_menu_action(cf, 0);
8c3a5a44 2691 break;
80351b1f 2692 case 'X':
3542fc51 2693 case 'x': /* Extra */
a2d0dbb4 2694 toggle_show_extra(cf);
3542fc51 2695 break;
8c3a5a44 2696 default:
ed8a13e6 2697 rc = main_menu_action(cf, key);
8460875d 2698 if (rc < 0)
8c3a5a44
KZ
2699 beep();
2700 break;
2701 }
8460875d
KZ
2702
2703 if (rc == 1)
2704 break; /* quit */
502c5186 2705 }
6dbe3af9 2706
8c3a5a44 2707 menu_pop(cf);
6dbe3af9 2708
14620822 2709 DBG(UI, ul_debug("end"));
8c3a5a44
KZ
2710 return 0;
2711}
6dbe3af9 2712
86be6a32 2713static void __attribute__((__noreturn__)) usage(void)
04915c3f 2714{
86be6a32 2715 FILE *out = stdout;
04915c3f 2716 fputs(USAGE_HEADER, out);
04915c3f
KZ
2717 fprintf(out,
2718 _(" %1$s [options] <disk>\n"), program_invocation_short_name);
2719
451dbcfa
BS
2720 fputs(USAGE_SEPARATOR, out);
2721 fputs(_("Display or manipulate a disk partition table.\n"), out);
2722
04915c3f 2723 fputs(USAGE_OPTIONS, out);
a7466bdc
SK
2724 fprintf(out,
2725 _(" -L, --color[=<when>] colorize output (%s, %s or %s)\n"), "auto", "always", "never");
5d51dc2a
KZ
2726 fprintf(out,
2727 " %s\n", USAGE_COLORS_DEFAULT);
2728 fputs(_(" -z, --zero start with zeroed partition table\n"), out);
ec8f7121
KZ
2729 fprintf(out,
2730 _(" --lock[=<mode>] use exclusive device lock (%s, %s or %s)\n"), "yes", "no", "nonblock");
0d182490 2731 fputs(_(" -r, --read-only forced open cfdisk in read-only mode\n"), out);
04915c3f 2732
fd38ee22
KZ
2733 fputs(_(" -b, --sector-size <size> physical and logical sector size\n"), out);
2734
04915c3f 2735 fputs(USAGE_SEPARATOR, out);
bad4c729 2736 fprintf(out, USAGE_HELP_OPTIONS(26));
04915c3f 2737
bad4c729 2738 fprintf(out, USAGE_MAN_TAIL("cfdisk(8)"));
86be6a32 2739 exit(EXIT_SUCCESS);
04915c3f
KZ
2740}
2741
8c3a5a44
KZ
2742int main(int argc, char *argv[])
2743{
ec8f7121 2744 const char *diskpath = NULL, *lockmode = NULL;
ad9fe47e 2745 int rc, c, colormode = UL_COLORMODE_UNDEF;
0d182490 2746 int read_only = 0;
fd38ee22 2747 size_t user_ss = 0;
8c3a5a44
KZ
2748 struct cfdisk _cf = { .lines_idx = 0 },
2749 *cf = &_cf;
ec8f7121
KZ
2750 enum {
2751 OPT_LOCK = CHAR_MAX + 1
2752 };
04915c3f
KZ
2753 static const struct option longopts[] = {
2754 { "color", optional_argument, NULL, 'L' },
ec8f7121 2755 { "lock", optional_argument, NULL, OPT_LOCK },
04915c3f 2756 { "help", no_argument, NULL, 'h' },
fd38ee22 2757 { "sector-size", required_argument, NULL, 'b' },
04915c3f 2758 { "version", no_argument, NULL, 'V' },
d121efdb 2759 { "zero", no_argument, NULL, 'z' },
0d182490 2760 { "read-only", no_argument, NULL, 'r' },
87918040 2761 { NULL, 0, NULL, 0 },
04915c3f
KZ
2762 };
2763
8c3a5a44
KZ
2764 setlocale(LC_ALL, "");
2765 bindtextdomain(PACKAGE, LOCALEDIR);
2766 textdomain(PACKAGE);
2c308875 2767 close_stdout_atexit();
6dbe3af9 2768
fd38ee22 2769 while((c = getopt_long(argc, argv, "b:L::hVzr", longopts, NULL)) != -1) {
04915c3f 2770 switch(c) {
fd38ee22
KZ
2771 case 'b':
2772 user_ss = strtou32_or_err(optarg,
2773 _("invalid sector size argument"));
2774 if (user_ss != 512 && user_ss != 1024 &&
2775 user_ss != 2048 && user_ss != 4096)
2776 errx(EXIT_FAILURE, _("invalid sector size argument"));
2777 break;
04915c3f 2778 case 'h':
86be6a32 2779 usage();
04915c3f
KZ
2780 break;
2781 case 'L':
2782 colormode = UL_COLORMODE_AUTO;
2783 if (optarg)
42b60bce 2784 colormode = colormode_or_err(optarg);
04915c3f 2785 break;
0d182490
DC
2786 case 'r':
2787 read_only = 1;
2788 break;
04915c3f 2789 case 'V':
2c308875 2790 print_version(EXIT_SUCCESS);
d121efdb
KZ
2791 case 'z':
2792 cf->zero_start = 1;
2793 break;
ec8f7121
KZ
2794 case OPT_LOCK:
2795 lockmode = "1";
2796 if (optarg) {
2797 if (*optarg == '=')
2798 optarg++;
2799 lockmode = optarg;
2800 }
2801 break;
677ec86c
KZ
2802 default:
2803 errtryhelp(EXIT_FAILURE);
04915c3f
KZ
2804 }
2805 }
2806
210bb492 2807 colors_init(colormode, "cfdisk");
04915c3f 2808
8c3a5a44 2809 fdisk_init_debug(0);
710ed55d 2810 scols_init_debug(0);
14620822 2811 cfdisk_init_debug();
8c3a5a44
KZ
2812 cf->cxt = fdisk_new_context();
2813 if (!cf->cxt)
2814 err(EXIT_FAILURE, _("failed to allocate libfdisk context"));
fd38ee22
KZ
2815 if (user_ss)
2816 fdisk_save_user_sector_size(cf->cxt, user_ss, user_ss);
6dbe3af9 2817
6a632136 2818 fdisk_set_ask(cf->cxt, ask_callback, (void *) cf);
6dbe3af9 2819
6e51ab0c
KZ
2820 if (optind == argc) {
2821 size_t i;
2822
2823 for (i = 0; i < ARRAY_SIZE(default_disks); i++) {
2824 if (access(default_disks[i], F_OK) == 0) {
2825 diskpath = default_disks[i];
2826 break;
2827 }
2828 }
2829 if (!diskpath)
2830 diskpath = default_disks[0]; /* default, used for "cannot open" */
2831 } else
a4f0a42d 2832 diskpath = argv[optind];
6dbe3af9 2833
0d182490
DC
2834 rc = fdisk_assign_device(cf->cxt, diskpath, read_only);
2835 if (rc == -EACCES && read_only == 0)
6a632136 2836 rc = fdisk_assign_device(cf->cxt, diskpath, 1);
e146ae4e 2837 if (rc != 0)
6e51ab0c 2838 err(EXIT_FAILURE, _("cannot open %s"), diskpath);
6dbe3af9 2839
044ebc0c 2840 if (!fdisk_is_readonly(cf->cxt)) {
ec8f7121
KZ
2841 if (blkdev_lock(fdisk_get_devfd(cf->cxt), diskpath, lockmode) != 0)
2842 return EXIT_FAILURE;
2843
044ebc0c
KZ
2844 cf->device_is_used = fdisk_device_is_used(cf->cxt);
2845 fdisk_get_partitions(cf->cxt, &cf->original_layout);
2846 }
2847
8c3a5a44
KZ
2848 /* Don't use err(), warn() from this point */
2849 ui_init(cf);
2850 ui_run(cf);
7aa0d529 2851 ui_end();
6dbe3af9 2852
dda7fe12 2853 cfdisk_free_lines(cf);
8c3a5a44 2854 free(cf->linesbuf);
41994e00 2855 free(cf->fields);
ed8a13e6 2856
dda7fe12
OO
2857 fdisk_unref_table(cf->table);
2858#ifdef HAVE_LIBMOUNT
2859 mnt_unref_table(cf->fstab);
2860 mnt_unref_table(cf->mtab);
2861 mnt_unref_cache(cf->mntcache);
2862#endif
6a632136 2863 rc = fdisk_deassign_device(cf->cxt, cf->nwrites == 0);
c7119037 2864 fdisk_unref_context(cf->cxt);
14620822 2865 DBG(MISC, ul_debug("bye! [rc=%d]", rc));
ed8a13e6 2866 return rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
6dbe3af9 2867}