FreeNOS
CreateFile.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 <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <errno.h>
22 #include <sys/stat.h>
23 #include <Log.h>
24 #include "CreateFile.h"
25 
26 CreateFile::CreateFile(int argc, char **argv)
27  : POSIXApplication(argc, argv)
28 {
29  parser().setDescription("Create new files the filesystem");
30  parser().registerPositional("FILE", "Name of the file(s) to create", 0);
31 }
32 
34 {
35 }
36 
38 {
39  const Vector<Argument *> & positionals = arguments().getPositionals();
40  Result result = Success;
41  Result ret = Success;
42 
43  // Perform a stat for each file
44  for (Size i = 0; i < positionals.count(); i++)
45  {
46  // Create the file immediately
47  result = createFile(positionals[i]->getValue());
48 
49  // Update exit status, if needed
50  if (result != Success)
51  {
52  ret = result;
53  }
54  }
55 
56  // Done
57  return ret;
58 }
59 
61 {
62  // Attempt to create the file. */
63  if (creat(*file, S_IRUSR|S_IWUSR) < 0)
64  {
65  ERROR("failed to create file `" << *file << "': " << strerror(errno));
66  return IOError;
67  }
68 
69  // Done
70  return Success;
71 }
S_IRUSR
#define S_IRUSR
Read permission, owner.
Definition: stat.h:100
errno
C int errno
The lvalue errno is used by many functions to return error values.
string.h
CreateFile::exec
virtual Result exec()
Execute the application.
Definition: CreateFile.cpp:37
String
Abstraction of strings.
Definition: String.h:41
CreateFile::createFile
Result createFile(const String &file) const
Create a new empty file.
Definition: CreateFile.cpp:60
S_IWUSR
#define S_IWUSR
Write permission, owner.
Definition: stat.h:103
POSIXApplication
POSIX-compatible application.
Definition: POSIXApplication.h:35
Application::Success
@ Success
Definition: Application.h:55
Application::arguments
const ArgumentContainer & arguments() const
Get program arguments.
Definition: Application.cpp:112
ArgumentParser::setDescription
void setDescription(const String &desc)
Set program description.
Definition: ArgumentParser.cpp:95
CreateFile.h
Log.h
Application::IOError
@ IOError
Definition: Application.h:57
Vector::count
virtual Size count() const
Returns the number of items inside the Vector.
Definition: Vector.h:204
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
Application::Result
Result
Result codes.
Definition: Application.h:53
creat
int creat(const char *path, mode_t mode)
Create a new file or rewrite an existing one.
Definition: creat.cpp:25
ArgumentParser::registerPositional
Result registerPositional(const char *name, const char *description, Size count=1)
Register a positional argument.
Definition: ArgumentParser.cpp:119
stat.h
CreateFile::CreateFile
CreateFile(int argc, char **argv)
Constructor.
Definition: CreateFile.cpp:26
Application::parser
ArgumentParser & parser()
Get program arguments parser.
Definition: Application.cpp:102
ERROR
#define ERROR(msg)
Output an error message.
Definition: Log.h:61
stdlib.h
CreateFile::~CreateFile
virtual ~CreateFile()
Destructor.
Definition: CreateFile.cpp:33
Vector< Argument * >
errno.h
ArgumentContainer::getPositionals
const Vector< Argument * > & getPositionals() const
Get positional arguments.
Definition: ArgumentContainer.cpp:39