#if 0
#ifndef lint
static char sccsid[] = "@(#)cmp.c 8.1 (Berkeley) 5/31/93";
#endif
#endif
#include <sys/cdefs.h>
__RCSID("$FreeBSD: src/bin/ls/cmp.c,v 1.12 2002/06/30 05:13:54 obrien Exp $");
#include <sys/types.h>
#include <sys/stat.h>
#include <fts.h>
#include <string.h>
#include "ls.h"
#include "extern.h"
#if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) || \
defined(_XOPEN_SOURCE) || defined(__NetBSD__)
#define ATIMENSEC_CMP(x, op, y) ((x)->st_atimensec op (y)->st_atimensec)
#define CTIMENSEC_CMP(x, op, y) ((x)->st_ctimensec op (y)->st_ctimensec)
#define MTIMENSEC_CMP(x, op, y) ((x)->st_mtimensec op (y)->st_mtimensec)
#define BTIMENSEC_CMP(x, op, y) ((x)->st_birthtimensec op (y)->st_birthtimensec)
#else
#define ATIMENSEC_CMP(x, op, y) \
((x)->st_atimespec.tv_nsec op (y)->st_atimespec.tv_nsec)
#define CTIMENSEC_CMP(x, op, y) \
((x)->st_ctimespec.tv_nsec op (y)->st_ctimespec.tv_nsec)
#define MTIMENSEC_CMP(x, op, y) \
((x)->st_mtimespec.tv_nsec op (y)->st_mtimespec.tv_nsec)
#define BTIMENSEC_CMP(x, op, y) \
((x)->st_birthtimespec.tv_nsec op (y)->st_birthtimespec.tv_nsec)
#endif
int
namecmp(const FTSENT *a, const FTSENT *b)
{
return (strcoll(a->fts_name, b->fts_name));
}
int
revnamecmp(const FTSENT *a, const FTSENT *b)
{
return (strcoll(b->fts_name, a->fts_name));
}
int
modcmp(const FTSENT *a, const FTSENT *b)
{
if (b->fts_statp->st_mtime > a->fts_statp->st_mtime)
return (1);
else if (b->fts_statp->st_mtime < a->fts_statp->st_mtime)
return (-1);
else if (MTIMENSEC_CMP(b->fts_statp, >, a->fts_statp))
return (1);
else if (MTIMENSEC_CMP(b->fts_statp, <, a->fts_statp))
return (-1);
else
return (namecmp(a, b));
}
int
revmodcmp(const FTSENT *a, const FTSENT *b)
{
if (b->fts_statp->st_mtime > a->fts_statp->st_mtime)
return (-1);
else if (b->fts_statp->st_mtime < a->fts_statp->st_mtime)
return (1);
else if (MTIMENSEC_CMP(b->fts_statp, >, a->fts_statp))
return (-1);
else if (MTIMENSEC_CMP(b->fts_statp, <, a->fts_statp))
return (1);
else
return (revnamecmp(a, b));
}
int
acccmp(const FTSENT *a, const FTSENT *b)
{
if (b->fts_statp->st_atime > a->fts_statp->st_atime)
return (1);
else if (b->fts_statp->st_atime < a->fts_statp->st_atime)
return (-1);
else if (ATIMENSEC_CMP(b->fts_statp, >, a->fts_statp))
return (1);
else if (ATIMENSEC_CMP(b->fts_statp, <, a->fts_statp))
return (-1);
else
return (namecmp(a, b));
}
int
revacccmp(const FTSENT *a, const FTSENT *b)
{
if (b->fts_statp->st_atime > a->fts_statp->st_atime)
return (-1);
else if (b->fts_statp->st_atime < a->fts_statp->st_atime)
return (1);
else if (ATIMENSEC_CMP(b->fts_statp, >, a->fts_statp))
return (-1);
else if (ATIMENSEC_CMP(b->fts_statp, <, a->fts_statp))
return (1);
else
return (revnamecmp(a, b));
}
int
statcmp(const FTSENT *a, const FTSENT *b)
{
if (b->fts_statp->st_ctime > a->fts_statp->st_ctime)
return (1);
else if (b->fts_statp->st_ctime < a->fts_statp->st_ctime)
return (-1);
else if (CTIMENSEC_CMP(b->fts_statp, >, a->fts_statp))
return (1);
else if (CTIMENSEC_CMP(b->fts_statp, <, a->fts_statp))
return (-1);
else
return (namecmp(a, b));
}
int
revstatcmp(const FTSENT *a, const FTSENT *b)
{
if (b->fts_statp->st_ctime > a->fts_statp->st_ctime)
return (-1);
else if (b->fts_statp->st_ctime < a->fts_statp->st_ctime)
return (1);
else if (CTIMENSEC_CMP(b->fts_statp, >, a->fts_statp))
return (-1);
else if (CTIMENSEC_CMP(b->fts_statp, <, a->fts_statp))
return (1);
else
return (revnamecmp(a, b));
}
int
sizecmp(a, b)
const FTSENT *a, *b;
{
if (b->fts_statp->st_size > a->fts_statp->st_size)
return (1);
if (b->fts_statp->st_size < a->fts_statp->st_size)
return (-1);
else
return (namecmp(a, b));
}
int
revsizecmp(a, b)
const FTSENT *a, *b;
{
if (b->fts_statp->st_size > a->fts_statp->st_size)
return (-1);
if (b->fts_statp->st_size < a->fts_statp->st_size)
return (1);
else
return (revnamecmp(a, b));
}
int
birthcmp(const FTSENT *a, const FTSENT *b)
{
if (b->fts_statp->st_birthtime > a->fts_statp->st_birthtime)
return (1);
else if (b->fts_statp->st_birthtime < a->fts_statp->st_birthtime)
return (-1);
else if (BTIMENSEC_CMP(b->fts_statp, >, a->fts_statp))
return (1);
else if (BTIMENSEC_CMP(b->fts_statp, <, a->fts_statp))
return (-1);
else
return (namecmp(a, b));
}
int
revbirthcmp(const FTSENT *a, const FTSENT *b)
{
if (b->fts_statp->st_birthtime > a->fts_statp->st_birthtime)
return (-1);
else if (b->fts_statp->st_birthtime < a->fts_statp->st_birthtime)
return (1);
else if (BTIMENSEC_CMP(b->fts_statp, >, a->fts_statp))
return (-1);
else if (BTIMENSEC_CMP(b->fts_statp, <, a->fts_statp))
return (1);
else
return (revnamecmp(a, b));
}