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