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.
20 #include "blkid/blkid.h"
22 char *progname = "blkid";
23 void print_version(FILE *out)
25 fprintf(stderr, "%s %s (%s)\n", progname, BLKID_VERSION, BLKID_DATE);
30 FILE *out = error ? stderr : stdout;
34 "usage:\t%s [-c <file>] [-h] "
35 "[-p] [-s <tag>] [-t <token>] [-v] [-w <file>] [dev ...]\n"
36 "\t-c\tcache file (default: /etc/blkid.tab, /dev/null = none)\n"
37 "\t-h\tprint this usage message and exit\n"
38 "\t-s\tshow specified tag(s) (default show all tags)\n"
39 "\t-t\tfind device with a specific token (NAME=value pair)\n"
40 "\t-v\tprint version and exit\n"
41 "\t-w\twrite cache to different file (/dev/null = no write)\n"
42 "\tdev\tspecify device(s) to probe (default: all devices)\n",
47 #define PT_FL_START 0x0001
48 #define PT_FL_TYPE 0x0002
50 static void print_tag(blkid_dev *dev, blkid_tag *tag, int *flags)
52 /* Print only one "dev:" per device */
53 if (!*flags & PT_FL_START) {
54 printf("%s: ", dev->bid_name);
55 *flags |= PT_FL_START;
57 /* Print only the primary TYPE per device */
58 if (!strcmp(tag->bit_name, "TYPE")) {
59 if (*flags & PT_FL_TYPE)
63 printf("%s=\"%s\" ", tag->bit_name, tag->bit_val);
66 void print_tags(blkid_dev *dev, char *show[], int numtag)
74 list_for_each(p, &dev->bid_tags) {
75 blkid_tag *tag = list_entry(p, blkid_tag, bit_tags);
78 /* Print all tokens if none is specified */
79 if (numtag == 0 || !show) {
80 print_tag(dev, tag, &flags);
81 /* Otherwise, only print specific tokens */
82 } else for (i = 0; i < numtag; i++) {
83 if (!strcmp(tag->bit_name, show[i]))
84 print_tag(dev, tag, &flags);
92 int main(int argc, char **argv)
94 blkid_cache *cache = NULL;
95 char *devices[128] = { NULL, };
96 char *show[128] = { NULL, };
97 blkid_tag *tag = NULL;
100 int numdev = 0, numtag = 0;
106 while ((c = getopt (argc, argv, "c:d:f:hps:t:w:v")) != EOF)
108 case 'd': /* deprecated */
109 if (numdev >= sizeof(devices) / sizeof(*devices)) {
111 "Too many devices specified\n");
114 devices[numdev++] = optarg;
117 if (optarg && !*optarg)
125 if (numtag >= sizeof(show) / sizeof(*show)) {
126 fprintf(stderr, "Too many tags specified\n");
129 show[numtag++] = optarg;
133 fprintf(stderr, "Can only search for "
134 "one NAME=value pair\n");
137 if (!(tag = blkid_token_to_tag(optarg))) {
138 fprintf(stderr, "-t needs NAME=value pair\n");
146 if (optarg && !*optarg)
157 while (optind < argc)
158 devices[numdev++] = argv[optind++];
161 print_version(stdout);
165 if (blkid_read_cache(&cache, read) < 0)
169 /* If looking for a specific NAME=value pair, print only that */
171 blkid_tag *found = NULL;
173 /* Load any additional devices not in the cache */
174 for (i = 0; i < numdev; i++)
175 blkid_get_devname(cache, devices[i]);
177 if ((found = blkid_get_tag_cache(cache, tag))) {
178 print_tags(found->bit_dev, show, numtag);
181 /* If we didn't specify a single device, show all available devices */
182 } else if (!numdev) {
185 blkid_probe_all(&cache);
187 list_for_each(p, &cache->bic_devs) {
188 blkid_dev *dev = list_entry(p, blkid_dev, bid_devs);
189 print_tags(dev, show, numtag);
192 /* Add all specified devices to cache (optionally display tags) */
193 } else for (i = 0; i < numdev; i++) {
194 blkid_dev *dev = blkid_get_devname(cache, devices[i]);
197 print_tags(dev, show, numtag);
204 blkid_save_cache(cache, write);
205 blkid_free_cache(cache);