@@ -58,14 +58,16 @@ char *cmn_strndup(const char *str, size_t n)
5858#endif
5959}
6060
61- struct iio_context * autodetect_context (bool rtn , const char * name , const char * scan )
61+ struct iio_context * autodetect_context (bool rtn , const char * name ,
62+ const char * scan , int * err_code )
6263{
6364 struct iio_scan_context * scan_ctx ;
6465 struct iio_context_info * * info ;
6566 struct iio_context * ctx = NULL ;
6667 unsigned int i ;
6768 ssize_t ret ;
6869 FILE * out ;
70+ int err = EXIT_FAILURE ;
6971
7072 scan_ctx = iio_create_scan_context (scan , 0 );
7173 if (!scan_ctx ) {
@@ -97,6 +99,7 @@ struct iio_context * autodetect_context(bool rtn, const char * name, const char
9799 } else {
98100 out = stdout ;
99101 fprintf (out , "Available contexts:\n" );
102+ err = EXIT_SUCCESS ;
100103 }
101104 for (i = 0 ; i < (size_t ) ret ; i ++ ) {
102105 fprintf (out , "\t%u: %s [%s]\n" ,
@@ -110,6 +113,8 @@ struct iio_context * autodetect_context(bool rtn, const char * name, const char
110113err_free_ctx :
111114 iio_scan_context_destroy (scan_ctx );
112115
116+ if (err_code )
117+ * err_code = err ;
113118 return ctx ;
114119}
115120
@@ -260,7 +265,8 @@ static const char *common_options_descriptions[] = {
260265
261266struct iio_context * handle_common_opts (char * name , int argc ,
262267 char * const argv [], const char * optstring ,
263- const struct option * options , const char * options_descriptions [])
268+ const struct option * options , const char * options_descriptions [],
269+ int * err_code )
264270{
265271 struct iio_context * ctx = NULL ;
266272 int c ;
@@ -280,7 +286,7 @@ struct iio_context * handle_common_opts(char * name, int argc,
280286 opts = add_common_options (options );
281287 if (!opts ) {
282288 fprintf (stderr , "Failed to add common options\n" );
283- return NULL ;
289+ goto err_fail ;
284290 }
285291
286292 while ((c = getopt_long (argc , argv , buf , /* Flawfinder: ignore */
@@ -296,43 +302,43 @@ struct iio_context * handle_common_opts(char * name, int argc,
296302 case 'n' :
297303 if (backend != IIO_LOCAL ) {
298304 fprintf (stderr , "-a, -x, -n and -u are mutually exclusive\n" );
299- return NULL ;
305+ goto err_fail ;
300306 }
301307 if (!optarg ) {
302308 fprintf (stderr , "network options requires a uri\n" );
303- return NULL ;
309+ goto err_fail ;
304310 }
305311 backend = IIO_NETWORK ;
306312 arg = optarg ;
307313 break ;
308314 case 'x' :
309315 if (backend != IIO_LOCAL ) {
310316 fprintf (stderr , "-a, -x, -n and -u are mutually exclusive\n" );
311- return NULL ;
317+ goto err_fail ;
312318 }
313319 if (!optarg ) {
314320 fprintf (stderr , "xml options requires a uri\n" );
315- return NULL ;
321+ goto err_fail ;
316322 }
317323 backend = IIO_XML ;
318324 arg = optarg ;
319325 break ;
320326 case 'u' :
321327 if (backend != IIO_LOCAL ) {
322328 fprintf (stderr , "-a, -x, -n and -u are mutually exclusive\n" );
323- return NULL ;
329+ goto err_fail ;
324330 }
325331 if (!optarg ) {
326332 fprintf (stderr , "uri options requires a uri\n" );
327- return NULL ;
333+ goto err_fail ;
328334 }
329335 backend = IIO_URI ;
330336 arg = optarg ;
331337 break ;
332338 case 'a' :
333339 if (backend != IIO_LOCAL ) {
334340 fprintf (stderr , "-a, -x, -n and -u are mutually exclusive\n" );
335- return NULL ;
341+ goto err_fail ;
336342 }
337343 backend = IIO_AUTO ;
338344 detect_context = true;
@@ -355,7 +361,7 @@ struct iio_context * handle_common_opts(char * name, int argc,
355361 case 'T' :
356362 if (!optarg ) {
357363 fprintf (stderr , "Timeout requires an argument\n" );
358- return NULL ;
364+ goto err_fail ;
359365 }
360366 timeout = sanitize_clamp ("timeout" , optarg , 0 , INT_MAX );
361367 break ;
@@ -368,10 +374,10 @@ struct iio_context * handle_common_opts(char * name, int argc,
368374 opterr = 1 ;
369375
370376 if (do_scan ) {
371- autodetect_context (false, name , arg );
377+ autodetect_context (false, name , arg , err_code );
372378 return NULL ;
373379 } else if (detect_context || backend == IIO_AUTO )
374- ctx = autodetect_context (true, name , arg );
380+ ctx = autodetect_context (true, name , arg , err_code );
375381 else if (!arg && backend != IIO_LOCAL )
376382 fprintf (stderr , "argument parsing error\n" );
377383 else if (backend == IIO_XML )
@@ -390,6 +396,7 @@ struct iio_context * handle_common_opts(char * name, int argc,
390396 fprintf (stderr , "Unable to create IIO context %s: %s\n" , arg , err_str );
391397 else
392398 fprintf (stderr , "Unable to create Local IIO context : %s\n" , err_str );
399+ goto err_fail ;
393400 }
394401
395402 if (ctx && timeout >= 0 ) {
@@ -400,11 +407,16 @@ struct iio_context * handle_common_opts(char * name, int argc,
400407 fprintf (stderr , "IIO contexts set timeout failed : %s\n" ,
401408 err_str );
402409 iio_context_destroy (ctx );
403- return NULL ;
410+ goto err_fail ;
404411 }
405412 }
406413
407414 return ctx ;
415+
416+ err_fail :
417+ if (err_code )
418+ * err_code = EXIT_FAILURE ;
419+ return NULL ;
408420}
409421
410422void usage (char * name , const struct option * options ,
0 commit comments