static void sfdisk_init(struct sfdisk *sf) { fdisk_init_debug(0); sfdiskprog_init_debug(); sf->cxt = fdisk_new_context(); if (!sf->cxt) err(EXIT_FAILURE, _("failed to allocate libfdisk context")); fdisk_set_ask(sf->cxt, ask_callback, (void *) sf); if (sf->label_nested) { struct fdisk_context *x = fdisk_new_nested_context(sf->cxt, sf->label_nested); if (!x) err(EXIT_FAILURE, _("failed to allocate nested libfdisk context")); /* the original context is available by fdisk_get_parent() */ sf->cxt = x; } }
static int test_listitems(struct fdisk_test *ts, int argc, char *argv[]) { const char *disk = argv[1]; struct fdisk_context *cxt; struct fdisk_labelitem *item; int i = 0, rc; cxt = fdisk_new_context(); item = fdisk_new_labelitem(); fdisk_assign_device(cxt, disk, 1); do { rc = fdisk_get_disklabel_item(cxt, i++, item); switch (rc) { case 0: /* success */ { const char *name = fdisk_labelitem_get_name(item); const char *str; uint64_t num; if (fdisk_labelitem_is_string(item) && fdisk_labelitem_get_data_string(item, &str) == 0) printf("%s: %s\n", name, str); else if (fdisk_labelitem_get_data_u64(item, &num) == 0) printf("%s: %ju\n", name, num); break; } case 1: /* item unsuported by label -- ignore */ rc = 0; break; case 2: /* end (out of range) */ break; default: /* error */ break; } } while (rc == 0); fdisk_unref_labelitem(item); fdisk_unref_context(cxt); return rc < 0 ? rc : 0; }
/* usable for example in usage() */ void list_available_columns(FILE *out) { size_t i; int termwidth; struct fdisk_label *lb = NULL; struct fdisk_context *cxt = fdisk_new_context(); if (!cxt) return; termwidth = get_terminal_width(); if (termwidth <= 0) termwidth = 80; fprintf(out, _("\nAvailable columns (for -o):\n")); while (fdisk_next_label(cxt, &lb) == 0) { size_t width = 6; /* label name and separators */ fprintf(out, " %s:", fdisk_label_get_name(lb)); for (i = 1; i < FDISK_NFIELDS; i++) { const struct fdisk_field *fl = fdisk_label_get_field(lb, i); const char *name = fl ? fdisk_field_get_name(fl) : NULL; size_t len; if (!name) continue; len = strlen(name) + 1; if (width + len > (size_t) termwidth) { fputs("\n ", out); width = 6; } fprintf(out, " %s", name); width += len; } fputc('\n', out); } fdisk_unref_context(cxt); }
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; }