cut: Accept input from stdin if no files are given

This commit is contained in:
Matthew Olsson 2020-07-02 17:59:25 -07:00 committed by Andreas Kling
parent 4c48c9d69d
commit 6cb6e47779
Notes: sideshowbarker 2024-07-19 05:13:18 +09:00

View file

@ -164,12 +164,13 @@ static void expand_list(Vector<String>& tokens, Vector<Index>& indexes)
static void cut_file(const String& file, const Vector<Index>& byte_vector)
{
FILE* fp = nullptr;
fp = fopen(file.characters(), "r");
if (!fp) {
fprintf(stderr, "cut: Could not open file '%s'\n", file.characters());
return;
FILE* fp = stdin;
if (!file.is_null()) {
fp = fopen(file.characters(), "r");
if (!fp) {
fprintf(stderr, "cut: Could not open file '%s'\n", file.characters());
return;
}
}
char* line = nullptr;
@ -195,7 +196,9 @@ static void cut_file(const String& file, const Vector<Index>& byte_vector)
if (line)
free(line);
fclose(fp);
if (!file.is_null())
fclose(fp);
}
int main(int argc, char** argv)
@ -227,12 +230,16 @@ int main(int argc, char** argv)
}
}
if (files.is_empty() || byte_list == "")
if (byte_list == "")
print_usage_and_exit(1);
Vector<Index> byte_vector;
expand_list(tokens, byte_vector);
quick_sort(byte_vector, [](auto& a, auto& b) { return a.m_from < b.m_from; });
if (files.is_empty())
files.append(String());
/* Process each file */
for (auto& file : files)
cut_file(file, byte_vector);