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