Commit 73b5c9d0 authored by Philippe Teuwen's avatar Philippe Teuwen

nfc_init() return rather than exit on malloc error, examples fixed accordingly

parent ae6062e5
......@@ -36,6 +36,10 @@ main(int argc, const char *argv[])
// Initialize libnfc and set the nfc_context
nfc_init(&context);
if (context == NULL) {
warnx("Unable to init libnfc (malloc)\n");
exit(EXIT_FAILURE);
}
// Display libnfc version
const char *acLibnfcVersion = nfc_version();
......
......@@ -28,6 +28,10 @@ main(int argc, const char *argv[])
// Initialize libnfc and set the nfc_context
nfc_init(&context);
if (context == NULL) {
ERR("Unable to init libnfc (malloc)");
exit(EXIT_FAILURE);
}
// Display libnfc version
const char *acLibnfcVersion = nfc_version();
......
......@@ -151,12 +151,16 @@ main(int argc, char *argv[])
nfc_context *context;
nfc_init(&context);
if (context == NULL) {
ERR("Unable to init libnfc (malloc)");
exit(EXIT_FAILURE);
}
// Try to open the NFC reader
pnd = nfc_open(context, NULL);
if (pnd == NULL) {
printf("Error opening NFC reader\n");
ERR("Error opening NFC reader");
nfc_exit(context);
exit(EXIT_FAILURE);
}
......
......@@ -73,10 +73,14 @@ main(int argc, const char *argv[])
nfc_context *context;
nfc_init(&context);
if (context == NULL) {
ERR("Unable to init libnfc (malloc)");
exit(EXIT_FAILURE);
}
pnd = nfc_open(context, NULL);
if (pnd == NULL) {
printf("Unable to open NFC device.\n");
ERR("Unable to open NFC device.");
nfc_exit(context);
exit(EXIT_FAILURE);
}
......
......@@ -72,6 +72,10 @@ main(int argc, const char *argv[])
nfc_context *context;
nfc_init(&context);
if (context == NULL) {
ERR("Unable to init libnfc (malloc)");
exit(EXIT_FAILURE);
}
#define MAX_DEVICE_COUNT 2
nfc_connstring connstrings[MAX_DEVICE_COUNT];
size_t szDeviceFound = nfc_list_devices(context, connstrings, MAX_DEVICE_COUNT);
......
......@@ -187,6 +187,10 @@ main(int argc, char *argv[])
nfc_context *context;
nfc_init(&context);
if (context == NULL) {
ERR("Unable to init libnfc (malloc)");
exit(EXIT_FAILURE);
}
pnd = nfc_open(context, NULL);
if (pnd == NULL) {
......
......@@ -181,6 +181,10 @@ main(int argc, char *argv[])
#endif
nfc_init(&context);
if (context == NULL) {
ERR("Unable to init libnfc (malloc)");
exit(EXIT_FAILURE);
}
// Display libnfc version
acLibnfcVersion = nfc_version();
......
......@@ -128,12 +128,16 @@ main(int argc, char *argv[])
nfc_context *context;
nfc_init(&context);
if (context == NULL) {
ERR("Unable to init libnfc (malloc)");
exit(EXIT_FAILURE);
}
// Try to open the NFC device
pnd = nfc_open(context, NULL);
if (pnd == NULL) {
printf("Unable to open NFC device\n");
ERR("Unable to open NFC device");
nfc_exit(context);
exit(EXIT_FAILURE);
}
......
......@@ -179,12 +179,16 @@ main(int argc, char *argv[])
nfc_context *context;
nfc_init(&context);
if (context == NULL) {
ERR("Unable to init libnfc (malloc)");
exit(EXIT_FAILURE);
}
// Try to open the NFC reader
pnd = nfc_open(context, NULL);
if (pnd == NULL) {
printf("Error opening NFC reader\n");
ERR("Error opening NFC reader");
nfc_exit(context);
exit(EXIT_FAILURE);
}
......
......@@ -105,6 +105,10 @@ main(int argc, const char *argv[])
nfc_context *context;
nfc_init(&context);
if (context == NULL) {
ERR("Unable to init libnfc (malloc)");
exit(EXIT_FAILURE);
}
pnd = nfc_open(context, NULL);
......
......@@ -110,6 +110,10 @@ main(int argc, char *argv[])
nfc_context *context;
nfc_init(&context);
if (context == NULL) {
ERR("Unable to init libnfc (malloc)");
exit(EXIT_FAILURE);
}
nfc_connstring connstrings[MAX_DEVICE_COUNT];
// List available devices
size_t szFound = nfc_list_devices(context, connstrings, MAX_DEVICE_COUNT);
......@@ -123,7 +127,7 @@ main(int argc, char *argv[])
// Try to open the NFC emulator device
pndTag = nfc_open(context, connstrings[0]);
if (pndTag == NULL) {
printf("Error opening NFC emulator device\n");
ERR("Error opening NFC emulator device");
nfc_exit(context);
exit(EXIT_FAILURE);
}
......
......@@ -69,6 +69,10 @@ main(int argc, const char *argv[])
nfc_context *context;
nfc_init(&context);
if (context == NULL) {
ERR("Unable to init libnfc (malloc)");
exit(EXIT_FAILURE);
}
// Display libnfc version
acLibnfcVersion = nfc_version();
......
......@@ -77,6 +77,10 @@ main(int argc, const char *argv[])
nfc_context *context;
nfc_init(&context);
if (context == NULL) {
ERR("Unable to init libnfc (malloc)");
exit(EXIT_FAILURE);
}
// Display libnfc version
const char *acLibnfcVersion = nfc_version();
......
......@@ -85,6 +85,10 @@ int main(int argc, const char *argv[])
nfc_context *context;
nfc_init(&context);
if (context == NULL) {
ERR("Unable to init libnfc (malloc)");
exit(EXIT_FAILURE);
}
// Try to open the NFC reader
pnd = nfc_open(context, NULL);
......
......@@ -174,8 +174,7 @@ nfc_init(nfc_context **context)
*context = nfc_context_new();
if (!context) {
perror("malloc");
// TODO: not a good idea to call exit() from a library, we should change API and return error
exit(EXIT_FAILURE);
return;
}
if (!nfc_drivers)
nfc_drivers_init();
......
......@@ -380,6 +380,10 @@ main(int argc, char *argv[])
nfc_context *context;
nfc_init(&context);
if (context == NULL) {
ERR("Unable to init libnfc (malloc)\n");
exit(EXIT_FAILURE);
}
// Try to open the NFC reader
pnd = nfc_open(context, NULL);
......
......@@ -71,6 +71,10 @@ main(int argc, const char *argv[])
nfc_context *context;
nfc_init(&context);
if (context == NULL) {
ERR("Unable to init libnfc (malloc)");
exit(EXIT_FAILURE);
}
// Display libnfc version
acLibnfcVersion = nfc_version();
......
......@@ -503,11 +503,15 @@ main(int argc, const char *argv[])
// printf("Successfully opened required files\n");
nfc_init(&context);
if (context == NULL) {
ERR("Unable to init libnfc (malloc)");
exit(EXIT_FAILURE);
}
// Try to open the NFC reader
pnd = nfc_open(context, NULL);
if (pnd == NULL) {
printf("Error opening NFC reader\n");
ERR("Error opening NFC reader");
nfc_exit(context);
exit(EXIT_FAILURE);
}
......
......@@ -206,11 +206,15 @@ main(int argc, const char *argv[])
nfc_context *context;
nfc_init(&context);
if (context == NULL) {
ERR("Unable to init libnfc (malloc)");
exit(EXIT_FAILURE);
}
// Try to open the NFC device
pnd = nfc_open(context, NULL);
if (pnd == NULL) {
ERR("Error opening NFC device\n");
ERR("Error opening NFC device");
nfc_exit(context);
exit(EXIT_FAILURE);
}
......
......@@ -198,6 +198,10 @@ main(int argc, char *argv[])
nfc_context *context;
nfc_init(&context);
if (context == NULL) {
ERR("Unable to init libnfc (malloc)\n");
exit(EXIT_FAILURE);
}
pnd = nfc_open(context, NULL);
......
......@@ -197,6 +197,10 @@ main(int argc, char *argv[])
nfc_context *context;
nfc_init(&context);
if (context == NULL) {
ERR("Unable to init libnfc (malloc)");
exit(EXIT_FAILURE);
}
nfc_connstring connstrings[MAX_DEVICE_COUNT];
// List available devices
......
......@@ -90,6 +90,10 @@ main(int argc, const char *argv[])
}
nfc_init(&context);
if (context == NULL) {
ERR("Unable to init libnfc (malloc)\n");
exit(EXIT_FAILURE);
}
// Display libnfc version
acLibnfcVersion = nfc_version();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment