1
2 #include "fdiskP.h"
3
4 /**
5 * SECTION: field
6 * @title: Field
7 * @short_description: description of the partition fields
8 *
9 * The fdisk fields are static user-friendly descriptions of the partition. The
10 * fields are used to avoid label specific stuff in the functions that list disk
11 * partitions (e.g. fdisk -l). The field Id is the same as Id for fdisk_partition_to_string().
12 *
13 * <informalexample>
14 * <programlisting>
15 * int *ids;
16 * size_t nids;
17 * struct fdisk_partition *pa = NULL;
18 * struct fdisk_label *lb = fdisk_get_label(cxt, NULL);
19 *
20 * fdisk_label_get_fields_ids(lb, cxt, &ids, &nids);
21 *
22 * fdisk_get_partition(cxt, 0, &pa);
23 *
24 * for (i = 0; i < nids; i++) {
25 * const struct fdisk_field *field = fdisk_label_get_field(lb, ids[i]);
26 *
27 * int id = fdisk_field_get_id(fl);
28 * const char *name = fdisk_field_get_name(fl);
29 * char *data;
30 *
31 * fdisk_partition_to_string(pa, id, &data);
32 * printf("%s: %s\n", name, data);
33 * free(data);
34 * }
35 * free(ids);
36 * </programlisting>
37 * </informalexample>
38 *
39 * This example lists all information about the first partition. It will work
40 * for MBR as well as for GPT because fields are not hardcoded in the example.
41 *
42 * See also fdisk_label_get_field_by_name(), fdisk_label_get_fields_ids_all()
43 * and fdisk_label_get_fields_ids().
44 */
45
46 /**
47 * fdisk_field_get_id:
48 * @field: field instance
49 *
50 * Returns: field Id (FDISK_FIELD_*)
51 */
fdisk_field_get_id(const struct fdisk_field * field)52 int fdisk_field_get_id(const struct fdisk_field *field)
53 {
54 return field ? field->id : -EINVAL;
55 }
56
57 /**
58 * fdisk_field_get_name:
59 * @field: field instance
60 *
61 * Returns: field name
62 */
fdisk_field_get_name(const struct fdisk_field * field)63 const char *fdisk_field_get_name(const struct fdisk_field *field)
64 {
65 return field ? field->name : NULL;
66 }
67
68 /**
69 * fdisk_field_get_width:
70 * @field: field instance
71 *
72 * Returns: libsmartcols compatible width.
73 */
fdisk_field_get_width(const struct fdisk_field * field)74 double fdisk_field_get_width(const struct fdisk_field *field)
75 {
76 return field ? field->width : -EINVAL;
77 }
78
79 /**
80 * fdisk_field_is_number:
81 * @field: field instance
82 *
83 * Returns: 1 if field represent number
84 */
fdisk_field_is_number(const struct fdisk_field * field)85 int fdisk_field_is_number(const struct fdisk_field *field)
86 {
87 return field->flags ? field->flags & FDISK_FIELDFL_NUMBER : 0;
88 }
89