FreeNOS
Remove.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 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 <unistd.h>
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <sys/stat.h>
25 #include "Remove.h"
26 
27 Remove::Remove(int argc, char **argv)
28  : POSIXApplication(argc, argv)
29 {
30  parser().setDescription("Remove file from the filesystem");
31  parser().registerPositional("FILE", "Name of the file(s) to remove", 0);
32 }
33 
35 {
36 }
37 
39 {
40  const Vector<Argument *> & positionals = arguments().getPositionals();
41 
42  // Delete all given files
43  for (Size i = 0; i < positionals.count(); i++)
44  {
45  const char *path = *(positionals[i]->getValue());
46 
47  // Attempt to remove the file
48  if (unlink(path) < 0)
49  {
50  ERROR("failed to unlink '" << path << "': " << strerror(errno));
51  return IOError;
52  }
53  }
54 
55  // Done
56  return Success;
57 }
Remove::~Remove
virtual ~Remove()
Destructor.
Definition: Remove.cpp:34
fcntl.h
errno
C int errno
The lvalue errno is used by many functions to return error values.
string.h
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
Remove::Remove
Remove(int argc, char **argv)
Constructor.
Definition: Remove.cpp:27
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
unlink
int unlink(const char *path)
Remove a file from the filesystem.
Definition: unlink.cpp:24
Application::Result
Result
Result codes.
Definition: Application.h:53
ArgumentParser::registerPositional
Result registerPositional(const char *name, const char *description, Size count=1)
Register a positional argument.
Definition: ArgumentParser.cpp:119
stat.h
unistd.h
Application::parser
ArgumentParser & parser()
Get program arguments parser.
Definition: Application.cpp:102
ERROR
#define ERROR(msg)
Output an error message.
Definition: Log.h:61
Remove.h
Remove::exec
virtual Result exec()
Execute the application.
Definition: Remove.cpp:38
stdlib.h
Vector< Argument * >
errno.h
ArgumentContainer::getPositionals
const Vector< Argument * > & getPositionals() const
Get positional arguments.
Definition: ArgumentContainer.cpp:39