[3745] | 1 | /**************************************************************************
|
---|
| 2 | *
|
---|
| 3 | * 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 | * $Id: simple_sum.c 3745 2003-02-20 21:20:24Z mdewsnip $
|
---|
| 21 | *
|
---|
| 22 | **************************************************************************/
|
---|
| 23 |
|
---|
| 24 | /*
|
---|
| 25 | $Log$
|
---|
| 26 | Revision 1.1 2003/02/20 21:20:24 mdewsnip
|
---|
| 27 | Addition of MG package for search and retrieval
|
---|
| 28 |
|
---|
| 29 | Revision 1.1 1999/08/10 21:18:33 sjboddie
|
---|
| 30 | renamed mg-1.3d directory mg
|
---|
| 31 |
|
---|
| 32 | Revision 1.1 1998/11/17 09:36:00 rjmcnab
|
---|
| 33 | *** empty log message ***
|
---|
| 34 |
|
---|
| 35 | */
|
---|
| 36 |
|
---|
| 37 | /*
|
---|
| 38 | * If given -l option then just prints the length.
|
---|
| 39 | * Normally prints the sum and the length for the file.
|
---|
| 40 | */
|
---|
| 41 |
|
---|
| 42 | static int length_only = 0;
|
---|
| 43 |
|
---|
| 44 | #include <stdio.h>
|
---|
| 45 |
|
---|
| 46 | void
|
---|
| 47 | output_sum(fname, input_file, output_file)
|
---|
| 48 | char *fname;
|
---|
| 49 | FILE *input_file;
|
---|
| 50 | FILE *output_file;
|
---|
| 51 | {
|
---|
| 52 | int ch = '\0';
|
---|
| 53 | unsigned long sum = 0;
|
---|
| 54 | unsigned long num_bytes = 0;
|
---|
| 55 |
|
---|
| 56 | while((ch = getc(input_file))!=EOF){
|
---|
| 57 | sum += ch;
|
---|
| 58 | num_bytes++;
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | if (length_only)
|
---|
| 62 | fprintf(output_file, "%s: %ld\n", fname, num_bytes);
|
---|
| 63 | else
|
---|
| 64 | fprintf(output_file, "%s: %ld %ld\n", fname, sum, num_bytes);
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | void
|
---|
| 68 | main(argc, argv)
|
---|
| 69 | int argc;
|
---|
| 70 | char *argv[];
|
---|
| 71 | {
|
---|
| 72 | int i = 0;
|
---|
| 73 |
|
---|
| 74 | /* use stdin */
|
---|
| 75 | if (argc == 1){
|
---|
| 76 | output_sum("-", stdin, stdout);
|
---|
| 77 | exit(0);
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | /* use args as file names for input */
|
---|
| 81 | for (i=1;i<argc; i++){
|
---|
| 82 | char *fname = argv[i];
|
---|
| 83 | FILE *input_file = NULL;
|
---|
| 84 |
|
---|
| 85 | if (!length_only && (argv[i][0] == '-') && (argv[i][1] =='l') )
|
---|
| 86 | {
|
---|
| 87 | length_only = 1;
|
---|
| 88 | continue;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | input_file = fopen(fname, "r");
|
---|
| 92 | if (!input_file){
|
---|
| 93 | fprintf(stderr,"Could not open %s\n", fname);
|
---|
| 94 | continue;
|
---|
| 95 | }
|
---|
| 96 | else{
|
---|
| 97 | output_sum(fname, input_file, stdout);
|
---|
| 98 | fclose(input_file);
|
---|
| 99 | }
|
---|
| 100 | }/*for*/
|
---|
| 101 | exit(0);
|
---|
| 102 |
|
---|
| 103 | }
|
---|