Sync with trunk revision 64099.
[reactos.git] / base / applications / cmdutils / comp / comp.c
1 /*
2 * ReactOS Win32 Applications
3 * Copyright (C) 2005 ReactOS Team
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 /*
20 * COPYRIGHT: See COPYING in the top level directory
21 * PROJECT: ReactOS comp utility
22 * FILE: comp.c
23 * PURPOSE: Compares the contents of two files
24 * PROGRAMMERS: Ged Murphy (gedmurphy@gmail.com)
25 * REVISIONS:
26 * GM 25/09/05 Created
27 *
28 */
29
30
31 #include <windows.h>
32 #include <tchar.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36
37 #define STRBUF 1024
38
39 /* getline: read a line, return length */
40 INT GetLine(char *line, FILE *in)
41 {
42 if (fgets(line, STRBUF, in) == NULL)
43 return 0;
44 else
45 return strlen(line);
46 }
47
48 /* print program usage */
49 VOID Usage(VOID)
50 {
51 _tprintf(_T("\nCompares the contents of two files or sets of files.\n\n"
52 "COMP [data1] [data2]\n\n"
53 " data1 Specifies location and name of first file to compare.\n"
54 " data2 Specifies location and name of second file to compare.\n"));
55 }
56
57
58 int _tmain (int argc, TCHAR *argv[])
59 {
60 INT i;
61 FILE *fp1, *fp2; // file pointers
62 PTCHAR Line1 = (TCHAR *)malloc(STRBUF * sizeof(TCHAR));
63 PTCHAR Line2 = (TCHAR *)malloc(STRBUF * sizeof(TCHAR));
64 TCHAR File1[_MAX_PATH], // file paths
65 File2[_MAX_PATH];
66 BOOL bMatch = TRUE, // files match
67 bAscii = FALSE, // /A switch
68 bLineNos = FALSE; // /L switch
69
70 /* parse command line for options */
71 for (i = 1; i < argc; i++)
72 {
73 if (argv[i][0] == '/')
74 {
75 --argc;
76 switch (argv[i][1]) {
77 case 'A': bAscii = TRUE;
78 _tprintf(_T("/a not Supported\n")); (void)bAscii; /*FIXME: needs adding */
79 break;
80 case 'L': bLineNos = TRUE;
81 _tprintf(_T("/l not supported\n")); (void)bLineNos; /*FIXME: needs adding */
82 break;
83 case '?': Usage();
84 return EXIT_SUCCESS;
85 default:
86 _tprintf(_T("Invalid switch - /%c\n"), argv[i][1]);
87 Usage();
88 return EXIT_FAILURE;
89 }
90 }
91 }
92
93 switch (argc)
94 {
95 case 1 :
96 _tprintf(_T("Name of first file to compare: "));
97 fgets(File1, _MAX_PATH, stdin);
98 for (i=0; i<_MAX_PATH; i++)
99 {
100 if (File1[i] == '\n')
101 {
102 File1[i] = '\0';
103 break;
104 }
105 }
106
107 _tprintf(_T("Name of second file to compare: "));
108 fgets(File2, _MAX_PATH, stdin);
109 for (i=0; i<_MAX_PATH; i++)
110 {
111 if (File2[i] == '\n')
112 {
113 File2[i] = '\0';
114 break;
115 }
116 }
117 break;
118 case 2 :
119 _tcsncpy(File1, argv[1], _MAX_PATH);
120 _tprintf(_T("Name of second file to compare: "));
121 fgets(File2, _MAX_PATH, stdin);
122 for (i=0; i<_MAX_PATH; i++)
123 {
124 if (File2[i] == '\n')
125 {
126 File2[i] = '\0';
127 break;
128 }
129 }
130 break;
131 case 3 :
132 _tcsncpy(File1, argv[1], _MAX_PATH);
133 _tcsncpy(File2, argv[2], _MAX_PATH);
134 break;
135 default :
136 _tprintf(_T("Bad command line syntax\n"));
137 return EXIT_FAILURE;
138 break;
139 }
140
141
142
143 if ((fp1 = fopen(File1, "r")) == NULL)
144 {
145 _tprintf(_T("Can't find/open file: %s\n"), File1);
146 return EXIT_FAILURE;
147 }
148 if ((fp2 = fopen(File2, "r")) == NULL)
149 {
150 _tprintf(_T("Can't find/open file: %s\n"), File2);
151 fclose(fp1);
152 return EXIT_FAILURE;
153 }
154
155
156 _tprintf(_T("Comparing %s and %s...\n"), File1, File2);
157
158 while ((GetLine(Line1, fp1) != 0) &&
159 (GetLine(Line2, fp2) != 0))
160 {
161 // LineCount++;
162 while ((*Line1 != '\0') && (*Line2 != '\0'))
163 {
164 if (*Line1 != *Line2)
165 {
166 bMatch = FALSE;
167 break;
168 }
169 Line1++, Line2++;
170 }
171 }
172
173 bMatch ? _tprintf(_T("Files compare OK\n")) : _tprintf(_T("Files are different sizes.\n"));
174
175 fclose(fp1);
176 fclose(fp2);
177
178
179 return EXIT_SUCCESS;
180 }
181 /* EOF */