A C implementation of the Unix/Linux cat command with support for standard options and functionality.
This project aims to create a fully-featured analog of the cat command found in Linux/Unix systems. The cat command is used to display file contents, concatenate files, and work with standard input/output streams.
-
Fix Current Bugs
- Fix buffer size issue in
fgets()call - Improve memory management and file handling
- Add proper error messages with file names
- Fix buffer size issue in
-
Basic Multiple File Support
- Handle multiple file arguments:
cat file1.txt file2.txt file3.txt - Concatenate files in the order specified
- Continue processing remaining files if one fails
- Handle multiple file arguments:
-
Standard Input Support
- Read from stdin when no arguments provided:
cat - Support reading from stdin with
-argument:cat file1.txt - file2.txt - Handle EOF properly from keyboard input (Ctrl+D on Unix, Ctrl+Z on Windows)
- Read from stdin when no arguments provided:
-
Line Numbering Options
-
-n, --number: Number all output lines starting from 1 -
-b, --number-nonblank: Number only non-empty lines
-
-
Visual Options
-
-e, --show-ends: Display$at the end of each line -
-t, --show-tabs: Display TAB characters as^I -
-v, --show-nonprinting: Display non-printing characters using^andM-notation
-
-
Blank Line Handling
-
-s, --squeeze-blank: Suppress repeated empty lines (show max one blank line)
-
-
Combined Options
-
-A, --show-all: Equivalent to-vET(show all non-printing chars, ends, tabs) -
-E, --show-ends: Same as-ebut as separate option -
-T, --show-tabs: Same as-tbut as separate option
-
-
Help and Version
-
--help: Display usage information and options -
--version: Display version information
-
-
Error Handling Improvements
- Detailed error messages with errno descriptions
- Handle different file types (directories, special files, etc.)
- Proper handling of binary files
- Handle files that don't exist vs. permission errors
-
Performance Optimization
- Implement efficient buffered I/O
- Handle large files without loading entire content into memory
- Optimize for different file sizes
-
Cross-Platform Support
- Windows line ending support (
\r\n) - Unix line ending support (
\n) - Handle different character encodings properly
- Windows line ending support (
-
Testing Suite
- Unit tests for core functions
- Integration tests with various file types
- Edge case testing (empty files, very large files, binary files)
- Cross-platform testing
-
Documentation
- Man page creation
- Usage examples
- Performance benchmarks vs. system cat
- C11 standard library
- CMake 3.30+
- Standard POSIX functions (for Unix/Linux compatibility)
cat-command/
├── src/
│ ├── main.c # Entry point and argument parsing
│ ├── cat_core.c # Core cat functionality
│ ├── cat_core.h # Core function declarations
│ ├── options.c # Command-line option parsing
│ ├── options.h # Option structure definitions
│ └── utils.c # Utility functions
├── tests/ # Test files and test suite
├── docs/ # Documentation
├── CMakeLists.txt
├── README.md
└── LICENSE
// Core functionality
int cat_file(const char* filename, const struct cat_options* opts);
int cat_stdin(const struct cat_options* opts);
int process_files(char* files[], int file_count, const struct cat_options* opts);
// Option parsing
struct cat_options parse_arguments(int argc, char* argv[]);
void print_help(void);
void print_version(void);
// Utility functions
void print_line_with_options(const char* line, int line_number, const struct cat_options* opts);
char* format_nonprinting_chars(const char* input);struct cat_options {
bool number_lines; // -n, --number
bool number_nonblank; // -b, --number-nonblank
bool show_ends; // -e, --show-ends
bool show_tabs; // -t, --show-tabs
bool show_nonprinting; // -v, --show-nonprinting
bool squeeze_blank; // -s, --squeeze-blank
bool show_all; // -A, --show-all
};# Create build directory
mkdir build && cd build
# Configure with CMake
cmake ..
# Build
make
# Run
./cat_command [options] [files...]# Display single file
./cat_command file.txt
# Display multiple files
./cat_command file1.txt file2.txt
# Read from stdin
./cat_command
# Number all lines
./cat_command -n file.txt
# Show line endings and tabs
./cat_command -et file.txt
# Squeeze blank lines and number non-empty lines
./cat_command -sb file.txt- Functional Testing: Compare output with system
catcommand - Edge Cases: Empty files, binary files, permission denied, non-existent files
- Performance: Test with large files (>100MB)
- Memory: Check for memory leaks using valgrind
- Cross-platform: Test on Windows, Linux, and macOS
MIT License - see LICENSE file for details.
- Follow the implementation roadmap phases
- Write tests for new functionality
- Ensure cross-platform compatibility
- Document any new options or behavior changes
- Performance test with large files before submitting