mirror of
https://github.com/PX4/PX4-Autopilot.git
synced 2026-05-26 17:36:40 +08:00
dumpfile: add documentation & do cleanup (remove err(), ...)
This commit is contained in:
@@ -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 <anton.babushkin@me.com>
|
||||
*/
|
||||
|
||||
#include <px4_config.h>
|
||||
#include <px4_log.h>
|
||||
#include <px4_module.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
@@ -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>", "File to dump", false);
|
||||
|
||||
}
|
||||
|
||||
int
|
||||
dumpfile_main(int argc, char *argv[])
|
||||
{
|
||||
if (argc < 2) {
|
||||
errx(1, "usage: dumpfile <filename>");
|
||||
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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user