- add more informational messages

- handle case where last argument is an option not a device (set -l)
This commit is contained in:
christos 2015-12-29 16:45:04 +00:00
parent d075aff38a
commit f9db75489b
10 changed files with 79 additions and 51 deletions

View File

@ -33,7 +33,7 @@
__FBSDID("$FreeBSD: src/sbin/gpt/add.c,v 1.14 2006/06/22 22:05:28 marcel Exp $");
#endif
#ifdef __RCSID
__RCSID("$NetBSD: add.c,v 1.42 2015/12/03 21:46:22 christos Exp $");
__RCSID("$NetBSD: add.c,v 1.43 2015/12/29 16:45:04 christos Exp $");
#endif
#include <sys/types.h>
@ -167,7 +167,7 @@ cmd_add(gpt_t gpt, int argc, char *argv[])
while ((ch = getopt(argc, argv, GPT_AIS "b:l:t:")) != -1) {
switch(ch) {
case 'b':
if (gpt_human_get(&block) == -1)
if (gpt_human_get(gpt, &block) == -1)
goto usage;
break;
case 'l':

View File

@ -1,4 +1,4 @@
/* $NetBSD: biosboot.c,v 1.23 2015/12/03 21:49:51 christos Exp $ */
/* $NetBSD: biosboot.c,v 1.24 2015/12/29 16:45:04 christos Exp $ */
/*
* Copyright (c) 2009 The NetBSD Foundation, Inc.
@ -37,7 +37,7 @@
#include <sys/cdefs.h>
#ifdef __RCSID
__RCSID("$NetBSD: biosboot.c,v 1.23 2015/12/03 21:49:51 christos Exp $");
__RCSID("$NetBSD: biosboot.c,v 1.24 2015/12/29 16:45:04 christos Exp $");
#endif
#include <sys/stat.h>
@ -266,7 +266,7 @@ cmd_biosboot(gpt_t gpt, int argc, char *argv[])
goto usage;
break;
case 'i':
if (gpt_uint_get(&entry) == -1)
if (gpt_uint_get(gpt, &entry) == -1)
goto usage;
break;
case 'L':

View File

@ -33,7 +33,7 @@
__FBSDID("$FreeBSD: src/sbin/gpt/create.c,v 1.11 2005/08/31 01:47:19 marcel Exp $");
#endif
#ifdef __RCSID
__RCSID("$NetBSD: create.c,v 1.19 2015/12/03 21:30:54 christos Exp $");
__RCSID("$NetBSD: create.c,v 1.20 2015/12/29 16:45:04 christos Exp $");
#endif
#include <sys/types.h>
@ -138,7 +138,7 @@ cmd_create(gpt_t gpt, int argc, char *argv[])
primary_only = 1;
break;
case 'p':
if (gpt_uint_get(&parts) == -1)
if (gpt_uint_get(gpt, &parts) == -1)
return -1;
break;
default:

View File

@ -35,7 +35,7 @@
__FBSDID("$FreeBSD: src/sbin/gpt/gpt.c,v 1.16 2006/07/07 02:44:23 marcel Exp $");
#endif
#ifdef __RCSID
__RCSID("$NetBSD: gpt.c,v 1.65 2015/12/26 13:12:16 jnemeth Exp $");
__RCSID("$NetBSD: gpt.c,v 1.66 2015/12/29 16:45:04 christos Exp $");
#endif
#include <sys/param.h>
@ -592,34 +592,39 @@ out:
close(gpt->fd);
}
static void
gpt_vwarnx(gpt_t gpt, const char *fmt, va_list ap, const char *e)
{
if (gpt && (gpt->flags & GPT_QUIET))
return;
fprintf(stderr, "%s: ", getprogname());
if (gpt)
fprintf(stderr, "%s: ", gpt->device_name);
vfprintf(stderr, fmt, ap);
if (e)
fprintf(stderr, " (%s)\n", e);
else
fputc('\n', stderr);
}
void
gpt_warnx(gpt_t gpt, const char *fmt, ...)
{
va_list ap;
if (gpt->flags & GPT_QUIET)
return;
fprintf(stderr, "%s: %s: ", getprogname(), gpt->device_name);
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
gpt_vwarnx(gpt, fmt, ap, NULL);
va_end(ap);
fprintf(stderr, "\n");
}
void
gpt_warn(gpt_t gpt, const char *fmt, ...)
{
va_list ap;
int e = errno;
if (gpt->flags & GPT_QUIET)
return;
fprintf(stderr, "%s: %s: ", getprogname(), gpt->device_name);
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
gpt_vwarnx(gpt, fmt, ap, strerror(errno));
va_end(ap);
fprintf(stderr, " (%s)\n", strerror(e));
errno = e;
}
void
@ -627,9 +632,10 @@ gpt_msg(gpt_t gpt, const char *fmt, ...)
{
va_list ap;
if (gpt->flags & GPT_QUIET)
if (gpt && (gpt->flags & GPT_QUIET))
return;
printf("%s: ", gpt->device_name);
if (gpt)
printf("%s: ", gpt->device_name);
va_start(ap, fmt);
vprintf(fmt, ap);
va_end(ap);
@ -922,17 +928,24 @@ gpt_size_get(gpt_t gpt, off_t *size)
}
int
gpt_human_get(off_t *human)
gpt_human_get(gpt_t gpt, off_t *human)
{
int64_t human_num;
if (*human > 0)
if (*human > 0) {
gpt_warn(gpt, "Already set to %jd new `%s'", (intmax_t)*human,
optarg);
return -1;
if (dehumanize_number(optarg, &human_num) < 0)
}
if (dehumanize_number(optarg, &human_num) < 0) {
gpt_warn(gpt, "Bad number `%s'", optarg);
return -1;
}
*human = human_num;
if (*human < 1)
if (*human < 1) {
gpt_warn(gpt, "Number `%s' < 1", optarg);
return -1;
}
return 0;
}
@ -941,16 +954,18 @@ gpt_add_find(gpt_t gpt, struct gpt_find *find, int ch)
{
switch (ch) {
case 'a':
if (find->all > 0)
if (find->all > 0) {
gpt_warn(gpt, "-a is already set");
return -1;
}
find->all = 1;
break;
case 'b':
if (gpt_human_get(&find->block) == -1)
if (gpt_human_get(gpt, &find->block) == -1)
return -1;
break;
case 'i':
if (gpt_uint_get(&find->entry) == -1)
if (gpt_uint_get(gpt, &find->entry) == -1)
return -1;
break;
case 'L':
@ -968,6 +983,7 @@ gpt_add_find(gpt_t gpt, struct gpt_find *find, int ch)
return -1;
break;
default:
gpt_warn(gpt, "Unknown find option `%c'", ch);
return -1;
}
return 0;
@ -1038,11 +1054,11 @@ gpt_add_ais(gpt_t gpt, off_t *alignment, u_int *entry, off_t *size, int ch)
{
switch (ch) {
case 'a':
if (gpt_human_get(alignment) == -1)
if (gpt_human_get(gpt, alignment) == -1)
return -1;
return 0;
case 'i':
if (gpt_uint_get(entry) == -1)
if (gpt_uint_get(gpt, entry) == -1)
return -1;
return 0;
case 's':
@ -1050,6 +1066,7 @@ gpt_add_ais(gpt_t gpt, off_t *alignment, u_int *entry, off_t *size, int ch)
return -1;
return 0;
default:
gpt_warn(gpt, "Unknown alignment/index/size option `%c'", ch);
return -1;
}
}
@ -1167,8 +1184,10 @@ gpt_attr_update(gpt_t gpt, u_int entry, uint64_t set, uint64_t clr)
struct gpt_ent *ent;
unsigned int i;
if (entry == 0 || (set == 0 && clr == 0))
if (entry == 0 || (set == 0 && clr == 0)) {
gpt_warnx(gpt, "Nothing to set");
return -1;
}
if ((hdr = gpt_hdr(gpt)) == NULL)
return -1;
@ -1203,14 +1222,16 @@ gpt_attr_update(gpt_t gpt, u_int entry, uint64_t set, uint64_t clr)
}
int
gpt_uint_get(u_int *entry)
gpt_uint_get(gpt_t gpt, u_int *entry)
{
char *p;
if (*entry > 0)
return -1;
*entry = (u_int)strtoul(optarg, &p, 10);
if (*p != 0 || *entry < 1)
if (*p != 0 || *entry < 1) {
gpt_warn(gpt, "Bad number `%s'", optarg);
return -1;
}
return 0;
}
int

View File

@ -123,8 +123,8 @@ int gpt_attr_get(gpt_t, uint64_t *);
const char *gpt_attr_list(char *, size_t, uint64_t);
void gpt_attr_help(const char *);
int gpt_attr_update(gpt_t, u_int, uint64_t, uint64_t);
int gpt_uint_get(u_int *);
int gpt_human_get(off_t *);
int gpt_uint_get(gpt_t, u_int *);
int gpt_human_get(gpt_t, off_t *);
int gpt_uuid_get(gpt_t, gpt_uuid_t *);
int gpt_name_get(gpt_t, void *);
int gpt_add_hdr(gpt_t, int, off_t);

View File

@ -1,4 +1,4 @@
/* $NetBSD: main.c,v 1.6 2015/12/03 02:02:43 christos Exp $ */
/* $NetBSD: main.c,v 1.7 2015/12/29 16:45:04 christos Exp $ */
/*-
* Copyright (c) 2002 Marcel Moolenaar
@ -34,7 +34,7 @@
#include <sys/cdefs.h>
#ifdef __RCSID
__RCSID("$NetBSD: main.c,v 1.6 2015/12/03 02:02:43 christos Exp $");
__RCSID("$NetBSD: main.c,v 1.7 2015/12/29 16:45:04 christos Exp $");
#endif
#include <stdio.h>
@ -165,7 +165,7 @@ main(int argc, char *argv[])
flags |= GPT_QUIET;
break;
case 's':
if (gpt_uint_get(&secsz) == -1)
if (gpt_uint_get(NULL, &secsz) == -1)
usage();
break;
case 'v':
@ -194,13 +194,20 @@ main(int argc, char *argv[])
prefix(cmd);
gpt = gpt_open(dev, flags | cmdsw[i]->flags, verbose, mediasz, secsz);
if (gpt == NULL)
return EXIT_FAILURE;
if (*dev != '-') {
gpt = gpt_open(dev, flags | cmdsw[i]->flags,
verbose, mediasz, secsz);
if (gpt == NULL)
return EXIT_FAILURE;
} else {
argc++;
gpt = NULL;
}
if ((*cmdsw[i]->fptr)(gpt, argc, argv) == -1)
return EXIT_FAILURE;
gpt_close(gpt);
if (gpt)
gpt_close(gpt);
return EXIT_SUCCESS;
}

View File

@ -33,7 +33,7 @@
__FBSDID("$FreeBSD: src/sbin/gpt/migrate.c,v 1.16 2005/09/01 02:42:52 marcel Exp $");
#endif
#ifdef __RCSID
__RCSID("$NetBSD: migrate.c,v 1.29 2015/12/06 04:27:05 christos Exp $");
__RCSID("$NetBSD: migrate.c,v 1.30 2015/12/29 16:45:04 christos Exp $");
#endif
#include <sys/types.h>
@ -337,7 +337,7 @@ cmd_migrate(gpt_t gpt, int argc, char *argv[])
force = 1;
break;
case 'p':
if (gpt_uint_get(&parts) == -1)
if (gpt_uint_get(gpt, &parts) == -1)
return usage();
break;
case 's':

View File

@ -33,7 +33,7 @@
__FBSDID("$FreeBSD: src/sbin/gpt/add.c,v 1.14 2006/06/22 22:05:28 marcel Exp $");
#endif
#ifdef __RCSID
__RCSID("$NetBSD: set.c,v 1.12 2015/12/06 00:39:26 christos Exp $");
__RCSID("$NetBSD: set.c,v 1.13 2015/12/29 16:45:04 christos Exp $");
#endif
#include <sys/types.h>
@ -79,7 +79,7 @@ cmd_set(gpt_t gpt, int argc, char *argv[])
return usage();
break;
case 'i':
if (gpt_uint_get(&entry) == -1)
if (gpt_uint_get(gpt, &entry) == -1)
return usage();
break;
case 'l':

View File

@ -33,7 +33,7 @@
__FBSDID("$FreeBSD: src/sbin/gpt/show.c,v 1.14 2006/06/22 22:22:32 marcel Exp $");
#endif
#ifdef __RCSID
__RCSID("$NetBSD: show.c,v 1.34 2015/12/26 13:08:45 martin Exp $");
__RCSID("$NetBSD: show.c,v 1.35 2015/12/29 16:45:04 christos Exp $");
#endif
#include <sys/types.h>
@ -310,7 +310,7 @@ cmd_show(gpt_t gpt, int argc, char *argv[])
xshow |= SHOW_GUID;
break;
case 'i':
if (gpt_uint_get(&entry) == -1)
if (gpt_uint_get(gpt, &entry) == -1)
return usage();
break;
case 'l':

View File

@ -33,7 +33,7 @@
__FBSDID("$FreeBSD: src/sbin/gpt/add.c,v 1.14 2006/06/22 22:05:28 marcel Exp $");
#endif
#ifdef __RCSID
__RCSID("$NetBSD: unset.c,v 1.12 2015/12/06 00:39:26 christos Exp $");
__RCSID("$NetBSD: unset.c,v 1.13 2015/12/29 16:45:04 christos Exp $");
#endif
#include <sys/types.h>
@ -78,7 +78,7 @@ cmd_unset(gpt_t gpt, int argc, char *argv[])
return usage();
break;
case 'i':
if (gpt_uint_get(&entry) == -1)
if (gpt_uint_get(gpt, &entry) == -1)
return usage();
break;
case 'l':