logo

utils-extra

Collection of extra tools for Unixes git clone https://anongit.hacktivis.me/git/utils-extra.git/
commit: ab267e25eac344209100c7057670afed57b07bd9
parent d749352609d337968c846bd9056462bebf7331a0
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Thu,  1 Jan 2026 07:33:09 +0100

cmd/disk_id: format

Diffstat:

Mcmd/disk_id.c48+++++++++++++++++++++++++++++-------------------
1 file changed, 29 insertions(+), 19 deletions(-)

diff --git a/cmd/disk_id.c b/cmd/disk_id.c @@ -3,15 +3,15 @@ // SPDX-License-Identifier: MPL-2.0 #define _DEFAULT_SOURCE -#include <string.h> // strerror -#include <stdio.h> +#include <ctype.h> // isalnum #include <errno.h> -#include <stropts.h> // ioctl -#include <fcntl.h> // open +#include <fcntl.h> // open #include <linux/hdreg.h> // HDIO_GET_IDENTITY -#include <unistd.h> // fsync, write -#include <stdint.h> // uint16_t -#include <ctype.h> // isalnum +#include <stdint.h> // uint16_t +#include <stdio.h> +#include <string.h> // strerror +#include <stropts.h> // ioctl +#include <unistd.h> // fsync, write static char * strip(char *s, size_t *len) @@ -22,9 +22,9 @@ strip(char *s, size_t *len) (*len)--; } - while(*len > 0 && s[(*len)-1] == ' ') + while(*len > 0 && s[(*len) - 1] == ' ') { - s[(*len)-1] = '\0'; + s[(*len) - 1] = '\0'; (*len)--; } @@ -47,17 +47,18 @@ strncpyalnum(char *dest, char *src, size_t len) } int -main(int argc, char *argv[]) { +main(int argc, char *argv[]) +{ if(argc != 2) { - fprintf(stderr, "disk_id: error: Need 1 argument, got %d\n", argc-1); + fprintf(stderr, "disk_id: error: Need 1 argument, got %d\n", argc - 1); fprintf(stderr, "Usage: disk_id <block_device_path>\n"); return 1; } const char *dev = argv[1]; - int fd = open(dev, O_RDONLY|O_NONBLOCK|O_LARGEFILE); + int fd = open(dev, O_RDONLY | O_NONBLOCK | O_LARGEFILE); // IDENTITY is 512 bytes static uint16_t identity[256]; @@ -67,13 +68,22 @@ main(int argc, char *argv[]) { switch(errno) { case EINVAL: - fprintf(stderr, "disk_id: error: Device '%s' -> ioctl(HDIO_GET_IDENTITY): [EINVAL] Is a partition not a disk\n", dev); + fprintf(stderr, + "disk_id: error: Device '%s' -> ioctl(HDIO_GET_IDENTITY): [EINVAL] Is a partition " + "not a disk\n", + dev); return 1; case ENOMSG: - fprintf(stderr, "disk_id: error: Device '%s' -> ioctl(HDIO_GET_IDENTITY): [ENOMSG] Information not available\n", dev); + fprintf(stderr, + "disk_id: error: Device '%s' -> ioctl(HDIO_GET_IDENTITY): [ENOMSG] Information not " + "available\n", + dev); return 1; default: - fprintf(stderr, "disk_id: error: Device '%s' -> ioctl(HDIO_GET_IDENTITY): %s\n", dev, strerror(errno)); + fprintf(stderr, + "disk_id: error: Device '%s' -> ioctl(HDIO_GET_IDENTITY): %s\n", + dev, + strerror(errno)); return 1; } } @@ -81,9 +91,9 @@ main(int argc, char *argv[]) { // TODO: Handle errno close(fd); - static char serno_buf[20] = ""; - static char model_buf[40] = ""; - static char disk_id[40+20] = ""; + static char serno_buf[20] = ""; + static char model_buf[40] = ""; + static char disk_id[40 + 20] = ""; size_t serno_len = 20; memcpy(serno_buf, (char *)&identity[10], serno_len); @@ -100,7 +110,7 @@ main(int argc, char *argv[]) { disk_id[modelz++] = '_'; - size_t serialz = strncpyalnum(disk_id+modelz, serno, serno_len); + size_t serialz = strncpyalnum(disk_id + modelz, serno, serno_len); if(serialz == 0) return 1; printf("%s\n", disk_id);