Fix FreeBSD build.
[haiku.git] / src / tools / fs_shell / fs_shell_command_unix.cpp
blob779d7a87cb2118e2a26dd0c7acc1bca7645d1213
1 /*
2 * Copyright 2005-2010, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
4 */
7 #include <errno.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
12 #include "fs_shell_command.h"
15 bool gUsesFifos = true;
18 bool
19 send_external_command(const char* command, int* result)
21 // open the pipe to the FS shell
22 FILE* out = fdopen(4, "w");
23 if (out == NULL) {
24 fprintf(stderr, "Error: Failed to open command output: %s\n",
25 strerror(errno));
26 return false;
29 // open the pipe from the FS shell
30 FILE* in = fdopen(3, "r");
31 if (in == NULL) {
32 fprintf(stderr, "Error: Failed to open command reply input: %s\n",
33 strerror(errno));
34 return false;
37 // write the command
38 if (fputs(command, out) == EOF || fputc('\n', out) == EOF
39 || fflush(out) == EOF) {
40 fprintf(stderr, "Error: Failed to write command to FS shell: %s\n",
41 strerror(errno));
42 return false;
45 // read the reply
46 char buffer[16];
47 if (fgets(buffer, sizeof(buffer), in) == NULL) {
48 fprintf(stderr, "Error: Failed to get command reply: %s\n",
49 strerror(errno));
50 return false;
53 // parse the number
54 char* end;
55 *result = strtol(buffer, &end, 10);
56 if (end == buffer) {
57 fprintf(stderr, "Error: Read non-number command reply from FS shell: "
58 "\"%s\"\n", buffer);
59 return false;
62 return true;