summaryrefslogtreecommitdiffstats
path: root/libblkid/samples/partitions.c
blob: c5830e657139cd688e671c2eb8053abd41c0a176 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
 * Copyright (C) 2009 Karel Zak <kzak@redhat.com>
 *
 * This file may be redistributed under the terms of the
 * GNU Lesser General Public License.
 */

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>

#include <blkid.h>
#include "c.h"

int main(int argc, char *argv[])
{
	int i, nparts;
	char *devname;
	blkid_probe pr;
	blkid_partlist ls;
	blkid_parttable root_tab;

	if (argc < 2) {
		fprintf(stderr, "usage: %s <device|file>  "
				"-- prints partitions\n",
				program_invocation_short_name);
		return EXIT_FAILURE;
	}

	devname = argv[1];
	pr = blkid_new_probe_from_filename(devname);
	if (!pr)
		err(EXIT_FAILURE, "%s: failed to create a new libblkid probe",
				devname);
	/* Binary interface */
	ls = blkid_probe_get_partitions(pr);
	if (!ls)
		errx(EXIT_FAILURE, "%s: failed to read partitions\n", devname);

	/*
	 * Print info about the primary (root) partition table
	 */
	root_tab = blkid_partlist_get_table(ls);
	if (!root_tab)
		errx(EXIT_FAILURE, "%s: does not contains any "
				 "known partition table\n", devname);

	printf("size: %jd, sector size: %u, PT: %s, offset: %jd, id=%s\n---\n",
		(intmax_t)blkid_probe_get_size(pr),
		blkid_probe_get_sectorsize(pr),
		blkid_parttable_get_type(root_tab),
		(intmax_t)blkid_parttable_get_offset(root_tab),
		blkid_parttable_get_id(root_tab));

	/*
	 * List partitions
	 */
	nparts = blkid_partlist_numof_partitions(ls);
	if (!nparts)
		goto done;

	for (i = 0; i < nparts; i++) {
		const char *p;
		blkid_partition par = blkid_partlist_get_partition(ls, i);
		blkid_parttable tab = blkid_partition_get_table(par);

		printf("#%d: %10llu %10llu  0x%x",
			blkid_partition_get_partno(par),
			(unsigned long long) blkid_partition_get_start(par),
			(unsigned long long) blkid_partition_get_size(par),
			blkid_partition_get_type(par));

		if (root_tab != tab)
			/* subpartition (BSD, Minix, ...) */
			printf(" (%s)", blkid_parttable_get_type(tab));

		p = blkid_partition_get_name(par);
		if (p)
			printf(" name='%s'", p);
		p = blkid_partition_get_uuid(par);
		if (p)
			printf(" uuid='%s'", p);
		p = blkid_partition_get_type_string(par);
		if (p)
			printf(" type='%s'", p);

		putc('\n', stdout);
	}

done:
	blkid_free_probe(pr);
	return EXIT_SUCCESS;
}