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.
18 extern int getopt(int argc, char * const argv[], const char *optstring);
23 #define OUTPUT_VALUE_ONLY 0x0001
24 #define OUTPUT_DEVICE_ONLY 0x0002
26 #include "blkid/blkid.h"
28 const char *progname = "blkid";
30 static void print_version(FILE *out)
32 fprintf(out, "%s %s (%s)\n", progname, BLKID_VERSION, BLKID_DATE);
35 static void usage(int error)
37 FILE *out = error ? stderr : stdout;
41 "usage:\t%s [-c <file>] [-ghl] [-o format] "
42 "[-s <tag>] [-t <token>]\n [-v] [-w <file>] [dev ...]\n"
43 "\t-c\tcache file (default: /etc/blkid.tab, /dev/null = none)\n"
44 "\t-h\tprint this usage message and exit\n"
45 "\t-g\tgarbage collect the blkid cache\n"
46 "\t-s\tshow specified tag(s) (default show all tags)\n"
47 "\t-t\tfind device with a specific token (NAME=value pair)\n"
48 "\t-l\tlookup the the first device with arguments specified by -t\n"
49 "\t-v\tprint version and exit\n"
50 "\t-w\twrite cache to different file (/dev/null = no write)\n"
51 "\tdev\tspecify device(s) to probe (default: all devices)\n",
57 * This function does "safe" printing. It will convert non-printable
58 * ASCII characters using '^' and M- notation.
60 static void safe_print(const char *cp, int len)
73 if ((ch < 32) || (ch == 0x7f)) {
75 ch ^= 0x40; /* ^@, ^A, ^B; ^? for DEL */
81 static void print_tags(blkid_dev dev, char *show[], int numtag, int output)
83 blkid_tag_iterate iter;
84 const char *type, *value;
90 if (output & OUTPUT_DEVICE_ONLY) {
91 printf("%s\n", blkid_dev_devname(dev));
95 iter = blkid_tag_iterate_begin(dev);
96 while (blkid_tag_next(iter, &type, &value) == 0) {
98 for (i=0; i < numtag; i++)
99 if (!strcmp(type, show[i]))
104 if (output & OUTPUT_VALUE_ONLY) {
105 fputs(value, stdout);
109 printf("%s: ", blkid_dev_devname(dev));
113 fputs("=\"", stdout);
114 safe_print(value, -1);
115 fputs("\" ", stdout);
118 blkid_tag_iterate_end(iter);
120 if (!first && !(output & OUTPUT_VALUE_ONLY))
124 int main(int argc, char **argv)
126 blkid_cache cache = NULL;
127 char *devices[128] = { NULL, };
128 char *show[128] = { NULL, };
129 char *search_type = NULL, *search_value = NULL;
132 unsigned int numdev = 0, numtag = 0;
136 int output_format = 0;
137 int lookup = 0, gc = 0;
140 while ((c = getopt (argc, argv, "c:f:ghlo:s:t:w:v")) != EOF)
143 if (optarg && !*optarg)
157 if (!strcmp(optarg, "value"))
158 output_format = OUTPUT_VALUE_ONLY;
159 else if (!strcmp(optarg, "device"))
160 output_format = OUTPUT_DEVICE_ONLY;
161 else if (!strcmp(optarg, "full"))
164 fprintf(stderr, "Invalid output format %s. Chose from value, device, or full\n", optarg);
169 if (numtag >= sizeof(show) / sizeof(*show)) {
170 fprintf(stderr, "Too many tags specified\n");
173 show[numtag++] = optarg;
177 fprintf(stderr, "Can only search for "
178 "one NAME=value pair\n");
181 if (blkid_parse_tag_string(optarg,
184 fprintf(stderr, "-t needs NAME=value pair\n");
192 if (optarg && !*optarg)
203 while (optind < argc)
204 devices[numdev++] = argv[optind++];
207 print_version(stdout);
211 if (blkid_get_cache(&cache, read) < 0)
216 blkid_gc_cache(cache);
221 fprintf(stderr, "The lookup option requires a "
222 "search type specified using -t\n");
225 /* Load any additional devices not in the cache */
226 for (i = 0; i < numdev; i++)
227 blkid_get_dev(cache, devices[i], BLKID_DEV_NORMAL);
229 if ((dev = blkid_find_dev_with_tag(cache, search_type,
231 print_tags(dev, show, numtag, output_format);
234 /* If we didn't specify a single device, show all available devices */
235 } else if (!numdev) {
236 blkid_dev_iterate iter;
239 blkid_probe_all(cache);
241 iter = blkid_dev_iterate_begin(cache);
242 blkid_dev_set_search(iter, search_type, search_value);
243 while (blkid_dev_next(iter, &dev) == 0) {
244 dev = blkid_verify(cache, dev);
247 print_tags(dev, show, numtag, output_format);
250 blkid_dev_iterate_end(iter);
251 /* Add all specified devices to cache (optionally display tags) */
252 } else for (i = 0; i < numdev; i++) {
253 blkid_dev dev = blkid_get_dev(cache, devices[i],
258 !blkid_dev_has_tag(dev, search_type,
261 print_tags(dev, show, numtag, output_format);
271 blkid_put_cache(cache);