diff --git a/src/tests/add-ons/kernel/file_systems/fs_shell/Jamfile b/src/tests/add-ons/kernel/file_systems/fs_shell/Jamfile new file mode 100644 index 0000000000..722f8c652b --- /dev/null +++ b/src/tests/add-ons/kernel/file_systems/fs_shell/Jamfile @@ -0,0 +1,10 @@ +SubDir OBOS_TOP src tests add-ons kernel file_systems fs_shell ; + +local fsShellCommandSources ; +if $(OS) = BEOS { + fsShellCommandSources = fs_shell_command_beos.cpp ; +} + +if $(fsShellCommandSources) { + BuildPlatformMain fs_shell_command : $(fsShellCommandSources) ; +} diff --git a/src/tests/add-ons/kernel/file_systems/fs_shell/fs_shell_command_beos.cpp b/src/tests/add-ons/kernel/file_systems/fs_shell/fs_shell_command_beos.cpp new file mode 100644 index 0000000000..4a5f6d0f8f --- /dev/null +++ b/src/tests/add-ons/kernel/file_systems/fs_shell/fs_shell_command_beos.cpp @@ -0,0 +1,111 @@ +/* + * Copyright 2005, Ingo Weinhold, bonefish@users.sf.net. + * Distributed under the terms of the MIT License. + */ + +#include +#include + +#include + +#include "fs_shell_command_beos.h" + +static void +add_char(char *&buffer, int &bufferSize, char c) +{ + if (bufferSize <= 0) { + fprintf(stderr, "Command line too long\n"); + exit(1); + } + + *buffer = c; + buffer++; + bufferSize--; +} + + +static void +prepare_command_string(const char *const *argv, int argc, char *buffer, + int bufferSize) +{ + for (int argi = 0; argi < argc; argi++) { + const char *arg = argv[argi]; + + if (argi > 0) + add_char(buffer, bufferSize, ' '); + + while (*arg) { + if (strchr("", *arg)) + add_char(buffer, bufferSize, '\\'); + add_char(buffer, bufferSize, *arg); + arg++; + } + } + + add_char(buffer, bufferSize, '\0'); +} + + +int +main(int argc, const char *const *argv) +{ + if (argc < 2) { + fprintf(stderr, "No command given.\n"); + exit(1); + } + + // prepare the command string + external_command_message message; + prepare_command_string(argv + 1, argc - 1, message.command, + sizeof(message.command)); + + // find the command port + port_id commandPort = find_port(kFSShellCommandPort); + if (commandPort < 0) { + fprintf(stderr, "Couldn't find fs_shell command port.\n"); + exit(1); + } + + // create a reply port + port_id replyPort = create_port(1, "fs shell reply port"); + if (replyPort < 0) { + fprintf(stderr, "Failed to create a reply port: %s\n", + strerror(replyPort)); + exit(1); + } + message.reply_port = replyPort; + + // send the command message + status_t error; + do { + error = write_port(commandPort, 0, &message, sizeof(message)); + } while (error == B_INTERRUPTED); + + if (error != B_OK) { + fprintf(stderr, "Failed to send command: %s\n", strerror(error)); + exit(1); + } + + // wait for the reply + external_command_reply reply; + ssize_t bytesRead; + do { + int32 code; + bytesRead = read_port(replyPort, &code, &reply, sizeof(reply)); + } while (bytesRead == B_INTERRUPTED); + + if (bytesRead < 0) { + fprintf(stderr, "Failed to read reply from fs_shell: %s\n", + strerror(bytesRead)); + exit(1); + } + + // evaluate result + if (reply.error != B_OK) { + fprintf(stderr, "Command failed: %lx\n", error); + exit(1); + } + + return 0; +} + diff --git a/src/tests/add-ons/kernel/file_systems/fs_shell/fs_shell_command_beos.h b/src/tests/add-ons/kernel/file_systems/fs_shell/fs_shell_command_beos.h new file mode 100644 index 0000000000..812ef8fccb --- /dev/null +++ b/src/tests/add-ons/kernel/file_systems/fs_shell/fs_shell_command_beos.h @@ -0,0 +1,19 @@ +/* + * Copyright 2005, Haiku Inc. All rights reserved. + * Distributed under the terms of the MIT License. + */ +#ifndef FS_SHELL_COMMAND_BEOS_H +#define FS_SHELL_COMMAND_BEOS_H + +static const char *kFSShellCommandPort = "fs shell command port"; + +struct external_command_message { + port_id reply_port; + char command[1024]; +}; + +struct external_command_reply { + int error; +}; + +#endif // FS_SHELL_COMMAND_BEOS_H