]>
Commit | Line | Data |
---|---|---|
e12f2ae7 TT |
1 | /* |
2 | * blkid.c - User command-line interface for libblkid | |
3 | * | |
4 | * Copyright (C) 2001 Andreas Dilger | |
5 | * | |
6 | * %Begin-Header% | |
7 | * This file may be redistributed under the terms of the | |
8 | * GNU Lesser General Public License. | |
9 | * %End-Header% | |
10 | */ | |
11 | ||
d1154eb4 | 12 | #include "config.h" |
e12f2ae7 | 13 | #include <stdio.h> |
7a603aa8 | 14 | #include <stdlib.h> |
3d05802f | 15 | #include <unistd.h> |
48e6e813 | 16 | #include <string.h> |
3d05802f TT |
17 | #ifdef HAVE_TERMIOS_H |
18 | #include <termios.h> | |
19 | #endif | |
20 | #ifdef HAVE_TERMIO_H | |
21 | #include <termio.h> | |
22 | #endif | |
23 | #ifdef HAVE_SYS_IOCTL_H | |
24 | #include <sys/ioctl.h> | |
25 | #endif | |
e12f2ae7 TT |
26 | #ifdef HAVE_GETOPT_H |
27 | #include <getopt.h> | |
28 | #else | |
a6383027 | 29 | extern int getopt(int argc, char * const argv[], const char *optstring); |
e12f2ae7 TT |
30 | extern char *optarg; |
31 | extern int optind; | |
32 | #endif | |
33 | ||
8927998f TT |
34 | #define OUTPUT_VALUE_ONLY 0x0001 |
35 | #define OUTPUT_DEVICE_ONLY 0x0002 | |
3d05802f | 36 | #define OUTPUT_PRETTY_LIST 0x0004 |
8927998f | 37 | |
3d05802f | 38 | #include "ext2fs/ext2fs.h" |
e12f2ae7 TT |
39 | #include "blkid/blkid.h" |
40 | ||
6596c997 | 41 | static const char *progname = "blkid"; |
7a603aa8 TT |
42 | |
43 | static void print_version(FILE *out) | |
e12f2ae7 | 44 | { |
54434927 | 45 | fprintf(out, "%s %s (%s)\n", progname, BLKID_VERSION, BLKID_DATE); |
e12f2ae7 TT |
46 | } |
47 | ||
7a603aa8 | 48 | static void usage(int error) |
e12f2ae7 TT |
49 | { |
50 | FILE *out = error ? stderr : stdout; | |
51 | ||
52 | print_version(out); | |
53 | fprintf(out, | |
3d05802f TT |
54 | "usage:\t%s [-c <file>] [-ghlLv] [-o format] " |
55 | "[-s <tag>] [-t <token>]\n [-w <file>] [dev ...]\n" | |
e12f2ae7 TT |
56 | "\t-c\tcache file (default: /etc/blkid.tab, /dev/null = none)\n" |
57 | "\t-h\tprint this usage message and exit\n" | |
46100e3f | 58 | "\t-g\tgarbage collect the blkid cache\n" |
e12f2ae7 TT |
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" | |
ed6acfa3 | 61 | "\t-l\tlookup the the first device with arguments specified by -t\n" |
e12f2ae7 TT |
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", | |
65 | progname); | |
66 | exit(error); | |
67 | } | |
68 | ||
f8efcda2 TT |
69 | /* |
70 | * This function does "safe" printing. It will convert non-printable | |
71 | * ASCII characters using '^' and M- notation. | |
72 | */ | |
73 | static void safe_print(const char *cp, int len) | |
74 | { | |
75 | unsigned char ch; | |
76 | ||
77 | if (len < 0) | |
78 | len = strlen(cp); | |
79 | ||
80 | while (len--) { | |
81 | ch = *cp++; | |
82 | if (ch > 128) { | |
83 | fputs("M-", stdout); | |
84 | ch -= 128; | |
85 | } | |
86 | if ((ch < 32) || (ch == 0x7f)) { | |
87 | fputc('^', stdout); | |
88 | ch ^= 0x40; /* ^@, ^A, ^B; ^? for DEL */ | |
89 | } | |
90 | fputc(ch, stdout); | |
91 | } | |
92 | } | |
93 | ||
3d05802f TT |
94 | static int get_terminal_width(void) |
95 | { | |
96 | #ifdef TIOCGSIZE | |
36f4c408 | 97 | struct ttysize t_win; |
3d05802f TT |
98 | #endif |
99 | #ifdef TIOCGWINSZ | |
36f4c408 | 100 | struct winsize w_win; |
3d05802f TT |
101 | #endif |
102 | const char *cp; | |
5bb4d0ce | 103 | int width = 80; |
3d05802f TT |
104 | |
105 | #ifdef TIOCGSIZE | |
5bb4d0ce TT |
106 | if (ioctl (0, TIOCGSIZE, &t_win) == 0) { |
107 | width = t_win.ts_cols; | |
108 | goto got_it; | |
109 | } | |
3d05802f TT |
110 | #endif |
111 | #ifdef TIOCGWINSZ | |
5bb4d0ce TT |
112 | if (ioctl (0, TIOCGWINSZ, &w_win) == 0) { |
113 | width = w_win.ws_col; | |
114 | goto got_it; | |
115 | } | |
3d05802f TT |
116 | #endif |
117 | cp = getenv("COLUMNS"); | |
118 | if (cp) | |
5bb4d0ce TT |
119 | width = atoi(cp); |
120 | got_it: | |
121 | if (width > 4096) | |
122 | return 4096; /* sanity check */ | |
123 | return width; | |
3d05802f TT |
124 | } |
125 | ||
126 | static int pretty_print_word(const char *str, int max_len, | |
127 | int left_len, int overflow_nl) | |
128 | { | |
129 | int len = strlen(str) + left_len; | |
130 | int ret = 0; | |
131 | ||
132 | fputs(str, stdout); | |
133 | if (overflow_nl && len > max_len) { | |
134 | fputc('\n', stdout); | |
135 | len = 0; | |
136 | } else if (len > max_len) | |
137 | ret = len - max_len; | |
6596c997 | 138 | do { |
3d05802f | 139 | fputc(' ', stdout); |
6596c997 | 140 | } while (len++ < max_len); |
3d05802f TT |
141 | return ret; |
142 | } | |
143 | ||
144 | static void pretty_print_line(const char *device, const char *fs_type, | |
145 | const char *label, const char *mtpt, | |
146 | const char *uuid) | |
147 | { | |
148 | static int device_len = 10, fs_type_len = 7; | |
149 | static int label_len = 8, mtpt_len = 14; | |
150 | static int term_width = -1; | |
151 | int len, w; | |
152 | ||
2fc92dbc | 153 | if (term_width < 0) { |
3d05802f TT |
154 | term_width = get_terminal_width(); |
155 | ||
2fc92dbc TT |
156 | if (term_width > 80) { |
157 | term_width -= 80; | |
158 | w = term_width / 10; | |
159 | if (w > 8) | |
160 | w = 8; | |
161 | term_width -= 2*w; | |
162 | label_len += w; | |
163 | fs_type_len += w; | |
164 | w = term_width/2; | |
165 | device_len += w; | |
166 | mtpt_len +=w; | |
167 | } | |
3d05802f TT |
168 | } |
169 | ||
170 | len = pretty_print_word(device, device_len, 0, 1); | |
171 | len = pretty_print_word(fs_type, fs_type_len, len, 0); | |
172 | len = pretty_print_word(label, label_len, len, 0); | |
173 | len = pretty_print_word(mtpt, mtpt_len, len, 0); | |
174 | fputs(uuid, stdout); | |
175 | fputc('\n', stdout); | |
176 | } | |
177 | ||
178 | static void pretty_print_dev(blkid_dev dev) | |
179 | { | |
180 | blkid_tag_iterate iter; | |
181 | const char *type, *value, *devname; | |
182 | const char *uuid = "", *fs_type = "", *label = ""; | |
3d05802f TT |
183 | int len, mount_flags; |
184 | char mtpt[80]; | |
185 | errcode_t retval; | |
186 | ||
187 | if (dev == NULL) { | |
188 | pretty_print_line("device", "fs_type", "label", | |
189 | "mount point", "UUID"); | |
190 | for (len=get_terminal_width()-1; len > 0; len--) | |
191 | fputc('-', stdout); | |
192 | fputc('\n', stdout); | |
193 | return; | |
194 | } | |
195 | ||
196 | devname = blkid_dev_devname(dev); | |
197 | if (access(devname, F_OK)) | |
198 | return; | |
199 | ||
200 | /* Get the uuid, label, type */ | |
201 | iter = blkid_tag_iterate_begin(dev); | |
202 | while (blkid_tag_next(iter, &type, &value) == 0) { | |
203 | if (!strcmp(type, "UUID")) | |
204 | uuid = value; | |
205 | if (!strcmp(type, "TYPE")) | |
206 | fs_type = value; | |
207 | if (!strcmp(type, "LABEL")) | |
208 | label = value; | |
209 | } | |
210 | blkid_tag_iterate_end(iter); | |
211 | ||
212 | /* Get the mount point */ | |
213 | mtpt[0] = 0; | |
214 | retval = ext2fs_check_mount_point(devname, &mount_flags, | |
215 | mtpt, sizeof(mtpt)); | |
216 | if (retval == 0) { | |
217 | if (mount_flags & EXT2_MF_MOUNTED) { | |
218 | if (!mtpt[0]) | |
219 | strcpy(mtpt, "(mounted, mtpt unknown)"); | |
220 | } else if (mount_flags & EXT2_MF_BUSY) | |
221 | strcpy(mtpt, "(in use)"); | |
222 | else | |
223 | strcpy(mtpt, "(not mounted)"); | |
224 | } | |
225 | ||
226 | pretty_print_line(devname, fs_type, label, mtpt, uuid); | |
227 | } | |
228 | ||
8927998f | 229 | static void print_tags(blkid_dev dev, char *show[], int numtag, int output) |
e12f2ae7 | 230 | { |
7a603aa8 TT |
231 | blkid_tag_iterate iter; |
232 | const char *type, *value; | |
ed1b33e8 | 233 | int i, first = 1; |
e12f2ae7 TT |
234 | |
235 | if (!dev) | |
236 | return; | |
237 | ||
3d05802f TT |
238 | if (output & OUTPUT_PRETTY_LIST) { |
239 | pretty_print_dev(dev); | |
240 | return; | |
241 | } | |
242 | ||
8927998f TT |
243 | if (output & OUTPUT_DEVICE_ONLY) { |
244 | printf("%s\n", blkid_dev_devname(dev)); | |
245 | return; | |
246 | } | |
247 | ||
7a603aa8 TT |
248 | iter = blkid_tag_iterate_begin(dev); |
249 | while (blkid_tag_next(iter, &type, &value) == 0) { | |
250 | if (numtag && show) { | |
251 | for (i=0; i < numtag; i++) | |
252 | if (!strcmp(type, show[i])) | |
253 | break; | |
254 | if (i >= numtag) | |
255 | continue; | |
256 | } | |
f8efcda2 TT |
257 | if (output & OUTPUT_VALUE_ONLY) { |
258 | fputs(value, stdout); | |
259 | fputc('\n', stdout); | |
260 | } else { | |
261 | if (first) { | |
262 | printf("%s: ", blkid_dev_devname(dev)); | |
263 | first = 0; | |
264 | } | |
265 | fputs(type, stdout); | |
266 | fputs("=\"", stdout); | |
267 | safe_print(value, -1); | |
268 | fputs("\" ", stdout); | |
e12f2ae7 TT |
269 | } |
270 | } | |
7a603aa8 | 271 | blkid_tag_iterate_end(iter); |
e12f2ae7 | 272 | |
8927998f | 273 | if (!first && !(output & OUTPUT_VALUE_ONLY)) |
e12f2ae7 TT |
274 | printf("\n"); |
275 | } | |
276 | ||
277 | int main(int argc, char **argv) | |
278 | { | |
7a603aa8 | 279 | blkid_cache cache = NULL; |
e12f2ae7 TT |
280 | char *devices[128] = { NULL, }; |
281 | char *show[128] = { NULL, }; | |
7a603aa8 | 282 | char *search_type = NULL, *search_value = NULL; |
e12f2ae7 TT |
283 | char *read = NULL; |
284 | char *write = NULL; | |
54434927 | 285 | unsigned int numdev = 0, numtag = 0; |
e12f2ae7 TT |
286 | int version = 0; |
287 | int err = 4; | |
54434927 | 288 | unsigned int i; |
8927998f | 289 | int output_format = 0; |
46100e3f | 290 | int lookup = 0, gc = 0; |
0af8c33b | 291 | int c; |
e12f2ae7 | 292 | |
3d05802f | 293 | while ((c = getopt (argc, argv, "c:f:ghlLo:s:t:w:v")) != EOF) |
e12f2ae7 | 294 | switch (c) { |
e12f2ae7 | 295 | case 'c': |
e5397d7a | 296 | read = optarg; |
e12f2ae7 TT |
297 | if (!write) |
298 | write = read; | |
299 | break; | |
ed6acfa3 TT |
300 | case 'l': |
301 | lookup++; | |
302 | break; | |
3d05802f TT |
303 | case 'L': |
304 | output_format = OUTPUT_PRETTY_LIST; | |
305 | break; | |
46100e3f TT |
306 | case 'g': |
307 | gc = 1; | |
308 | break; | |
8927998f TT |
309 | case 'o': |
310 | if (!strcmp(optarg, "value")) | |
311 | output_format = OUTPUT_VALUE_ONLY; | |
312 | else if (!strcmp(optarg, "device")) | |
313 | output_format = OUTPUT_DEVICE_ONLY; | |
3d05802f TT |
314 | else if (!strcmp(optarg, "list")) |
315 | output_format = OUTPUT_PRETTY_LIST; | |
8927998f TT |
316 | else if (!strcmp(optarg, "full")) |
317 | output_format = 0; | |
318 | else { | |
3d05802f TT |
319 | fprintf(stderr, "Invalid output format %s. " |
320 | "Choose from value,\n\t" | |
321 | "device, list, or full\n", optarg); | |
8927998f TT |
322 | exit(1); |
323 | } | |
324 | break; | |
e12f2ae7 TT |
325 | case 's': |
326 | if (numtag >= sizeof(show) / sizeof(*show)) { | |
327 | fprintf(stderr, "Too many tags specified\n"); | |
328 | usage(err); | |
329 | } | |
330 | show[numtag++] = optarg; | |
331 | break; | |
332 | case 't': | |
7a603aa8 | 333 | if (search_type) { |
e12f2ae7 TT |
334 | fprintf(stderr, "Can only search for " |
335 | "one NAME=value pair\n"); | |
336 | usage(err); | |
337 | } | |
7a603aa8 TT |
338 | if (blkid_parse_tag_string(optarg, |
339 | &search_type, | |
340 | &search_value)) { | |
e12f2ae7 TT |
341 | fprintf(stderr, "-t needs NAME=value pair\n"); |
342 | usage(err); | |
343 | } | |
344 | break; | |
345 | case 'v': | |
346 | version = 1; | |
347 | break; | |
348 | case 'w': | |
e5397d7a | 349 | write = optarg; |
e12f2ae7 TT |
350 | break; |
351 | case 'h': | |
352 | err = 0; | |
46802efa | 353 | /* fallthrough */ |
e12f2ae7 TT |
354 | default: |
355 | usage(err); | |
356 | } | |
357 | ||
358 | while (optind < argc) | |
359 | devices[numdev++] = argv[optind++]; | |
360 | ||
361 | if (version) { | |
362 | print_version(stdout); | |
363 | goto exit; | |
364 | } | |
365 | ||
50b380b4 | 366 | if (blkid_get_cache(&cache, read) < 0) |
e12f2ae7 TT |
367 | goto exit; |
368 | ||
369 | err = 2; | |
46100e3f TT |
370 | if (gc) { |
371 | blkid_gc_cache(cache); | |
3d05802f TT |
372 | goto exit; |
373 | } | |
374 | if (output_format & OUTPUT_PRETTY_LIST) | |
375 | pretty_print_dev(NULL); | |
376 | ||
377 | if (lookup) { | |
ed6acfa3 TT |
378 | blkid_dev dev; |
379 | ||
380 | if (!search_type) { | |
381 | fprintf(stderr, "The lookup option requires a " | |
382 | "search type specified using -t\n"); | |
383 | exit(1); | |
384 | } | |
385 | /* Load any additional devices not in the cache */ | |
386 | for (i = 0; i < numdev; i++) | |
387 | blkid_get_dev(cache, devices[i], BLKID_DEV_NORMAL); | |
388 | ||
389 | if ((dev = blkid_find_dev_with_tag(cache, search_type, | |
390 | search_value))) { | |
391 | print_tags(dev, show, numtag, output_format); | |
392 | err = 0; | |
393 | } | |
e12f2ae7 | 394 | /* If we didn't specify a single device, show all available devices */ |
ed6acfa3 | 395 | } else if (!numdev) { |
7a603aa8 TT |
396 | blkid_dev_iterate iter; |
397 | blkid_dev dev; | |
e12f2ae7 | 398 | |
50b380b4 | 399 | blkid_probe_all(cache); |
e12f2ae7 | 400 | |
7a603aa8 | 401 | iter = blkid_dev_iterate_begin(cache); |
c37543df | 402 | blkid_dev_set_search(iter, search_type, search_value); |
7a603aa8 | 403 | while (blkid_dev_next(iter, &dev) == 0) { |
18d12963 TT |
404 | dev = blkid_verify(cache, dev); |
405 | if (!dev) | |
406 | continue; | |
8927998f | 407 | print_tags(dev, show, numtag, output_format); |
e12f2ae7 TT |
408 | err = 0; |
409 | } | |
7a603aa8 | 410 | blkid_dev_iterate_end(iter); |
e12f2ae7 TT |
411 | /* Add all specified devices to cache (optionally display tags) */ |
412 | } else for (i = 0; i < numdev; i++) { | |
98999c39 | 413 | blkid_dev dev = blkid_get_dev(cache, devices[i], |
50b380b4 | 414 | BLKID_DEV_NORMAL); |
e12f2ae7 TT |
415 | |
416 | if (dev) { | |
efc6f628 TT |
417 | if (search_type && |
418 | !blkid_dev_has_tag(dev, search_type, | |
c37543df | 419 | search_value)) |
18d12963 | 420 | continue; |
8927998f | 421 | print_tags(dev, show, numtag, output_format); |
e12f2ae7 TT |
422 | err = 0; |
423 | } | |
424 | } | |
425 | ||
426 | exit: | |
45e338f5 JM |
427 | free(search_type); |
428 | free(search_value); | |
50b380b4 | 429 | blkid_put_cache(cache); |
e12f2ae7 TT |
430 | return err; |
431 | } |