[Toybox] fdisk without optargs

Dima Krasner dima at dimakrasner.com
Mon Aug 3 12:29:27 PDT 2015


Hi,

I've added support for running fdisk without specifying a partition (so it scans /proc/partitions).

Cheers,
Dima

diff --git a/toys/other/blkid.c b/toys/other/blkid.c
index c18cc9c..1e1f7ed 100644
--- a/toys/other/blkid.c
+++ b/toys/other/blkid.c
@@ -4,7 +4,7 @@
  *
  * See ftp://ftp.kernel.org/pub/linux/utils/util-linux/v2.24/libblkid-docs/api-index-full.html
 
-USE_BLKID(NEWTOY(blkid, "<1", TOYFLAG_BIN))
+USE_BLKID(NEWTOY(blkid, NULL, TOYFLAG_BIN))
 USE_FSTYPE(NEWTOY(fstype, "<1", TOYFLAG_BIN))
 
 config BLKID
@@ -27,6 +27,8 @@ config FSTYPE
 #define FOR_blkid
 #include "toys.h"
 
+#define ONE_K       1024
+
 struct fstype {
   char *name;
   uint64_t magic;
@@ -56,7 +58,6 @@ static const struct fstype fstypes[] = {
   {"vfat", 0x31544146, 4, 54, 39+(4<<24), 11, 43}     // fat1
 };
 
-/* TODO if no args use proc/partitions */
 static void do_blkid(int fd, char *name)
 {
   int off, i, j;
@@ -134,12 +135,36 @@ static void do_blkid(int fd, char *name)
   printf(" TYPE=\"%s\"\n", type);
 }
 
+static void loop_partitions()
+{
+  unsigned int ma, mi, sz, fd;
+  char device[14];
+  char *name = toybuf, *buffer = toybuf + ONE_K;
+  FILE* fp = xfopen("/proc/partitions", "r");
+
+  while (fgets(buffer, ONE_K, fp)) {
+    name[0] = '\0';
+    if (sscanf(buffer, " %u %u %u %[^\n ]", &ma, &mi, &sz, name) != 4)
+      continue;
+
+    sprintf(device,"/dev/%s",name);
+    fd = open(device, O_RDONLY);
+    if (-1 == fd) perror_exit("Could not open %s", device);
+    do_blkid(fd, device);
+    close(fd);
+  }
+  fclose(fp);
+}
+
 void blkid_main(void)
 {
-  loopfiles(toys.optargs, do_blkid);
+  if (*toys.optargs)
+    loopfiles(toys.optargs, do_blkid);
+  else
+    loop_partitions();
 }
 
 void fstype_main(void)
 {
-  blkid_main();
+  loopfiles(toys.optargs, do_blkid);
 }
-- 
Dima Krasner <dima at dimakrasner.com>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: blkid.patch
Type: text/x-diff
Size: 1675 bytes
Desc: not available
URL: <http://lists.landley.net/pipermail/toybox-landley.net/attachments/20150803/c9776ba5/attachment-0004.patch>


More information about the Toybox mailing list