source: gsdl/trunk/trunk/mg/test/simple_sum.c@ 16583

Last change on this file since 16583 was 16583, checked in by davidb, 16 years ago

Undoing change commited in r16582

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 2.1 KB
Line 
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
28static int length_only = 0;
29
30#include <stdio.h>
31
32void
33output_sum(fname, input_file, output_file)
34char *fname;
35FILE *input_file;
36FILE *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
53void
54main(argc, argv)
55int argc;
56char *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}
Note: See TracBrowser for help on using the repository browser.