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.
23 #ifdef HAVE_SYS_IOCTL_H
24 #include <sys/ioctl.h>
29 extern int getopt(int argc, char * const argv[], const char *optstring);
34 #define OUTPUT_VALUE_ONLY 0x0001
35 #define OUTPUT_DEVICE_ONLY 0x0002
36 #define OUTPUT_PRETTY_LIST 0x0004
38 #include "ext2fs/ext2fs.h"
39 #include "blkid/blkid.h"
41 static const char *progname = "blkid";
43 static void print_version(FILE *out)
45 fprintf(out, "%s %s (%s)\n", progname, BLKID_VERSION, BLKID_DATE);
48 static void usage(int error)
50 FILE *out = error ? stderr : stdout;
54 "usage:\t%s [-c <file>] [-ghlLv] [-o format] "
55 "[-s <tag>] [-t <token>]\n [-w <file>] [dev ...]\n"
56 "\t-c\tcache file (default: /etc/blkid.tab, /dev/null = none)\n"
57 "\t-h\tprint this usage message and exit\n"
58 "\t-g\tgarbage collect the blkid cache\n"
59 "\t-s\tshow specified tag(s) (default show all tags)\n"
60 "\t-t\tfind device with a specific token (NAME=value pair)\n"
61 "\t-l\tlookup the the first device with arguments specified by -t\n"
62 "\t-v\tprint version and exit\n"
63 "\t-w\twrite cache to different file (/dev/null = no write)\n"
64 "\tdev\tspecify device(s) to probe (default: all devices)\n",
70 * This function does "safe" printing. It will convert non-printable
71 * ASCII characters using '^' and M- notation.
73 static void safe_print(const char *cp, int len)
86 if ((ch < 32) || (ch == 0x7f)) {
88 ch ^= 0x40; /* ^@, ^A, ^B; ^? for DEL */
94 static int get_terminal_width(void)
100 struct winsize w_win;
105 if (ioctl (0, TIOCGSIZE, &t_win) == 0)
106 return (t_win.ts_cols);
109 if (ioctl (0, TIOCGWINSZ, &w_win) == 0)
110 return (w_win.ws_col);
112 cp = getenv("COLUMNS");
114 return strtol(cp, NULL, 10);
118 static int pretty_print_word(const char *str, int max_len,
119 int left_len, int overflow_nl)
121 int len = strlen(str) + left_len;
125 if (overflow_nl && len > max_len) {
128 } else if (len > max_len)
132 } while (len++ < max_len);
136 static void pretty_print_line(const char *device, const char *fs_type,
137 const char *label, const char *mtpt,
140 static int device_len = 10, fs_type_len = 7;
141 static int label_len = 8, mtpt_len = 14;
142 static int term_width = -1;
145 if (term_width < 0) {
146 term_width = get_terminal_width();
148 if (term_width > 80) {
162 len = pretty_print_word(device, device_len, 0, 1);
163 len = pretty_print_word(fs_type, fs_type_len, len, 0);
164 len = pretty_print_word(label, label_len, len, 0);
165 len = pretty_print_word(mtpt, mtpt_len, len, 0);
170 static void pretty_print_dev(blkid_dev dev)
172 blkid_tag_iterate iter;
173 const char *type, *value, *devname;
174 const char *uuid = "", *fs_type = "", *label = "";
175 int len, mount_flags;
180 pretty_print_line("device", "fs_type", "label",
181 "mount point", "UUID");
182 for (len=get_terminal_width()-1; len > 0; len--)
188 devname = blkid_dev_devname(dev);
189 if (access(devname, F_OK))
192 /* Get the uuid, label, type */
193 iter = blkid_tag_iterate_begin(dev);
194 while (blkid_tag_next(iter, &type, &value) == 0) {
195 if (!strcmp(type, "UUID"))
197 if (!strcmp(type, "TYPE"))
199 if (!strcmp(type, "LABEL"))
202 blkid_tag_iterate_end(iter);
204 /* Get the mount point */
206 retval = ext2fs_check_mount_point(devname, &mount_flags,
209 if (mount_flags & EXT2_MF_MOUNTED) {
211 strcpy(mtpt, "(mounted, mtpt unknown)");
212 } else if (mount_flags & EXT2_MF_BUSY)
213 strcpy(mtpt, "(in use)");
215 strcpy(mtpt, "(not mounted)");
218 pretty_print_line(devname, fs_type, label, mtpt, uuid);
221 static void print_tags(blkid_dev dev, char *show[], int numtag, int output)
223 blkid_tag_iterate iter;
224 const char *type, *value;
230 if (output & OUTPUT_PRETTY_LIST) {
231 pretty_print_dev(dev);
235 if (output & OUTPUT_DEVICE_ONLY) {
236 printf("%s\n", blkid_dev_devname(dev));
240 iter = blkid_tag_iterate_begin(dev);
241 while (blkid_tag_next(iter, &type, &value) == 0) {
242 if (numtag && show) {
243 for (i=0; i < numtag; i++)
244 if (!strcmp(type, show[i]))
249 if (output & OUTPUT_VALUE_ONLY) {
250 fputs(value, stdout);
254 printf("%s: ", blkid_dev_devname(dev));
258 fputs("=\"", stdout);
259 safe_print(value, -1);
260 fputs("\" ", stdout);
263 blkid_tag_iterate_end(iter);
265 if (!first && !(output & OUTPUT_VALUE_ONLY))
269 int main(int argc, char **argv)
271 blkid_cache cache = NULL;
272 char *devices[128] = { NULL, };
273 char *show[128] = { NULL, };
274 char *search_type = NULL, *search_value = NULL;
277 unsigned int numdev = 0, numtag = 0;
281 int output_format = 0;
282 int lookup = 0, gc = 0;
285 while ((c = getopt (argc, argv, "c:f:ghlLo:s:t:w:v")) != EOF)
288 if (optarg && !*optarg)
299 output_format = OUTPUT_PRETTY_LIST;
305 if (!strcmp(optarg, "value"))
306 output_format = OUTPUT_VALUE_ONLY;
307 else if (!strcmp(optarg, "device"))
308 output_format = OUTPUT_DEVICE_ONLY;
309 else if (!strcmp(optarg, "list"))
310 output_format = OUTPUT_PRETTY_LIST;
311 else if (!strcmp(optarg, "full"))
314 fprintf(stderr, "Invalid output format %s. "
315 "Choose from value,\n\t"
316 "device, list, or full\n", optarg);
321 if (numtag >= sizeof(show) / sizeof(*show)) {
322 fprintf(stderr, "Too many tags specified\n");
325 show[numtag++] = optarg;
329 fprintf(stderr, "Can only search for "
330 "one NAME=value pair\n");
333 if (blkid_parse_tag_string(optarg,
336 fprintf(stderr, "-t needs NAME=value pair\n");
344 if (optarg && !*optarg)
356 while (optind < argc)
357 devices[numdev++] = argv[optind++];
360 print_version(stdout);
364 if (blkid_get_cache(&cache, read) < 0)
369 blkid_gc_cache(cache);
372 if (output_format & OUTPUT_PRETTY_LIST)
373 pretty_print_dev(NULL);
379 fprintf(stderr, "The lookup option requires a "
380 "search type specified using -t\n");
383 /* Load any additional devices not in the cache */
384 for (i = 0; i < numdev; i++)
385 blkid_get_dev(cache, devices[i], BLKID_DEV_NORMAL);
387 if ((dev = blkid_find_dev_with_tag(cache, search_type,
389 print_tags(dev, show, numtag, output_format);
392 /* If we didn't specify a single device, show all available devices */
393 } else if (!numdev) {
394 blkid_dev_iterate iter;
397 blkid_probe_all(cache);
399 iter = blkid_dev_iterate_begin(cache);
400 blkid_dev_set_search(iter, search_type, search_value);
401 while (blkid_dev_next(iter, &dev) == 0) {
402 dev = blkid_verify(cache, dev);
405 print_tags(dev, show, numtag, output_format);
408 blkid_dev_iterate_end(iter);
409 /* Add all specified devices to cache (optionally display tags) */
410 } else for (i = 0; i < numdev; i++) {
411 blkid_dev dev = blkid_get_dev(cache, devices[i],
416 !blkid_dev_has_tag(dev, search_type,
419 print_tags(dev, show, numtag, output_format);
427 blkid_put_cache(cache);