1 | /**************************************************************************
|
---|
2 | *
|
---|
3 | * simple_sum.c -- byte summing program
|
---|
4 | * Copyright (C) 1995 Tim Shimmin
|
---|
5 | *
|
---|
6 | * This program is free software; you can redistribute it and/or modify
|
---|
7 | * it under the terms of the GNU General Public License as published by
|
---|
8 | * the Free Software Foundation; either version 2 of the License, or
|
---|
9 | * (at your option) any later version.
|
---|
10 | *
|
---|
11 | * This program is distributed in the hope that it will be useful,
|
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
14 | * GNU General Public License for more details.
|
---|
15 | *
|
---|
16 | * You should have received a copy of the GNU General Public License
|
---|
17 | * along with this program; if not, write to the Free Software
|
---|
18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
19 | *
|
---|
20 | *
|
---|
21 | **************************************************************************/
|
---|
22 |
|
---|
23 | /*
|
---|
24 | * If given -l option then just prints the length.
|
---|
25 | * Normally prints the sum and the length for the file.
|
---|
26 | */
|
---|
27 |
|
---|
28 | static int length_only = 0;
|
---|
29 |
|
---|
30 | #include <stdio.h>
|
---|
31 |
|
---|
32 | void
|
---|
33 | output_sum(fname, input_file, output_file)
|
---|
34 | char *fname;
|
---|
35 | FILE *input_file;
|
---|
36 | FILE *output_file;
|
---|
37 | {
|
---|
38 | int ch = '\0';
|
---|
39 | unsigned long sum = 0;
|
---|
40 | unsigned long num_bytes = 0;
|
---|
41 |
|
---|
42 | while((ch = getc(input_file))!=EOF){
|
---|
43 | sum += ch;
|
---|
44 | num_bytes++;
|
---|
45 | }
|
---|
46 |
|
---|
47 | if (length_only)
|
---|
48 | fprintf(output_file, "%s: %ld\n", fname, num_bytes);
|
---|
49 | else
|
---|
50 | fprintf(output_file, "%s: %ld %ld\n", fname, sum, num_bytes);
|
---|
51 | }
|
---|
52 |
|
---|
53 | void
|
---|
54 | main(argc, argv)
|
---|
55 | int argc;
|
---|
56 | char *argv[];
|
---|
57 | {
|
---|
58 | int i = 0;
|
---|
59 |
|
---|
60 | /* use stdin */
|
---|
61 | if (argc == 1){
|
---|
62 | output_sum("-", stdin, stdout);
|
---|
63 | exit(0);
|
---|
64 | }
|
---|
65 |
|
---|
66 | /* use args as file names for input */
|
---|
67 | for (i=1;i<argc; i++){
|
---|
68 | char *fname = argv[i];
|
---|
69 | FILE *input_file = NULL;
|
---|
70 |
|
---|
71 | if (!length_only && (argv[i][0] == '-') && (argv[i][1] =='l') )
|
---|
72 | {
|
---|
73 | length_only = 1;
|
---|
74 | continue;
|
---|
75 | }
|
---|
76 |
|
---|
77 | input_file = fopen(fname, "r");
|
---|
78 | if (!input_file){
|
---|
79 | fprintf(stderr,"Could not open %s\n", fname);
|
---|
80 | continue;
|
---|
81 | }
|
---|
82 | else{
|
---|
83 | output_sum(fname, input_file, stdout);
|
---|
84 | fclose(input_file);
|
---|
85 | }
|
---|
86 | }/*for*/
|
---|
87 | exit(0);
|
---|
88 |
|
---|
89 | }
|
---|