]>
git.ipfire.org Git - thirdparty/e2fsprogs.git/blob - misc/blkid.c
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 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;
146 term_width
= get_terminal_width();
148 if (term_width
> 80) {
161 len
= pretty_print_word(device
, device_len
, 0, 1);
162 len
= pretty_print_word(fs_type
, fs_type_len
, len
, 0);
163 len
= pretty_print_word(label
, label_len
, len
, 0);
164 len
= pretty_print_word(mtpt
, mtpt_len
, len
, 0);
169 static void pretty_print_dev(blkid_dev dev
)
171 blkid_tag_iterate iter
;
172 const char *type
, *value
, *devname
;
173 const char *uuid
= "", *fs_type
= "", *label
= "";
174 int len
, mount_flags
;
179 pretty_print_line("device", "fs_type", "label",
180 "mount point", "UUID");
181 for (len
=get_terminal_width()-1; len
> 0; len
--)
187 devname
= blkid_dev_devname(dev
);
188 if (access(devname
, F_OK
))
191 /* Get the uuid, label, type */
192 iter
= blkid_tag_iterate_begin(dev
);
193 while (blkid_tag_next(iter
, &type
, &value
) == 0) {
194 if (!strcmp(type
, "UUID"))
196 if (!strcmp(type
, "TYPE"))
198 if (!strcmp(type
, "LABEL"))
201 blkid_tag_iterate_end(iter
);
203 /* Get the mount point */
205 retval
= ext2fs_check_mount_point(devname
, &mount_flags
,
208 if (mount_flags
& EXT2_MF_MOUNTED
) {
210 strcpy(mtpt
, "(mounted, mtpt unknown)");
211 } else if (mount_flags
& EXT2_MF_BUSY
)
212 strcpy(mtpt
, "(in use)");
214 strcpy(mtpt
, "(not mounted)");
217 pretty_print_line(devname
, fs_type
, label
, mtpt
, uuid
);
220 static void print_tags(blkid_dev dev
, char *show
[], int numtag
, int output
)
222 blkid_tag_iterate iter
;
223 const char *type
, *value
;
229 if (output
& OUTPUT_PRETTY_LIST
) {
230 pretty_print_dev(dev
);
234 if (output
& OUTPUT_DEVICE_ONLY
) {
235 printf("%s\n", blkid_dev_devname(dev
));
239 iter
= blkid_tag_iterate_begin(dev
);
240 while (blkid_tag_next(iter
, &type
, &value
) == 0) {
241 if (numtag
&& show
) {
242 for (i
=0; i
< numtag
; i
++)
243 if (!strcmp(type
, show
[i
]))
248 if (output
& OUTPUT_VALUE_ONLY
) {
249 fputs(value
, stdout
);
253 printf("%s: ", blkid_dev_devname(dev
));
257 fputs("=\"", stdout
);
258 safe_print(value
, -1);
259 fputs("\" ", stdout
);
262 blkid_tag_iterate_end(iter
);
264 if (!first
&& !(output
& OUTPUT_VALUE_ONLY
))
268 int main(int argc
, char **argv
)
270 blkid_cache cache
= NULL
;
271 char *devices
[128] = { NULL
, };
272 char *show
[128] = { NULL
, };
273 char *search_type
= NULL
, *search_value
= NULL
;
276 unsigned int numdev
= 0, numtag
= 0;
280 int output_format
= 0;
281 int lookup
= 0, gc
= 0;
284 while ((c
= getopt (argc
, argv
, "c:f:ghlLo:s:t:w:v")) != EOF
)
287 if (optarg
&& !*optarg
)
298 output_format
= OUTPUT_PRETTY_LIST
;
304 if (!strcmp(optarg
, "value"))
305 output_format
= OUTPUT_VALUE_ONLY
;
306 else if (!strcmp(optarg
, "device"))
307 output_format
= OUTPUT_DEVICE_ONLY
;
308 else if (!strcmp(optarg
, "list"))
309 output_format
= OUTPUT_PRETTY_LIST
;
310 else if (!strcmp(optarg
, "full"))
313 fprintf(stderr
, "Invalid output format %s. "
314 "Choose from value,\n\t"
315 "device, list, or full\n", optarg
);
320 if (numtag
>= sizeof(show
) / sizeof(*show
)) {
321 fprintf(stderr
, "Too many tags specified\n");
324 show
[numtag
++] = optarg
;
328 fprintf(stderr
, "Can only search for "
329 "one NAME=value pair\n");
332 if (blkid_parse_tag_string(optarg
,
335 fprintf(stderr
, "-t needs NAME=value pair\n");
343 if (optarg
&& !*optarg
)
354 while (optind
< argc
)
355 devices
[numdev
++] = argv
[optind
++];
358 print_version(stdout
);
362 if (blkid_get_cache(&cache
, read
) < 0)
367 blkid_gc_cache(cache
);
370 if (output_format
& OUTPUT_PRETTY_LIST
)
371 pretty_print_dev(NULL
);
377 fprintf(stderr
, "The lookup option requires a "
378 "search type specified using -t\n");
381 /* Load any additional devices not in the cache */
382 for (i
= 0; i
< numdev
; i
++)
383 blkid_get_dev(cache
, devices
[i
], BLKID_DEV_NORMAL
);
385 if ((dev
= blkid_find_dev_with_tag(cache
, search_type
,
387 print_tags(dev
, show
, numtag
, output_format
);
390 /* If we didn't specify a single device, show all available devices */
391 } else if (!numdev
) {
392 blkid_dev_iterate iter
;
395 blkid_probe_all(cache
);
397 iter
= blkid_dev_iterate_begin(cache
);
398 blkid_dev_set_search(iter
, search_type
, search_value
);
399 while (blkid_dev_next(iter
, &dev
) == 0) {
400 dev
= blkid_verify(cache
, dev
);
403 print_tags(dev
, show
, numtag
, output_format
);
406 blkid_dev_iterate_end(iter
);
407 /* Add all specified devices to cache (optionally display tags) */
408 } else for (i
= 0; i
< numdev
; i
++) {
409 blkid_dev dev
= blkid_get_dev(cache
, devices
[i
],
414 !blkid_dev_has_tag(dev
, search_type
,
417 print_tags(dev
, show
, numtag
, output_format
);
425 blkid_put_cache(cache
);