summaryrefslogtreecommitdiffstats
path: root/blkid.c
blob: d5bb7cf57d8d3818f2d8ef266239073ec0ca45e2 (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
/*
 * Print an MBR disk signature for use in a Linux PARTUUID
 *
 * Copyright (C) 2020  Patrick McDermott
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

static int
read_id(const char *dev)
{
	FILE *fp;
	uint8_t id[4];

	if ((fp = fopen(dev, "r")) == NULL) {
		perror("Failed to open device");
		return 1;
	}

	if (fseek(fp, 440L, SEEK_SET) != 0) {
		perror("Failed to seek on device");
		fclose(fp);
		return 1;
	}

	if (fread(id, sizeof(*id), sizeof(id) / sizeof(*id), fp) != 4) {
		perror("Failed to read from device");
		fclose(fp);
		return 1;
	}

	printf("%02" PRIx8 "%02" PRIx8 "%02" PRIx8 "%02" PRIx8 "\n",
			id[3], id[2], id[1], id[0]);

	fclose(fp);
	return 0;
}

int
main(int argc, char *argv[])
{
	int errors = 0;

	while (--argc) {
		errors += read_id(*++argv);
	}

	return (errors > 0) ? EXIT_FAILURE : EXIT_SUCCESS;
}