2 * blkid.c - User command-line interface for libblkid
4 * Copyright (C) 2001 Andreas Dilger
7 * This file may be redistributed under the terms of the
8 * GNU Lesser General Public License.
22 #define OUTPUT_VALUE_ONLY 0x0001
23 #define OUTPUT_DEVICE_ONLY 0x0002
25 #include "blkid/blkid.h"
27 const char *progname = "blkid";
29 static void print_version(FILE *out)
31 fprintf(out, "%s %s (%s)\n", progname, BLKID_VERSION, BLKID_DATE);
34 static void usage(int error)
36 FILE *out = error ? stderr : stdout;
40 "usage:\t%s [-c <file>] [-hl] [-o format] "
41 "[-s <tag>] [-t <token>]\n [-v] [-w <file>] [dev ...]\n"
42 "\t-c\tcache file (default: /etc/blkid.tab, /dev/null = none)\n"
43 "\t-h\tprint this usage message and exit\n"
44 "\t-s\tshow specified tag(s) (default show all tags)\n"
45 "\t-t\tfind device with a specific token (NAME=value pair)\n"
46 "\t-l\tlookup the the first device with arguments specified by -t\n"
47 "\t-v\tprint version and exit\n"
48 "\t-w\twrite cache to different file (/dev/null = no write)\n"
49 "\tdev\tspecify device(s) to probe (default: all devices)\n",
54 static void print_tags(blkid_dev dev, char *show[], int numtag, int output)
56 blkid_tag_iterate iter;
57 const char *type, *value;
63 if (output & OUTPUT_DEVICE_ONLY) {
64 printf("%s\n", blkid_dev_devname(dev));
68 iter = blkid_tag_iterate_begin(dev);
69 while (blkid_tag_next(iter, &type, &value) == 0) {
71 for (i=0; i < numtag; i++)
72 if (!strcmp(type, show[i]))
77 if (first && !(output & OUTPUT_VALUE_ONLY)) {
78 printf("%s: ", blkid_dev_devname(dev));
81 if ((output & OUTPUT_VALUE_ONLY))
82 printf("%s\n", value);
84 printf("%s=\"%s\" ", type, value);
86 blkid_tag_iterate_end(iter);
88 if (!first && !(output & OUTPUT_VALUE_ONLY))
92 int main(int argc, char **argv)
94 blkid_cache cache = NULL;
95 char *devices[128] = { NULL, };
96 char *show[128] = { NULL, };
97 char *search_type = NULL, *search_value = NULL;
100 unsigned int numdev = 0, numtag = 0;
104 int output_format = 0;
108 while ((c = getopt (argc, argv, "c:f:hlo:s:t:w:v")) != EOF)
111 if (optarg && !*optarg)
122 if (!strcmp(optarg, "value"))
123 output_format = OUTPUT_VALUE_ONLY;
124 else if (!strcmp(optarg, "device"))
125 output_format = OUTPUT_DEVICE_ONLY;
126 else if (!strcmp(optarg, "full"))
129 fprintf(stderr, "Invalid output format %s. Chose from value, device, or full\n", optarg);
134 if (numtag >= sizeof(show) / sizeof(*show)) {
135 fprintf(stderr, "Too many tags specified\n");
138 show[numtag++] = optarg;
142 fprintf(stderr, "Can only search for "
143 "one NAME=value pair\n");
146 if (blkid_parse_tag_string(optarg,
149 fprintf(stderr, "-t needs NAME=value pair\n");
157 if (optarg && !*optarg)
168 while (optind < argc)
169 devices[numdev++] = argv[optind++];
172 print_version(stdout);
176 if (blkid_get_cache(&cache, read) < 0)
184 fprintf(stderr, "The lookup option requires a "
185 "search type specified using -t\n");
188 /* Load any additional devices not in the cache */
189 for (i = 0; i < numdev; i++)
190 blkid_get_dev(cache, devices[i], BLKID_DEV_NORMAL);
192 if ((dev = blkid_find_dev_with_tag(cache, search_type,
194 print_tags(dev, show, numtag, output_format);
197 /* If we didn't specify a single device, show all available devices */
198 } else if (!numdev) {
199 blkid_dev_iterate iter;
202 blkid_probe_all(cache);
204 iter = blkid_dev_iterate_begin(cache);
205 blkid_dev_set_search(iter, search_type, search_value);
206 while (blkid_dev_next(iter, &dev) == 0) {
207 dev = blkid_verify(cache, dev);
210 print_tags(dev, show, numtag, output_format);
213 blkid_dev_iterate_end(iter);
214 /* Add all specified devices to cache (optionally display tags) */
215 } else for (i = 0; i < numdev; i++) {
216 blkid_dev dev = blkid_get_dev(cache, devices[i],
221 !blkid_dev_has_tag(dev, search_type,
224 print_tags(dev, show, numtag, output_format);
234 blkid_put_cache(cache);