282 lines
7.2 KiB
C
282 lines
7.2 KiB
C
/*
|
|
* CDE - Common Desktop Environment
|
|
*
|
|
* Copyright (c) 1993-2012, The Open Group. All rights reserved.
|
|
*
|
|
* These libraries and programs are free software; you can
|
|
* redistribute them and/or modify them under the terms of the GNU
|
|
* Lesser General Public License as published by the Free Software
|
|
* Foundation; either version 2 of the License, or (at your option)
|
|
* any later version.
|
|
*
|
|
* These libraries and programs are distributed in the hope that
|
|
* they 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 these librararies and programs; if not, write
|
|
* to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
|
|
* Floor, Boston, MA 02110-1301 USA
|
|
*/
|
|
/* $XConsortium: cmp.c /main/3 1995/11/01 19:04:41 rswiston $ */
|
|
/***************************************************************
|
|
* *
|
|
* AT&T - PROPRIETARY *
|
|
* *
|
|
* THIS IS PROPRIETARY SOURCE CODE LICENSED BY *
|
|
* AT&T CORP. *
|
|
* *
|
|
* Copyright (c) 1995 AT&T Corp. *
|
|
* All Rights Reserved *
|
|
* *
|
|
* This software is licensed by AT&T Corp. *
|
|
* under the terms and conditions of the license in *
|
|
* http://www.research.att.com/orgs/ssr/book/reuse *
|
|
* *
|
|
* This software was created by the *
|
|
* Software Engineering Research Department *
|
|
* AT&T Bell Laboratories *
|
|
* *
|
|
* For further information contact *
|
|
* gsf@research.att.com *
|
|
* *
|
|
***************************************************************/
|
|
|
|
/* : : generated by proto : : */
|
|
|
|
#if !defined(__PROTO__)
|
|
#if defined(__STDC__) || defined(__cplusplus) || defined(_proto) || defined(c_plusplus)
|
|
#if defined(__cplusplus)
|
|
#define __MANGLE__ "C"
|
|
#else
|
|
#define __MANGLE__
|
|
#endif
|
|
#define __STDARG__
|
|
#define __PROTO__(x) x
|
|
#define __OTORP__(x)
|
|
#define __PARAM__(n,o) n
|
|
#if !defined(__STDC__) && !defined(__cplusplus)
|
|
#if !defined(c_plusplus)
|
|
#define const
|
|
#endif
|
|
#define signed
|
|
#define void int
|
|
#define volatile
|
|
#define __V_ char
|
|
#else
|
|
#define __V_ void
|
|
#endif
|
|
#else
|
|
#define __PROTO__(x) ()
|
|
#define __OTORP__(x) x
|
|
#define __PARAM__(n,o) o
|
|
#define __MANGLE__
|
|
#define __V_ char
|
|
#define const
|
|
#define signed
|
|
#define void int
|
|
#define volatile
|
|
#endif
|
|
#if defined(__cplusplus) || defined(c_plusplus)
|
|
#define __VARARG__ ...
|
|
#else
|
|
#define __VARARG__
|
|
#endif
|
|
#if defined(__STDARG__)
|
|
#define __VA_START__(p,a) va_start(p,a)
|
|
#else
|
|
#define __VA_START__(p,a) va_start(p)
|
|
#endif
|
|
#endif
|
|
static const char id[] = "\n@(#)cmp (AT&T Bell Laboratories) 07/17/94\0\n";
|
|
|
|
#include <cmdlib.h>
|
|
#include <ls.h>
|
|
|
|
#define CMP_VERBOSE 1
|
|
#define CMP_SILENT 2
|
|
|
|
/*
|
|
* compare two files
|
|
*/
|
|
|
|
static int
|
|
cmp __PARAM__((const char* file1, Sfio_t* f1, const char* file2, Sfio_t* f2, int flags), (file1, f1, file2, f2, flags)) __OTORP__(const char* file1; Sfio_t* f1; const char* file2; Sfio_t* f2; int flags;){
|
|
register int c1;
|
|
register int c2;
|
|
register unsigned char* p1 = 0;
|
|
register unsigned char* p2 = 0;
|
|
register int lines = 1;
|
|
register unsigned char* e1 = 0;
|
|
register unsigned char* e2 = 0;
|
|
unsigned long pos = 0;
|
|
int ret = 0;
|
|
unsigned char* last;
|
|
|
|
for (;;)
|
|
{
|
|
if ((c1 = e1 - p1) <= 0)
|
|
{
|
|
if (!(p1 = (unsigned char*)sfreserve(f1, SF_UNBOUND, 0)) || (c1 = sfslen()) <= 0)
|
|
{
|
|
if ((e2 - p2) > 0 || sfreserve(f2, SF_UNBOUND, 0) && sfslen() > 0)
|
|
{
|
|
ret = 1;
|
|
if (!(flags & CMP_SILENT))
|
|
error(ERROR_exit(1), "%s: EOF", file1);
|
|
}
|
|
return(ret);
|
|
}
|
|
e1 = p1 + c1;
|
|
}
|
|
if ((c2 = e2 - p2) <= 0)
|
|
{
|
|
if (!(p2 = (unsigned char*)sfreserve(f2, SF_UNBOUND, 0)) || (c2 = sfslen()) <= 0)
|
|
{
|
|
if (!(flags & CMP_SILENT))
|
|
error(ERROR_exit(1), "%s: EOF", file2);
|
|
return(1);
|
|
}
|
|
e2 = p2 + c2;
|
|
}
|
|
if (c1 > c2)
|
|
c1 = c2;
|
|
pos += c1;
|
|
if (flags & CMP_SILENT)
|
|
{
|
|
if (memcmp(p1, p2, c1))
|
|
return(1);
|
|
p1 += c1;
|
|
p2 += c1;
|
|
}
|
|
else
|
|
{
|
|
last = p1 + c1;
|
|
while (p1 < last)
|
|
{
|
|
if ((c1 = *p1++) != *p2++)
|
|
{
|
|
if (flags)
|
|
{
|
|
ret = 1;
|
|
sfprintf(sfstdout, "%6ld %3o %3o\n", pos - (last - p1), c1, *(p2 - 1));
|
|
}
|
|
else
|
|
{
|
|
sfprintf(sfstdout, "%s %s differ: char %ld, line %d\n", file1, file2, pos - (last - p1), lines);
|
|
return(1);
|
|
}
|
|
}
|
|
if (c1 == '\n')
|
|
lines++;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
int
|
|
b_cmp __PARAM__((int argc, register char** argv), (argc, argv)) __OTORP__(int argc; register char** argv;){
|
|
char* s;
|
|
char* e;
|
|
Sfio_t* f1 = 0;
|
|
Sfio_t* f2 = 0;
|
|
char* file1;
|
|
char* file2;
|
|
int n;
|
|
off_t o1 = 0;
|
|
off_t o2 = 0;
|
|
struct stat s1;
|
|
struct stat s2;
|
|
|
|
int flags = 0;
|
|
|
|
NoP(argc);
|
|
NoP(id[0]);
|
|
cmdinit(argv);
|
|
while (n = optget(argv, "ls file1 file2 [skip1] [skip2]")) switch (n)
|
|
{
|
|
case 'l':
|
|
flags |= CMP_VERBOSE;
|
|
break;
|
|
case 's':
|
|
flags |= CMP_SILENT;
|
|
break;
|
|
case ':':
|
|
error(2, opt_info.arg);
|
|
break;
|
|
case '?':
|
|
error(ERROR_usage(2), opt_info.arg);
|
|
break;
|
|
}
|
|
argv += opt_info.index;
|
|
if (error_info.errors || !(file1 = *argv++) || !(file2 = *argv++))
|
|
error(ERROR_usage(2), optusage(NiL));
|
|
n = 2;
|
|
if (streq(file1, "-"))
|
|
f1 = sfstdin;
|
|
else if (!(f1 = sfopen(NiL, file1, "r")))
|
|
{
|
|
if (!(flags & CMP_SILENT))
|
|
error(ERROR_system(0), "%s: cannot open", file1);
|
|
goto done;
|
|
}
|
|
if (streq(file2, "-"))
|
|
f2 = sfstdin;
|
|
else if (!(f2 = sfopen(NiL, file2, "r")))
|
|
{
|
|
if (!(flags & CMP_SILENT))
|
|
error(ERROR_system(0), "%s: cannot open", file2);
|
|
goto done;
|
|
}
|
|
if (s = *argv++)
|
|
{
|
|
o1 = strtol(s, &e, 0);
|
|
if (*e)
|
|
{
|
|
error(ERROR_exit(0), "%s: %s: invalid skip", file1, s);
|
|
goto done;
|
|
}
|
|
if (sfseek(f1, o1, 0) != o1)
|
|
{
|
|
if (!(flags & CMP_SILENT))
|
|
error(ERROR_exit(0), "%s: EOF", file1);
|
|
n = 1;
|
|
goto done;
|
|
}
|
|
if (s = *argv++)
|
|
{
|
|
o2 = strtol(s, &e, 0);
|
|
if (*e)
|
|
{
|
|
error(ERROR_exit(0), "%s: %s: invalid skip", file2, s);
|
|
goto done;
|
|
}
|
|
if (sfseek(f2, o2, 0) != o2)
|
|
{
|
|
if (!(flags & CMP_SILENT))
|
|
error(ERROR_exit(0), "%s: EOF", file2);
|
|
n = 1;
|
|
goto done;
|
|
}
|
|
}
|
|
if (*argv)
|
|
{
|
|
error(ERROR_usage(0), optusage(NiL));
|
|
goto done;
|
|
}
|
|
}
|
|
if (fstat(sffileno(f1), &s1))
|
|
error(ERROR_system(0), "%s: cannot stat", file1);
|
|
else if (fstat(sffileno(f2), &s2))
|
|
error(ERROR_system(0), "%s: cannot stat", file1);
|
|
else if (s1.st_ino == s2.st_ino && s1.st_dev == s2.st_dev && o1 == o2)
|
|
n = 0;
|
|
else n = ((flags & CMP_SILENT) && S_ISREG(s1.st_mode) && S_ISREG(s2.st_mode) && (s1.st_size - o1) != (s2.st_size - o2)) ? 1 : cmp(file1, f1, file2, f2, flags);
|
|
done:
|
|
if (f1 && f1 != sfstdin) sfclose(f1);
|
|
if (f2 && f2 != sfstdin) sfclose(f2);
|
|
return(n);
|
|
}
|