Commit Diff


commit - 44121ddf9f1fabdd3e85b8a8cdbfa27245f902c7
commit + 0c422a5b7d0b4f4ddf3f70089c95d9ae2d2baf06
blob - b8555e87ca541cdabe8ccbc609f16a6ff126ec65
blob + faadd5ac7957d4c3cc3487fa9e3610d40587a282
--- ChangeLog.md
+++ ChangeLog.md
@@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](http
   (at least through the disklabel interface).
 - Print fancy unicode characters by default.
 - Use err(3) instead of die()
+- Use opendev(3) instead of open(2)
 
 ### Removed
 - Options:
blob - b9ecab4f9f2f0d22ef1252e2373e34a666f0394a
blob + 087992bd3d6fe74119dca5a03212f9f3643c214e
--- Makefile
+++ Makefile
@@ -6,11 +6,12 @@ VERSION = 1.2
 PREFIX = /usr/local
 MANPREFIX = ${PREFIX}/man
 MY_CFLAGS = -Wall -Wextra -Werror -pedantic -std=c99 -DVERSION=\"${VERSION}\" ${CFLAGS}
+LIBS = -lutil
 
 all: lsblk
 
 lsblk: lsblk.c
-	${CC} -o lsblk lsblk.c ${MY_CFLAGS}
+	${CC} -o lsblk lsblk.c ${MY_CFLAGS} ${LIBS}
 
 clean:
 	rm -f lsblk
blob - 33ea632f20af5cceeffa5d25d564049f4b0bdf09
blob + 1ec4f487a9b69ba4bd77d67fff959873d6511dfe
--- README.md
+++ README.md
@@ -20,7 +20,7 @@ doas make unroot
 ## TODO
 - [x] `lsblk sd0`
 - [ ] `lsblk sd0a`
-- [ ] open(2) -> opendev(3)
+- [ ] `lsblk -f duid,size`
 - [ ] Add an option to make the output script-friendly (like -c for CSV).
 - [ ] Maybe: Display the child device of a RAID partition
 - [x] Show the DUID
blob - 0328e8c612b9e2a393f234d2cc0dc62c84bde83d
blob + 4141794cedcce65dbb0f62e3ccbddd8735e1e870
--- lsblk.c
+++ lsblk.c
@@ -35,6 +35,7 @@
 #include <stdio.h>
 #include <fcntl.h>
 #include <errno.h>
+#include <util.h>
 #include <err.h>
 
 static int diskcount (void)
@@ -286,23 +287,25 @@ static struct my_diskinfo read_disk (const char *name)
 {
     struct my_diskinfo disk;
     struct disklabel label;
+    char *ppath, *letter;
 
     bzero (&disk, sizeof disk);
 
     {   // Read disklabel.
-        char *path = NULL;
+        size_t len;
         int fd;
 
-        asprintf (&path, "/dev/%sc", name);
-        fd = open (path, O_RDONLY);
+        fd = opendev (name, O_RDONLY, OPENDEV_PART | OPENDEV_BLCK, &ppath);
         if (fd < 0)
             err (1, "opendev(%s)", name);
+
         if (ioctl (fd, DIOCGDINFO, &label) < 0)
             err (1, "ioctl(%s, DIOCGDINFO)", name);
         close (fd);
-        free (path);
-    }
 
+        len = strlen (ppath);
+        letter = ppath + len - 1;
+    }
 
     memcpy (disk.name, name, 3);
     disk.name[3] = '\0';
@@ -317,7 +320,6 @@ static struct my_diskinfo read_disk (const char *name)
         const struct partition *p = &label.d_partitions[i];
         const struct statfs *mnt;
         struct my_partinfo part;
-        char *path;
 
         bzero (&part, sizeof part);
 
@@ -327,9 +329,8 @@ static struct my_diskinfo read_disk (const char *name)
             continue;
 
         part.letter = 'a' + i;
-        asprintf (&path, "/dev/%s%c", disk.name, part.letter);
-        mnt = find_mount (path);
-        free (path);
+        *letter = part.letter;
+        mnt = find_mount (ppath);
 
         if (mnt) {
             const uint64_t bs = mnt->f_bsize;