mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 13:17:44 +00:00
mknod: Use ArgsParser for argument parsing
This commit is contained in:
parent
3bc62b7c7b
commit
704d289851
1 changed files with 34 additions and 21 deletions
|
@ -5,36 +5,36 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <AK/Format.h>
|
#include <AK/Format.h>
|
||||||
|
#include <LibCore/ArgsParser.h>
|
||||||
#include <LibCore/System.h>
|
#include <LibCore/System.h>
|
||||||
#include <LibMain/Main.h>
|
#include <LibMain/Main.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <sys/sysmacros.h>
|
#include <sys/sysmacros.h>
|
||||||
|
|
||||||
static int usage()
|
|
||||||
{
|
|
||||||
warnln("usage: mknod <name> <c|b|p> [<major> <minor>]");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
{
|
{
|
||||||
TRY(Core::System::pledge("stdio dpath"));
|
TRY(Core::System::pledge("stdio dpath"));
|
||||||
|
|
||||||
|
StringView name;
|
||||||
|
StringView type_string;
|
||||||
|
StringView major_string;
|
||||||
|
StringView minor_string;
|
||||||
|
|
||||||
|
Core::ArgsParser args_parser;
|
||||||
|
args_parser.set_general_help("Creates a file system node.");
|
||||||
|
args_parser.add_positional_argument(name, "Pathname to create", "name", Core::ArgsParser::Required::Yes);
|
||||||
|
args_parser.add_positional_argument(type_string, "Type of file to create", "type", Core::ArgsParser::Required::Yes);
|
||||||
|
args_parser.add_positional_argument(major_string, "Major device number", "major", Core::ArgsParser::Required::No);
|
||||||
|
args_parser.add_positional_argument(minor_string, "Minor device number", "minor", Core::ArgsParser::Required::No);
|
||||||
|
args_parser.parse(arguments);
|
||||||
|
|
||||||
// FIXME: Add some kind of option for specifying the file permissions.
|
// FIXME: Add some kind of option for specifying the file permissions.
|
||||||
if (arguments.strings.size() < 3)
|
|
||||||
return usage();
|
|
||||||
|
|
||||||
if (arguments.strings[2].starts_with('p')) {
|
|
||||||
if (arguments.strings.size() != 3)
|
|
||||||
return usage();
|
|
||||||
} else if (arguments.strings.size() != 5) {
|
|
||||||
return usage();
|
|
||||||
}
|
|
||||||
|
|
||||||
auto name = arguments.strings[1];
|
|
||||||
mode_t mode = 0666;
|
mode_t mode = 0666;
|
||||||
switch (arguments.strings[2][0]) {
|
char type = type_string[0];
|
||||||
|
|
||||||
|
switch (type) {
|
||||||
case 'c':
|
case 'c':
|
||||||
case 'u':
|
case 'u':
|
||||||
mode |= S_IFCHR;
|
mode |= S_IFCHR;
|
||||||
|
@ -46,14 +46,27 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
mode |= S_IFIFO;
|
mode |= S_IFIFO;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
return usage();
|
warnln("Invalid device type {}", type);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (type == 'p') {
|
||||||
|
if (!major_string.is_empty() || !minor_string.is_empty()) {
|
||||||
|
warnln("Do not set device numbers when creating FIFO");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (major_string.is_empty() || minor_string.is_empty()) {
|
||||||
|
warnln("Major and minor device numbers are required");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int major = 0;
|
int major = 0;
|
||||||
int minor = 0;
|
int minor = 0;
|
||||||
if (arguments.strings.size() == 5) {
|
if (type != 'p') {
|
||||||
major = atoi(arguments.argv[3]);
|
major = atoi(major_string.characters_without_null_termination());
|
||||||
minor = atoi(arguments.argv[4]);
|
minor = atoi(minor_string.characters_without_null_termination());
|
||||||
}
|
}
|
||||||
|
|
||||||
TRY(Core::System::mknod(name, mode, makedev(major, minor)));
|
TRY(Core::System::mknod(name, mode, makedev(major, minor)));
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue