blkid.c revision 544349270e4c74a6feb971123884a8cf5052a7ee
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 12#include <stdio.h> 13#include <stdlib.h> 14#include <string.h> 15#ifdef HAVE_GETOPT_H 16#include <getopt.h> 17#else 18extern char *optarg; 19extern int optind; 20#endif 21 22#include "blkid/blkid.h" 23 24const char *progname = "blkid"; 25 26static void print_version(FILE *out) 27{ 28 fprintf(out, "%s %s (%s)\n", progname, BLKID_VERSION, BLKID_DATE); 29} 30 31static void usage(int error) 32{ 33 FILE *out = error ? stderr : stdout; 34 35 print_version(out); 36 fprintf(out, 37 "usage:\t%s [-c <file>] [-h] " 38 "[-p] [-s <tag>] [-t <token>] [-v] [-w <file>] [dev ...]\n" 39 "\t-c\tcache file (default: /etc/blkid.tab, /dev/null = none)\n" 40 "\t-h\tprint this usage message and exit\n" 41 "\t-s\tshow specified tag(s) (default show all tags)\n" 42 "\t-t\tfind device with a specific token (NAME=value pair)\n" 43 "\t-v\tprint version and exit\n" 44 "\t-w\twrite cache to different file (/dev/null = no write)\n" 45 "\tdev\tspecify device(s) to probe (default: all devices)\n", 46 progname); 47 exit(error); 48} 49 50static void print_tags(blkid_dev dev, char *show[], int numtag) 51{ 52 blkid_tag_iterate iter; 53 const char *type, *value; 54 int i, first = 1; 55 56 if (!dev) 57 return; 58 59 iter = blkid_tag_iterate_begin(dev); 60 while (blkid_tag_next(iter, &type, &value) == 0) { 61 if (numtag && show) { 62 for (i=0; i < numtag; i++) 63 if (!strcmp(type, show[i])) 64 break; 65 if (i >= numtag) 66 continue; 67 } 68 if (first) { 69 printf("%s: ", blkid_dev_devname(dev)); 70 first = 0; 71 } 72 printf("%s=\"%s\" ", type, value); 73 } 74 blkid_tag_iterate_end(iter); 75 76 if (!first) 77 printf("\n"); 78} 79 80int main(int argc, char **argv) 81{ 82 blkid_cache cache = NULL; 83 char *devices[128] = { NULL, }; 84 char *show[128] = { NULL, }; 85 char *search_type = NULL, *search_value = NULL; 86 char *read = NULL; 87 char *write = NULL; 88 unsigned int numdev = 0, numtag = 0; 89 int version = 0; 90 int err = 4; 91 unsigned int i; 92 char c; 93 94 while ((c = getopt (argc, argv, "c:f:hps:t:w:v")) != EOF) 95 switch (c) { 96 case 'c': 97 if (optarg && !*optarg) 98 read = NULL; 99 else 100 read = optarg; 101 if (!write) 102 write = read; 103 break; 104 case 's': 105 if (numtag >= sizeof(show) / sizeof(*show)) { 106 fprintf(stderr, "Too many tags specified\n"); 107 usage(err); 108 } 109 show[numtag++] = optarg; 110 break; 111 case 't': 112 if (search_type) { 113 fprintf(stderr, "Can only search for " 114 "one NAME=value pair\n"); 115 usage(err); 116 } 117 if (blkid_parse_tag_string(optarg, 118 &search_type, 119 &search_value)) { 120 fprintf(stderr, "-t needs NAME=value pair\n"); 121 usage(err); 122 } 123 break; 124 case 'v': 125 version = 1; 126 break; 127 case 'w': 128 if (optarg && !*optarg) 129 write = NULL; 130 else 131 write = optarg; 132 break; 133 case 'h': 134 err = 0; 135 default: 136 usage(err); 137 } 138 139 while (optind < argc) 140 devices[numdev++] = argv[optind++]; 141 142 if (version) { 143 print_version(stdout); 144 goto exit; 145 } 146 147 if (blkid_get_cache(&cache, read) < 0) 148 goto exit; 149 150 err = 2; 151 /* If looking for a specific NAME=value pair, print only that */ 152 if (search_type) { 153 blkid_dev dev; 154 155 /* Load any additional devices not in the cache */ 156 for (i = 0; i < numdev; i++) 157 blkid_get_dev(cache, devices[i], BLKID_DEV_NORMAL); 158 159 if ((dev = blkid_find_dev_with_tag(cache, search_type, 160 search_value))) { 161 print_tags(dev, show, numtag); 162 err = 0; 163 } 164 /* If we didn't specify a single device, show all available devices */ 165 } else if (!numdev) { 166 blkid_dev_iterate iter; 167 blkid_dev dev; 168 169 blkid_probe_all(cache); 170 171 iter = blkid_dev_iterate_begin(cache); 172 while (blkid_dev_next(iter, &dev) == 0) { 173 print_tags(dev, show, numtag); 174 err = 0; 175 } 176 blkid_dev_iterate_end(iter); 177 /* Add all specified devices to cache (optionally display tags) */ 178 } else for (i = 0; i < numdev; i++) { 179 blkid_dev dev = blkid_get_dev(cache, devices[i], 180 BLKID_DEV_NORMAL); 181 182 if (dev) { 183 print_tags(dev, show, numtag); 184 err = 0; 185 } 186 } 187 188exit: 189 if (search_type) 190 free(search_type); 191 if (search_value) 192 free(search_value); 193 blkid_put_cache(cache); 194 return err; 195} 196