2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2021-05-31 17:43:25 +03:00
|
|
|
#include <AK/Format.h>
|
2022-01-14 19:23:48 +03:00
|
|
|
#include <LibCore/System.h>
|
|
|
|
#include <LibMain/Main.h>
|
2019-05-03 23:59:58 +03:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/stat.h>
|
2021-11-26 18:55:45 +03:00
|
|
|
#include <sys/sysmacros.h>
|
2019-05-03 23:59:58 +03:00
|
|
|
|
|
|
|
static int usage()
|
|
|
|
{
|
2021-05-31 17:43:25 +03:00
|
|
|
warnln("usage: mknod <name> <c|b|p> [<major> <minor>]");
|
2021-06-01 21:27:59 +03:00
|
|
|
return 1;
|
2019-05-03 23:59:58 +03:00
|
|
|
}
|
|
|
|
|
2022-01-14 19:23:48 +03:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2019-05-03 23:59:58 +03:00
|
|
|
{
|
2022-01-14 19:23:48 +03:00
|
|
|
TRY(Core::System::pledge("stdio dpath"));
|
2020-01-27 23:38:36 +03:00
|
|
|
|
2019-05-03 23:59:58 +03:00
|
|
|
// FIXME: Add some kind of option for specifying the file permissions.
|
2022-01-14 19:23:48 +03:00
|
|
|
if (arguments.strings.size() < 3)
|
2019-05-03 23:59:58 +03:00
|
|
|
return usage();
|
|
|
|
|
2022-01-14 19:23:48 +03:00
|
|
|
if (arguments.strings[2].starts_with('p')) {
|
|
|
|
if (arguments.strings.size() != 3)
|
2020-07-17 00:34:43 +03:00
|
|
|
return usage();
|
2022-01-14 19:23:48 +03:00
|
|
|
} else if (arguments.strings.size() != 5) {
|
2020-07-17 00:34:43 +03:00
|
|
|
return usage();
|
|
|
|
}
|
|
|
|
|
2022-01-14 19:23:48 +03:00
|
|
|
auto name = arguments.strings[1];
|
2019-05-03 23:59:58 +03:00
|
|
|
mode_t mode = 0666;
|
2022-01-14 19:23:48 +03:00
|
|
|
switch (arguments.strings[2][0]) {
|
2019-05-03 23:59:58 +03:00
|
|
|
case 'c':
|
|
|
|
case 'u':
|
|
|
|
mode |= S_IFCHR;
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
mode |= S_IFBLK;
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
mode |= S_IFIFO;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return usage();
|
|
|
|
}
|
|
|
|
|
2020-07-17 00:34:43 +03:00
|
|
|
int major = 0;
|
|
|
|
int minor = 0;
|
2022-01-14 19:23:48 +03:00
|
|
|
if (arguments.strings.size() == 5) {
|
|
|
|
major = atoi(arguments.argv[3]);
|
|
|
|
minor = atoi(arguments.argv[4]);
|
2020-07-17 00:34:43 +03:00
|
|
|
}
|
2019-05-03 23:59:58 +03:00
|
|
|
|
2022-01-14 19:23:48 +03:00
|
|
|
TRY(Core::System::mknod(name, mode, makedev(major, minor)));
|
|
|
|
|
2019-05-03 23:59:58 +03:00
|
|
|
return 0;
|
|
|
|
}
|