From 0fdd2b9feac05775553a53475b99b9bf6de6c6d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beat=20K=C3=BCng?= Date: Thu, 4 May 2017 16:38:15 +0200 Subject: [PATCH] dumpfile: add documentation & do cleanup (remove err(), ...) --- src/systemcmds/dumpfile/dumpfile.c | 34 +++++++++++++++++++----------- 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/src/systemcmds/dumpfile/dumpfile.c b/src/systemcmds/dumpfile/dumpfile.c index f0e5581ab7..981c701b6d 100644 --- a/src/systemcmds/dumpfile/dumpfile.c +++ b/src/systemcmds/dumpfile/dumpfile.c @@ -34,12 +34,12 @@ /** * @file dumpfile.c * - * Dump file utility. Prints file size and contents in binary mode (don't replace LF with CR LF) to stdout. - * * @author Anton Babushkin */ #include +#include +#include #include #include #include @@ -51,11 +51,21 @@ __EXPORT int dumpfile_main(int argc, char *argv[]); +static void print_usage(void) +{ + PRINT_MODULE_DESCRIPTION("Dump file utility. Prints file size and contents in binary mode (don't replace LF with CR LF) to stdout."); + + PRINT_MODULE_USAGE_NAME_SIMPLE("dumpfile", "command"); + PRINT_MODULE_USAGE_ARG("", "File to dump", false); + +} + int dumpfile_main(int argc, char *argv[]) { if (argc < 2) { - errx(1, "usage: dumpfile "); + print_usage(); + return 1; } /* open input file */ @@ -63,8 +73,8 @@ dumpfile_main(int argc, char *argv[]) f = fopen(argv[1], "r"); if (f == NULL) { - printf("ERROR\n"); - exit(1); + PX4_ERR("Failed to open file (%i)", errno); + return 1; } /* get file size */ @@ -72,7 +82,7 @@ dumpfile_main(int argc, char *argv[]) int size = ftell(f); fseek(f, 0L, SEEK_SET); - printf("OK %d\n", size); + printf("File size: %d bytes\n", size); /* configure stdout */ int out = fileno(stdout); @@ -88,8 +98,8 @@ dumpfile_main(int argc, char *argv[]) tc.c_oflag &= ~ONLCR; if (tcsetattr(out, TCSANOW, &tc) < 0) { - warnx("ERROR setting stdout attributes"); - exit(1); + PX4_ERR("failed setting stdout attributes"); + return 1; } char buf[512]; @@ -98,7 +108,7 @@ dumpfile_main(int argc, char *argv[]) /* dump file */ while ((nread = fread(buf, 1, sizeof(buf), f)) > 0) { if (write(out, buf, nread) <= 0) { - warnx("error dumping file"); + PX4_ERR("write failed"); break; } } @@ -108,9 +118,9 @@ dumpfile_main(int argc, char *argv[]) /* restore old terminal attributes */ if (tcsetattr(out, TCSANOW, &tc_old) < 0) { - warnx("ERROR restoring stdout attributes"); - exit(1); + PX4_ERR("failed to restore stdout attributes"); + return 1; } - return OK; + return 0; }