From 7698541e6beb9133c5841593681f5e9595ef354e Mon Sep 17 00:00:00 2001 From: hyche Date: Sun, 4 Jun 2017 23:07:38 +0700 Subject: [PATCH] btrfs_shell: Added cat command --- src/tools/btrfs_shell/Jamfile | 5 ++ src/tools/btrfs_shell/additional_commands.cpp | 19 ++++++ src/tools/btrfs_shell/command_cat.cpp | 61 +++++++++++++++++++ src/tools/btrfs_shell/command_cat.h | 17 ++++++ 4 files changed, 102 insertions(+) create mode 100644 src/tools/btrfs_shell/additional_commands.cpp create mode 100644 src/tools/btrfs_shell/command_cat.cpp create mode 100644 src/tools/btrfs_shell/command_cat.h diff --git a/src/tools/btrfs_shell/Jamfile b/src/tools/btrfs_shell/Jamfile index a3786e6f40..dcf071e66d 100644 --- a/src/tools/btrfs_shell/Jamfile +++ b/src/tools/btrfs_shell/Jamfile @@ -26,8 +26,11 @@ if ! $(HOST_PLATFORM_BEOS_COMPATIBLE) { fsShellCommandLibs = $(HOST_NETWORK_LIBS) ; } +UseHeaders [ FDirName $(HAIKU_TOP) headers build ] : true ; + if ! $(HOST_PLATFORM_BEOS_COMPATIBLE) { UseHeaders [ FDirName $(HAIKU_TOP) headers build os ] : true ; + UseHeaders [ FDirName $(HAIKU_TOP) headers build os support ] : true ; } UsePrivateHeaders shared storage fs_shell ; @@ -50,6 +53,8 @@ BuildPlatformMergeObject btrfs.o : $(btrfsSources) ; BuildPlatformMain btrfs_shell : + additional_commands.cpp + command_cat.cpp : btrfs.o fs_shell.a $(libHaikuCompat) $(HOST_LIBSUPC++) $(HOST_LIBSTDC++) diff --git a/src/tools/btrfs_shell/additional_commands.cpp b/src/tools/btrfs_shell/additional_commands.cpp new file mode 100644 index 0000000000..82a1f12a87 --- /dev/null +++ b/src/tools/btrfs_shell/additional_commands.cpp @@ -0,0 +1,19 @@ +#include "fssh.h" + +#include "command_cat.h" + + +namespace FSShell { + + +void +register_additional_commands() +{ + CommandManager::Default()->AddCommands( + command_cat, "cat", "concatenate file(s) to stdout", + NULL + ); +} + + +} // namespace FSShell diff --git a/src/tools/btrfs_shell/command_cat.cpp b/src/tools/btrfs_shell/command_cat.cpp new file mode 100644 index 0000000000..e1417a87dc --- /dev/null +++ b/src/tools/btrfs_shell/command_cat.cpp @@ -0,0 +1,61 @@ +#include +#include + +#include "syscalls.h" +#include "system_dependencies.h" + + +namespace FSShell { + + +fssh_status_t +command_cat(int argc, const char* const* argv) +{ + long numBytes = 10; + int fileStart = 1; + if (argc < 2 || strcmp(argv[1], "--help") == 0) { + printf( + "Usage: %s [ -n ] [FILE]...\n" + "\t -n\tNumber of bytes to read\n", + argv[0] + ); + return FSSH_B_OK; + } + + if (argc > 3 && strcmp(argv[1], "-n") == 0) { + fileStart += 2; + numBytes = strtol(argv[2], NULL, 10); + } + + const char* const* files = argv + fileStart; + for (; *files; files++) { + const char* file = *files; + int fd = _kern_open(-1, file, O_RDONLY, O_RDONLY); + if (fd < 0) { + fssh_dprintf("Error: %s\n", fssh_strerror(fd)); + return FSSH_B_BAD_VALUE; + } + + char buffer[numBytes + 1]; + if (buffer == NULL) { + fssh_dprintf("Error: No memory\n"); + _kern_close(fd); + return FSSH_B_NO_MEMORY; + } + + if (_kern_read(fd, 0, buffer, numBytes) != numBytes) { + fssh_dprintf("Error: fail to read, length: %i\n", numBytes); + _kern_close(fd); + return FSSH_B_BAD_VALUE; + } + + _kern_close(fd); + buffer[numBytes] = '\0'; + printf("%s\n", buffer); + } + + return FSSH_B_OK; +} + + +} // namespace FSShell diff --git a/src/tools/btrfs_shell/command_cat.h b/src/tools/btrfs_shell/command_cat.h new file mode 100644 index 0000000000..a86fd78a25 --- /dev/null +++ b/src/tools/btrfs_shell/command_cat.h @@ -0,0 +1,17 @@ +#ifndef COMMAND_CAT_H +#define COMMAND_CAT_H + + +#include "fssh_types.h" + + +namespace FSShell { + + +fssh_status_t command_cat(int argc, const char* const* argv); + + +} // namespace FSShell + + +#endif // COMMAND_CAT_H