HaikuDepot: notify work status from main window
[haiku.git] / src / tools / btrfs_shell / command_cat.cpp
blobe1417a87dcba6ddc7c037b153ae9ac0886f0c6fa
1 #include <stdio.h>
2 #include <stdlib.h>
4 #include "syscalls.h"
5 #include "system_dependencies.h"
8 namespace FSShell {
11 fssh_status_t
12 command_cat(int argc, const char* const* argv)
14 long numBytes = 10;
15 int fileStart = 1;
16 if (argc < 2 || strcmp(argv[1], "--help") == 0) {
17 printf(
18 "Usage: %s [ -n ] [FILE]...\n"
19 "\t -n\tNumber of bytes to read\n",
20 argv[0]
22 return FSSH_B_OK;
25 if (argc > 3 && strcmp(argv[1], "-n") == 0) {
26 fileStart += 2;
27 numBytes = strtol(argv[2], NULL, 10);
30 const char* const* files = argv + fileStart;
31 for (; *files; files++) {
32 const char* file = *files;
33 int fd = _kern_open(-1, file, O_RDONLY, O_RDONLY);
34 if (fd < 0) {
35 fssh_dprintf("Error: %s\n", fssh_strerror(fd));
36 return FSSH_B_BAD_VALUE;
39 char buffer[numBytes + 1];
40 if (buffer == NULL) {
41 fssh_dprintf("Error: No memory\n");
42 _kern_close(fd);
43 return FSSH_B_NO_MEMORY;
46 if (_kern_read(fd, 0, buffer, numBytes) != numBytes) {
47 fssh_dprintf("Error: fail to read, length: %i\n", numBytes);
48 _kern_close(fd);
49 return FSSH_B_BAD_VALUE;
52 _kern_close(fd);
53 buffer[numBytes] = '\0';
54 printf("%s\n", buffer);
57 return FSSH_B_OK;
61 } // namespace FSShell