Write the wc utility in C along with some integration tests in <REDACTED>, and…
- User
Write the wc utility in C along with some integration tests in , and benchmarks with /usr/bin/time so we can improve it.
- Assistant
Write the wc utility in C along with some integration tests in , and benchmarks with /usr/bin/time so we can improve it.
Below is one complete solution that includes:
A C implementation of the basicwcutilitythat counts lines, words, and bytes.
A Bash integration-test scriptthat builds your version, creates some test files, and compares its output against the system’swc.
A Bash benchmark scriptthat uses/usr/bin/timeto measure performance on a large file.
A C implementation of the basicwcutilitythat counts lines, words, and bytes.
A Bash integration-test scriptthat builds your version, creates some test files, and compares its output against the system’swc.
A Bash benchmark scriptthat uses/usr/bin/timeto measure performance on a large file.
I'll explain each part briefly before showing the code.
This implementation follows the standard approach:
Byte counting:Every character read increments the byte count.
Line counting:Each newline character ('\n') is counted as one line.
Word counting:We keep a boolean flag (in_word) to track whether we are inside a word. When a non‐whitespace character is found right after whitespace, we count a new word.
Byte counting:Every character read increments the byte count.
Line counting:Each newline character ('\n') is counted as one line.
Word counting:We keep a boolean flag (in_word) to track whether we are inside a word. When a non‐whitespace character is found right after whitespace, we count a new word.
The program supports:
No arguments:It reads from standard input.
One or more filenames:It opens each file and prints a line of counts (with fixed-width fields for nice alignment). If more than one file is provided, a “total” line is printed.
No arguments:It reads from standard input.
One or more filenames:It opens each file and prints a line of counts (with fixed-width fields for nice alignment). If more than one file is provided, a “total” line is printed.
Here’s the completewc.c:
────────────────────────────────────────────────────────────────────
cCopy// wc.c #include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <stdbool.h>
typedef struct { unsigned long lines; unsigned long words; unsigned long bytes; } Count;
void count_stream(FILE *fp, Count *count) { int c; bool in_word = false; while ((c = fgetc(fp)) != EOF) { count->bytes++; if (c == '\n') ; if (isspace(c)) { in_word = false; } else if (!) { count->words++; = true; } } }
int main(int argc, char *argv[]) { Count total = {0, 0, 0}; int files_counted = 0;
if (argc == 1) {
// No filename given; read from stdin.
Count count = {0, 0, 0};
count_stream(stdin, &count);
printf("%7lu %7lu %7lu\n", count.lines, count.words, count.bytes);
return 0;
}
// Process each file provided as an argument.
for (int i = 1; i < argc; i++) {
FILE *fp = fopen(argv[i], "rb");
if (fp == NULL) {
perror(argv[i]);
continue;
}
Count count = {0, 0, 0};
count_stream(fp, &count);
<REDACTED>);
printf("%7lu %7lu %7lu %s\n", count.lines, count.words, count.bytes, argv[i]);
total.lines += count.lines;
total.words += count.words;
<URL>tes += count.bytes;
files_counted++;
}
// If more than one file, print totals.
if (files_counted > 1) {
printf("%7lu %7lu %7lu total\n", total.lines, total.words, total.bytes);
}
return 0;
}
To compile this file, you can run (for example):
────────────────────────────────────────────────────────────────────
bashCopygcc -O2 -o mywc wc.c
This script does the following:
Compilesyourwcimplementation.
Creates test fileswith known content.
Runs testson:A single file,Multiple files,Reading from standard input.
A single file,
Multiple files,
Reading from standard input.
Comparesyour program’s output with the output from the systemwc.
Compilesyourwcimplementation.
Creates test fileswith known content.
Runs testson:
A single file,
Multiple files,
Reading from standard input.
A single file,
Multiple files,
Reading from standard input.
Comparesyour program’s output with the output from the systemwc.
Here is the integration test script:
────────────────────────────────────────────────────────────────────
bashCopy#!/bin/bash
set -euo pipefail
gcc -O2 -o mywc wc.c
echo -e "Hello world\nThis is a test" > test1.txt echo -e "Another test file\nWith multiple lines\nand some words" > test2.txt
echo "Running single file test..." output_mywc=(wc test1.txt) if [ "output_wc" ]; then echo "Single file test failed." echo "mywc output: output_wc" exit 1 fi
echo "Running multiple files test..." output_mywc=(wc test1.txt test2.txt) if [ "output_wc" ]; then echo "Multiple files test failed." echo "mywc output: output_wc" exit 1 fi
echo "Running stdin test..." output_mywc=(echo -e "One line" | wc) if [ "output_wc" ]; then echo "Stdin test failed." echo "mywc output: output_wc" exit 1 fi
echo "All integration tests passed."
rm -f test1.txt test2.txt mywc
Make sure to make the script executable:
────────────────────────────────────────────────────────────────────
bashCopychmod +x test_wc.sh
Then run it:
────────────────────────────────────────────────────────────────────
bashCopy./test_wc.sh
Note:Depending on your system, the exact spacing in the output might differ slightly. You can adjust the comparison (for example, by using tools likediff -w) if necessary.
This script compiles your implementation with optimizations and then benchmarks it on a large file. If the file does not exist, it generates one (here by concatenating the system dictionary multiple times). It then uses/usr/bin/time -vto display detailed timing and resource usage for both your program and the systemwc.
Here is the benchmark script:
────────────────────────────────────────────────────────────────────
bashCopy#!/bin/bash
set -euo pipefail
gcc -O2 -o mywc wc.c
if [ ! -f large_test.txt ]; then echo "Generating large_test.txt..." for i in {1..100}; do cat /usr/share/dict/words >> large_test.txt done fi
echo "Benchmarking mywc with /usr/bin/time..." /usr/bin/time -v ./mywc large_test.txt
echo "Benchmarking system wc with /usr/bin/time..." /usr/bin/time -v wc large_test.txt
Again, make the script executable:
────────────────────────────────────────────────────────────────────
bashCopychmod +x benchmark_wc.sh
Then run it:
────────────────────────────────────────────────────────────────────
bashCopy./benchmark_wc.sh
Summary
wc.cis your C implementation of thewcutility.
test_wc.shruns integration tests comparing your tool with the systemwc.
benchmark_wc.shbenchmarks both your tool and the systemwcusing/usr/bin/time.
wc.cis your C implementation of thewcutility.
test_wc.shruns integration tests comparing your tool with the systemwc.
benchmark_wc.shbenchmarks both your tool and the systemwcusing/usr/bin/time.
You can now use and extend these files as needed to further optimize or add features to yourwcimplementation. Happy coding!
Not yet included in a published release.