Handling errors returned from the library.
#include <stdio.h>
#include <stdlib.h>
void end_program(int return_code)
{
if (port_list != NULL)
if (config != NULL)
if (port != NULL)
exit(return_code);
}
{
int error_code;
char *error_message;
switch (result) {
printf("Error: Invalid argument.\n");
end_program(1);
printf("Error: Failed: OS error code: %d, message: '%s'\n",
error_code, error_message);
end_program(2);
printf("Error: Not supported.\n");
end_program(3);
end_program(4);
default:
printf("Operation succeeded.\n");
return result;
}
}
int main(int argc, char **argv)
{
printf("Getting list of ports.\n");
printf("Creating a new port configuration.\n");
printf("Trying to find a port that doesn't exist.\n");
end_program(0);
}