File: fc.c

package info (click to toggle)
wine 10.0~repack-12
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 326,476 kB
  • sloc: ansic: 4,155,993; perl: 23,800; yacc: 22,031; javascript: 15,872; makefile: 12,346; pascal: 9,519; objc: 6,923; lex: 5,273; xml: 3,219; python: 2,688; cpp: 1,741; sh: 871; java: 750; asm: 299; cs: 62
file content (414 lines) | stat: -rw-r--r-- 12,451 bytes parent folder | download | duplicates (3)
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/*
 * Copyright 2018 Fabian Maurer
 * Copyright 2024 Hans Leidekker for CodeWeavers
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include <stdarg.h>
#include <unistd.h>

#include <windef.h>
#include <winbase.h>
#include <winnls.h>
#include <consoleapi.h>

#include "wine/debug.h"
#include "wine/list.h"

WINE_DEFAULT_DEBUG_CHANNEL(fc);

static BOOL option_case_insensitive;

enum section_type
{
    SECTION_TYPE_COPY,
    SECTION_TYPE_DELETE,
    SECTION_TYPE_INSERT
};

struct section
{
    struct list entry;
    enum section_type type;
    unsigned int start;     /* index in line array */
    unsigned int len;       /* number of lines */
};
static struct list sections = LIST_INIT( sections );

struct common_subseq
{
    unsigned int pos_first;  /* position in first file */
    unsigned int pos_second;
    unsigned int len;
};

struct line
{
    const char  *start;
    unsigned int len;
};

struct lines
{
    unsigned int count;
    unsigned int capacity;
    struct line *entry;
};
static struct lines lines1, lines2;

static void append_line( struct lines *lines, const char *start, unsigned int len )
{
    if (lines->count >= lines->capacity)
    {
        lines->capacity = max( 128, lines->capacity * 2 );
        lines->entry = realloc( lines->entry, lines->capacity * sizeof(*lines->entry) );
    }
    lines->entry[lines->count].start = start;
    lines->entry[lines->count].len = len;
    lines->count++;
}

static void split_lines( struct lines *lines, const char *data, unsigned int len )
{
    const char *p = data, *q = data;

    while (len)
    {
        if (p[0] == '\n' || (p[0] == '\r' && p[1] == '\n'))
        {
            append_line( lines, q, p - q );
            if (p[0] == '\r') { p++; len--; }
            q = p + 1;
        }
        p++; len--;
    }
    if (p != q) append_line( lines, q, p - q );
}

static char *data1, *data2;
static unsigned int len_data1, len_data2;

static int equal_lines( const struct line *line1, const struct line *line2 )
{
    if (line1->len != line2->len) return 0;
    if (option_case_insensitive) return !memicmp( line1->start, line2->start, line1->len );
    else return !memcmp( line1->start, line2->start, line1->len );
}

/* return the number of equal lines at given ranges */
static unsigned int common_length( unsigned int first_pos, unsigned int first_end,
                                   unsigned int second_pos, unsigned int second_end )
{
    unsigned int len = 0;

    while (first_pos < first_end && second_pos < second_end)
    {
        if (!equal_lines( &lines1.entry[first_pos++], &lines2.entry[second_pos++] )) break;
        len++;
    }
    return len;
}

/* return the longest sequence of equal lines between given ranges */
static int longest_common_subsequence( unsigned int first_start, unsigned int first_end,
                                       unsigned int second_start, unsigned int second_end,
                                       struct common_subseq *result )
{
    unsigned int i, j;
    int ret = 0;

    memset( result, 0, sizeof(*result) );

    for (i = first_start; i < first_end; i++)
    {
        for (j = second_start; j < second_end; j++)
        {
            unsigned int len = common_length( i, first_end, j, second_end );
            if (len > result->len)
            {
                result->len = len;
                result->pos_first = i;
                result->pos_second = j;
                ret = 1;
            }
        }
    }
    return ret;
}

static struct section *alloc_section( enum section_type type, unsigned int start, unsigned int len )
{
    struct section *ret = malloc( sizeof(*ret) );

    ret->type = type;
    ret->start = start;
    ret->len = len;
    return ret;
}

static unsigned int non_matching_lines;

static void diff( unsigned int first_start, unsigned int first_end,
                  unsigned int second_start, unsigned int second_end )
{
    struct common_subseq subseq;
    struct section *section;

    if (longest_common_subsequence( first_start, first_end, second_start, second_end, &subseq ))
    {
        /* recursively find sections before this one */
        diff( first_start, subseq.pos_first, second_start, subseq.pos_second );

        section = alloc_section( SECTION_TYPE_COPY, subseq.pos_first, subseq.len );
        list_add_tail( &sections, &section->entry );

        /* recursively find sections after this one */
        diff( subseq.pos_first + subseq.len, first_end, subseq.pos_second + subseq.len, second_end );
        return;
    }

    if (first_start < first_end)
    {
        section = alloc_section( SECTION_TYPE_DELETE, first_start, first_end - first_start );
        list_add_tail( &sections, &section->entry );
        non_matching_lines++;
    }

    if (second_start < second_end)
    {
        section = alloc_section( SECTION_TYPE_INSERT, second_start, second_end - second_start );
        list_add_tail( &sections, &section->entry );
        non_matching_lines++;
    }
}

static struct section *find_context_before( struct section *section )
{
    struct section *cursor = section;

    while ((cursor = LIST_ENTRY( list_prev(&sections, &cursor->entry), struct section, entry )))
    {
        if (cursor->type == SECTION_TYPE_COPY) return cursor;
    }
    return NULL;
}

static struct section *find_context_after( struct section *section )
{
    struct section *cursor = section;

    while ((cursor = LIST_ENTRY( list_next(&sections, &cursor->entry), struct section, entry )))
    {
        if (cursor->type == SECTION_TYPE_COPY) return cursor;
    }
    return NULL;
}

static void output_stringW( const WCHAR *str, int len )
{
    DWORD count;
    int lenA;
    char *strA;

    if (len < 0) len = wcslen( str );
    if (WriteConsoleW( GetStdHandle(STD_OUTPUT_HANDLE), str, len, &count, NULL )) return;

    lenA = WideCharToMultiByte( GetOEMCP(), 0, str, len, NULL, 0, NULL, NULL );
    if ((strA = malloc( lenA )))
    {
        WideCharToMultiByte( GetOEMCP(), 0, str, len, strA, lenA, NULL, NULL );
        WriteFile( GetStdHandle(STD_OUTPUT_HANDLE), strA, lenA, &count, FALSE );
        free( strA );
    }
}

static void output_stringA( const char *str, int len )
{
    DWORD count;

    if (len < 0) len = strlen( str );
    if (WriteConsoleA( GetStdHandle(STD_OUTPUT_HANDLE), str, len, &count, NULL )) return;
    WriteFile( GetStdHandle(STD_OUTPUT_HANDLE), str, len, &count, FALSE );
}

static void output_header( const WCHAR *filename )
{
    output_stringW( L"***** ", 6 );
    output_stringW( filename, -1 );
    output_stringW( L"\r\n", 2 );
}

static void output_line( const struct lines *lines, unsigned int index )
{
    output_stringA( lines->entry[index].start, lines->entry[index].len );
    output_stringA( "\r\n", 2 );
}

static void output_context( unsigned int line )
{
    if (lines1.count < 2 || lines2.count < 2) return;
    output_line( &lines1, line );
}

static char no_data[] = "";

static char *map_file( HANDLE handle, unsigned int *size )
{
    HANDLE mapping;
    char *ret;

    if (!(*size = GetFileSize( handle, NULL ))) return no_data;
    if (!(mapping = CreateFileMappingW( handle, NULL, PAGE_READONLY, 0, 0, NULL ))) return NULL;
    ret = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 0 );
    CloseHandle( mapping );
    return ret;
}

static int compare_files( const WCHAR *filename1, const WCHAR *filename2 )
{
    HANDLE handle1, handle2;
    struct section *section;
    unsigned int i;
    int ret = 2;

    handle1 = CreateFileW( filename1, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL );
    if (handle1 == INVALID_HANDLE_VALUE) return 2;

    handle2 = CreateFileW( filename2, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL );
    if (handle2 == INVALID_HANDLE_VALUE)
    {
        CloseHandle( handle2 );
        return 2;
    }

    if (!(data1 = map_file( handle1, &len_data1 ))) goto done;
    if (!(data2 = map_file( handle2, &len_data2 ))) goto done;

    split_lines( &lines1, data1, len_data1 );
    split_lines( &lines2, data2, len_data2 );

    diff( 0, lines1.count, 0, lines2.count );

    output_stringW( L"Comparing files ", 16 );
    output_stringW( filename1, -1 );
    output_stringW( L" and ", 5 );
    output_stringW( filename2, -1 );
    output_stringW( L"\r\n", 2 );

    if (!non_matching_lines)
    {
        output_stringW( L"FC: no differences encountered\r\n\r\n", -1 );
        ret = 0;
        goto done;
    }

    LIST_FOR_EACH_ENTRY( section, &sections, struct section, entry )
    {
        struct section *ctx_before = find_context_before( section ), *ctx_after = find_context_after( section );

        if (section->type == SECTION_TYPE_DELETE)
        {
            struct section *next_section = LIST_ENTRY( list_next(&sections, &section->entry), struct section, entry );

            output_header( filename1 );
            if (ctx_before) output_context( ctx_before->start + ctx_before->len - 1 );
            for (i = 0; i < section->len; i++) output_line( &lines1, section->start + i );
            if (ctx_after) output_context( ctx_after->start );

            if (next_section && next_section->type != SECTION_TYPE_INSERT)
            {
                output_header( filename2 );
                if (ctx_before) output_context( ctx_before->start + ctx_before->len - 1 );
                if (ctx_after) output_context( ctx_after->start );
            }
            else if (!next_section)
            {
                output_header( filename2 );
                output_stringW( L"*****\r\n\r\n", 9 );
            }
        }
        else if (section->type == SECTION_TYPE_INSERT)
        {
            struct section *prev_section = LIST_ENTRY( list_prev(&sections, &section->entry), struct section, entry );

            if (prev_section && prev_section->type != SECTION_TYPE_DELETE)
            {
                output_header( filename1 );
                if (ctx_before) output_context( ctx_before->start + ctx_before->len - 1 );
                if (ctx_after) output_context( ctx_after->start );
            }
            else if (!prev_section) output_header( filename1 );

            output_header( filename2 );
            if (ctx_before) output_context( ctx_before->start + ctx_before->len - 1 );
            for (i = 0; i < section->len; i++) output_line( &lines2, section->start + i );
            if (ctx_after) output_context( ctx_after->start );
            output_stringW( L"*****\r\n\r\n", 9 );
        }
    }

    ret = 1;

done:
    if (data1 != no_data) UnmapViewOfFile( data1 );
    if (data2 != no_data) UnmapViewOfFile( data2 );
    CloseHandle( handle1 );
    CloseHandle( handle2 );
    return ret;
}

int __cdecl wmain(int argc, WCHAR *argv[])
{
    BOOL unsupported = FALSE;
    const WCHAR *filename1 = NULL, *filename2 = NULL;
    int i;

    for (i = 1; i < argc; i++)
    {
        if (argv[i][0] == '/')
        {
            if (!wcsicmp( argv[i] + 1, L"c" )) option_case_insensitive = TRUE;
            else
            {
                FIXME( "option %s not supported\n", debugstr_w(argv[i]) );
                unsupported = TRUE;
            }
        }
        else if (!filename1) filename1 = argv[i];
        else if (!filename2) filename2 = argv[i];
        else
        {
            wprintf( L"FC: Wrong number of files %s\n", argv[i] );
            return 2;
        }
    }

    if (unsupported) return 2;

    if (!filename1 || !filename2)
    {
        wprintf( L"FC: Wrong number of files\n" );
        return 2;
    }

    if (wcschr( filename1, '?' ) || wcschr( filename1, '*' ) || wcschr( filename2, '?' ) || wcschr( filename2, '*' ))
    {
        FIXME( "wildcards not supported\n" );
        return 2;
    }

    return compare_files( filename1, filename2 );
}