/* add a new line to @tb, the content is based on @st */ static int add_line_from_stat(struct libscols_table *tb, struct libscols_line *parent, int parent_fd, struct stat *st, const char *name) { struct libscols_line *ln; char modbuf[11], *p; mode_t mode = st->st_mode; int rc = 0; ln = scols_table_new_line(tb, parent); if (!ln) err(EXIT_FAILURE, "failed to create output line"); /* MODE; local buffer, use scols_line_set_data() that calls strdup() */ xstrmode(mode, modbuf); if (scols_line_set_data(ln, COL_MODE, modbuf)) goto fail; /* SIZE; already allocated string, use scols_line_refer_data() */ p = size_to_human_string(0, st->st_size); if (!p || scols_line_refer_data(ln, COL_SIZE, p)) goto fail; /* NAME */ if (scols_line_set_data(ln, COL_NAME, name)) goto fail; /* colors */ if (scols_table_colors_wanted(tb)) { struct libscols_cell *ce = scols_line_get_cell(ln, COL_NAME); if (S_ISDIR(mode)) scols_cell_set_color(ce, "blue"); else if (S_ISLNK(mode)) scols_cell_set_color(ce, "cyan"); else if (S_ISBLK(mode)) scols_cell_set_color(ce, "magenta"); else if ((mode & S_IXOTH) || (mode & S_IXGRP) || (mode & S_IXUSR)) scols_cell_set_color(ce, "green"); } if (S_ISDIR(st->st_mode)) { int fd; if (parent_fd >= 0) fd = openat(parent_fd, name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC); else fd = open(name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC); if (fd >= 0) { rc = add_children(tb, ln, fd); close(fd); } } return rc; fail: err(EXIT_FAILURE, "failed to create cell data"); return -1; }
int main(int argc, char *argv[]) { struct libscols_table *tb; struct libscols_symbols *sy; struct libscols_cell *title; setlocale(LC_ALL, ""); /* just to have enable UTF8 chars */ scols_init_debug(0); tb = scols_new_table(); if (!tb) err(EXIT_FAILURE, "failed to create output table"); scols_table_enable_colors(tb, isatty(STDOUT_FILENO)); setup_columns(tb); add_line(tb, "foo", "bla bla bla"); add_line(tb, "bar", "alb alb alb"); title = scols_table_get_title(tb); /* right */ scols_cell_set_data(title, "This is right title"); scols_cell_set_color(title, "red"); scols_cell_set_flags(title, SCOLS_CELL_FL_RIGHT); scols_print_table(tb); /* center */ sy = scols_new_symbols(); if (!sy) err_oom(); scols_table_set_symbols(tb, sy); scols_symbols_set_title_padding(sy, "="); scols_cell_set_data(title, "This is center title (with padding)"); scols_cell_set_color(title, "green"); scols_cell_set_flags(title, SCOLS_CELL_FL_CENTER); scols_print_table(tb); /* left */ scols_symbols_set_title_padding(sy, "-"); scols_cell_set_data(title, "This is left title (with padding)"); scols_cell_set_color(title, "blue"); scols_cell_set_flags(title, SCOLS_CELL_FL_LEFT); scols_print_table(tb); scols_unref_table(tb); return EXIT_SUCCESS; }
void list_disklabel(struct fdisk_context *cxt) { struct fdisk_table *tb = NULL; struct fdisk_partition *pa = NULL; struct fdisk_iter *itr = NULL; struct fdisk_label *lb; struct libscols_table *out = NULL; const char *bold = NULL; int *ids = NULL; /* IDs of fdisk_fields */ size_t nids = 0, i; int post = 0; /* print label specific stuff by libfdisk FDISK_ASK_INFO API */ fdisk_list_disklabel(cxt); /* get partitions and generate output */ if (fdisk_get_partitions(cxt, &tb) || fdisk_table_get_nents(tb) <= 0) goto done; ids = init_fields(cxt, NULL, &nids); if (!ids) goto done; itr = fdisk_new_iter(FDISK_ITER_FORWARD); if (!itr) { fdisk_warn(cxt, _("faild to allocate iterator")); goto done; } out = scols_new_table(); if (!out) { fdisk_warn(cxt, _("faild to allocate output table")); goto done; } if (colors_wanted()) { scols_table_enable_colors(out, 1); bold = color_scheme_get_sequence("header", UL_COLOR_BOLD); } lb = fdisk_get_label(cxt, NULL); assert(lb); /* define output table columns */ for (i = 0; i < nids; i++) { int fl = 0; struct libscols_column *co; const struct fdisk_field *field = fdisk_label_get_field(lb, ids[i]); if (!field) continue; if (fdisk_field_is_number(field)) fl |= SCOLS_FL_RIGHT; if (fdisk_field_get_id(field) == FDISK_FIELD_TYPE) fl |= SCOLS_FL_TRUNC; co = scols_table_new_column(out, fdisk_field_get_name(field), fdisk_field_get_width(field), fl); if (!co) goto done; /* set colum header color */ 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); if (!ln) { fdisk_warn(cxt, _("faild to allocate output line")); goto done; } for (i = 0; i < nids; i++) { char *data = NULL; if (fdisk_partition_to_string(pa, cxt, ids[i], &data)) continue; scols_line_refer_data(ln, i, data); } } /* print */ if (!scols_table_is_empty(out)) { fputc('\n', stdout); scols_print_table(out); } /* print warnings */ fdisk_reset_iter(itr, FDISK_ITER_FORWARD); while (itr && fdisk_table_next_partition(tb, itr, &pa) == 0) { if (!fdisk_partition_has_start(pa)) continue; if (!fdisk_lba_is_phy_aligned(cxt, fdisk_partition_get_start(pa))) { if (!post) fputc('\n', stdout); fdisk_warnx(cxt, _("Partition %zu does not start on physical sector boundary."), fdisk_partition_get_partno(pa) + 1); post++; } } if (fdisk_table_wrong_order(tb)) { if (!post) fputc('\n', stdout); fdisk_info(cxt, _("Partition table entries are not in disk order.")); } done: scols_unref_table(out); fdisk_unref_table(tb); fdisk_free_iter(itr); }
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); }
int main(int argc, char *argv[]) { struct libscols_table *tb; struct libscols_symbols *sy; struct libscols_cell *title; int c; static const struct option longopts[] = { { "maxout", 0, NULL, 'm' }, { "width", 1, NULL, 'w' }, { NULL, 0, NULL, 0 }, }; setlocale(LC_ALL, ""); /* just to have enable UTF8 chars */ scols_init_debug(0); tb = scols_new_table(); if (!tb) err(EXIT_FAILURE, "failed to create output table"); while((c = getopt_long(argc, argv, "mw:", longopts, NULL)) != -1) { switch(c) { case 'm': scols_table_enable_maxout(tb, TRUE); break; case 'w': scols_table_set_termforce(tb, SCOLS_TERMFORCE_ALWAYS); scols_table_set_termwidth(tb, strtou32_or_err(optarg, "failed to parse terminal width")); break; } } scols_table_enable_colors(tb, isatty(STDOUT_FILENO)); setup_columns(tb); add_line(tb, "foo", "bla bla bla"); add_line(tb, "bar", "alb alb alb"); title = scols_table_get_title(tb); /* right */ scols_cell_set_data(title, "This is right title"); scols_cell_set_color(title, "red"); scols_cell_set_flags(title, SCOLS_CELL_FL_RIGHT); scols_print_table(tb); /* center */ sy = scols_new_symbols(); if (!sy) err_oom(); scols_table_set_symbols(tb, sy); scols_symbols_set_title_padding(sy, "="); scols_cell_set_data(title, "This is center title (with padding)"); scols_cell_set_color(title, "green"); scols_cell_set_flags(title, SCOLS_CELL_FL_CENTER); scols_print_table(tb); /* left */ scols_symbols_set_title_padding(sy, "-"); scols_cell_set_data(title, "This is left title (with padding)"); scols_cell_set_color(title, "blue"); scols_cell_set_flags(title, SCOLS_CELL_FL_LEFT); scols_print_table(tb); scols_unref_table(tb); return EXIT_SUCCESS; }