NetBSD-Users archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
Re: How high can wc go?
On 06/17/10 10:14, Ray Phillips wrote:
The other day on a NetBSD/i386 5.0.2 machine I made a 1833 GiB file
consisting of 30752636928 64-character lines (including the new line
char). Later I ran wc on it, expecting its output to be incorrect as a
result of overflows in the variables it uses, but the answers were right:
% ls -l /mnt2
total 3845018400
-rw-r--r-- 1 root wheel 1968168763392 Jun 14 08:28 1833GiB.txt
% /usr/bin/time wc /mnt/1833GiB.txt
30752636928 30752636928 1968168763392 /mnt2/1833GiB.txt
125997.76 real 121238.03 user 3731.70 sys
%
% dc
4k
1833 2 30^ * 64 /p
30752636928.0000
30752636928 64 * p
1968168763392
q
%
I was wondering if anyone knows how many lines, words, and characters wc
can keep track of? It seems that it uses some kind of tricky technique
like dc to avoid overflows occurring?
Ray
If you look in src/usr.bin/wc/wc.c this contains the wc_count_t typedef
#ifdef NO_QUAD
typedef u_long wc_count_t;
# define WCFMT " %7lu"
# define WCCAST unsigned long
#else
typedef u_quad_t wc_count_t;
# define WCFMT " %7llu"
# define WCCAST unsigned long long
#endif
And this shows that it will support either a 64bit counter or a 32bit
counter. This in a typical case you will notice a wrap when you count
more than 2^64 characters.
>>> print 2**64
18446744073709551616
Or this amount in byte.
E.
Home |
Main Index |
Thread Index |
Old Index