diff options
Diffstat (limited to 'blkid.c')
-rw-r--r-- | blkid.c | 66 |
1 files changed, 66 insertions, 0 deletions
@@ -0,0 +1,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("%" PRIx8 "%" PRIx8 "%" PRIx8 "%" 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; +} |