From 8927998f8229a103037ba5f49abe30c620ce322c Mon Sep 17 00:00:00 2001 From: Theodore Ts'o Date: Sat, 20 Mar 2004 16:30:10 -0500 Subject: [PATCH] blkid.c (main, print_tags): Add new option -o which allows the user to control the output format of blkid. --- misc/ChangeLog | 3 +++ misc/blkid.8.in | 19 +++++++++++++++++++ misc/blkid.c | 44 ++++++++++++++++++++++++++++++++++---------- 3 files changed, 56 insertions(+), 10 deletions(-) diff --git a/misc/ChangeLog b/misc/ChangeLog index 91d26efd..9ba40fbf 100644 --- a/misc/ChangeLog +++ b/misc/ChangeLog @@ -1,5 +1,8 @@ 2004-03-20 Theodore Ts'o + * blkid.c (main, print_tags): Add new option -o which allows the + user to control the output format of blkid. + * mke2fs.8.in: Fix spelling typos (Addresses Debian Bug #238741) 2004-03-08 Theodore Ts'o diff --git a/misc/blkid.8.in b/misc/blkid.8.in index b39a6bbf..6a4ca2db 100644 --- a/misc/blkid.8.in +++ b/misc/blkid.8.in @@ -22,6 +22,10 @@ blkid \- command\-line utility to locate/print block device attributes .I writecachefile ] [ +.B \-o +.I format +] +[ .B \-s .I tag ] @@ -59,6 +63,21 @@ scanned but not necessarily available at this time), specify .B \-h Display a usage message and exit. .TP +.B \-o +Display +.BR blkid 's +output using the specified format. The +.I format +parameter may be +.IR full , +(the default), +.IR value , +(only print the value of any tags printed by +.BR blkid) +or +.I device +(only print the device name). +.TP .B \-p Probe all available devices. This is the default when displaying tokens. When searching for a token normally the cache file is diff --git a/misc/blkid.c b/misc/blkid.c index 9caebbdf..c529b07a 100644 --- a/misc/blkid.c +++ b/misc/blkid.c @@ -19,6 +19,9 @@ extern char *optarg; extern int optind; #endif +#define OUTPUT_VALUE_ONLY 0x0001 +#define OUTPUT_DEVICE_ONLY 0x0002 + #include "blkid/blkid.h" const char *progname = "blkid"; @@ -34,8 +37,8 @@ static void usage(int error) print_version(out); fprintf(out, - "usage:\t%s [-c ] [-h] " - "[-p] [-s ] [-t ] [-v] [-w ] [dev ...]\n" + "usage:\t%s [-c ] [-h] [-o format] " + "[-p] [-s ] [-t ]\n [-v] [-w ] [dev ...]\n" "\t-c\tcache file (default: /etc/blkid.tab, /dev/null = none)\n" "\t-h\tprint this usage message and exit\n" "\t-s\tshow specified tag(s) (default show all tags)\n" @@ -47,7 +50,7 @@ static void usage(int error) exit(error); } -static void print_tags(blkid_dev dev, char *show[], int numtag) +static void print_tags(blkid_dev dev, char *show[], int numtag, int output) { blkid_tag_iterate iter; const char *type, *value; @@ -56,6 +59,11 @@ static void print_tags(blkid_dev dev, char *show[], int numtag) if (!dev) return; + if (output & OUTPUT_DEVICE_ONLY) { + printf("%s\n", blkid_dev_devname(dev)); + return; + } + iter = blkid_tag_iterate_begin(dev); while (blkid_tag_next(iter, &type, &value) == 0) { if (numtag && show) { @@ -65,15 +73,18 @@ static void print_tags(blkid_dev dev, char *show[], int numtag) if (i >= numtag) continue; } - if (first) { + if (first && !(output & OUTPUT_VALUE_ONLY)) { printf("%s: ", blkid_dev_devname(dev)); first = 0; } - printf("%s=\"%s\" ", type, value); + if ((output & OUTPUT_VALUE_ONLY)) + printf("%s\n", value); + else + printf("%s=\"%s\" ", type, value); } blkid_tag_iterate_end(iter); - if (!first) + if (!first && !(output & OUTPUT_VALUE_ONLY)) printf("\n"); } @@ -89,9 +100,10 @@ int main(int argc, char **argv) int version = 0; int err = 4; unsigned int i; + int output_format = 0; char c; - while ((c = getopt (argc, argv, "c:f:hps:t:w:v")) != EOF) + while ((c = getopt (argc, argv, "c:f:ho:ps:t:w:v")) != EOF) switch (c) { case 'c': if (optarg && !*optarg) @@ -101,6 +113,18 @@ int main(int argc, char **argv) if (!write) write = read; break; + case 'o': + if (!strcmp(optarg, "value")) + output_format = OUTPUT_VALUE_ONLY; + else if (!strcmp(optarg, "device")) + output_format = OUTPUT_DEVICE_ONLY; + else if (!strcmp(optarg, "full")) + output_format = 0; + else { + fprintf(stderr, "Invalid output format %s. Chose from value, device, or full\n", optarg); + exit(1); + } + break; case 's': if (numtag >= sizeof(show) / sizeof(*show)) { fprintf(stderr, "Too many tags specified\n"); @@ -158,7 +182,7 @@ int main(int argc, char **argv) if ((dev = blkid_find_dev_with_tag(cache, search_type, search_value))) { - print_tags(dev, show, numtag); + print_tags(dev, show, numtag, output_format); err = 0; } /* If we didn't specify a single device, show all available devices */ @@ -170,7 +194,7 @@ int main(int argc, char **argv) iter = blkid_dev_iterate_begin(cache); while (blkid_dev_next(iter, &dev) == 0) { - print_tags(dev, show, numtag); + print_tags(dev, show, numtag, output_format); err = 0; } blkid_dev_iterate_end(iter); @@ -180,7 +204,7 @@ int main(int argc, char **argv) BLKID_DEV_NORMAL); if (dev) { - print_tags(dev, show, numtag); + print_tags(dev, show, numtag, output_format); err = 0; } }