FreeNOS
WriteCommand.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2009 Niek Linnenbank
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include <fcntl.h>
19 #include <unistd.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <String.h>
25 #include <Log.h>
26 #include "WriteCommand.h"
27 
29  : ShellCommand("write", 2)
30 {
31  m_help = "Write data to a file";
32 }
33 
34 int WriteCommand::execute(const Size nparams, const char **params)
35 {
36  int fd;
37  String buf;
38 
39  // Attempt to open the file first
40  if ((fd = open(params[0], O_WRONLY)) < 0)
41  {
42  ERROR("failed to open `" << params[0] << "': " << strerror(errno));
43  return EXIT_FAILURE;
44  }
45  // Fill write buffer
46  for (Size i = 1; i < nparams; i++)
47  {
48  buf << params[i];
49  buf << " ";
50  }
51  // Append newline
52  buf << "\n";
53 
54  // Write buffer to the file
55  if (write(fd, *buf, buf.length()) < 0)
56  {
57  ERROR("failed to write `" << params[0] << "': " << strerror(errno));
58  return EXIT_FAILURE;
59  }
60 
61  // Close the file
62  close(fd);
63  return 0;
64 }
EXIT_FAILURE
#define EXIT_FAILURE
Unsuccessful termination.
Definition: stdlib.h:36
fcntl.h
write
ssize_t write(int fildes, const void *buf, size_t nbyte)
Write on a file.
Definition: write.cpp:22
errno
C int errno
The lvalue errno is used by many functions to return error values.
String::length
Size length() const
Same as count().
Definition: String.cpp:105
string.h
ShellCommand
Builtin command for the Shell.
Definition: ShellCommand.h:38
String
Abstraction of strings.
Definition: String.h:41
ShellCommand::m_help
const char * m_help
Command help text.
Definition: ShellCommand.h:91
open
int open(const char *path, int oflag,...)
Open file relative to directory file descriptor.
Definition: open.cpp:26
Log.h
O_WRONLY
#define O_WRONLY
Open for writing only.
Definition: fcntl.h:93
WriteCommand.h
close
int close(int fildes)
Close a file descriptor.
Definition: close.cpp:22
stdio.h
strerror
char * strerror(int errnum)
The strerror function maps the number in errnum to a message string.
Definition: strerror.cpp:20
Size
unsigned int Size
Any sane size indicator cannot go negative.
Definition: Types.h:128
WriteCommand::execute
virtual int execute(const Size nparams, const char **params)
Executes the command.
Definition: WriteCommand.cpp:34
unistd.h
ERROR
#define ERROR(msg)
Output an error message.
Definition: Log.h:61
WriteCommand::WriteCommand
WriteCommand()
Constructor function.
Definition: WriteCommand.cpp:28
String.h
stdlib.h
errno.h