fgrep.cpp 591 B

12345678910111213141516171819202122232425262728
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Assertions.h>
  7. #include <AK/Format.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <unistd.h>
  11. int main(int argc, char** argv)
  12. {
  13. if (argc < 2) {
  14. warnln("usage: fgrep <str>");
  15. return 1;
  16. }
  17. for (;;) {
  18. char buf[4096];
  19. auto* str = fgets(buf, sizeof(buf), stdin);
  20. if (str && strstr(str, argv[1]))
  21. write(1, buf, strlen(buf));
  22. if (feof(stdin))
  23. return 0;
  24. VERIFY(str);
  25. }
  26. }