btrfs_shell: Added cat command

This commit is contained in:
hyche 2017-06-04 23:07:38 +07:00
parent 548a0d80a0
commit 7698541e6b
4 changed files with 102 additions and 0 deletions

View File

@ -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 <build>btrfs.o : $(btrfsSources) ;
BuildPlatformMain <build>btrfs_shell
:
additional_commands.cpp
command_cat.cpp
:
<build>btrfs.o
<build>fs_shell.a $(libHaikuCompat) $(HOST_LIBSUPC++) $(HOST_LIBSTDC++)

View File

@ -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

View File

@ -0,0 +1,61 @@
#include <stdio.h>
#include <stdlib.h>
#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

View File

@ -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