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 #ifdef HAVE_SYS_IOCTL_H
23 #include <sys/ioctl.h>
28 extern int getopt(int argc, char * const argv[], const char *optstring);
33 #define OUTPUT_VALUE_ONLY 0x0001
34 #define OUTPUT_DEVICE_ONLY 0x0002
35 #define OUTPUT_PRETTY_LIST 0x0004
37 #include "ext2fs/ext2fs.h"
38 #include "blkid/blkid.h"
40 const char *progname = "blkid";
42 static void print_version(FILE *out)
44 fprintf(out, "%s %s (%s)\n", progname, BLKID_VERSION, BLKID_DATE);
47 static void usage(int error)
49 FILE *out = error ? stderr : stdout;
53 "usage:\t%s [-c <file>] [-ghlLv] [-o format] "
54 "[-s <tag>] [-t <token>]\n [-w <file>] [dev ...]\n"
55 "\t-c\tcache file (default: /etc/blkid.tab, /dev/null = none)\n"
56 "\t-h\tprint this usage message and exit\n"
57 "\t-g\tgarbage collect the blkid cache\n"
58 "\t-s\tshow specified tag(s) (default show all tags)\n"
59 "\t-t\tfind device with a specific token (NAME=value pair)\n"
60 "\t-l\tlookup the the first device with arguments specified by -t\n"
61 "\t-v\tprint version and exit\n"
62 "\t-w\twrite cache to different file (/dev/null = no write)\n"
63 "\tdev\tspecify device(s) to probe (default: all devices)\n",
69 * This function does "safe" printing. It will convert non-printable
70 * ASCII characters using '^' and M- notation.
72 static void safe_print(const char *cp, int len)
85 if ((ch < 32) || (ch == 0x7f)) {
87 ch ^= 0x40; /* ^@, ^A, ^B; ^? for DEL */
93 static int get_terminal_width(void)
104 if (ioctl (0, TIOCGSIZE, &t_win) == 0)
105 return (t_win.ts_cols);
108 if (ioctl (0, TIOCGWINSZ, &w_win) == 0)
109 return (w_win.ws_col);
111 cp = getenv("COLUMNS");
113 return strtol(cp, NULL, 10);
117 static int pretty_print_word(const char *str, int max_len,
118 int left_len, int overflow_nl)
120 int len = strlen(str) + left_len;
124 if (overflow_nl && len > max_len) {
127 } else if (len > max_len)
131 while (len++ < max_len);
135 static void pretty_print_line(const char *device, const char *fs_type,
136 const char *label, const char *mtpt,
139 static int device_len = 10, fs_type_len = 7;
140 static int label_len = 8, mtpt_len = 14;
141 static int term_width = -1;
145 term_width = get_terminal_width();
147 if (term_width > 80) {
160 len = pretty_print_word(device, device_len, 0, 1);
161 len = pretty_print_word(fs_type, fs_type_len, len, 0);
162 len = pretty_print_word(label, label_len, len, 0);
163 len = pretty_print_word(mtpt, mtpt_len, len, 0);
168 static void pretty_print_dev(blkid_dev dev)
170 blkid_tag_iterate iter;
171 const char *type, *value, *devname;
172 const char *uuid = "", *fs_type = "", *label = "";
173 int len, mount_flags;
178 pretty_print_line("device", "fs_type", "label",
179 "mount point", "UUID");
180 for (len=get_terminal_width()-1; len > 0; len--)
186 devname = blkid_dev_devname(dev);
187 if (access(devname, F_OK))
190 /* Get the uuid, label, type */
191 iter = blkid_tag_iterate_begin(dev);
192 while (blkid_tag_next(iter, &type, &value) == 0) {
193 if (!strcmp(type, "UUID"))
195 if (!strcmp(type, "TYPE"))
197 if (!strcmp(type, "LABEL"))
200 blkid_tag_iterate_end(iter);
202 /* Get the mount point */
204 retval = ext2fs_check_mount_point(devname, &mount_flags,
207 if (mount_flags & EXT2_MF_MOUNTED) {
209 strcpy(mtpt, "(mounted, mtpt unknown)");
210 } else if (mount_flags & EXT2_MF_BUSY)
211 strcpy(mtpt, "(in use)");
213 strcpy(mtpt, "(not mounted)");
216 pretty_print_line(devname, fs_type, label, mtpt, uuid);
219 static void print_tags(blkid_dev dev, char *show[], int numtag, int output)
221 blkid_tag_iterate iter;
222 const char *type, *value;
228 if (output & OUTPUT_PRETTY_LIST) {
229 pretty_print_dev(dev);
233 if (output & OUTPUT_DEVICE_ONLY) {
234 printf("%s\n", blkid_dev_devname(dev));
238 iter = blkid_tag_iterate_begin(dev);
239 while (blkid_tag_next(iter, &type, &value) == 0) {
240 if (numtag && show) {
241 for (i=0; i < numtag; i++)
242 if (!strcmp(type, show[i]))
247 if (output & OUTPUT_VALUE_ONLY) {
248 fputs(value, stdout);
252 printf("%s: ", blkid_dev_devname(dev));
256 fputs("=\"", stdout);
257 safe_print(value, -1);
258 fputs("\" ", stdout);
261 blkid_tag_iterate_end(iter);
263 if (!first && !(output & OUTPUT_VALUE_ONLY))
267 int main(int argc, char **argv)
269 blkid_cache cache = NULL;
270 char *devices[128] = { NULL, };
271 char *show[128] = { NULL, };
272 char *search_type = NULL, *search_value = NULL;
275 unsigned int numdev = 0, numtag = 0;
279 int output_format = 0;
280 int lookup = 0, gc = 0;
283 while ((c = getopt (argc, argv, "c:f:ghlLo:s:t:w:v")) != EOF)
286 if (optarg && !*optarg)
297 output_format = OUTPUT_PRETTY_LIST;
303 if (!strcmp(optarg, "value"))
304 output_format = OUTPUT_VALUE_ONLY;
305 else if (!strcmp(optarg, "device"))
306 output_format = OUTPUT_DEVICE_ONLY;
307 else if (!strcmp(optarg, "list"))
308 output_format = OUTPUT_PRETTY_LIST;
309 else if (!strcmp(optarg, "full"))
312 fprintf(stderr, "Invalid output format %s. "
313 "Choose from value,\n\t"
314 "device, list, or full\n", optarg);
319 if (numtag >= sizeof(show) / sizeof(*show)) {
320 fprintf(stderr, "Too many tags specified\n");
323 show[numtag++] = optarg;
327 fprintf(stderr, "Can only search for "
328 "one NAME=value pair\n");
331 if (blkid_parse_tag_string(optarg,
334 fprintf(stderr, "-t needs NAME=value pair\n");
342 if (optarg && !*optarg)
353 while (optind < argc)
354 devices[numdev++] = argv[optind++];
357 print_version(stdout);
361 if (blkid_get_cache(&cache, read) < 0)
366 blkid_gc_cache(cache);
369 if (output_format & OUTPUT_PRETTY_LIST)
370 pretty_print_dev(NULL);
376 fprintf(stderr, "The lookup option requires a "
377 "search type specified using -t\n");
380 /* Load any additional devices not in the cache */
381 for (i = 0; i < numdev; i++)
382 blkid_get_dev(cache, devices[i], BLKID_DEV_NORMAL);
384 if ((dev = blkid_find_dev_with_tag(cache, search_type,
386 print_tags(dev, show, numtag, output_format);
389 /* If we didn't specify a single device, show all available devices */
390 } else if (!numdev) {
391 blkid_dev_iterate iter;
394 blkid_probe_all(cache);
396 iter = blkid_dev_iterate_begin(cache);
397 blkid_dev_set_search(iter, search_type, search_value);
398 while (blkid_dev_next(iter, &dev) == 0) {
399 dev = blkid_verify(cache, dev);
402 print_tags(dev, show, numtag, output_format);
405 blkid_dev_iterate_end(iter);
406 /* Add all specified devices to cache (optionally display tags) */
407 } else for (i = 0; i < numdev; i++) {
408 blkid_dev dev = blkid_get_dev(cache, devices[i],
413 !blkid_dev_has_tag(dev, search_type,
416 print_tags(dev, show, numtag, output_format);
424 blkid_put_cache(cache);