Serenity Operating System
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/JsonArray.h>
29#include <AK/JsonObject.h>
30#include <AK/Vector.h>
31#include <LibCore/File.h>
32#include <fcntl.h>
33#include <stdio.h>
34#include <stdlib.h>
35#include <unistd.h>
36
37struct FileSystem {
38 String fs;
39 size_t total_block_count { 0 };
40 size_t free_block_count { 0 };
41 size_t total_inode_count { 0 };
42 size_t free_inode_count { 0 };
43 String mount_point;
44};
45
46int main(int, char**)
47{
48 auto file = Core::File::construct("/proc/df");
49 if (!file->open(Core::IODevice::ReadOnly)) {
50 fprintf(stderr, "Failed to open /proc/df: %s\n", file->error_string());
51 return 1;
52 }
53 printf("Filesystem Blocks Used Available Mount point\n");
54
55 auto file_contents = file->read_all();
56 auto json = JsonValue::from_string(file_contents).as_array();
57 json.for_each([](auto& value) {
58 auto fs_object = value.as_object();
59 auto fs = fs_object.get("class_name").to_string();
60 auto total_block_count = fs_object.get("total_block_count").to_u32();
61 auto free_block_count = fs_object.get("free_block_count").to_u32();
62 auto total_inode_count = fs_object.get("total_inode_count").to_u32();
63 auto free_inode_count = fs_object.get("free_inode_count").to_u32();
64 auto mount_point = fs_object.get("mount_point").to_string();
65
66 (void)total_inode_count;
67 (void)free_inode_count;
68
69 printf("%-10s", fs.characters());
70 printf("%10u ", total_block_count);
71 printf("%10u ", total_block_count - free_block_count);
72 printf("%10u ", free_block_count);
73 printf("%s", mount_point.characters());
74 printf("\n");
75 });
76
77 return 0;
78}