Allow mifare-classic-format to be run non-interactively.

This commit is contained in:
Romain Tartiere 2010-07-03 09:14:32 +00:00
parent 6cbb9f4f9d
commit 9e5820b89e

View file

@ -49,8 +49,10 @@ static int mod_block = 10;
struct { struct {
bool fast; bool fast;
bool interactive;
} format_options = { } format_options = {
.fast = false .fast = false,
.interactive = true
}; };
void void
@ -124,9 +126,10 @@ try_format_sector (MifareTag tag, MifareClassicSectorNumber sector)
void void
usage(char *progname) usage(char *progname)
{ {
fprintf (stderr, "usage: %s [-f]\n", progname); fprintf (stderr, "usage: %s [-fy]\n", progname);
fprintf (stderr, "\nOptions:\n"); fprintf (stderr, "\nOptions:\n");
fprintf (stderr, " -f Fast format (only erase MAD)\n"); fprintf (stderr, " -f Fast format (only erase MAD)\n");
fprintf (stderr, " -y Do not ask for confirmation (dangerous)\n");
} }
int int
@ -138,7 +141,7 @@ main(int argc, char *argv[])
MifareTag *tags = NULL; MifareTag *tags = NULL;
(void)argc, (void)argv; (void)argc, (void)argv;
while ((ch = getopt (argc, argv, "fh")) != -1) { while ((ch = getopt (argc, argv, "fhy")) != -1) {
switch (ch) { switch (ch) {
case 'f': case 'f':
format_options.fast = true; format_options.fast = true;
@ -146,6 +149,10 @@ main(int argc, char *argv[])
case 'h': case 'h':
usage(argv[0]); usage(argv[0]);
exit (EXIT_SUCCESS); exit (EXIT_SUCCESS);
break;
case 'y':
format_options.interactive = false;
break;
default: default:
usage(argv[0]); usage(argv[0]);
exit (EXIT_FAILURE); exit (EXIT_FAILURE);
@ -176,9 +183,15 @@ main(int argc, char *argv[])
char *tag_uid = freefare_get_tag_uid (tags[i]); char *tag_uid = freefare_get_tag_uid (tags[i]);
char buffer[BUFSIZ]; char buffer[BUFSIZ];
printf ("Found %s with UID %s. Format [yN] ", freefare_get_tag_friendly_name (tags[i]), tag_uid); printf ("Found %s with UID %s.", freefare_get_tag_friendly_name (tags[i]), tag_uid);
fgets (buffer, BUFSIZ, stdin); bool format = true;
bool format = ((buffer[0] == 'y') || (buffer[0] == 'Y')); if (format_options.interactive) {
printf ("Format [yN] ");
fgets (buffer, BUFSIZ, stdin);
format = ((buffer[0] == 'y') || (buffer[0] == 'Y'));
} else {
printf ("\n");
}
if (format) { if (format) {
enum mifare_tag_type tt = freefare_get_tag_type (tags[i]); enum mifare_tag_type tt = freefare_get_tag_type (tags[i]);