Program Usage
The ZLIB program is a command-line utility that opens a file and processes operations specified by command-line arguments.Usage Syntax
Options
| Option | Description | Required |
|---|---|---|
-i gz_file | Input GZ file | Yes (except with -h) |
-h | Print help message | No |
-m | Print member summary | No (mutually exclusive) |
-c | Compress the file | No (mutually exclusive) |
-d | Decompress the file | No (mutually exclusive) |
-o out_file | Output file for -c or -d | Required for -c and -d |
Square brackets indicate optional arguments. The
-i flag is required for all operations except -h.Operation Modes
The program supports three mutually exclusive operation modes:Help Mode (-h)
Displays usage information and exits successfully.
- Prints usage message to stdout
- Exits with
EXIT_SUCCESS(0) - Ignores all other arguments
PRINT_USAGE macro from global.h:
Member Summary Mode (-m)
Displays metadata for all members in a GZIP file.
<label>- Member name (from header) or index number (0, 1, 2…)<cm>- Compression method (0 = no compression, 8 = DEFLATE)<mtime>- Last modification time (Unix timestamp)<os>- Operating system identifier<extra_len>- Length of extra field<comment>- Optional comment (only shown if present)<size>- Uncompressed size in bytes<valid|invalid>- CRC validation result
global.h:
main.c:82-100 for the implementation example.
Decompress Mode (-d)
Decompresses a GZIP file and writes the raw uncompressed bytes to an output file.
- The
-oflag is required and must specify the output file - Validates CRC and HCRC checksums
- Prints errors to stderr if validation fails
- Exits with
EXIT_FAILUREon errors
- Invalid CRC or HCRC
- Parsing failures
- File I/O errors
- Invalid block types
Compress Mode (-c)
Compresses a file using the DEFLATE algorithm and writes a GZIP file.
- The
-oflag is required and must specify the output file - Produces valid GZIP format
- Computes CRC checksum
- Writes proper GZIP header and footer
Two-Pass Argument Processing
The program uses a two-pass approach to process command-line arguments. Seemain.c:8-64 for the implementation.
First Pass: Validation
Check for help flag
If
-h is specified, print usage and exit with EXIT_SUCCESS, ignoring all other argumentsSecond Pass: Execution
After successful validation:- Open the input file
- Execute the requested operation
- Write results to output file or stdout
- Exit with appropriate status
Argument Validation Rules
Required Arguments
If-i is missing or has no filename:
Mutually Exclusive Operations
If none specified or multiple specified:Output File Requirement
If-o is missing for compress or decompress:
Error Messages
All error messages use macros fromglobal.h and are printed to stderr:
| Macro | When to Use |
|---|---|
PRINT_ERROR_MISSING_I_FLAG() | -i flag not provided |
PRINT_ERROR_REQUIRE_ONE_OF_MCD() | No mode or multiple modes specified |
PRINT_ERROR_MISSING_O_FLAG() | -o missing for -c or -d |
PRINT_ERROR_OPEN_FILE(filename) | Cannot open specified file |
PRINT_ERROR_BAD_HEADER() | Invalid GZIP magic number |
Exit Codes
EXIT_SUCCESS(0) - Successful executionEXIT_FAILURE(1) - Any error condition
All error messages must go to stderr, not stdout. The program must exit immediately after printing an error.
Implementation Example
Here’s the argument parsing logic frommain.c:8-64:
Program Modes
The program defines three mode constants inour_zlib.h:5-7:
File Operations
Opening Files
Files are opened in binary mode:Writing Output
Output files are opened in binary write mode:Testing Examples
Valid Usage
Invalid Usage
References
main.c:8-197- Complete argument processing implementationglobal.h:8-25- All output and error macrosour_zlib.h:5-7- Program mode definitions- Beej’s Guide - Command Line Arguments