[FORMAT]
[reactos.git] / rosapps / 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 INT LineLen1, LineLen2;
62 FILE *fp1, *fp2; // file pointers
63 PTCHAR Line1 = (TCHAR *)malloc(STRBUF * sizeof(TCHAR));
64 PTCHAR Line2 = (TCHAR *)malloc(STRBUF * sizeof(TCHAR));
65 TCHAR File1[_MAX_PATH], // file paths
66 File2[_MAX_PATH];
67 BOOL bMatch = TRUE, // files match
68 bAscii = FALSE, // /A switch
69 bLineNos = FALSE; // /L switch
70
71 /* parse command line for options */
72 for (i = 1; i < argc; i++)
73 {
74 if (argv[i][0] == '/')
75 {
76 --argc;
77 switch (argv[i][1]) {
78 case 'A': bAscii = TRUE;
79 _tprintf(_T("/a not Supported\n")); /*FIXME: needs adding */
80 break;
81 case 'L': bLineNos = TRUE;
82 _tprintf(_T("/l not supported\n")); /*FIXME: needs adding */
83 break;
84 case '?': Usage();
85 return EXIT_SUCCESS;
86 default:
87 _tprintf(_T("Invalid switch - /%c\n"), argv[i][1]);
88 Usage();
89 return EXIT_FAILURE;
90 }
91 }
92 }
93
94 switch (argc)
95 {
96 case 1 :
97 _tprintf(_T("Name of first file to compare: "));
98 fgets(File1, _MAX_PATH, stdin);
99 for (i=0; i<_MAX_PATH; i++)
100 {
101 if (File1[i] == '\n')
102 {
103 File1[i] = '\0';
104 break;
105 }
106 }
107
108 _tprintf(_T("Name of second file to compare: "));
109 fgets(File2, _MAX_PATH, stdin);
110 for (i=0; i<_MAX_PATH; i++)
111 {
112 if (File2[i] == '\n')
113 {
114 File2[i] = '\0';
115 break;
116 }
117 }
118 break;
119 case 2 :
120 _tcsncpy(File1, argv[1], _MAX_PATH);
121 _tprintf(_T("Name of second file to compare: "));
122 fgets(File2, _MAX_PATH, stdin);
123 for (i=0; i<_MAX_PATH; i++)
124 {
125 if (File2[i] == '\n')
126 {
127 File2[i] = '\0';
128 break;
129 }
130 }
131 break;
132 case 3 :
133 _tcsncpy(File1, argv[1], _MAX_PATH);
134 _tcsncpy(File2, argv[2], _MAX_PATH);
135 break;
136 default :
137 _tprintf(_T("Bad command line syntax\n"));
138 return EXIT_FAILURE;
139 break;
140 }
141
142
143
144 if ((fp1 = fopen(File1, "r")) == NULL)
145 {
146 _tprintf(_T("Can't find/open file: %s\n"), File1);
147 return EXIT_FAILURE;
148 }
149 if ((fp2 = fopen(File2, "r")) == NULL)
150 {
151 _tprintf(_T("Can't find/open file: %s\n"), File2);
152 fclose(fp1);
153 return EXIT_FAILURE;
154 }
155
156
157 _tprintf(_T("Comparing %s and %s...\n"), File1, File2);
158
159 while ((LineLen1 = GetLine(Line1, fp1) != 0) &&
160 (LineLen2 = GetLine(Line2, fp2) != 0))
161 {
162 // LineCount++;
163 while ((*Line1 != '\0') && (*Line2 != '\0'))
164 {
165 if (*Line1 != *Line2)
166 {
167 bMatch = FALSE;
168 break;
169 }
170 Line1++, Line2++;
171 }
172 }
173
174 bMatch ? _tprintf(_T("Files compare OK\n")) : _tprintf(_T("Files are different sizes.\n"));
175
176 fclose(fp1);
177 fclose(fp2);
178
179
180 return EXIT_SUCCESS;
181 }
182 /* EOF */