vfs: check userland buffers before reading them.
[haiku.git] / src / tools / fs_shell / fs_shell_command.cpp
blobe7c27d527c043a63858c9d94752ae84bc4d9ca43
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.h"
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
13 static void
14 add_char(char *&buffer, int &bufferSize, char c)
16 if (bufferSize <= 0) {
17 fprintf(stderr, "Error: Command line too long\n");
18 exit(1);
21 *buffer = c;
22 buffer++;
23 bufferSize--;
27 static void
28 prepare_command_string(const char *const *argv, int argc, char *buffer,
29 int bufferSize)
31 for (int argi = 0; argi < argc; argi++) {
32 const char *arg = argv[argi];
34 if (argi > 0)
35 add_char(buffer, bufferSize, ' ');
37 while (*arg) {
38 if (strchr(" \"'\\", *arg))
39 add_char(buffer, bufferSize, '\\');
40 add_char(buffer, bufferSize, *arg);
41 arg++;
45 add_char(buffer, bufferSize, '\0');
49 int
50 main(int argc, const char *const *argv)
52 if (argc < 2) {
53 fprintf(stderr, "Error: No command given.\n");
54 exit(1);
57 if (strcmp(argv[1], "--uses-fifos") == 0)
58 exit(gUsesFifos ? 0 : 1);
60 // prepare the command string
61 char command[102400];
62 prepare_command_string(argv + 1, argc - 1, command, sizeof(command));
64 // send the command
65 int result;
66 if (!send_external_command(command, &result))
67 exit(1);
69 // evaluate result
70 if (result != 0) {
71 fprintf(stderr, "Error: Command failed: %s\n", strerror(result));
72 fprintf(stderr, "Error: Command was:\n %s\n", command);
73 exit(1);
76 return 0;