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>] [-h] [-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-v\tprint version and exit\n"
47 "\t-w\twrite cache to different file (/dev/null = no write)\n"
48 "\tdev\tspecify device(s) to probe (default: all devices)\n",
53 static void print_tags(blkid_dev dev, char *show[], int numtag, int output)
55 blkid_tag_iterate iter;
56 const char *type, *value;
62 if (output & OUTPUT_DEVICE_ONLY) {
63 printf("%s\n", blkid_dev_devname(dev));
67 iter = blkid_tag_iterate_begin(dev);
68 while (blkid_tag_next(iter, &type, &value) == 0) {
70 for (i=0; i < numtag; i++)
71 if (!strcmp(type, show[i]))
76 if (first && !(output & OUTPUT_VALUE_ONLY)) {
77 printf("%s: ", blkid_dev_devname(dev));
80 if ((output & OUTPUT_VALUE_ONLY))
81 printf("%s\n", value);
83 printf("%s=\"%s\" ", type, value);
85 blkid_tag_iterate_end(iter);
87 if (!first && !(output & OUTPUT_VALUE_ONLY))
91 int compare_search_type(blkid_dev dev, const char *search_type,
92 const char *search_value)
94 blkid_tag_iterate tag_iter;
95 const char *type, *value;
98 tag_iter = blkid_tag_iterate_begin(dev);
99 while (blkid_tag_next(tag_iter, &type, &value) == 0) {
100 if (!strcmp(type, search_type) &&
101 !strcmp(value, search_value)) {
106 blkid_tag_iterate_end(tag_iter);
111 int main(int argc, char **argv)
113 blkid_cache cache = NULL;
114 char *devices[128] = { NULL, };
115 char *show[128] = { NULL, };
116 char *search_type = NULL, *search_value = NULL;
119 unsigned int numdev = 0, numtag = 0;
123 int output_format = 0;
126 while ((c = getopt (argc, argv, "c:f:ho:s:t:w:v")) != EOF)
129 if (optarg && !*optarg)
137 if (!strcmp(optarg, "value"))
138 output_format = OUTPUT_VALUE_ONLY;
139 else if (!strcmp(optarg, "device"))
140 output_format = OUTPUT_DEVICE_ONLY;
141 else if (!strcmp(optarg, "full"))
144 fprintf(stderr, "Invalid output format %s. Chose from value, device, or full\n", optarg);
149 if (numtag >= sizeof(show) / sizeof(*show)) {
150 fprintf(stderr, "Too many tags specified\n");
153 show[numtag++] = optarg;
157 fprintf(stderr, "Can only search for "
158 "one NAME=value pair\n");
161 if (blkid_parse_tag_string(optarg,
164 fprintf(stderr, "-t needs NAME=value pair\n");
172 if (optarg && !*optarg)
183 while (optind < argc)
184 devices[numdev++] = argv[optind++];
187 print_version(stdout);
191 if (blkid_get_cache(&cache, read) < 0)
195 /* If we didn't specify a single device, show all available devices */
197 blkid_dev_iterate iter;
200 blkid_probe_all(cache);
202 iter = blkid_dev_iterate_begin(cache);
203 while (blkid_dev_next(iter, &dev) == 0) {
204 dev = blkid_verify(cache, dev);
208 !compare_search_type(dev, search_type,
211 print_tags(dev, show, numtag, output_format);
214 blkid_dev_iterate_end(iter);
215 /* Add all specified devices to cache (optionally display tags) */
216 } else for (i = 0; i < numdev; i++) {
217 blkid_dev dev = blkid_get_dev(cache, devices[i],
222 !compare_search_type(dev, search_type,
225 print_tags(dev, show, numtag, output_format);
235 blkid_put_cache(cache);