Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 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 | |
Theodore Ts'o | d1154eb | 2011-09-18 17:34:37 -0400 | [diff] [blame] | 12 | #include "config.h" |
Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 13 | #include <stdio.h> |
Theodore Ts'o | 7a603aa | 2003-01-26 01:54:39 -0500 | [diff] [blame] | 14 | #include <stdlib.h> |
Theodore Ts'o | 3d05802 | 2008-07-12 22:06:30 -0400 | [diff] [blame] | 15 | #include <unistd.h> |
Theodore Ts'o | 48e6e81 | 2003-07-06 00:36:48 -0400 | [diff] [blame] | 16 | #include <string.h> |
Theodore Ts'o | 3d05802 | 2008-07-12 22:06:30 -0400 | [diff] [blame] | 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 |
Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 26 | #ifdef HAVE_GETOPT_H |
| 27 | #include <getopt.h> |
| 28 | #else |
Matthias Andree | a638302 | 2006-05-30 16:27:45 +0200 | [diff] [blame] | 29 | extern int getopt(int argc, char * const argv[], const char *optstring); |
Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 30 | extern char *optarg; |
| 31 | extern int optind; |
| 32 | #endif |
| 33 | |
Theodore Ts'o | 8927998 | 2004-03-20 16:30:10 -0500 | [diff] [blame] | 34 | #define OUTPUT_VALUE_ONLY 0x0001 |
| 35 | #define OUTPUT_DEVICE_ONLY 0x0002 |
Theodore Ts'o | 3d05802 | 2008-07-12 22:06:30 -0400 | [diff] [blame] | 36 | #define OUTPUT_PRETTY_LIST 0x0004 |
Theodore Ts'o | 8927998 | 2004-03-20 16:30:10 -0500 | [diff] [blame] | 37 | |
Theodore Ts'o | 3d05802 | 2008-07-12 22:06:30 -0400 | [diff] [blame] | 38 | #include "ext2fs/ext2fs.h" |
Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 39 | #include "blkid/blkid.h" |
| 40 | |
Theodore Ts'o | 6596c99 | 2014-01-10 21:40:28 -0500 | [diff] [blame] | 41 | static const char *progname = "blkid"; |
Theodore Ts'o | 7a603aa | 2003-01-26 01:54:39 -0500 | [diff] [blame] | 42 | |
| 43 | static void print_version(FILE *out) |
Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 44 | { |
Theodore Ts'o | 5443492 | 2003-12-07 01:28:50 -0500 | [diff] [blame] | 45 | fprintf(out, "%s %s (%s)\n", progname, BLKID_VERSION, BLKID_DATE); |
Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 46 | } |
| 47 | |
Theodore Ts'o | 7a603aa | 2003-01-26 01:54:39 -0500 | [diff] [blame] | 48 | static void usage(int error) |
Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 49 | { |
| 50 | FILE *out = error ? stderr : stdout; |
| 51 | |
| 52 | print_version(out); |
| 53 | fprintf(out, |
Theodore Ts'o | 3d05802 | 2008-07-12 22:06:30 -0400 | [diff] [blame] | 54 | "usage:\t%s [-c <file>] [-ghlLv] [-o format] " |
| 55 | "[-s <tag>] [-t <token>]\n [-w <file>] [dev ...]\n" |
Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 56 | "\t-c\tcache file (default: /etc/blkid.tab, /dev/null = none)\n" |
| 57 | "\t-h\tprint this usage message and exit\n" |
Theodore Ts'o | 46100e3 | 2007-05-18 00:16:02 -0400 | [diff] [blame] | 58 | "\t-g\tgarbage collect the blkid cache\n" |
Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 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" |
Theodore Ts'o | ed6acfa | 2005-05-07 17:06:27 -0400 | [diff] [blame] | 61 | "\t-l\tlookup the the first device with arguments specified by -t\n" |
Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 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 | |
Theodore Ts'o | f8efcda | 2007-12-16 12:26:57 -0500 | [diff] [blame] | 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 | |
Theodore Ts'o | 3d05802 | 2008-07-12 22:06:30 -0400 | [diff] [blame] | 94 | static int get_terminal_width(void) |
| 95 | { |
| 96 | #ifdef TIOCGSIZE |
Theodore Ts'o | 36f4c40 | 2008-07-14 17:49:38 -0400 | [diff] [blame] | 97 | struct ttysize t_win; |
Theodore Ts'o | 3d05802 | 2008-07-12 22:06:30 -0400 | [diff] [blame] | 98 | #endif |
| 99 | #ifdef TIOCGWINSZ |
Theodore Ts'o | 36f4c40 | 2008-07-14 17:49:38 -0400 | [diff] [blame] | 100 | struct winsize w_win; |
Theodore Ts'o | 3d05802 | 2008-07-12 22:06:30 -0400 | [diff] [blame] | 101 | #endif |
| 102 | const char *cp; |
Theodore Ts'o | 5bb4d0c | 2014-01-30 16:19:01 -0500 | [diff] [blame] | 103 | int width = 80; |
Theodore Ts'o | 3d05802 | 2008-07-12 22:06:30 -0400 | [diff] [blame] | 104 | |
| 105 | #ifdef TIOCGSIZE |
Theodore Ts'o | 5bb4d0c | 2014-01-30 16:19:01 -0500 | [diff] [blame] | 106 | if (ioctl (0, TIOCGSIZE, &t_win) == 0) { |
| 107 | width = t_win.ts_cols; |
| 108 | goto got_it; |
| 109 | } |
Theodore Ts'o | 3d05802 | 2008-07-12 22:06:30 -0400 | [diff] [blame] | 110 | #endif |
| 111 | #ifdef TIOCGWINSZ |
Theodore Ts'o | 5bb4d0c | 2014-01-30 16:19:01 -0500 | [diff] [blame] | 112 | if (ioctl (0, TIOCGWINSZ, &w_win) == 0) { |
| 113 | width = w_win.ws_col; |
| 114 | goto got_it; |
| 115 | } |
Theodore Ts'o | 3d05802 | 2008-07-12 22:06:30 -0400 | [diff] [blame] | 116 | #endif |
| 117 | cp = getenv("COLUMNS"); |
| 118 | if (cp) |
Theodore Ts'o | 5bb4d0c | 2014-01-30 16:19:01 -0500 | [diff] [blame] | 119 | width = atoi(cp); |
| 120 | got_it: |
| 121 | if (width > 4096) |
| 122 | return 4096; /* sanity check */ |
| 123 | return width; |
Theodore Ts'o | 3d05802 | 2008-07-12 22:06:30 -0400 | [diff] [blame] | 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; |
Theodore Ts'o | 6596c99 | 2014-01-10 21:40:28 -0500 | [diff] [blame] | 138 | do { |
Theodore Ts'o | 3d05802 | 2008-07-12 22:06:30 -0400 | [diff] [blame] | 139 | fputc(' ', stdout); |
Theodore Ts'o | 6596c99 | 2014-01-10 21:40:28 -0500 | [diff] [blame] | 140 | } while (len++ < max_len); |
Theodore Ts'o | 3d05802 | 2008-07-12 22:06:30 -0400 | [diff] [blame] | 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 | |
Theodore Ts'o | 2fc92db | 2014-01-10 23:25:54 -0500 | [diff] [blame] | 153 | if (term_width < 0) { |
Theodore Ts'o | 3d05802 | 2008-07-12 22:06:30 -0400 | [diff] [blame] | 154 | term_width = get_terminal_width(); |
| 155 | |
Theodore Ts'o | 2fc92db | 2014-01-10 23:25:54 -0500 | [diff] [blame] | 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 | } |
Theodore Ts'o | 3d05802 | 2008-07-12 22:06:30 -0400 | [diff] [blame] | 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 = ""; |
Theodore Ts'o | 3d05802 | 2008-07-12 22:06:30 -0400 | [diff] [blame] | 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 | |
Theodore Ts'o | 8927998 | 2004-03-20 16:30:10 -0500 | [diff] [blame] | 229 | static void print_tags(blkid_dev dev, char *show[], int numtag, int output) |
Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 230 | { |
Theodore Ts'o | 7a603aa | 2003-01-26 01:54:39 -0500 | [diff] [blame] | 231 | blkid_tag_iterate iter; |
| 232 | const char *type, *value; |
Theodore Ts'o | ed1b33e | 2003-03-01 19:29:01 -0500 | [diff] [blame] | 233 | int i, first = 1; |
Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 234 | |
| 235 | if (!dev) |
| 236 | return; |
| 237 | |
Theodore Ts'o | 3d05802 | 2008-07-12 22:06:30 -0400 | [diff] [blame] | 238 | if (output & OUTPUT_PRETTY_LIST) { |
| 239 | pretty_print_dev(dev); |
| 240 | return; |
| 241 | } |
| 242 | |
Theodore Ts'o | 8927998 | 2004-03-20 16:30:10 -0500 | [diff] [blame] | 243 | if (output & OUTPUT_DEVICE_ONLY) { |
| 244 | printf("%s\n", blkid_dev_devname(dev)); |
| 245 | return; |
| 246 | } |
| 247 | |
Theodore Ts'o | 7a603aa | 2003-01-26 01:54:39 -0500 | [diff] [blame] | 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; |
Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 256 | } |
Theodore Ts'o | f8efcda | 2007-12-16 12:26:57 -0500 | [diff] [blame] | 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); |
Theodore Ts'o | 7a603aa | 2003-01-26 01:54:39 -0500 | [diff] [blame] | 269 | } |
Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 270 | } |
Theodore Ts'o | 7a603aa | 2003-01-26 01:54:39 -0500 | [diff] [blame] | 271 | blkid_tag_iterate_end(iter); |
Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 272 | |
Theodore Ts'o | 8927998 | 2004-03-20 16:30:10 -0500 | [diff] [blame] | 273 | if (!first && !(output & OUTPUT_VALUE_ONLY)) |
Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 274 | printf("\n"); |
| 275 | } |
| 276 | |
| 277 | int main(int argc, char **argv) |
| 278 | { |
Theodore Ts'o | 7a603aa | 2003-01-26 01:54:39 -0500 | [diff] [blame] | 279 | blkid_cache cache = NULL; |
Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 280 | char *devices[128] = { NULL, }; |
| 281 | char *show[128] = { NULL, }; |
Theodore Ts'o | 7a603aa | 2003-01-26 01:54:39 -0500 | [diff] [blame] | 282 | char *search_type = NULL, *search_value = NULL; |
Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 283 | char *read = NULL; |
| 284 | char *write = NULL; |
Theodore Ts'o | 5443492 | 2003-12-07 01:28:50 -0500 | [diff] [blame] | 285 | unsigned int numdev = 0, numtag = 0; |
Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 286 | int version = 0; |
| 287 | int err = 4; |
Theodore Ts'o | 5443492 | 2003-12-07 01:28:50 -0500 | [diff] [blame] | 288 | unsigned int i; |
Theodore Ts'o | 8927998 | 2004-03-20 16:30:10 -0500 | [diff] [blame] | 289 | int output_format = 0; |
Theodore Ts'o | 46100e3 | 2007-05-18 00:16:02 -0400 | [diff] [blame] | 290 | int lookup = 0, gc = 0; |
Theodore Ts'o | 0af8c33 | 2005-05-06 00:10:52 -0400 | [diff] [blame] | 291 | int c; |
Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 292 | |
Theodore Ts'o | 3d05802 | 2008-07-12 22:06:30 -0400 | [diff] [blame] | 293 | while ((c = getopt (argc, argv, "c:f:ghlLo:s:t:w:v")) != EOF) |
Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 294 | switch (c) { |
Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 295 | case 'c': |
Theodore Ts'o | e5397d7 | 2014-01-30 18:02:37 -0500 | [diff] [blame] | 296 | read = optarg; |
Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 297 | if (!write) |
| 298 | write = read; |
| 299 | break; |
Theodore Ts'o | ed6acfa | 2005-05-07 17:06:27 -0400 | [diff] [blame] | 300 | case 'l': |
| 301 | lookup++; |
| 302 | break; |
Theodore Ts'o | 3d05802 | 2008-07-12 22:06:30 -0400 | [diff] [blame] | 303 | case 'L': |
| 304 | output_format = OUTPUT_PRETTY_LIST; |
| 305 | break; |
Theodore Ts'o | 46100e3 | 2007-05-18 00:16:02 -0400 | [diff] [blame] | 306 | case 'g': |
| 307 | gc = 1; |
| 308 | break; |
Theodore Ts'o | 8927998 | 2004-03-20 16:30:10 -0500 | [diff] [blame] | 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; |
Theodore Ts'o | 3d05802 | 2008-07-12 22:06:30 -0400 | [diff] [blame] | 314 | else if (!strcmp(optarg, "list")) |
| 315 | output_format = OUTPUT_PRETTY_LIST; |
Theodore Ts'o | 8927998 | 2004-03-20 16:30:10 -0500 | [diff] [blame] | 316 | else if (!strcmp(optarg, "full")) |
| 317 | output_format = 0; |
| 318 | else { |
Theodore Ts'o | 3d05802 | 2008-07-12 22:06:30 -0400 | [diff] [blame] | 319 | fprintf(stderr, "Invalid output format %s. " |
| 320 | "Choose from value,\n\t" |
| 321 | "device, list, or full\n", optarg); |
Theodore Ts'o | 8927998 | 2004-03-20 16:30:10 -0500 | [diff] [blame] | 322 | exit(1); |
| 323 | } |
| 324 | break; |
Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 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': |
Theodore Ts'o | 7a603aa | 2003-01-26 01:54:39 -0500 | [diff] [blame] | 333 | if (search_type) { |
Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 334 | fprintf(stderr, "Can only search for " |
| 335 | "one NAME=value pair\n"); |
| 336 | usage(err); |
| 337 | } |
Theodore Ts'o | 7a603aa | 2003-01-26 01:54:39 -0500 | [diff] [blame] | 338 | if (blkid_parse_tag_string(optarg, |
| 339 | &search_type, |
| 340 | &search_value)) { |
Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 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': |
Theodore Ts'o | e5397d7 | 2014-01-30 18:02:37 -0500 | [diff] [blame] | 349 | write = optarg; |
Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 350 | break; |
| 351 | case 'h': |
| 352 | err = 0; |
Theodore Ts'o | 46802ef | 2014-01-09 15:03:40 -0500 | [diff] [blame] | 353 | /* fallthrough */ |
Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 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 | |
Theodore Ts'o | 50b380b | 2003-02-12 23:51:21 -0500 | [diff] [blame] | 366 | if (blkid_get_cache(&cache, read) < 0) |
Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 367 | goto exit; |
| 368 | |
| 369 | err = 2; |
Theodore Ts'o | 46100e3 | 2007-05-18 00:16:02 -0400 | [diff] [blame] | 370 | if (gc) { |
| 371 | blkid_gc_cache(cache); |
Theodore Ts'o | 3d05802 | 2008-07-12 22:06:30 -0400 | [diff] [blame] | 372 | goto exit; |
| 373 | } |
| 374 | if (output_format & OUTPUT_PRETTY_LIST) |
| 375 | pretty_print_dev(NULL); |
| 376 | |
| 377 | if (lookup) { |
Theodore Ts'o | ed6acfa | 2005-05-07 17:06:27 -0400 | [diff] [blame] | 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 | } |
Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 394 | /* If we didn't specify a single device, show all available devices */ |
Theodore Ts'o | ed6acfa | 2005-05-07 17:06:27 -0400 | [diff] [blame] | 395 | } else if (!numdev) { |
Theodore Ts'o | 7a603aa | 2003-01-26 01:54:39 -0500 | [diff] [blame] | 396 | blkid_dev_iterate iter; |
| 397 | blkid_dev dev; |
Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 398 | |
Theodore Ts'o | 50b380b | 2003-02-12 23:51:21 -0500 | [diff] [blame] | 399 | blkid_probe_all(cache); |
Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 400 | |
Theodore Ts'o | 7a603aa | 2003-01-26 01:54:39 -0500 | [diff] [blame] | 401 | iter = blkid_dev_iterate_begin(cache); |
Theodore Ts'o | c37543d | 2005-05-07 13:32:47 -0400 | [diff] [blame] | 402 | blkid_dev_set_search(iter, search_type, search_value); |
Theodore Ts'o | 7a603aa | 2003-01-26 01:54:39 -0500 | [diff] [blame] | 403 | while (blkid_dev_next(iter, &dev) == 0) { |
Theodore Ts'o | 18d1296 | 2005-01-27 19:51:47 -0500 | [diff] [blame] | 404 | dev = blkid_verify(cache, dev); |
| 405 | if (!dev) |
| 406 | continue; |
Theodore Ts'o | 8927998 | 2004-03-20 16:30:10 -0500 | [diff] [blame] | 407 | print_tags(dev, show, numtag, output_format); |
Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 408 | err = 0; |
| 409 | } |
Theodore Ts'o | 7a603aa | 2003-01-26 01:54:39 -0500 | [diff] [blame] | 410 | blkid_dev_iterate_end(iter); |
Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 411 | /* Add all specified devices to cache (optionally display tags) */ |
| 412 | } else for (i = 0; i < numdev; i++) { |
Theodore Ts'o | 98999c3 | 2003-02-16 00:47:07 -0500 | [diff] [blame] | 413 | blkid_dev dev = blkid_get_dev(cache, devices[i], |
Theodore Ts'o | 50b380b | 2003-02-12 23:51:21 -0500 | [diff] [blame] | 414 | BLKID_DEV_NORMAL); |
Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 415 | |
| 416 | if (dev) { |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 417 | if (search_type && |
| 418 | !blkid_dev_has_tag(dev, search_type, |
Theodore Ts'o | c37543d | 2005-05-07 13:32:47 -0400 | [diff] [blame] | 419 | search_value)) |
Theodore Ts'o | 18d1296 | 2005-01-27 19:51:47 -0500 | [diff] [blame] | 420 | continue; |
Theodore Ts'o | 8927998 | 2004-03-20 16:30:10 -0500 | [diff] [blame] | 421 | print_tags(dev, show, numtag, output_format); |
Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 422 | err = 0; |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | exit: |
Jim Meyering | 45e338f | 2009-02-23 18:07:50 +0100 | [diff] [blame] | 427 | free(search_type); |
| 428 | free(search_value); |
Theodore Ts'o | 50b380b | 2003-02-12 23:51:21 -0500 | [diff] [blame] | 429 | blkid_put_cache(cache); |
Theodore Ts'o | e12f2ae | 2003-01-23 16:45:16 -0500 | [diff] [blame] | 430 | return err; |
| 431 | } |