tail: -50 text bytes
[oweals/busybox.git] / coreutils / sum.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * sum -- checksum and count the blocks in a file
4  *     Like BSD sum or SysV sum -r, except like SysV sum if -s option is given.
5  *
6  * Copyright (C) 86, 89, 91, 1995-2002, 2004 Free Software Foundation, Inc.
7  * Copyright (C) 2005 by Erik Andersen <andersen@codepoet.org>
8  * Copyright (C) 2005 by Mike Frysinger <vapier@gentoo.org>
9  *
10  * Written by Kayvan Aghaiepour and David MacKenzie
11  * Taken from coreutils and turned into a busybox applet by Mike Frysinger
12  *
13  * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
14  */
15
16 #include "busybox.h"
17
18 /* 1 if any of the files read were the standard input */
19 static int have_read_stdin;
20
21 /* Calculate and print the rotated checksum and the size in 1K blocks
22    of file FILE, or of the standard input if FILE is "-".
23    If PRINT_NAME is >1, print FILE next to the checksum and size.
24    The checksum varies depending on sizeof (int).
25    Return 1 if successful.  */
26 static int bsd_sum_file(const char *file, int print_name)
27 {
28         FILE *fp;
29         int checksum = 0;          /* The checksum mod 2^16. */
30         uintmax_t total_bytes = 0; /* The number of bytes. */
31         int ch;                    /* Each character read. */
32         int ret = 0;
33
34         if (LONE_DASH(file)) {
35                 fp = stdin;
36                 have_read_stdin++;
37         } else {
38                 fp = fopen_or_warn(file, "r");
39                 if (fp == NULL)
40                         goto out;
41         }
42
43         while ((ch = getc(fp)) != EOF) {
44                 ++total_bytes;
45                 checksum = (checksum >> 1) + ((checksum & 1) << 15);
46                 checksum += ch;
47                 checksum &= 0xffff;             /* Keep it within bounds. */
48         }
49
50         if (ferror(fp)) {
51                 bb_perror_msg(file);
52                 fclose_if_not_stdin(fp);
53                 goto out;
54         }
55
56         if (fclose_if_not_stdin(fp) == EOF) {
57                 bb_perror_msg(file);
58                 goto out;
59         }
60         ret++;
61         printf("%05d %5ju ", checksum, (total_bytes+1023)/1024);
62         if (print_name > 1)
63                 puts(file);
64         else
65                 puts("");
66 out:
67         return ret;
68 }
69
70 /* Calculate and print the checksum and the size in 512-byte blocks
71    of file FILE, or of the standard input if FILE is "-".
72    If PRINT_NAME is >0, print FILE next to the checksum and size.
73    Return 1 if successful.  */
74 #define MY_BUF_SIZE 8192
75 static int sysv_sum_file(const char *file, int print_name)
76 {
77         RESERVE_CONFIG_BUFFER(buf, MY_BUF_SIZE);
78         int fd;
79         uintmax_t total_bytes = 0;
80
81         /* The sum of all the input bytes, modulo (UINT_MAX + 1).  */
82         unsigned int s = 0;
83
84         if (LONE_DASH(file)) {
85                 fd = 0;
86                 have_read_stdin = 1;
87         } else {
88                 fd = open(file, O_RDONLY);
89                 if (fd == -1)
90                         goto release_and_ret;
91         }
92
93         while (1) {
94                 size_t bytes_read = safe_read(fd, buf, MY_BUF_SIZE);
95
96                 if (bytes_read == 0)
97                         break;
98
99                 if (bytes_read == -1) {
100 release_and_ret:
101                         bb_perror_msg(file);
102                         RELEASE_CONFIG_BUFFER(buf);
103                         if (NOT_LONE_DASH(file))
104                                 close(fd);
105                         return 0;
106                 }
107
108                 total_bytes += bytes_read;
109                 while (bytes_read--)
110                         s += buf[bytes_read];
111         }
112
113         if (NOT_LONE_DASH(file) && close(fd) == -1)
114                 goto release_and_ret;
115         else
116                 RELEASE_CONFIG_BUFFER(buf);
117
118         {
119                 int r = (s & 0xffff) + ((s & 0xffffffff) >> 16);
120                 s = (r & 0xffff) + (r >> 16);
121
122                 printf("%d %ju ", s, (total_bytes+511)/512);
123         }
124         puts(print_name ? file : "");
125
126         return 1;
127 }
128
129 int sum_main(int argc, char **argv)
130 {
131         int flags;
132         int ok;
133         int (*sum_func)(const char *, int) = bsd_sum_file;
134
135         /* give the bsd func priority over sysv func */
136         flags = getopt32(argc, argv, "sr");
137         if (flags & 1)
138                 sum_func = sysv_sum_file;
139         if (flags & 2)
140                 sum_func = bsd_sum_file;
141
142         have_read_stdin = 0;
143         if ((argc - optind) == 0)
144                 ok = sum_func("-", 0);
145         else
146                 for (ok = 1; optind < argc; optind++)
147                         ok &= sum_func(argv[optind], 1);
148
149         if (have_read_stdin && fclose(stdin) == EOF)
150                 bb_perror_msg_and_die("-");
151
152         exit(ok ? EXIT_SUCCESS : EXIT_FAILURE);
153 }