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 
25 MakeNode::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 
36 {
37 }
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 }
DeviceID::minor
u16 minor
Device specific minor ID number.
Definition: Types.h:151
MakeNode::MakeNode
MakeNode(int argc, char **argv)
Constructor.
Definition: MakeNode.cpp:25
errno
C int errno
The lvalue errno is used by many functions to return error values.
string.h
atoi
C int atoi(const char *nptr)
Convert a string to an integer.
Definition: atoi.cpp:21
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
Application::IOError
@ IOError
Definition: Application.h:57
MakeNode::~MakeNode
virtual ~MakeNode()
Destructor.
Definition: MakeNode.cpp:35
S_IFCHR
#define S_IFCHR
Character special.
Definition: stat.h:64
stdio.h
strerror
char * strerror(int errnum)
The strerror function maps the number in errnum to a message string.
Definition: strerror.cpp:20
Application::Result
Result
Result codes.
Definition: Application.h:53
DeviceID::major
ProcessID major
Major device ID number is a PID.
Definition: Types.h:148
ArgumentParser::registerPositional
Result registerPositional(const char *name, const char *description, Size count=1)
Register a positional argument.
Definition: ArgumentParser.cpp:119
stat.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
mknod
int mknod(const char *path, mode_t mode, dev_t)
Make directory, special file, or regular file.
Definition: mknod.cpp:25
DeviceID
Describes a device ID number.
Definition: Types.h:145
MakeNode::exec
virtual Result exec()
Execute the application.
Definition: MakeNode.cpp:39
stdlib.h
MakeNode.h
errno.h