#include <dirent.h>
#include <errno.h>
#include <unistd.h>
+#include <libgen.h>
#ifdef HAVE_GETOPTLONG
# include <getopt.h>
NOHEADINGS_ARG,
NOOPENCOUNT_ARG,
NOTABLE_ARG,
+ OPTIONS_ARG,
UUID_ARG,
VERBOSE_ARG,
VERSION_ARG,
static int _switches[NUM_SWITCHES];
static int _values[NUM_SWITCHES];
static char *_uuid;
+static char *_fields;
/*
* Commands
uuid = dm_task_get_uuid(dmt);
- printf("%s:%d:%d:%s%s%s%s:%d:%d:%" PRIu32 ":%s\n",
- dm_task_get_name(dmt),
- info->major, info->minor,
- info->live_table ? "L" : "-",
- info->inactive_table ? "I" : "-",
- info->suspended ? "s" : "-",
- info->read_only ? "r" : "w",
- info->open_count, info->target_count, info->event_nr,
- uuid && *uuid ? uuid : "");
+ if (_switches[OPTIONS_ARG])
+ printf("%s\n", dm_task_get_name(dmt));
+ else
+ printf("%s:%d:%d:%s%s%s%s:%d:%d:%" PRIu32 ":%s\n",
+ dm_task_get_name(dmt),
+ info->major, info->minor,
+ info->live_table ? "L" : "-",
+ info->inactive_table ? "I" : "-",
+ info->suspended ? "s" : "-",
+ info->read_only ? "r" : "w",
+ info->open_count, info->target_count, info->event_nr,
+ uuid && *uuid ? uuid : "");
}
static void _display_info_cols(struct dm_task *dmt, struct dm_info *info)
}
if (!_headings) {
- printf("Name Maj Min Stat Open Targ Event UUID\n");
+ if (_switches[OPTIONS_ARG])
+ printf("Name\n");
+ else
+ printf("Name Maj Min Stat Open Targ "
+ "Event UUID\n");
_headings = 1;
}
- printf("%-16s ", dm_task_get_name(dmt));
-
- printf("%3d %3d %s%s%s%s %4d %4d %6" PRIu32 " ",
- info->major, info->minor,
- info->live_table ? "L" : "-",
- info->inactive_table ? "I" : "-",
- info->suspended ? "s" : "-",
- info->read_only ? "r" : "w",
- info->open_count, info->target_count, info->event_nr);
-
- if ((uuid = dm_task_get_uuid(dmt)) && *uuid)
- printf("%s", uuid);
+ if (_switches[OPTIONS_ARG])
+ printf("%s\n", dm_task_get_name(dmt));
+ else {
+ printf("%-16s %3d %3d %s%s%s%s %4d %4d %6" PRIu32 " ",
+ dm_task_get_name(dmt),
+ info->major, info->minor,
+ info->live_table ? "L" : "-",
+ info->inactive_table ? "I" : "-",
+ info->suspended ? "s" : "-",
+ info->read_only ? "r" : "w",
+ info->open_count, info->target_count, info->event_nr);
+
+ if ((uuid = dm_task_get_uuid(dmt)) && *uuid)
+ printf("%s", uuid);
- printf("\n");
+ printf("\n");
+ }
}
static void _display_info_long(struct dm_task *dmt, struct dm_info *info)
printf("\n");
}
-static void _display_info(struct dm_task *dmt)
+static int _display_info(struct dm_task *dmt)
{
struct dm_info info;
if (!dm_task_get_info(dmt, &info))
- return;
+ return 0;
if (!_switches[COLS_ARG])
_display_info_long(dmt, &info);
_display_info_cols_noheadings(dmt, &info);
else
_display_info_cols(dmt, &info);
+
+ return info.exists ? 1 : 0;
}
static int _set_task_device(struct dm_task *dmt, const char *name, int optional)
r = 1;
if (_switches[VERBOSE_ARG])
- _display_info(dmt);
+ r = _display_info(dmt);
out:
dm_task_destroy(dmt);
r = 1;
if (_switches[VERBOSE_ARG])
- _display_info(dmt);
+ r = _display_info(dmt);
out:
dm_task_destroy(dmt);
r = dm_task_run(dmt);
if (r && display && _switches[VERBOSE_ARG])
- _display_info(dmt);
+ r = _display_info(dmt);
out:
dm_task_destroy(dmt);
if (!dm_task_run(dmt))
goto out;
- _display_info(dmt);
-
- r = 1;
+ r = _display_info(dmt);
out:
dm_task_destroy(dmt);
static int _process_switches(int *argc, char ***argv)
{
+ char *base, *namebase;
int ind;
int c;
{"noheadings", 0, NULL, NOHEADINGS_ARG},
{"noopencount", 0, NULL, NOOPENCOUNT_ARG},
{"notable", 0, NULL, NOTABLE_ARG},
+ {"options", 1, NULL, OPTIONS_ARG},
{"uuid", 1, NULL, UUID_ARG},
{"verbose", 1, NULL, VERBOSE_ARG},
{"version", 0, NULL, VERSION_ARG},
memset(&_switches, 0, sizeof(_switches));
memset(&_values, 0, sizeof(_values));
+ namebase = strdup((*argv)[0]);
+ base = basename(namebase);
+
+ if (!strcmp(base, "devmap_name")) {
+ free(namebase);
+ _switches[COLS_ARG]++;
+ _switches[NOHEADINGS_ARG]++;
+ _switches[OPTIONS_ARG]++;
+ _switches[MAJOR_ARG]++;
+ _switches[MINOR_ARG]++;
+ _fields = (char *) "name";
+
+ if (*argc == 3) {
+ _values[MAJOR_ARG] = atoi((*argv)[1]);
+ _values[MINOR_ARG] = atoi((*argv)[2]);
+ *argc -= 2;
+ *argv += 2;
+ } else if ((*argc == 2) &&
+ (2 == sscanf((*argv)[1], "%i:%i",
+ &_values[MAJOR_ARG],
+ &_values[MINOR_ARG]))) {
+ *argc -= 1;
+ *argv += 1;
+ } else {
+ fprintf(stderr, "Usage: devmap_name <major> <minor>\n");
+ return 0;
+ }
+
+ (*argv)[0] = (char *) "info";
+ return 1;
+ }
+
+ free(namebase);
+
optarg = 0;
optind = OPTIND_INIT;
- while ((c = GETOPTLONG_FN(*argc, *argv, "cCj:m:nru:v",
+ while ((c = GETOPTLONG_FN(*argc, *argv, "cCj:m:no:ru:v",
long_options, &ind)) != -1) {
if (c == 'c' || c == 'C' || ind == COLS_ARG)
_switches[COLS_ARG]++;
}
if (c == 'n' || ind == NOTABLE_ARG)
_switches[NOTABLE_ARG]++;
+ if (c == 'o' || ind == OPTIONS_ARG) {
+ _switches[OPTIONS_ARG]++;
+ _fields = optarg;
+ }
if (c == 'v' || ind == VERBOSE_ARG)
_switches[VERBOSE_ARG]++;
if (c == 'u' || ind == UUID_ARG) {
return 0;
}
+ if (_switches[OPTIONS_ARG] && strcmp(_fields, "name")) {
+ fprintf(stderr, "Only -o name is supported so far.\n");
+ return 0;
+ }
+
*argv += optind;
*argc -= optind;
return 1;
struct command *c;
if (!_process_switches(&argc, &argv)) {
- fprintf(stderr, "Couldn't process command line switches.\n");
+ fprintf(stderr, "Couldn't process command line.\n");
exit(1);
}