Host: Examples: Add error check for open failures

If open output file failed (e.g. missing permission) this was not
tracked and the C rx example crashed when try to write to the
empty file descriptor. This checks the result of fopen and prints
the str error of the failure for graceful exit.
This commit is contained in:
Lars Amsel 2025-10-23 10:02:34 +02:00 committed by Jörg Hofrichter
parent 12de60b20b
commit 192b3b24f0

View File

@ -9,6 +9,7 @@
#include "getopt.h"
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -35,6 +36,16 @@ void print_help(void)
" -h (print this help message)\n");
};
int open_file(const char* filename, FILE** fp)
{
*fp = fopen(filename, "wb");
if (!*fp) {
fprintf(stderr, "Cannot open %s: %s\n", filename, strerror(errno));
return UHD_ERROR_OS;
}
return UHD_ERROR_NONE;
}
int main(int argc, char* argv[])
{
int option = 0;
@ -176,7 +187,8 @@ int main(int argc, char* argv[])
free_buffer, uhd_rx_streamer_issue_stream_cmd(rx_streamer, &stream_cmd))
// Set up file output
fp = fopen(filename, "wb");
EXECUTE_OR_GOTO(
free_buffer, open_file(filename, &fp));
// Actual streaming
while (num_acc_samps < n_samples) {