]>
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 | ||
7a603aa8 TT |
41 | const char *progname = "blkid"; |
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; | |
103 | ||
104 | #ifdef TIOCGSIZE | |
36f4c408 TT |
105 | if (ioctl (0, TIOCGSIZE, &t_win) == 0) |
106 | return (t_win.ts_cols); | |
3d05802f TT |
107 | #endif |
108 | #ifdef TIOCGWINSZ | |
36f4c408 TT |
109 | if (ioctl (0, TIOCGWINSZ, &w_win) == 0) |
110 | return (w_win.ws_col); | |
3d05802f TT |
111 | #endif |
112 | cp = getenv("COLUMNS"); | |
113 | if (cp) | |
114 | return strtol(cp, NULL, 10); | |
115 | return 80; | |
116 | } | |
117 | ||
118 | static int pretty_print_word(const char *str, int max_len, | |
119 | int left_len, int overflow_nl) | |
120 | { | |
121 | int len = strlen(str) + left_len; | |
122 | int ret = 0; | |
123 | ||
124 | fputs(str, stdout); | |
125 | if (overflow_nl && len > max_len) { | |
126 | fputc('\n', stdout); | |
127 | len = 0; | |
128 | } else if (len > max_len) | |
129 | ret = len - max_len; | |
130 | do | |
131 | fputc(' ', stdout); | |
132 | while (len++ < max_len); | |
133 | return ret; | |
134 | } | |
135 | ||
136 | static void pretty_print_line(const char *device, const char *fs_type, | |
137 | const char *label, const char *mtpt, | |
138 | const char *uuid) | |
139 | { | |
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; | |
143 | int len, w; | |
144 | ||
145 | if (term_width < 0) | |
146 | term_width = get_terminal_width(); | |
147 | ||
148 | if (term_width > 80) { | |
149 | term_width -= 80; | |
150 | w = term_width / 10; | |
151 | if (w > 8) | |
152 | w = 8; | |
153 | term_width -= 2*w; | |
154 | label_len += w; | |
155 | fs_type_len += w; | |
156 | w = term_width/2; | |
157 | device_len += w; | |
158 | mtpt_len +=w; | |
159 | } | |
160 | ||
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); | |
165 | fputs(uuid, stdout); | |
166 | fputc('\n', stdout); | |
167 | } | |
168 | ||
169 | static void pretty_print_dev(blkid_dev dev) | |
170 | { | |
171 | blkid_tag_iterate iter; | |
172 | const char *type, *value, *devname; | |
173 | const char *uuid = "", *fs_type = "", *label = ""; | |
3d05802f TT |
174 | int len, mount_flags; |
175 | char mtpt[80]; | |
176 | errcode_t retval; | |
177 | ||
178 | if (dev == NULL) { | |
179 | pretty_print_line("device", "fs_type", "label", | |
180 | "mount point", "UUID"); | |
181 | for (len=get_terminal_width()-1; len > 0; len--) | |
182 | fputc('-', stdout); | |
183 | fputc('\n', stdout); | |
184 | return; | |
185 | } | |
186 | ||
187 | devname = blkid_dev_devname(dev); | |
188 | if (access(devname, F_OK)) | |
189 | return; | |
190 | ||
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")) | |
195 | uuid = value; | |
196 | if (!strcmp(type, "TYPE")) | |
197 | fs_type = value; | |
198 | if (!strcmp(type, "LABEL")) | |
199 | label = value; | |
200 | } | |
201 | blkid_tag_iterate_end(iter); | |
202 | ||
203 | /* Get the mount point */ | |
204 | mtpt[0] = 0; | |
205 | retval = ext2fs_check_mount_point(devname, &mount_flags, | |
206 | mtpt, sizeof(mtpt)); | |
207 | if (retval == 0) { | |
208 | if (mount_flags & EXT2_MF_MOUNTED) { | |
209 | if (!mtpt[0]) | |
210 | strcpy(mtpt, "(mounted, mtpt unknown)"); | |
211 | } else if (mount_flags & EXT2_MF_BUSY) | |
212 | strcpy(mtpt, "(in use)"); | |
213 | else | |
214 | strcpy(mtpt, "(not mounted)"); | |
215 | } | |
216 | ||
217 | pretty_print_line(devname, fs_type, label, mtpt, uuid); | |
218 | } | |
219 | ||
8927998f | 220 | static void print_tags(blkid_dev dev, char *show[], int numtag, int output) |
e12f2ae7 | 221 | { |
7a603aa8 TT |
222 | blkid_tag_iterate iter; |
223 | const char *type, *value; | |
ed1b33e8 | 224 | int i, first = 1; |
e12f2ae7 TT |
225 | |
226 | if (!dev) | |
227 | return; | |
228 | ||
3d05802f TT |
229 | if (output & OUTPUT_PRETTY_LIST) { |
230 | pretty_print_dev(dev); | |
231 | return; | |
232 | } | |
233 | ||
8927998f TT |
234 | if (output & OUTPUT_DEVICE_ONLY) { |
235 | printf("%s\n", blkid_dev_devname(dev)); | |
236 | return; | |
237 | } | |
238 | ||
7a603aa8 TT |
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])) | |
244 | break; | |
245 | if (i >= numtag) | |
246 | continue; | |
247 | } | |
f8efcda2 TT |
248 | if (output & OUTPUT_VALUE_ONLY) { |
249 | fputs(value, stdout); | |
250 | fputc('\n', stdout); | |
251 | } else { | |
252 | if (first) { | |
253 | printf("%s: ", blkid_dev_devname(dev)); | |
254 | first = 0; | |
255 | } | |
256 | fputs(type, stdout); | |
257 | fputs("=\"", stdout); | |
258 | safe_print(value, -1); | |
259 | fputs("\" ", stdout); | |
e12f2ae7 TT |
260 | } |
261 | } | |
7a603aa8 | 262 | blkid_tag_iterate_end(iter); |
e12f2ae7 | 263 | |
8927998f | 264 | if (!first && !(output & OUTPUT_VALUE_ONLY)) |
e12f2ae7 TT |
265 | printf("\n"); |
266 | } | |
267 | ||
268 | int main(int argc, char **argv) | |
269 | { | |
7a603aa8 | 270 | blkid_cache cache = NULL; |
e12f2ae7 TT |
271 | char *devices[128] = { NULL, }; |
272 | char *show[128] = { NULL, }; | |
7a603aa8 | 273 | char *search_type = NULL, *search_value = NULL; |
e12f2ae7 TT |
274 | char *read = NULL; |
275 | char *write = NULL; | |
54434927 | 276 | unsigned int numdev = 0, numtag = 0; |
e12f2ae7 TT |
277 | int version = 0; |
278 | int err = 4; | |
54434927 | 279 | unsigned int i; |
8927998f | 280 | int output_format = 0; |
46100e3f | 281 | int lookup = 0, gc = 0; |
0af8c33b | 282 | int c; |
e12f2ae7 | 283 | |
3d05802f | 284 | while ((c = getopt (argc, argv, "c:f:ghlLo:s:t:w:v")) != EOF) |
e12f2ae7 | 285 | switch (c) { |
e12f2ae7 TT |
286 | case 'c': |
287 | if (optarg && !*optarg) | |
288 | read = NULL; | |
289 | else | |
290 | read = optarg; | |
291 | if (!write) | |
292 | write = read; | |
293 | break; | |
ed6acfa3 TT |
294 | case 'l': |
295 | lookup++; | |
296 | break; | |
3d05802f TT |
297 | case 'L': |
298 | output_format = OUTPUT_PRETTY_LIST; | |
299 | break; | |
46100e3f TT |
300 | case 'g': |
301 | gc = 1; | |
302 | break; | |
8927998f TT |
303 | case 'o': |
304 | if (!strcmp(optarg, "value")) | |
305 | output_format = OUTPUT_VALUE_ONLY; | |
306 | else if (!strcmp(optarg, "device")) | |
307 | output_format = OUTPUT_DEVICE_ONLY; | |
3d05802f TT |
308 | else if (!strcmp(optarg, "list")) |
309 | output_format = OUTPUT_PRETTY_LIST; | |
8927998f TT |
310 | else if (!strcmp(optarg, "full")) |
311 | output_format = 0; | |
312 | else { | |
3d05802f TT |
313 | fprintf(stderr, "Invalid output format %s. " |
314 | "Choose from value,\n\t" | |
315 | "device, list, or full\n", optarg); | |
8927998f TT |
316 | exit(1); |
317 | } | |
318 | break; | |
e12f2ae7 TT |
319 | case 's': |
320 | if (numtag >= sizeof(show) / sizeof(*show)) { | |
321 | fprintf(stderr, "Too many tags specified\n"); | |
322 | usage(err); | |
323 | } | |
324 | show[numtag++] = optarg; | |
325 | break; | |
326 | case 't': | |
7a603aa8 | 327 | if (search_type) { |
e12f2ae7 TT |
328 | fprintf(stderr, "Can only search for " |
329 | "one NAME=value pair\n"); | |
330 | usage(err); | |
331 | } | |
7a603aa8 TT |
332 | if (blkid_parse_tag_string(optarg, |
333 | &search_type, | |
334 | &search_value)) { | |
e12f2ae7 TT |
335 | fprintf(stderr, "-t needs NAME=value pair\n"); |
336 | usage(err); | |
337 | } | |
338 | break; | |
339 | case 'v': | |
340 | version = 1; | |
341 | break; | |
342 | case 'w': | |
343 | if (optarg && !*optarg) | |
344 | write = NULL; | |
345 | else | |
346 | write = optarg; | |
347 | break; | |
348 | case 'h': | |
349 | err = 0; | |
350 | default: | |
351 | usage(err); | |
352 | } | |
353 | ||
354 | while (optind < argc) | |
355 | devices[numdev++] = argv[optind++]; | |
356 | ||
357 | if (version) { | |
358 | print_version(stdout); | |
359 | goto exit; | |
360 | } | |
361 | ||
50b380b4 | 362 | if (blkid_get_cache(&cache, read) < 0) |
e12f2ae7 TT |
363 | goto exit; |
364 | ||
365 | err = 2; | |
46100e3f TT |
366 | if (gc) { |
367 | blkid_gc_cache(cache); | |
3d05802f TT |
368 | goto exit; |
369 | } | |
370 | if (output_format & OUTPUT_PRETTY_LIST) | |
371 | pretty_print_dev(NULL); | |
372 | ||
373 | if (lookup) { | |
ed6acfa3 TT |
374 | blkid_dev dev; |
375 | ||
376 | if (!search_type) { | |
377 | fprintf(stderr, "The lookup option requires a " | |
378 | "search type specified using -t\n"); | |
379 | exit(1); | |
380 | } | |
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); | |
384 | ||
385 | if ((dev = blkid_find_dev_with_tag(cache, search_type, | |
386 | search_value))) { | |
387 | print_tags(dev, show, numtag, output_format); | |
388 | err = 0; | |
389 | } | |
e12f2ae7 | 390 | /* If we didn't specify a single device, show all available devices */ |
ed6acfa3 | 391 | } else if (!numdev) { |
7a603aa8 TT |
392 | blkid_dev_iterate iter; |
393 | blkid_dev dev; | |
e12f2ae7 | 394 | |
50b380b4 | 395 | blkid_probe_all(cache); |
e12f2ae7 | 396 | |
7a603aa8 | 397 | iter = blkid_dev_iterate_begin(cache); |
c37543df | 398 | blkid_dev_set_search(iter, search_type, search_value); |
7a603aa8 | 399 | while (blkid_dev_next(iter, &dev) == 0) { |
18d12963 TT |
400 | dev = blkid_verify(cache, dev); |
401 | if (!dev) | |
402 | continue; | |
8927998f | 403 | print_tags(dev, show, numtag, output_format); |
e12f2ae7 TT |
404 | err = 0; |
405 | } | |
7a603aa8 | 406 | blkid_dev_iterate_end(iter); |
e12f2ae7 TT |
407 | /* Add all specified devices to cache (optionally display tags) */ |
408 | } else for (i = 0; i < numdev; i++) { | |
98999c39 | 409 | blkid_dev dev = blkid_get_dev(cache, devices[i], |
50b380b4 | 410 | BLKID_DEV_NORMAL); |
e12f2ae7 TT |
411 | |
412 | if (dev) { | |
efc6f628 TT |
413 | if (search_type && |
414 | !blkid_dev_has_tag(dev, search_type, | |
c37543df | 415 | search_value)) |
18d12963 | 416 | continue; |
8927998f | 417 | print_tags(dev, show, numtag, output_format); |
e12f2ae7 TT |
418 | err = 0; |
419 | } | |
420 | } | |
421 | ||
422 | exit: | |
45e338f5 JM |
423 | free(search_type); |
424 | free(search_value); | |
50b380b4 | 425 | blkid_put_cache(cache); |
e12f2ae7 TT |
426 | return err; |
427 | } |