summaryrefslogtreecommitdiff
path: root/Userland/Utilities/fgrep.cpp
blob: 8f383a940389768a40305674b4b92357a3057546 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/*
 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

int main(int argc, char** argv)
{
    if (argc < 2) {
        printf("usage: fgrep <str>\n");
        return 0;
    }
    for (;;) {
        char buf[4096];
        auto* str = fgets(buf, sizeof(buf), stdin);
        if (str && strstr(str, argv[1]))
            write(1, buf, strlen(buf));
        if (feof(stdin))
            return 0;
        VERIFY(str);
    }
    return 0;
}