/
solidbase
/
C2_SimpleBashUtils
Обзор
Документация
Войти
/
solidbase
/
C2_SimpleBashUtils
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
develop
src/grep/s21_grep.c
59 строк
1 KB
Dmitrii Isaev
cat/grep: refactor cat and grep to unified logic
30 ноя 2025, 14:21
30 ноя 2025, 14:21
a6b55b2
Код
Авторство
О чём код?
#include <stdbool.h> #include "options.h" #include "process.h" enum { GREP_EXIT_MATCH_FOUND = 0, GREP_EXIT_NO_MATCHES = 1, GREP_EXIT_ERROR = 2 }; int main(int argc, char* argv[]) { Options opts; init_options(&opts); int first_file_index = 0; if (parse_options(argc, argv, &opts, &first_file_index) != 0) { free_options(&opts); return GREP_EXIT_ERROR; } int filec = argc - first_file_index; char* const* filev = &argv[first_file_index]; bool any_match = false; bool had_error = false; bool show_filename = (filec > 1) && !opts.flag_h; for (int i = 0; i < filec; ++i) { const char* path = filev[i]; if (!path) { had_error = true; continue; } bool file_matched = false; bool ok = grep_file(path, &opts, show_filename, &file_matched); if (file_matched) { any_match = true; } if (!ok) { had_error = true; } } free_options(&opts); if (had_error) { return GREP_EXIT_ERROR; } if (any_match) { return GREP_EXIT_MATCH_FOUND; } return GREP_EXIT_NO_MATCHES; }