diff options
Diffstat (limited to 'toys/other/blkid.c')
-rw-r--r-- | toys/other/blkid.c | 50 |
1 files changed, 43 insertions, 7 deletions
diff --git a/toys/other/blkid.c b/toys/other/blkid.c index 01b5971b..bed20b2c 100644 --- a/toys/other/blkid.c +++ b/toys/other/blkid.c @@ -5,19 +5,20 @@ * See ftp://ftp.kernel.org/pub/linux/utils/util-linux/v2.24/libblkid-docs/api-index-full.html * TODO: -U and -L should require arguments -USE_BLKID(NEWTOY(blkid, "ULs*[!LU]", TOYFLAG_BIN)) +USE_BLKID(NEWTOY(blkid, "ULo:s*[!LU]", TOYFLAG_BIN)) USE_FSTYPE(NEWTOY(fstype, "<1", TOYFLAG_BIN)) config BLKID bool "blkid" default y help - usage: blkid [-s TAG] [-UL] DEV... + usage: blkid [-o TYPE] [-s TAG] [-UL] DEV... Print type, label and UUID of filesystem on a block device or image. -U Show UUID only (or device with that UUID) -L Show LABEL only (or device with that LABEL) + -o TYPE Output format (full, value, export) -s TAG Only show matching tags (default all) config FSTYPE @@ -34,6 +35,7 @@ config FSTYPE GLOBALS( struct arg_list *s; + char *o; ) struct fstype { @@ -52,6 +54,7 @@ struct fstype { {"btrfs", 0x4D5F53665248425FULL, 8, 65600, 65803, 256, 65819}, {"cramfs", 0x28cd3d45, 4, 0, 0, 16, 48}, {"f2fs", 0xF2F52010, 4, 1024, 1132, 512, 0x47c}, + {"iso9660", 0x444301, 3, 0x8000, 0x832d, 32, 0x8028}, {"jfs", 0x3153464a, 4, 32768, 32920, 16, 32904}, {"nilfs", 0x3434, 2, 1030, 1176, 80, 1192}, {"reiserfs", 0x724573496552ULL, 6, 8244, 8276, 16, 8292}, @@ -64,6 +67,19 @@ struct fstype { {"vfat", 0x31544146, 4, 54, 39, 11, 43} // fat1 }; +static void escape(char *str, int force) +{ + if (!force && str[strcspn(str, "\" \\\n\t$<>|&;`'~()!#?")]) force++; + if (!force) return xputsn(str); + + putchar('"'); + while (*str) { + if (strchr("\" \\", *str)) putchar('\\'); + putchar(*str++); + } + putchar('"'); +} + static void show_tag(char *key, char *value) { int show = 0; @@ -73,7 +89,13 @@ static void show_tag(char *key, char *value) for (al = TT.s; al; al = al->next) if (!strcmp(key, al->arg)) show = 1; } else show = 1; - if (show && *value) printf(" %s=\"%s\"", key, value); + if (!show || !*value) return; + if (!strcasecmp(TT.o, "full") || !strcasecmp(TT.o, "export")) { + printf(" %s="+!(*TT.o=='f'), key); + escape(value, *TT.o=='f'); + if (*TT.o=='e') xputc('\n'); + } else if (!strcasecmp(TT.o, "value")) xputs(value); + else error_exit("bad -o %s", TT.o); } static void flagshow(char *s, char *name) @@ -137,13 +159,16 @@ static void do_blkid(int fd, char *name) } // output for blkid - if (!FLAG(L) && !FLAG(U)) printf("%s:",name); + if (!FLAG(L) && !FLAG(U)) { + if (!TT.o || !strcasecmp(TT.o, "full")) printf("%s:", name); + else if (!strcasecmp(TT.o, "export")) show_tag("DEVNAME", name); + } len = fstypes[i].label_len; if (!FLAG(U) && len) { s = toybuf+fstypes[i].label_off-off; - if (!strcmp(type, "vfat")) { - show_tag("SEC_TYPE", "msdos"); + if (!strcmp(type, "vfat") || !strcmp(type, "iso9660")) { + if (*type=='v') show_tag("SEC_TYPE", "msdos"); while (len && s[len-1]==' ') len--; if (strstart(&s, "NO NAME")) len=0; } @@ -174,6 +199,15 @@ static void do_blkid(int fd, char *name) } else if (!strcmp(type, "vfat")) { s += sprintf(s, "%02X%02X-%02X%02X", toybuf[uoff+3], toybuf[uoff+2], toybuf[uoff+1], toybuf[uoff]); + } else if (!strcmp(type, "iso9660")) { + s += sprintf(s, "%c%c%c%c-", toybuf[uoff], toybuf[uoff+1], + toybuf[uoff+2], toybuf[uoff+3]); + s += sprintf(s, "%c%c-", toybuf[uoff+4], toybuf[uoff+5]); + s += sprintf(s, "%c%c-", toybuf[uoff+6], toybuf[uoff+7]); + s += sprintf(s, "%c%c-", toybuf[uoff+8], toybuf[uoff+9]); + s += sprintf(s, "%c%c-", toybuf[uoff+10], toybuf[uoff+11]); + s += sprintf(s, "%c%c-", toybuf[uoff+12], toybuf[uoff+13]); + s += sprintf(s, "%c%c", toybuf[uoff+14], toybuf[uoff+15]); } else { for (j = 0; j < 16; j++) s += sprintf(s, "-%02x"+!(0x550 & (1<<j)), toybuf[uoff+j]); @@ -189,11 +223,13 @@ static void do_blkid(int fd, char *name) if (FLAG(U) || FLAG(L)) return; show_tag("TYPE", type); - xputc('\n'); + if (!strcasecmp(TT.o, "full")) xputc('\n'); } void blkid_main(void) { + if (!TT.o) TT.o = "full"; + if (*toys.optargs && !FLAG(L) && !FLAG(U)) loopfiles(toys.optargs, do_blkid); else { unsigned int ma, mi, sz, fd; |