Browse Source

dumpfile: add documentation & do cleanup (remove err(), ...)

sbg
Beat Küng 8 years ago
parent
commit
0fdd2b9fea
  1. 34
      src/systemcmds/dumpfile/dumpfile.c

34
src/systemcmds/dumpfile/dumpfile.c

@ -34,12 +34,12 @@
/** /**
* @file dumpfile.c * @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 <anton.babushkin@me.com> * @author Anton Babushkin <anton.babushkin@me.com>
*/ */
#include <px4_config.h> #include <px4_config.h>
#include <px4_log.h>
#include <px4_module.h>
#include <unistd.h> #include <unistd.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
@ -51,11 +51,21 @@
__EXPORT int dumpfile_main(int argc, char *argv[]); __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>", "File to dump", false);
}
int int
dumpfile_main(int argc, char *argv[]) dumpfile_main(int argc, char *argv[])
{ {
if (argc < 2) { if (argc < 2) {
errx(1, "usage: dumpfile <filename>"); print_usage();
return 1;
} }
/* open input file */ /* open input file */
@ -63,8 +73,8 @@ dumpfile_main(int argc, char *argv[])
f = fopen(argv[1], "r"); f = fopen(argv[1], "r");
if (f == NULL) { if (f == NULL) {
printf("ERROR\n"); PX4_ERR("Failed to open file (%i)", errno);
exit(1); return 1;
} }
/* get file size */ /* get file size */
@ -72,7 +82,7 @@ dumpfile_main(int argc, char *argv[])
int size = ftell(f); int size = ftell(f);
fseek(f, 0L, SEEK_SET); fseek(f, 0L, SEEK_SET);
printf("OK %d\n", size); printf("File size: %d bytes\n", size);
/* configure stdout */ /* configure stdout */
int out = fileno(stdout); int out = fileno(stdout);
@ -88,8 +98,8 @@ dumpfile_main(int argc, char *argv[])
tc.c_oflag &= ~ONLCR; tc.c_oflag &= ~ONLCR;
if (tcsetattr(out, TCSANOW, &tc) < 0) { if (tcsetattr(out, TCSANOW, &tc) < 0) {
warnx("ERROR setting stdout attributes"); PX4_ERR("failed setting stdout attributes");
exit(1); return 1;
} }
char buf[512]; char buf[512];
@ -98,7 +108,7 @@ dumpfile_main(int argc, char *argv[])
/* dump file */ /* dump file */
while ((nread = fread(buf, 1, sizeof(buf), f)) > 0) { while ((nread = fread(buf, 1, sizeof(buf), f)) > 0) {
if (write(out, buf, nread) <= 0) { if (write(out, buf, nread) <= 0) {
warnx("error dumping file"); PX4_ERR("write failed");
break; break;
} }
} }
@ -108,9 +118,9 @@ dumpfile_main(int argc, char *argv[])
/* restore old terminal attributes */ /* restore old terminal attributes */
if (tcsetattr(out, TCSANOW, &tc_old) < 0) { if (tcsetattr(out, TCSANOW, &tc_old) < 0) {
warnx("ERROR restoring stdout attributes"); PX4_ERR("failed to restore stdout attributes");
exit(1); return 1;
} }
return OK; return 0;
} }

Loading…
Cancel
Save