source: branches/stable/mindi-busybox/coreutils/cksum.c @ 1247

Last change on this file since 1247 was 821, checked in by Bruno Cornec, 14 years ago

Addition of busybox 1.2.1 as a mindi-busybox new package
This should avoid delivering binary files in mindi not built there (Fedora and Debian are quite serious about that)

File size: 1.2 KB
RevLine 
[821]1/* vi: set sw=4 ts=4: */
2/*
3 * cksum - calculate the CRC32 checksum of a file
4 *
5 * Copyright (C) 2006 by Rob Sullivan, with ideas from code by Walter Harms
6 *
7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. */
8
9#include <stdio.h>
10#include <unistd.h>
11#include <fcntl.h>
12#include "busybox.h"
13
14int cksum_main(int argc, char **argv) {
15   
16    uint32_t *crc32_table = bb_crc32_filltable(1);
17
18    FILE *fp;
19    uint32_t crc;
20    long length, filesize;
21    int bytes_read;
22    char *cp;
23    RESERVE_CONFIG_BUFFER(buf, BUFSIZ);
24    int inp_stdin = (argc == optind) ? 1 : 0;
25   
26    do {
27        fp = bb_wfopen_input((inp_stdin) ? bb_msg_standard_input : *++argv);
28           
29        crc = 0;
30        length = 0;
31       
32        while ((bytes_read = fread(buf, 1, BUFSIZ, fp)) > 0) {
33            cp = buf;
34            length += bytes_read;
35            while (bytes_read--)
36                crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ (*cp++)) & 0xffL];
37        }
38       
39        filesize = length;
40       
41        for (; length; length >>= 8)
42            crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ length) & 0xffL];
43        crc ^= 0xffffffffL;
44
45        if (inp_stdin) {
46            printf("%"PRIu32" %li\n", crc, filesize);
47            break;
48        }
49       
50        printf("%"PRIu32" %li %s\n", crc, filesize, *argv); 
51        fclose(fp);
52    } while (*(argv+1));
53   
54    return EXIT_SUCCESS;
55}
Note: See TracBrowser for help on using the repository browser.