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

@ -89,13 +89,13 @@ main(int argc, char *argv[])
for (arg = 1; arg < argc; arg++) {
if (0 == strcmp(argv[arg], "-h")) {
print_usage(argv);
return EXIT_SUCCESS;
exit(EXIT_SUCCESS);
} else if (0 == strcmp(argv[arg], "-q")) {
quiet_output = true;
} else {
ERR("%s is not supported option.", argv[arg]);
print_usage(argv);
return EXIT_FAILURE;
exit(EXIT_FAILURE);
}
}
@ -116,14 +116,16 @@ main(int argc, char *argv[])
if (szFound < 2) {
ERR("%zd device found but two opened devices are needed to relay NFC.", szFound);
return EXIT_FAILURE;
nfc_exit(context);
exit(EXIT_FAILURE);
}
// Try to open the NFC emulator device
pndTag = nfc_open(context, connstrings[0]);
if (pndTag == NULL) {
printf("Error opening NFC emulator device\n");
return EXIT_FAILURE;
nfc_exit(context);
exit(EXIT_FAILURE);
}
printf("Hint: tag <---> initiator (relay) <---> target (relay) <---> original reader\n\n");
@ -153,30 +155,44 @@ main(int argc, char *argv[])
ERR("%s", "Initialization of NFC emulator failed");
nfc_close(pndTag);
nfc_exit(context);
return EXIT_FAILURE;
exit(EXIT_FAILURE);
}
printf("%s", "Configuring emulator settings...");
if ((nfc_device_set_property_bool(pndTag, NP_HANDLE_CRC, false) < 0) ||
(nfc_device_set_property_bool(pndTag, NP_HANDLE_PARITY, false) < 0) || (nfc_device_set_property_bool(pndTag, NP_ACCEPT_INVALID_FRAMES, true)) < 0) {
nfc_perror(pndTag, "nfc_device_set_property_bool");
nfc_close(pndTag);
nfc_exit(context);
exit(EXIT_FAILURE);
}
printf("%s", "Done, emulated tag is initialized");
// Try to open the NFC reader
pndReader = nfc_open(context, connstrings[1]);
if (pndReader == NULL) {
printf("Error opening NFC reader device\n");
nfc_close(pndTag);
nfc_exit(context);
exit(EXIT_FAILURE);
}
printf("NFC reader device: %s opened", nfc_device_get_name(pndReader));
printf("%s", "Configuring NFC reader settings...");
if (nfc_initiator_init(pndReader) < 0) {
nfc_perror(pndReader, "nfc_initiator_init");
nfc_close(pndTag);
nfc_close(pndReader);
nfc_exit(context);
exit(EXIT_FAILURE);
}
if ((nfc_device_set_property_bool(pndReader, NP_HANDLE_CRC, false) < 0) ||
(nfc_device_set_property_bool(pndReader, NP_HANDLE_PARITY, false) < 0) ||
(nfc_device_set_property_bool(pndReader, NP_ACCEPT_INVALID_FRAMES, true)) < 0) {
nfc_perror(pndReader, "nfc_device_set_property_bool");
nfc_close(pndTag);
nfc_close(pndReader);
nfc_exit(context);
exit(EXIT_FAILURE);
}
printf("%s", "Done, relaying frames now!");
@ -189,12 +205,18 @@ main(int argc, char *argv[])
// Drop down field for a very short time (original tag will reboot)
if (nfc_device_set_property_bool(pndReader, NP_ACTIVATE_FIELD, false) < 0) {
nfc_perror(pndReader, "nfc_device_set_property_bool");
nfc_close(pndTag);
nfc_close(pndReader);
nfc_exit(context);
exit(EXIT_FAILURE);
}
if (!quiet_output)
printf("\n");
if (nfc_device_set_property_bool(pndReader, NP_ACTIVATE_FIELD, true) < 0) {
nfc_perror(pndReader, "nfc_device_set_property_bool");
nfc_close(pndTag);
nfc_close(pndReader);
nfc_exit(context);
exit(EXIT_FAILURE);
}
}
@ -209,6 +231,9 @@ main(int argc, char *argv[])
// Redirect the answer back to the reader
if (nfc_target_send_bits(pndTag, abtTagRx, szTagRxBits, abtTagRxPar) < 0) {
nfc_perror(pndTag, "nfc_target_send_bits");
nfc_close(pndTag);
nfc_close(pndReader);
nfc_exit(context);
exit(EXIT_FAILURE);
}
// Print the tag frame to the screen