add 'w' option
[unix-history] / usr / src / usr.bin / cksum / sum1.c
CommitLineData
6fab43e6 1/*-
8964deec
KB
2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
6fab43e6
KB
4 *
5 * %sccs.include.redist.c%
6 */
7
8#ifndef lint
8964deec 9static char sccsid[] = "@(#)sum1.c 8.1 (Berkeley) %G%";
6fab43e6
KB
10#endif /* not lint */
11
12#include <sys/types.h>
13#include <unistd.h>
14
31785468 15int
6fab43e6
KB
16csum1(fd, cval, clen)
17 register int fd;
18 u_long *cval, *clen;
19{
20 register u_long total;
21 register int nr;
22 register u_int crc;
23 register u_char *p;
24 u_char buf[8192];
25
26 /*
27 * 16-bit checksum, rotating right before each addition;
28 * overflow is discarded.
29 */
30 crc = total = 0;
31 while ((nr = read(fd, buf, sizeof(buf))) > 0)
32 for (total += nr, p = buf; nr--; ++p) {
33 if (crc & 1)
34 crc |= 0x10000;
35 crc = ((crc >> 1) + *p) & 0xffff;
36 }
37 if (nr < 0)
38 return(1);
39
40 *cval = crc;
41 *clen = total;
42 return(0);
43}