/* The MIT License (MIT) Copyright © 2020-2025 pacman64 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /* dedup [files...] DEDUPlicate lines, emitting each distinct line from standard-input only once. */ // g++ -Wall -O2 -s -o dedup dedup.cpp #include #include #include #include using namespace std; void handle_input(istream& in, string& line, set& seen) { while (getline(in, line)) { if (seen.find(line) != seen.end()) { continue; } // cout.write(line.c_str(), line.size()); // cout.write("\n", 1); // cout.flush(); cout << line << endl; if (cout.eof()) { break; } seen.insert(line); } } bool handle_file(const char* path, string& line, set& seen) { ifstream f(path); if (!f.is_open()) { const auto msg = "can't open file named"; cerr << "\x1b[31m" << msg << " '" << path << "'\x1b[0m" << endl; return false; } handle_input(f, line, seen); return true; } int main(int argc, char** argv) { string line; set seen; cin.tie(NULL); ios_base::sync_with_stdio(false); size_t errors = 0; for (int i = 1; i < argc && !cout.eof(); i++) { if (argv[i][0] == '-' && argv[i][1] == 0) { handle_input(cin, line, seen); continue; } if (!handle_file(argv[i], line, seen)) { errors++; } } if (argc < 2) { handle_input(cin, line, seen); } return errors; }