source: trunk/gsdl/packages/mg/test/simple_sum.c@ 1015

Last change on this file since 1015 was 439, checked in by sjboddie, 25 years ago

renamed mg-1.3d directory mg

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