File: cmp.c

package info (click to toggle)
ftnchek 3.3.1-7
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,684 kB
  • sloc: ansic: 21,908; fortran: 5,748; yacc: 4,071; sh: 3,035; makefile: 895; lisp: 322; f90: 118; perl: 76
file content (132 lines) | stat: -rw-r--r-- 3,515 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/* cmp.c:

   Simple replacement for Unix cmp, for use with the check.bat script.
   This program lacks options such as -l and -s.

*/


/*

Copyright (c) 2000 by Robert K. Moniot.

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
ROBERT K. MONIOT OR FORDHAM UNIVERSITY BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name of ftnchek shall not be used
in advertising or otherwise to promote the sale, use or other dealings in
this Software without prior written authorization from the author.

*/


#include <stdio.h>
#include <stdlib.h>

#define MAXLINE 1024		/* Big enough for present purposes */

long char_count, line_count;	/* Total byte and line counts */

char *myname;			/* For error messages */

				/* Prototypes */
FILE *fopen_or_fail( char *path, char *mode );
int cmp_lines( char *line1, char *line2);


				/* Open file or fail with error message.
				 */
FILE *fopen_or_fail( char *path, char *mode )
{
    FILE *fd;
    fd = fopen(path,mode);
    if( fd == NULL ) {
	fprintf(stderr,"%s: ",myname);
	perror(path);
	exit(2);
    }
    return fd;
}

			/* Compare two lines, return 0 if equal and 1 if
			   different.  Also keep track of line & char count.
			 */
int cmp_lines( char *line1, char *line2)
{
    int i;
    for(i=0; line1[i] != '\0' && line2[i] != '\0'; i++) {
	if(line1[i] != line2[i])
	    return 1;

	char_count++;
	if(line1[i] == '\n')
	    line_count++;
    }
    return 0;
}

int main( int argc, char *argv[] )
{
    FILE *fd1, *fd2;			 /* input file descriptors */
    char line1[MAXLINE], line2[MAXLINE]; /* buffers for input */
    char *read1, *read2;		 /* return status from fgets */
    int status = 0;			 /* exit status */

    myname = argv[0];
				/* Make sure exactly 2 args are present */
    if( argc != 3 ) {
	fprintf(stderr,"Usage: %s file1 file2\n", myname);
	exit(2);
    }

				/* Open files, die if cannot */
    fd1 = fopen_or_fail( argv[1], "r" );
    fd2 = fopen_or_fail( argv[2], "r" );

				/* Read files and compare line by line */

    line_count = char_count = 1;

    for(;;) {

	read1 = fgets(line1, sizeof(line1), fd1);
	read2 = fgets(line2, sizeof(line2), fd2);

	if( read1 == NULL || read2 == NULL ) {
	    if(read1 != NULL || read2 != NULL) {
		printf("%s: EOF on %s\n", myname,
		       read1 == NULL? argv[1]: argv[2]);
		status = 1;
	    }
	    break;
	}

				/* Compare the two lines */
	if( (status = cmp_lines(line1, line2)) != 0 ) {
	    printf("%s %s differ: char %ld, line %ld\n",argv[1],argv[2],
		   char_count,line_count);
	    break;
	}

    }

    fclose(fd1);
    fclose(fd2);

    exit(status);
}