Serenity Operating System
at portability 125 lines 4.1 kB view raw
1/* 2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this 9 * list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#include <AK/String.h> 28#include <AK/StringBuilder.h> 29#include <AK/Vector.h> 30#include <LibCore/ArgsParser.h> 31#include <LibCore/DirIterator.h> 32#include <LibCore/File.h> 33#include <dirent.h> 34#include <errno.h> 35#include <fcntl.h> 36#include <stdio.h> 37#include <string.h> 38#include <unistd.h> 39 40static String read_var(const String& name) 41{ 42 StringBuilder builder; 43 builder.append("/proc/sys/"); 44 builder.append(name); 45 auto path = builder.to_string(); 46 auto f = Core::File::construct(path); 47 if (!f->open(Core::IODevice::ReadOnly)) { 48 fprintf(stderr, "open: %s", f->error_string()); 49 exit(1); 50 } 51 const auto& b = f->read_all(); 52 if (f->error() < 0) { 53 fprintf(stderr, "read: %s", f->error_string()); 54 exit(1); 55 } 56 return String((const char*)b.data(), b.size(), Chomp); 57} 58 59static void write_var(const String& name, const String& value) 60{ 61 StringBuilder builder; 62 builder.append("/proc/sys/"); 63 builder.append(name); 64 auto path = builder.to_string(); 65 auto f = Core::File::construct(path); 66 if (!f->open(Core::IODevice::WriteOnly)) { 67 fprintf(stderr, "open: %s", f->error_string()); 68 exit(1); 69 } 70 f->write(value); 71 if (f->error() < 0) { 72 fprintf(stderr, "write: %s", f->error_string()); 73 exit(1); 74 } 75} 76 77static int handle_show_all() 78{ 79 Core::DirIterator di("/proc/sys", Core::DirIterator::SkipDots); 80 if (di.has_error()) { 81 fprintf(stderr, "CDirIterator: %s\n", di.error_string()); 82 return 1; 83 } 84 85 while (di.has_next()) { 86 String variable_name = di.next_path(); 87 printf("%s = %s\n", variable_name.characters(), read_var(variable_name).characters()); 88 } 89 return 0; 90} 91 92static int handle_var(const String& var) 93{ 94 String spec(var.characters(), Chomp); 95 auto parts = spec.split('='); 96 String variable_name = parts[0]; 97 bool is_write = parts.size() > 1; 98 99 if (!is_write) { 100 printf("%s = %s\n", variable_name.characters(), read_var(variable_name).characters()); 101 return 0; 102 } 103 104 printf("%s = %s", variable_name.characters(), read_var(variable_name).characters()); 105 write_var(variable_name, parts[1]); 106 printf(" -> %s\n", read_var(variable_name).characters()); 107 return 0; 108} 109 110int main(int argc, char** argv) 111{ 112 bool show_all = false; 113 const char* var = nullptr; 114 115 Core::ArgsParser args_parser; 116 args_parser.add_option(show_all, "Show all variables", nullptr, 'a'); 117 args_parser.add_positional_argument(var, "Command (var[=value])", "command"); 118 args_parser.parse(argc, argv); 119 120 if (show_all) { 121 return handle_show_all(); 122 } 123 124 return handle_var(var); 125}