shell: Add an md5 hash command for files.

This command lets you get an MD5 hash for a file in an RTEMS file system.
This commit is contained in:
Chris Johns
2014-09-15 09:10:45 +10:00
parent ebf9a057b5
commit b299960aed
4 changed files with 258 additions and 70 deletions

View File

@@ -85,9 +85,9 @@ libshell_a_SOURCES = shell/cat_file.c shell/cmds.c shell/internal.h \
shell/main_cp.c shell/main_cpuuse.c shell/main_date.c shell/main_dir.c \ shell/main_cp.c shell/main_cpuuse.c shell/main_date.c shell/main_dir.c \
shell/main_echo.c shell/main_exit.c shell/main_halt.c shell/main_help.c \ shell/main_echo.c shell/main_exit.c shell/main_halt.c shell/main_help.c \
shell/main_id.c shell/main_logoff.c shell/main_ln.c shell/main_ls.c \ shell/main_id.c shell/main_logoff.c shell/main_ln.c shell/main_ls.c \
shell/main_mallocinfo.c shell/main_mdump.c shell/main_medit.c \ shell/main_mallocinfo.c shell/main_md5.c shell/main_mdump.c \
shell/main_mfill.c shell/main_mkdir.c shell/main_mount.c \ shell/main_medit.c shell/main_mfill.c shell/main_mkdir.c \
shell/main_mmove.c shell/main_msdosfmt.c \ shell/main_mount.c shell/main_mmove.c shell/main_msdosfmt.c \
shell/main_mv.c shell/main_perioduse.c shell/main_ping.c \ shell/main_mv.c shell/main_perioduse.c shell/main_ping.c \
shell/main_pwd.c shell/main_rm.c shell/main_rmdir.c shell/main_sleep.c \ shell/main_pwd.c shell/main_rm.c shell/main_rmdir.c shell/main_sleep.c \
shell/main_stackuse.c shell/main_tty.c shell/main_umask.c \ shell/main_stackuse.c shell/main_tty.c shell/main_umask.c \

View File

@@ -0,0 +1,110 @@
/*
* MD5 Shell Command Implmentation
*
* The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at
* http://www.rtems.com/license/LICENSE.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <rtems.h>
#include <rtems/shell.h>
#include <md5.h>
#define BUFFER_SIZE (4 * 1024)
static int rtems_shell_main_md5(
int argc,
char *argv[])
{
uint8_t* buffer;
uint8_t hash[16];
size_t h;
buffer = malloc(BUFFER_SIZE);
if (!buffer)
{
printf("error: no memory\n");
return 1;
}
--argc;
++argv;
while (argc)
{
struct stat sb;
MD5_CTX md5;
int in;
if (stat(*argv, &sb) < 0)
{
free(buffer);
printf("error: stat of %s: %s\n", *argv, strerror(errno));
return 1;
}
in = open(*argv, O_RDONLY);
if (in < 0)
{
free(buffer);
printf("error: opening %s: %s\n", *argv, strerror(errno));
return 1;
}
MD5Init(&md5);
while (sb.st_size)
{
ssize_t size = sb.st_size > BUFFER_SIZE ? BUFFER_SIZE : sb.st_size;
if (read(in, buffer, size) != size)
{
close(in);
free(buffer);
printf("error: reading %s: %s\n", *argv, strerror(errno));
return 1;
}
MD5Update(&md5, buffer, size);
sb.st_size -= size;
}
MD5Final(hash, &md5);
close(in);
printf("MD5 (%s) = ", *argv);
for (h = 0; h < sizeof(hash); ++h)
printf("%02x", (int) hash[h]);
printf("\n");
--argc;
++argv;
}
free(buffer);
return 0;
}
rtems_shell_cmd_t rtems_shell_MD5_Command = {
"md5", /* name */
"md5 [file ...]", /* usage */
"files", /* topic */
rtems_shell_main_md5, /* command */
NULL, /* alias */
NULL /* next */
};

View File

@@ -71,6 +71,7 @@ extern rtems_shell_cmd_t rtems_shell_DD_Command;
extern rtems_shell_cmd_t rtems_shell_HEXDUMP_Command; extern rtems_shell_cmd_t rtems_shell_HEXDUMP_Command;
extern rtems_shell_cmd_t rtems_shell_DEBUGRFS_Command; extern rtems_shell_cmd_t rtems_shell_DEBUGRFS_Command;
extern rtems_shell_cmd_t rtems_shell_DF_Command; extern rtems_shell_cmd_t rtems_shell_DF_Command;
extern rtems_shell_cmd_t rtems_shell_MD5_Command;
extern rtems_shell_cmd_t rtems_shell_RTC_Command; extern rtems_shell_cmd_t rtems_shell_RTC_Command;
@@ -382,6 +383,11 @@ extern rtems_shell_alias_t *rtems_shell_Initial_aliases[];
defined(CONFIGURE_SHELL_COMMAND_DF) defined(CONFIGURE_SHELL_COMMAND_DF)
&rtems_shell_DF_Command, &rtems_shell_DF_Command,
#endif #endif
#if (defined(CONFIGURE_SHELL_COMMANDS_ALL) && \
!defined(CONFIGURE_SHELL_NO_COMMAND_MD5)) || \
defined(CONFIGURE_SHELL_COMMAND_MD5)
&rtems_shell_MD5_Command,
#endif
/* /*
* RTEMS Related commands * RTEMS Related commands

View File

@@ -36,6 +36,7 @@ The RTEMS shell has the following file and directory commands:
@item @code{mkrfs} - format RFS file system @item @code{mkrfs} - format RFS file system
@item @code{cd} - alias for chdir @item @code{cd} - alias for chdir
@item @code{df} - display file system disk space usage @item @code{df} - display file system disk space usage
@item @code{md5} - display file system disk space usage
@end itemize @end itemize
@@ -2708,3 +2709,74 @@ following prototype:
extern rtems_shell_cmd_t rtems_shell_DF_Command; extern rtems_shell_cmd_t rtems_shell_DF_Command;
@end example @end example
@c
@c
@c
@page
@subsection md5 - compute the Md5 hash of a file or list of files
@pgindex md5
@subheading SYNOPSYS:
@example
md5 <files>
@end example
@subheading DESCRIPTION:
This command prints the MD5 of a file. You can provide one or more
files on the command line and a hash for each file is printed in a
single line of output.
@subheading EXIT STATUS:
This command returns 0 on success and non-zero if an error is encountered.
@subheading NOTES:
NONE
@subheading EXAMPLES:
The following is an example of how to use @code{md5}:
@example
SHLL [/] $ md5 shell-init
MD5 (shell-init) = 43b4d2e71b47db79eae679a2efeacf31
@end example
@subheading CONFIGURATION:
@findex CONFIGURE_SHELL_NO_COMMAND_MD5
@findex CONFIGURE_SHELL_COMMAND_MD5
This command is included in the default shell command set.
When building a custom command set, define
@code{CONFIGURE_SHELL_COMMAND_MD5} to have this
command included.
This command can be excluded from the shell command set by
defining @code{CONFIGURE_SHELL_NO_COMMAND_MD5} when all
shell commands have been configured.
@subheading PROGRAMMING INFORMATION:
@findex rtems_shell_rtems_main_md5
The @code{df} is implemented by a C language function
which has the following prototype:
@example
int rtems_shell_main_md5(
int argc,
char **argv
);
@end example
The configuration structure for the @code{md5} has the
following prototype:
@example
extern rtems_shell_cmd_t rtems_shell_MD5_Command;
@end example