| 1 |
// RUNNABLE_PHOBOS_TEST |
| 2 |
// PERMUTE_ARGS: |
| 3 |
// EXECUTE_ARGS: runnable/wc.d |
| 4 |
|
| 5 |
import std.file; |
| 6 |
|
| 7 |
extern(C) int printf(const char*, ...); |
| 8 |
|
| 9 |
int main (string[] args) |
| 10 |
{ |
| 11 |
int w_total; |
| 12 |
int l_total; |
| 13 |
int c_total; |
| 14 |
|
| 15 |
printf (" lines words bytes file\n"); |
| 16 |
foreach (arg; args[1 .. args.length]) |
| 17 |
{ |
| 18 |
string input; |
| 19 |
int w_cnt, l_cnt, c_cnt; |
| 20 |
int inword; |
| 21 |
|
| 22 |
input = cast(string)std.file.read(arg); |
| 23 |
|
| 24 |
foreach (char c; input) |
| 25 |
{ |
| 26 |
if (c == '\n') |
| 27 |
++l_cnt; |
| 28 |
if (c != ' ') |
| 29 |
{ |
| 30 |
if (!inword) |
| 31 |
{ |
| 32 |
inword = 1; |
| 33 |
++w_cnt; |
| 34 |
} |
| 35 |
} |
| 36 |
else |
| 37 |
inword = 0; |
| 38 |
++c_cnt; |
| 39 |
} |
| 40 |
printf ("%8u%8u%8u %.*s\n", l_cnt, w_cnt, c_cnt, cast(int)arg.length, arg.ptr); |
| 41 |
l_total += l_cnt; |
| 42 |
w_total += w_cnt; |
| 43 |
c_total += c_cnt; |
| 44 |
} |
| 45 |
if (args.length > 2) |
| 46 |
{ |
| 47 |
printf ("--------------------------------------\n%8u%8u%8u total", |
| 48 |
l_total, w_total, c_total); |
| 49 |
} |
| 50 |
return 0; |
| 51 |
} |