Error conditions in utils & examples: fix leaks, unify style (see details)

* in main():
** errx()/err()/return -> exit()
** return values -> EXIT_SUCCESS & EXIT_FAILURE

* out of main:
** err()/errx()/exit() -> return
** change retval from size_t to int to allow returning errors
** don't use EXIT_SUCCESS / EXIT_FAILURE as retvals

* add nfc_close() & nfc_exit() to exit() on errors
* add missing fclose() on errors
* add missing test if (pnd == NULL)
* unify style if (pnd == / != NULL)
* remove goto's
* few related fixes
* remove if(pnd!=NULL) test on nfc_close() calls
This commit is contained in:
Philippe Teuwen 2013-03-05 19:44:59 +01:00
parent 232930c3d5
commit bece73faaf
21 changed files with 433 additions and 298 deletions

View file

@ -178,7 +178,7 @@ main(int argc, const char *argv[])
printf("r|w - Perform read from or write to card\n");
printf("<dump.mfd> - MiFare Dump (MFD) used to write (card to MFD) or (MFD to card)\n");
printf("\n");
return 1;
exit(EXIT_FAILURE);
}
DBG("\nChecking arguments and settings\n");
@ -192,13 +192,13 @@ main(int argc, const char *argv[])
if (pfDump == NULL) {
ERR("Could not open dump file: %s\n", argv[2]);
return 1;
exit(EXIT_FAILURE);
}
if (fread(&mtDump, 1, sizeof(mtDump), pfDump) != sizeof(mtDump)) {
ERR("Could not read from dump file: %s\n", argv[2]);
fclose(pfDump);
return 1;
exit(EXIT_FAILURE);
}
fclose(pfDump);
}
@ -211,17 +211,22 @@ main(int argc, const char *argv[])
pnd = nfc_open(context, NULL);
if (pnd == NULL) {
ERR("Error opening NFC device\n");
return 1;
nfc_exit(context);
exit(EXIT_FAILURE);
}
if (nfc_initiator_init(pnd) < 0) {
nfc_perror(pnd, "nfc_initiator_init");
nfc_close(pnd);
nfc_exit(context);
exit(EXIT_FAILURE);
}
// Let the device only try once to find a tag
if (nfc_device_set_property_bool(pnd, NP_INFINITE_SELECT, false) < 0) {
nfc_perror(pnd, "nfc_device_set_property_bool");
nfc_close(pnd);
nfc_exit(context);
exit(EXIT_FAILURE);
}
@ -232,7 +237,7 @@ main(int argc, const char *argv[])
ERR("no tag was found\n");
nfc_close(pnd);
nfc_exit(context);
return EXIT_FAILURE;
exit(EXIT_FAILURE);
}
// Test if we are dealing with a MIFARE compatible tag
@ -240,7 +245,7 @@ main(int argc, const char *argv[])
ERR("tag is not a MIFARE Ultralight card\n");
nfc_close(pnd);
nfc_exit(context);
return EXIT_FAILURE;
exit(EXIT_FAILURE);
}
// Get the info from the current tag
printf("Found MIFARE Ultralight card with UID: ");
@ -257,12 +262,16 @@ main(int argc, const char *argv[])
pfDump = fopen(argv[2], "wb");
if (pfDump == NULL) {
printf("Could not open file: %s\n", argv[2]);
return EXIT_FAILURE;
nfc_close(pnd);
nfc_exit(context);
exit(EXIT_FAILURE);
}
if (fwrite(&mtDump, 1, sizeof(mtDump), pfDump) != sizeof(mtDump)) {
printf("Could not write to file: %s\n", argv[2]);
fclose(pfDump);
return EXIT_FAILURE;
nfc_close(pnd);
nfc_exit(context);
exit(EXIT_FAILURE);
}
fclose(pfDump);
printf("Done.\n");
@ -273,5 +282,5 @@ main(int argc, const char *argv[])
nfc_close(pnd);
nfc_exit(context);
return EXIT_SUCCESS;
exit(EXIT_SUCCESS);
}