Maybe the values returned by picoproj will never be used, but it is
better to use a clean style in example code.
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
int ret;
int main(int argc, char *argv[])
{
int ret;
- int exit_code = EXIT_SUCCESS;
int opt;
char filename[FILENAME_MAX] = {0};
int opt;
char filename[FILENAME_MAX] = {0};
device_index = atoi(optarg);
if (device_index < 0) {
fprintf(stderr, "Unsupported device index\n");
device_index = atoi(optarg);
if (device_index < 0) {
fprintf(stderr, "Unsupported device index\n");
+ ret = -EINVAL;
+ goto out;
break;
default:
fprintf(stderr, "Unsupported format\n");
break;
default:
fprintf(stderr, "Unsupported format\n");
+ ret = -EINVAL;
+ goto out;
default:
fprintf(stderr, "Invalid power mode value, must be between %d and %d\n",
AM7XXX_POWER_OFF, AM7XXX_POWER_TURBO);
default:
fprintf(stderr, "Invalid power mode value, must be between %d and %d\n",
AM7XXX_POWER_OFF, AM7XXX_POWER_TURBO);
+ ret = -EINVAL;
+ goto out;
default:
fprintf(stderr, "Invalid zoom mode value, must be between %d and %d\n",
AM7XXX_ZOOM_ORIGINAL, AM7XXX_ZOOM_TEST);
default:
fprintf(stderr, "Invalid zoom mode value, must be between %d and %d\n",
AM7XXX_ZOOM_ORIGINAL, AM7XXX_ZOOM_TEST);
+ ret = -EINVAL;
+ goto out;
}
break;
case 'W':
width = atoi(optarg);
if (width < 0) {
fprintf(stderr, "Unsupported width\n");
}
break;
case 'W':
width = atoi(optarg);
if (width < 0) {
fprintf(stderr, "Unsupported width\n");
+ ret = -EINVAL;
+ goto out;
}
break;
case 'H':
height = atoi(optarg);
if (height < 0) {
fprintf(stderr, "Unsupported height\n");
}
break;
case 'H':
height = atoi(optarg);
if (height < 0) {
fprintf(stderr, "Unsupported height\n");
+ ret = -EINVAL;
+ goto out;
}
break;
case 'h':
usage(argv[0]);
}
break;
case 'h':
usage(argv[0]);
default: /* '?' */
usage(argv[0]);
default: /* '?' */
usage(argv[0]);
+ ret = -EINVAL;
+ goto out;
}
}
if (filename[0] == '\0') {
fprintf(stderr, "An image file MUST be specified with the -f option.\n\n");
usage(argv[0]);
}
}
if (filename[0] == '\0') {
fprintf(stderr, "An image file MUST be specified with the -f option.\n\n");
usage(argv[0]);
- exit_code = EXIT_FAILURE;
goto out;
}
image_fp = fopen(filename, "rb");
if (image_fp == NULL) {
perror("fopen");
goto out;
}
image_fp = fopen(filename, "rb");
if (image_fp == NULL) {
perror("fopen");
- exit_code = EXIT_FAILURE;
- if (fstat(fileno(image_fp), &st) < 0) {
+ ret = fstat(fileno(image_fp), &st);
+ if (ret < 0) {
- exit_code = EXIT_FAILURE;
goto out_close_image_fp;
}
size = st.st_size;
goto out_close_image_fp;
}
size = st.st_size;
image = malloc(size * sizeof(unsigned char));
if (image == NULL) {
perror("malloc");
image = malloc(size * sizeof(unsigned char));
if (image == NULL) {
perror("malloc");
- exit_code = EXIT_FAILURE;
goto out_close_image_fp;
}
goto out_close_image_fp;
}
else
fprintf(stderr, "Unexpected error condition.\n");
else
fprintf(stderr, "Unexpected error condition.\n");
+ if (ret >= 0)
+ ret = -EINVAL;
goto out_free_image;
}
ret = am7xxx_init(&ctx);
if (ret < 0) {
perror("am7xxx_init");
goto out_free_image;
}
ret = am7xxx_init(&ctx);
if (ret < 0) {
perror("am7xxx_init");
- exit_code = EXIT_FAILURE;
ret = am7xxx_open_device(ctx, &dev, 0);
if (ret < 0) {
perror("am7xxx_open_device");
ret = am7xxx_open_device(ctx, &dev, 0);
if (ret < 0) {
perror("am7xxx_open_device");
- exit_code = EXIT_FAILURE;
goto cleanup;
}
ret = am7xxx_close_device(dev);
if (ret < 0) {
perror("am7xxx_close_device");
goto cleanup;
}
ret = am7xxx_close_device(dev);
if (ret < 0) {
perror("am7xxx_close_device");
- exit_code = EXIT_FAILURE;
goto cleanup;
}
ret = am7xxx_open_device(ctx, &dev, device_index);
if (ret < 0) {
perror("am7xxx_open_device");
goto cleanup;
}
ret = am7xxx_open_device(ctx, &dev, device_index);
if (ret < 0) {
perror("am7xxx_open_device");
- exit_code = EXIT_FAILURE;
goto cleanup;
}
ret = am7xxx_get_device_info(dev, &device_info);
if (ret < 0) {
perror("am7xxx_get_device_info");
goto cleanup;
}
ret = am7xxx_get_device_info(dev, &device_info);
if (ret < 0) {
perror("am7xxx_get_device_info");
- exit_code = EXIT_FAILURE;
goto cleanup;
}
printf("Native resolution: %dx%d\n",
goto cleanup;
}
printf("Native resolution: %dx%d\n",
ret = am7xxx_set_zoom_mode(dev, zoom);
if (ret < 0) {
perror("am7xxx_set_zoom_mode");
ret = am7xxx_set_zoom_mode(dev, zoom);
if (ret < 0) {
perror("am7xxx_set_zoom_mode");
- exit_code = EXIT_FAILURE;
goto cleanup;
}
ret = am7xxx_set_power_mode(dev, power_mode);
if (ret < 0) {
perror("am7xxx_set_power_mode");
goto cleanup;
}
ret = am7xxx_set_power_mode(dev, power_mode);
if (ret < 0) {
perror("am7xxx_set_power_mode");
- exit_code = EXIT_FAILURE;
ret = am7xxx_send_image(dev, format, width, height, image, (unsigned int)size);
if (ret < 0) {
perror("am7xxx_send_image");
ret = am7xxx_send_image(dev, format, width, height, image, (unsigned int)size);
if (ret < 0) {
perror("am7xxx_send_image");
- exit_code = EXIT_FAILURE;
- exit_code = EXIT_SUCCESS;
cleanup:
am7xxx_shutdown(ctx);
cleanup:
am7xxx_shutdown(ctx);
free(image);
out_close_image_fp:
free(image);
out_close_image_fp:
- ret = fclose(image_fp);
- if (ret == EOF)
+ if (fclose(image_fp) == EOF)