Fix FreeBSD build.
[haiku.git] / src / tools / fs_shell / fs_shell_command_beos.cpp
blob5247d9938585b66fa8536d1aeb8efe5de100a50f
1 /*
2 * Copyright 2005-2007, Ingo Weinhold, bonefish@cs.tu-berlin.de.
3 * Distributed under the terms of the MIT License.
4 */
6 #include "fs_shell_command_beos.h"
8 #include <stdio.h>
9 #include <string.h>
11 #include <OS.h>
13 #include "fs_shell_command.h"
16 bool gUsesFifos = false;
19 bool
20 send_external_command(const char *command, int *result)
22 int commandLen = strlen(command);
23 if (commandLen > kMaxCommandLength) {
24 fprintf(stderr, "Error: Command line too long.\n");
25 return false;
28 char _message[sizeof(external_command_message) + kMaxCommandLength];
29 external_command_message* message = (external_command_message*)_message;
30 strcpy(message->command, command);
32 // find the command port
33 port_id commandPort = find_port(kFSShellCommandPort);
34 if (commandPort < 0) {
35 fprintf(stderr, "Error: Couldn't find fs_shell command port.\n");
36 return false;
39 // create a reply port
40 port_id replyPort = create_port(1, "fs shell reply port");
41 if (replyPort < 0) {
42 fprintf(stderr, "Error: Failed to create a reply port: %s\n",
43 strerror(replyPort));
44 return false;
46 message->reply_port = replyPort;
48 // send the command message
49 status_t error;
50 do {
51 error = write_port(commandPort, 0, message,
52 sizeof(external_command_message) + commandLen);
53 } while (error == B_INTERRUPTED);
55 if (error != B_OK) {
56 fprintf(stderr, "Error: Failed to send command: %s\n", strerror(error));
57 return false;
60 // wait for the reply
61 external_command_reply reply;
62 ssize_t bytesRead;
63 do {
64 int32 code;
65 bytesRead = read_port(replyPort, &code, &reply, sizeof(reply));
66 } while (bytesRead == B_INTERRUPTED);
68 if (bytesRead < 0) {
69 fprintf(stderr, "Error: Failed to read reply from fs_shell: %s\n",
70 strerror(bytesRead));
71 return false;
74 *result = reply.error;
75 return true;