xref: /haiku/src/tools/fs_shell/fs_shell_command_unix.cpp (revision 002f37b0cca92e4cf72857c72ac95db5a8b09615)
1 /*
2  * Copyright 2005-2010, Ingo Weinhold, ingo_weinhold@gmx.de.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include <errno.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 
12 #include "fs_shell_command.h"
13 
14 
15 bool gUsesFifos = true;
16 
17 
18 bool
19 send_external_command(const char* command, int* result)
20 {
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;
27 	}
28 
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;
35 	}
36 
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;
43 	}
44 
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;
51 	}
52 
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;
60 	}
61 
62 	return true;
63 }
64 
65