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 #include "blkid/blkid.h"
24 const char *progname = "blkid";
26 static void print_version(FILE *out)
28 fprintf(out, "%s %s (%s)\n", progname, BLKID_VERSION, BLKID_DATE);
31 static void usage(int error)
33 FILE *out = error ? stderr : stdout;
37 "usage:\t%s [-c <file>] [-h] "
38 "[-p] [-s <tag>] [-t <token>] [-v] [-w <file>] [dev ...]\n"
39 "\t-c\tcache file (default: /etc/blkid.tab, /dev/null = none)\n"
40 "\t-h\tprint this usage message and exit\n"
41 "\t-s\tshow specified tag(s) (default show all tags)\n"
42 "\t-t\tfind device with a specific token (NAME=value pair)\n"
43 "\t-v\tprint version and exit\n"
44 "\t-w\twrite cache to different file (/dev/null = no write)\n"
45 "\tdev\tspecify device(s) to probe (default: all devices)\n",
50 static void print_tags(blkid_dev dev, char *show[], int numtag)
52 blkid_tag_iterate iter;
53 const char *type, *value;
59 iter = blkid_tag_iterate_begin(dev);
60 while (blkid_tag_next(iter, &type, &value) == 0) {
62 for (i=0; i < numtag; i++)
63 if (!strcmp(type, show[i]))
69 printf("%s: ", blkid_dev_devname(dev));
72 printf("%s=\"%s\" ", type, value);
74 blkid_tag_iterate_end(iter);
80 int main(int argc, char **argv)
82 blkid_cache cache = NULL;
83 char *devices[128] = { NULL, };
84 char *show[128] = { NULL, };
85 char *search_type = NULL, *search_value = NULL;
88 unsigned int numdev = 0, numtag = 0;
94 while ((c = getopt (argc, argv, "c:f:hps:t:w:v")) != EOF)
97 if (optarg && !*optarg)
105 if (numtag >= sizeof(show) / sizeof(*show)) {
106 fprintf(stderr, "Too many tags specified\n");
109 show[numtag++] = optarg;
113 fprintf(stderr, "Can only search for "
114 "one NAME=value pair\n");
117 if (blkid_parse_tag_string(optarg,
120 fprintf(stderr, "-t needs NAME=value pair\n");
128 if (optarg && !*optarg)
139 while (optind < argc)
140 devices[numdev++] = argv[optind++];
143 print_version(stdout);
147 if (blkid_get_cache(&cache, read) < 0)
151 /* If looking for a specific NAME=value pair, print only that */
155 /* Load any additional devices not in the cache */
156 for (i = 0; i < numdev; i++)
157 blkid_get_dev(cache, devices[i], BLKID_DEV_NORMAL);
159 if ((dev = blkid_find_dev_with_tag(cache, search_type,
161 print_tags(dev, show, numtag);
164 /* If we didn't specify a single device, show all available devices */
165 } else if (!numdev) {
166 blkid_dev_iterate iter;
169 blkid_probe_all(cache);
171 iter = blkid_dev_iterate_begin(cache);
172 while (blkid_dev_next(iter, &dev) == 0) {
173 print_tags(dev, show, numtag);
176 blkid_dev_iterate_end(iter);
177 /* Add all specified devices to cache (optionally display tags) */
178 } else for (i = 0; i < numdev; i++) {
179 blkid_dev dev = blkid_get_dev(cache, devices[i],
183 print_tags(dev, show, numtag);
193 blkid_put_cache(cache);