ladybird/Userland/Utilities/fgrep.cpp

30 lines
736 B
C++
Raw Normal View History

/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
2021-12-21 22:22:53 +03:00
#include "AK/String.h"
2021-05-19 22:35:55 +03:00
#include <AK/Assertions.h>
#include <AK/Format.h>
2021-12-21 22:22:53 +03:00
#include <LibCore/System.h>
#include <LibMain/Main.h>
#include <stdio.h>
2021-12-21 22:22:53 +03:00
ErrorOr<int> serenity_main(Main::Arguments arguments)
{
2021-12-21 22:22:53 +03:00
if (arguments.strings.size() < 2) {
warnln("usage: fgrep <str>");
return 1;
}
for (;;) {
2021-12-21 22:22:53 +03:00
char buffer[4096];
auto str = StringView(fgets(buffer, sizeof(buffer), stdin));
if (str.contains(arguments.strings[1]))
TRY(Core::System::write(1, str.bytes()));
if (feof(stdin))
return 0;
2021-12-21 22:22:53 +03:00
VERIFY(str.to_string().characters());
}
}