int main(int argc, char *argv[]) { static const struct option longopts[] = { { "colors", optional_argument, 0, 'c' }, { NULL, 0, 0, 0 } }; int c, mode = UL_COLORMODE_NEVER; /* default */ while ((c = getopt_long(argc, argv, "c::", longopts, NULL)) != -1) { switch (c) { case 'c': mode = UL_COLORMODE_AUTO; if (optarg) { mode = colormode_or_err(optarg, "unsupported color mode"); printf("%d\n", mode); } printf("%s\n", optarg); break; } } colors_init(mode); color_enable(UL_COLOR_RED); printf("Hello World!\n"); color_disable(); color_enable(UL_COLOR_GREEN); printf("Hello World!\n"); color_disable(); return EXIT_SUCCESS; }
static void backup_partition_table(struct sfdisk *sf, const char *devname) { const char *name; char *tpl; uint64_t offset = 0; size_t size = 0; int i = 0; assert(sf); if (!fdisk_has_label(sf->cxt)) return; if (!sf->backup_file) { /* initialize default backup filename */ const char *home = getenv ("HOME"); if (!home) errx(EXIT_FAILURE, _("failed to create a signature backup, $HOME undefined")); xasprintf(&tpl, "%s/sfdisk-%s-", home, basename(devname)); } else xasprintf(&tpl, "%s-%s-", sf->backup_file, basename(devname)); color_scheme_enable("header", UL_COLOR_BOLD); fdisk_info(sf->cxt, _("Backup files:")); color_disable(); while (fdisk_locate_disklabel(sf->cxt, i++, &name, &offset, &size) == 0 && size) backup_sectors(sf, tpl, name, devname, offset, size); if (!sf->quiet) fputc('\n', stdout); free(tpl); }
void list_disk_geometry(struct fdisk_context *cxt) { char *id = NULL; struct fdisk_label *lb = fdisk_get_label(cxt, NULL); uint64_t bytes = fdisk_get_nsectors(cxt) * fdisk_get_sector_size(cxt); char *strsz = size_to_human_string(SIZE_SUFFIX_SPACE | SIZE_SUFFIX_3LETTER, bytes); color_scheme_enable("header", UL_COLOR_BOLD); fdisk_info(cxt, _("Disk %s: %s, %ju bytes, %ju sectors"), fdisk_get_devname(cxt), strsz, bytes, (uintmax_t) fdisk_get_nsectors(cxt)); color_disable(); free(strsz); if (lb && (fdisk_label_require_geometry(lb) || fdisk_use_cylinders(cxt))) fdisk_info(cxt, _("Geometry: %d heads, %llu sectors/track, %llu cylinders"), fdisk_get_geom_heads(cxt), fdisk_get_geom_sectors(cxt), fdisk_get_geom_cylinders(cxt)); fdisk_info(cxt, _("Units: %s of %d * %ld = %ld bytes"), fdisk_get_unit(cxt, FDISK_PLURAL), fdisk_get_units_per_sector(cxt), fdisk_get_sector_size(cxt), fdisk_get_units_per_sector(cxt) * fdisk_get_sector_size(cxt)); fdisk_info(cxt, _("Sector size (logical/physical): %lu bytes / %lu bytes"), fdisk_get_sector_size(cxt), fdisk_get_physector_size(cxt)); fdisk_info(cxt, _("I/O size (minimum/optimal): %lu bytes / %lu bytes"), fdisk_get_minimal_iosize(cxt), fdisk_get_optimal_iosize(cxt)); if (fdisk_get_alignment_offset(cxt)) fdisk_info(cxt, _("Alignment offset: %lu bytes"), fdisk_get_alignment_offset(cxt)); if (fdisk_has_label(cxt)) fdisk_info(cxt, _("Disklabel type: %s"), fdisk_label_get_name(lb)); if (fdisk_get_disklabel_id(cxt, &id) == 0 && id) fdisk_info(cxt, _("Disk identifier: %s"), id); }
static void colors_debug(struct ul_color_ctl *cc) { size_t i; if (!cc) return; printf("Colors:\n"); printf("\tutilname = '%s'\n", cc->utilname); printf("\ttermname = '%s'\n", cc->termname); printf("\tscheme file = '%s'\n", cc->sfile); printf("\tmode = %s\n", cc->mode == UL_COLORMODE_UNDEF ? "undefined" : cc->mode == UL_COLORMODE_AUTO ? "auto" : cc->mode == UL_COLORMODE_NEVER ? "never" : cc->mode == UL_COLORMODE_ALWAYS ? "always" : "???"); printf("\thas_colors = %d\n", cc->has_colors); printf("\tdisabled = %d\n", cc->disabled); printf("\tconfigured = %d\n", cc->configured); printf("\tcs configured = %d\n", cc->cs_configured); fputc('\n', stdout); for (i = 0; i < ARRAY_SIZE(cc->scores); i++) printf("\tscore %s = %d\n", i == UL_COLORFILE_DISABLE ? "disable" : i == UL_COLORFILE_ENABLE ? "enable" : i == UL_COLORFILE_SCHEME ? "scheme" : "???", cc->scores[i]); fputc('\n', stdout); for (i = 0; i < cc->nschemes; i++) { printf("\tscheme #%02zu ", i); color_scheme_enable(cc->schemes[i].name, NULL); fputs(cc->schemes[i].name, stdout); color_disable(); fputc('\n', stdout); } fputc('\n', stdout); }
static int verify_device(struct sfdisk *sf, const char *devname) { int rc = 1; fdisk_enable_listonly(sf->cxt, 1); if (fdisk_assign_device(sf->cxt, devname, 1)) { warn(_("cannot open %s"), devname); return 1; } color_scheme_enable("header", UL_COLOR_BOLD); fdisk_info(sf->cxt, "%s:", devname); color_disable(); if (!fdisk_has_label(sf->cxt)) fdisk_info(sf->cxt, _("unrecognized partition table type")); else rc = fdisk_verify_disklabel(sf->cxt); fdisk_deassign_device(sf->cxt, 1); return rc; }
int main(int argc, char **argv) { int i, c, act = ACT_FDISK; int colormode = UL_COLORMODE_AUTO; struct fdisk_context *cxt; setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); atexit(close_stdout); fdisk_init_debug(0); cxt = fdisk_new_context(); if (!cxt) err(EXIT_FAILURE, _("failed to allocate libfdisk context")); fdisk_context_set_ask(cxt, ask_callback, NULL); while ((c = getopt(argc, argv, "b:c::C:hH:lL::sS:t:u::vV")) != -1) { switch (c) { case 'b': { size_t sz = strtou32_or_err(optarg, _("invalid sector size argument")); if (sz != 512 && sz != 1024 && sz != 2048 && sz != 4096) usage(stderr); fdisk_save_user_sector_size(cxt, sz, sz); break; } case 'C': fdisk_save_user_geometry(cxt, strtou32_or_err(optarg, _("invalid cylinders argument")), 0, 0); break; case 'c': if (optarg) { /* this setting is independent on the current * actively used label */ struct fdisk_label *lb = fdisk_context_get_label(cxt, "dos"); if (!lb) err(EXIT_FAILURE, _("not found DOS label driver")); if (strcmp(optarg, "=dos") == 0) fdisk_dos_enable_compatible(lb, TRUE); else if (strcmp(optarg, "=nondos") == 0) fdisk_dos_enable_compatible(lb, FALSE); else usage(stderr); } /* use default if no optarg specified */ break; case 'H': fdisk_save_user_geometry(cxt, 0, strtou32_or_err(optarg, _("invalid heads argument")), 0); break; case 'S': fdisk_save_user_geometry(cxt, 0, 0, strtou32_or_err(optarg, _("invalid sectors argument"))); break; case 'l': act = ACT_LIST; break; case 'L': if (optarg) colormode = colormode_or_err(optarg, _("unsupported color mode")); break; case 's': act = ACT_SHOWSIZE; break; case 't': { struct fdisk_label *lb = NULL; while (fdisk_context_next_label(cxt, &lb) == 0) fdisk_label_set_disabled(lb, 1); lb = fdisk_context_get_label(cxt, optarg); if (!lb) errx(EXIT_FAILURE, _("unsupported disklabel: %s"), optarg); fdisk_label_set_disabled(lb, 0); } case 'u': if (optarg && *optarg == '=') optarg++; if (fdisk_context_set_unit(cxt, optarg) != 0) usage(stderr); break; case 'V': case 'v': printf(UTIL_LINUX_VERSION); return EXIT_SUCCESS; case 'h': usage(stdout); default: usage(stderr); } } if (argc-optind != 1 && fdisk_has_user_device_properties(cxt)) warnx(_("The device properties (sector size and geometry) should" " be used with one specified device only.")); colors_init(colormode); switch (act) { case ACT_LIST: fdisk_context_enable_listonly(cxt, 1); if (argc > optind) { int k; for (k = optind; k < argc; k++) print_device_pt(cxt, argv[k]); } else print_all_devices_pt(cxt); break; case ACT_SHOWSIZE: /* deprecated */ if (argc - optind <= 0) usage(stderr); for (i = optind; i < argc; i++) { if (argc - optind == 1) printf("%llu\n", get_dev_blocks(argv[i])); else printf("%s: %llu\n", argv[i], get_dev_blocks(argv[i])); } break; case ACT_FDISK: if (argc-optind != 1) usage(stderr); if (fdisk_context_assign_device(cxt, argv[optind], 0) != 0) err(EXIT_FAILURE, _("cannot open %s"), argv[optind]); /* Here starts interactive mode, use fdisk_{warn,info,..} functions */ color_enable(UL_COLOR_GREEN); fdisk_info(cxt, _("Welcome to fdisk (%s)."), PACKAGE_STRING); color_disable(); fdisk_info(cxt, _("Changes will remain in memory only, until you decide to write them.\n" "Be careful before using the write command.\n")); fflush(stdout); if (!fdisk_dev_has_disklabel(cxt)) { fdisk_warnx(cxt, _("Device does not contain a recognized partition table.")); fdisk_create_disklabel(cxt, NULL); } while (1) process_fdisk_menu(&cxt); } fdisk_free_context(cxt); return EXIT_SUCCESS; }
void list_freespace(struct fdisk_context *cxt) { struct fdisk_table *tb = NULL; struct fdisk_partition *pa = NULL; struct fdisk_iter *itr = NULL; struct libscols_table *out = NULL; const char *bold = NULL; size_t i; uintmax_t sumsize = 0, bytes = 0; char *strsz; static const char *colnames[] = { N_("Start"), N_("End"), N_("Sectors"), N_("Size") }; static const int colids[] = { FDISK_FIELD_START, FDISK_FIELD_END, FDISK_FIELD_SECTORS, FDISK_FIELD_SIZE }; if (fdisk_get_freespaces(cxt, &tb)) goto done; itr = fdisk_new_iter(FDISK_ITER_FORWARD); if (!itr) { fdisk_warn(cxt, _("failed to allocate iterator")); goto done; } out = scols_new_table(); if (!out) { fdisk_warn(cxt, _("failed to allocate output table")); goto done; } if (colors_wanted()) { scols_table_enable_colors(out, 1); bold = color_scheme_get_sequence("header", UL_COLOR_BOLD); } for (i = 0; i < ARRAY_SIZE(colnames); i++) { struct libscols_column *co = scols_table_new_column(out, _(colnames[i]), 5, SCOLS_FL_RIGHT); if (!co) goto done; if (bold) scols_cell_set_color(scols_column_get_header(co), bold); } /* fill-in output table */ while (fdisk_table_next_partition(tb, itr, &pa) == 0) { struct libscols_line *ln = scols_table_new_line(out, NULL); char *data; if (!ln) { fdisk_warn(cxt, _("failed to allocate output line")); goto done; } for (i = 0; i < ARRAY_SIZE(colids); i++) { if (fdisk_partition_to_string(pa, cxt, colids[i], &data)) continue; scols_line_refer_data(ln, i, data); } if (fdisk_partition_has_size(pa)) sumsize += fdisk_partition_get_size(pa); } bytes = sumsize * fdisk_get_sector_size(cxt); strsz = size_to_human_string(SIZE_SUFFIX_SPACE | SIZE_SUFFIX_3LETTER, bytes); color_scheme_enable("header", UL_COLOR_BOLD); fdisk_info(cxt, _("Unpartitioned space %s: %s, %ju bytes, %ju sectors"), fdisk_get_devname(cxt), strsz, bytes, sumsize); color_disable(); free(strsz); fdisk_info(cxt, _("Units: %s of %d * %ld = %ld bytes"), fdisk_get_unit(cxt, FDISK_PLURAL), fdisk_get_units_per_sector(cxt), fdisk_get_sector_size(cxt), fdisk_get_units_per_sector(cxt) * fdisk_get_sector_size(cxt)); fdisk_info(cxt, _("Sector size (logical/physical): %lu bytes / %lu bytes"), fdisk_get_sector_size(cxt), fdisk_get_physector_size(cxt)); /* print */ if (!scols_table_is_empty(out)) { fdisk_info(cxt, ""); /* line break */ scols_print_table(out); } done: scols_unref_table(out); fdisk_unref_table(tb); fdisk_free_iter(itr); }