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
27Remove::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
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}
Result
Result codes.
Definition Application.h:54
const ArgumentContainer & arguments() const
Get program arguments.
ArgumentParser & parser()
Get program arguments parser.
const Vector< Argument * > & getPositionals() const
Get positional arguments.
void setDescription(const String &desc)
Set program description.
Result registerPositional(const char *name, const char *description, Size count=1)
Register a positional argument.
POSIX-compatible application.
virtual ~Remove()
Destructor.
Definition Remove.cpp:34
virtual Result exec()
Execute the application.
Definition Remove.cpp:38
Remove(int argc, char **argv)
Constructor.
Definition Remove.cpp:27
Vectors are dynamically resizeable Arrays.
Definition Vector.h:42
virtual Size count() const
Returns the number of items inside the Vector.
Definition Vector.h:204
C int unlink(const char *path)
Remove a file from the filesystem.
Definition unlink.cpp:24
C char * strerror(int errnum)
The strerror function maps the number in errnum to a message string.
Definition strerror.cpp:20
C int errno
The lvalue errno is used by many functions to return error values.
#define ERROR(msg)
Output an error message.
Definition Log.h:61
unsigned int Size
Any sane size indicator cannot go negative.
Definition Types.h:128