mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-06-10 18:10:56 +09:00
Userland: Port sysctl to use ArgsParser, and minor cleanups
This commit is contained in:
parent
6dd7ee53ea
commit
56aad835ad
Notes:
sideshowbarker
2024-07-19 14:04:19 +09:00
Author: https://github.com/rburchell
Commit: 56aad835ad
Pull-request: https://github.com/SerenityOS/serenity/pull/50
1 changed files with 59 additions and 73 deletions
|
@ -3,82 +3,12 @@
|
|||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <getopt.h>
|
||||
#include <fcntl.h>
|
||||
#include <AK/ArgsParser.h>
|
||||
#include <AK/AKString.h>
|
||||
#include <AK/StringBuilder.h>
|
||||
#include <AK/Vector.h>
|
||||
|
||||
static bool flag_show_all = false;
|
||||
static int show_all();
|
||||
static int handle_var(const char*);
|
||||
|
||||
static void usage()
|
||||
{
|
||||
printf("usage: sysctl [-a] [variable[=value]]\n");
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
int opt;
|
||||
while ((opt = getopt(argc, argv, "a")) != -1) {
|
||||
switch (opt) {
|
||||
case 'a':
|
||||
flag_show_all = true;
|
||||
break;
|
||||
default:
|
||||
usage();
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (flag_show_all)
|
||||
return show_all();
|
||||
|
||||
if (optind >= argc) {
|
||||
usage();
|
||||
return 0;
|
||||
}
|
||||
|
||||
const char* var = argv[optind];
|
||||
|
||||
return handle_var(var);
|
||||
}
|
||||
|
||||
int show_all()
|
||||
{
|
||||
DIR* dirp = opendir("/proc/sys");
|
||||
if (!dirp) {
|
||||
perror("opendir");
|
||||
return 1;
|
||||
}
|
||||
char pathbuf[PATH_MAX];
|
||||
|
||||
while (auto* de = readdir(dirp)) {
|
||||
if (de->d_name[0] == '.')
|
||||
continue;
|
||||
sprintf(pathbuf, "/proc/sys/%s", de->d_name);
|
||||
int fd = open(pathbuf, O_RDONLY);
|
||||
if (fd < 0) {
|
||||
perror("open");
|
||||
continue;
|
||||
}
|
||||
char buffer[1024];
|
||||
int nread = read(fd, buffer, sizeof(buffer));
|
||||
close(fd);
|
||||
if (nread < 0) {
|
||||
perror("read");
|
||||
continue;
|
||||
}
|
||||
buffer[nread] = '\0';
|
||||
printf("%s = %s", de->d_name, buffer);
|
||||
if (nread && buffer[nread - 1] != '\n')
|
||||
printf("\n");
|
||||
}
|
||||
closedir(dirp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static String read_var(const String& name)
|
||||
{
|
||||
StringBuilder builder;
|
||||
|
@ -119,9 +49,44 @@ static void write_var(const String& name, const String& value)
|
|||
close(fd);
|
||||
}
|
||||
|
||||
int handle_var(const char* var)
|
||||
|
||||
static int handle_show_all()
|
||||
{
|
||||
String spec(var, Chomp);
|
||||
DIR* dirp = opendir("/proc/sys");
|
||||
if (!dirp) {
|
||||
perror("opendir");
|
||||
return 1;
|
||||
}
|
||||
char pathbuf[PATH_MAX];
|
||||
|
||||
while (auto* de = readdir(dirp)) {
|
||||
if (de->d_name[0] == '.')
|
||||
continue;
|
||||
sprintf(pathbuf, "/proc/sys/%s", de->d_name);
|
||||
int fd = open(pathbuf, O_RDONLY);
|
||||
if (fd < 0) {
|
||||
perror("open");
|
||||
continue;
|
||||
}
|
||||
char buffer[1024];
|
||||
int nread = read(fd, buffer, sizeof(buffer));
|
||||
close(fd);
|
||||
if (nread < 0) {
|
||||
perror("read");
|
||||
continue;
|
||||
}
|
||||
buffer[nread] = '\0';
|
||||
printf("%s = %s", de->d_name, buffer);
|
||||
if (nread && buffer[nread - 1] != '\n')
|
||||
printf("\n");
|
||||
}
|
||||
closedir(dirp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int handle_var(const String& var)
|
||||
{
|
||||
String spec(var.characters(), Chomp);
|
||||
auto parts = spec.split('=');
|
||||
String variable_name = parts[0];
|
||||
bool is_write = parts.size() > 1;
|
||||
|
@ -136,3 +101,24 @@ int handle_var(const char* var)
|
|||
printf(" -> %s\n", read_var(variable_name).characters());
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
AK::ArgsParser args_parser("sysctl");
|
||||
|
||||
args_parser.add_arg("a", "show all variables");
|
||||
args_parser.set_single_value("variable=[value]");
|
||||
|
||||
AK::ArgsParserResult args = args_parser.parse(argc, (const char**)argv);
|
||||
|
||||
if (args.is_present("a")) {
|
||||
return handle_show_all();
|
||||
} else if (args.get_single_values().size() != 1) {
|
||||
args_parser.print_usage();
|
||||
return 0;
|
||||
}
|
||||
|
||||
Vector<String> values = args.get_single_values();
|
||||
return handle_var(values[0]);
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue