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.
21 #include "blkid/blkid.h"
23 const char *progname = "blkid";
25 static void print_version(FILE *out)
27 fprintf(stderr, "%s %s (%s)\n", progname, BLKID_VERSION, BLKID_DATE);
30 static void usage(int error)
32 FILE *out = error ? stderr : stdout;
36 "usage:\t%s [-c <file>] [-h] "
37 "[-p] [-s <tag>] [-t <token>] [-v] [-w <file>] [dev ...]\n"
38 "\t-c\tcache file (default: /etc/blkid.tab, /dev/null = none)\n"
39 "\t-h\tprint this usage message and exit\n"
40 "\t-s\tshow specified tag(s) (default show all tags)\n"
41 "\t-t\tfind device with a specific token (NAME=value pair)\n"
42 "\t-v\tprint version and exit\n"
43 "\t-w\twrite cache to different file (/dev/null = no write)\n"
44 "\tdev\tspecify device(s) to probe (default: all devices)\n",
49 static void print_tags(blkid_dev dev, char *show[], int numtag)
51 blkid_tag_iterate iter;
52 const char *type, *value;
53 int i, first = 1, printed_type = 0;
58 iter = blkid_tag_iterate_begin(dev);
59 while (blkid_tag_next(iter, &type, &value) == 0) {
61 for (i=0; i < numtag; i++)
62 if (!strcmp(type, show[i]))
68 printf("%s: ", blkid_devname_name(dev));
71 if (!strcmp(type, "TYPE")) {
76 printf("%s=\"%s\" ", type, value);
78 blkid_tag_iterate_end(iter);
84 int main(int argc, char **argv)
86 blkid_cache cache = NULL;
87 char *devices[128] = { NULL, };
88 char *show[128] = { NULL, };
89 char *search_type = NULL, *search_value = NULL;
92 int numdev = 0, numtag = 0;
98 while ((c = getopt (argc, argv, "c:d:f:hps:t:w:v")) != EOF)
100 case 'd': /* deprecated */
101 if (numdev >= sizeof(devices) / sizeof(*devices)) {
103 "Too many devices specified\n");
106 devices[numdev++] = optarg;
109 if (optarg && !*optarg)
117 if (numtag >= sizeof(show) / sizeof(*show)) {
118 fprintf(stderr, "Too many tags specified\n");
121 show[numtag++] = optarg;
125 fprintf(stderr, "Can only search for "
126 "one NAME=value pair\n");
129 if (blkid_parse_tag_string(optarg,
132 fprintf(stderr, "-t needs NAME=value pair\n");
140 if (optarg && !*optarg)
151 while (optind < argc)
152 devices[numdev++] = argv[optind++];
155 print_version(stdout);
159 if (blkid_read_cache(&cache, read) < 0)
163 /* If looking for a specific NAME=value pair, print only that */
167 /* Load any additional devices not in the cache */
168 for (i = 0; i < numdev; i++)
169 blkid_get_devname(cache, devices[i]);
171 if ((dev = blkid_find_dev_with_tag(cache, search_type,
173 print_tags(dev, show, numtag);
176 /* If we didn't specify a single device, show all available devices */
177 } else if (!numdev) {
178 blkid_dev_iterate iter;
181 blkid_probe_all(&cache);
183 iter = blkid_dev_iterate_begin(cache);
184 while (blkid_dev_next(iter, &dev) == 0) {
185 print_tags(dev, show, numtag);
188 blkid_dev_iterate_end(iter);
189 /* Add all specified devices to cache (optionally display tags) */
190 } else for (i = 0; i < numdev; i++) {
191 blkid_dev dev = blkid_get_devname(cache, devices[i]);
194 print_tags(dev, show, numtag);
204 blkid_save_cache(cache, write);
205 blkid_free_cache(cache);