]> git.ipfire.org Git - thirdparty/util-linux.git/blame - shlibs/blkid/samples/mkfs.c
build-sys: provide alternatives for err, errx, warn and warnx
[thirdparty/util-linux.git] / shlibs / blkid / samples / mkfs.c
CommitLineData
b95d4a73
KZ
1/*
2 * Copyright (C) 2009 Karel Zak <kzak@redhat.com>
3 *
4 * This file may be redistributed under the terms of the
5 * GNU Lesser General Public License.
6 */
7
8#include <stdio.h>
9#include <stdlib.h>
10#include <sys/types.h>
11#include <sys/stat.h>
12#include <fcntl.h>
b95d4a73
KZ
13#include <errno.h>
14
15#include <blkid.h>
16
3ebd663b 17#include "c.h"
b95d4a73
KZ
18
19int main(int argc, char *argv[])
20{
f38db0cf 21 int rc;
b95d4a73
KZ
22 char *devname;
23 blkid_probe pr;
24 blkid_topology tp;
25
26 if (argc < 2) {
27 fprintf(stderr, "usage: %s <device> "
28 "-- checks based on libblkid for mkfs-like programs.\n",
29 program_invocation_short_name);
30 return EXIT_FAILURE;
31 }
32
33 devname = argv[1];
f38db0cf 34 pr = blkid_new_probe_from_filename(devname);
b95d4a73 35 if (!pr)
f38db0cf
KZ
36 err(EXIT_FAILURE, "%s: faild to create a new libblkid probe",
37 devname);
b95d4a73
KZ
38
39 /*
40 * check Filesystems / Partitions overwrite
41 */
42
43 /* enable partitions probing (superblocks are enabled by default) */
44 blkid_probe_enable_partitions(pr, TRUE);
45
46 rc = blkid_do_fullprobe(pr);
47 if (rc == -1)
48 errx(EXIT_FAILURE, "%s: blkid_do_fullprobe() failed", devname);
49 else if (rc == 0) {
50 const char *type;
51
52 if (!blkid_probe_lookup_value(pr, "TYPE", &type, NULL))
53 errx(EXIT_FAILURE, "%s: appears to contain an existing "
0b4dedb3 54 "%s superblock", devname, type);
b95d4a73
KZ
55
56 if (!blkid_probe_lookup_value(pr, "PTTYPE", &type, NULL))
57 errx(EXIT_FAILURE, "%s: appears to contain an partition "
58 "table (%s)", devname, type);
59 }
60
61 /*
62 * get topology details
63 */
64 tp = blkid_probe_get_topology(pr);
65 if (!tp)
79e1f9f1 66 errx(EXIT_FAILURE, "%s: failed to read topology", devname);
b95d4a73
KZ
67
68
69 /* ... your mkfs.<type> code or so ...
70
71 off = blkid_topology_get_alignment_offset(tp);
72
b95d4a73
KZ
73 */
74
79e1f9f1
KZ
75 blkid_free_probe(pr);
76
b95d4a73
KZ
77 return EXIT_SUCCESS;
78}