FreeNOS
MakeNode.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 <errno.h>
21#include <string.h>
22#include <sys/stat.h>
23#include "MakeNode.h"
24
25MakeNode::MakeNode(int argc, char **argv)
26 : POSIXApplication(argc, argv)
27{
28 parser().setDescription("Create new device file node");
29 parser().registerPositional("FILE", "Name of the device file to create");
30 parser().registerPositional("TYPE", "Type of file to create (c=char,b=block)");
31 parser().registerPositional("MAJOR", "Major number for the device node");
32 parser().registerPositional("MINOR", "Minor number for the device node");
33}
34
38
40{
41 dev_t dev;
42
43 // Fill in major/minor numbers
44 dev.major = atoi(arguments().get("MAJOR"));
45 dev.minor = atoi(arguments().get("MINOR"));
46
47 // Attempt to create the file
48 if (mknod(arguments().get("FILE"), S_IFCHR, dev) < 0)
49 {
50 ERROR("failed to create '" << arguments().get("FILE") << "': " << strerror(errno));
51 return IOError;
52 }
53 return Success;
54}
Result
Result codes.
Definition Application.h:54
const ArgumentContainer & arguments() const
Get program arguments.
ArgumentParser & parser()
Get program arguments parser.
void setDescription(const String &desc)
Set program description.
Result registerPositional(const char *name, const char *description, Size count=1)
Register a positional argument.
virtual ~MakeNode()
Destructor.
Definition MakeNode.cpp:35
virtual Result exec()
Execute the application.
Definition MakeNode.cpp:39
MakeNode(int argc, char **argv)
Constructor.
Definition MakeNode.cpp:25
POSIX-compatible application.
C int atoi(const char *nptr)
Convert a string to an integer.
Definition atoi.cpp:21
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.
C int mknod(const char *path, mode_t mode, dev_t dev)
Make directory, special file, or regular file.
Definition mknod.cpp:25
#define S_IFCHR
Character special.
Definition stat.h:64
#define ERROR(msg)
Output an error message.
Definition Log.h:61
Describes a device ID number.
Definition Types.h:146
ProcessID major
Major device ID number is a PID.
Definition Types.h:148
u16 minor
Device specific minor ID number.
Definition Types.h:151