Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:19

0001 /*  $NetBSD: cmp.c,v 1.17 2003/08/07 09:05:14 agc Exp $ */
0002 
0003 /*
0004  * Copyright (c) 1989, 1993
0005  *  The Regents of the University of California.  All rights reserved.
0006  *
0007  * This code is derived from software contributed to Berkeley by
0008  * Michael Fischbein.
0009  *
0010  * Redistribution and use in source and binary forms, with or without
0011  * modification, are permitted provided that the following conditions
0012  * are met:
0013  * 1. Redistributions of source code must retain the above copyright
0014  *    notice, this list of conditions and the following disclaimer.
0015  * 2. Redistributions in binary form must reproduce the above copyright
0016  *    notice, this list of conditions and the following disclaimer in the
0017  *    documentation and/or other materials provided with the distribution.
0018  * 3. Neither the name of the University nor the names of its contributors
0019  *    may be used to endorse or promote products derived from this software
0020  *    without specific prior written permission.
0021  *
0022  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
0023  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0024  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0025  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
0026  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
0027  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
0028  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
0029  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
0030  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
0031  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
0032  * SUCH DAMAGE.
0033  */
0034 
0035 #ifdef HAVE_CONFIG_H
0036 #include "config.h"
0037 #endif
0038 
0039 #if 0
0040 #include <sys/cdefs.h>
0041 #ifndef lint
0042 #if 0
0043 static char sccsid[] = "@(#)cmp.c   8.1 (Berkeley) 5/31/93";
0044 #else
0045 __RCSID("$NetBSD: cmp.c,v 1.17 2003/08/07 09:05:14 agc Exp $");
0046 #endif
0047 #endif /* not lint */
0048 #endif
0049 
0050 #include <sys/types.h>
0051 #include <sys/stat.h>
0052 
0053 #include "fts.h"
0054 #include <string.h>
0055 
0056 #include "extern-ls.h"
0057 
0058 #if defined(__rtems__) || defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) || \
0059     defined(_XOPEN_SOURCE) || defined(__NetBSD__)
0060 #define ATIMENSEC_CMP(x, op, y) ((x)->st_atime op (y)->st_atime)
0061 #define CTIMENSEC_CMP(x, op, y) ((x)->st_ctime op (y)->st_ctime)
0062 #define MTIMENSEC_CMP(x, op, y) ((x)->st_mtime op (y)->st_mtime)
0063 #else
0064 #define ATIMENSEC_CMP(x, op, y) \
0065     ((x)->st_atime.tv_nsec op (y)->st_atime.tv_nsec)
0066 #define CTIMENSEC_CMP(x, op, y) \
0067     ((x)->st_ctime.tv_nsec op (y)->st_ctime.tv_nsec)
0068 #define MTIMENSEC_CMP(x, op, y) \
0069     ((x)->st_mtime.tv_nsec op (y)->st_mtime.tv_nsec)
0070 #endif
0071 
0072 int
0073 namecmp(const FTSENT *a, const FTSENT *b)
0074 {
0075 
0076     return (strcmp(a->fts_name, b->fts_name));
0077 }
0078 
0079 int
0080 revnamecmp(const FTSENT *a, const FTSENT *b)
0081 {
0082 
0083     return (strcmp(b->fts_name, a->fts_name));
0084 }
0085 
0086 int
0087 modcmp(const FTSENT *a, const FTSENT *b)
0088 {
0089 
0090     if (b->fts_statp->st_mtime > a->fts_statp->st_mtime)
0091         return (1);
0092     else if (b->fts_statp->st_mtime < a->fts_statp->st_mtime)
0093         return (-1);
0094     else if (MTIMENSEC_CMP(b->fts_statp, >, a->fts_statp))
0095         return (1);
0096     else if (MTIMENSEC_CMP(b->fts_statp, <, a->fts_statp))
0097         return (-1);
0098     else
0099         return (namecmp(a, b));
0100 }
0101 
0102 int
0103 revmodcmp(const FTSENT *a, const FTSENT *b)
0104 {
0105 
0106     if (b->fts_statp->st_mtime > a->fts_statp->st_mtime)
0107         return (-1);
0108     else if (b->fts_statp->st_mtime < a->fts_statp->st_mtime)
0109         return (1);
0110     else if (MTIMENSEC_CMP(b->fts_statp, >, a->fts_statp))
0111         return (-1);
0112     else if (MTIMENSEC_CMP(b->fts_statp, <, a->fts_statp))
0113         return (1);
0114     else
0115         return (revnamecmp(a, b));
0116 }
0117 
0118 int
0119 acccmp(const FTSENT *a, const FTSENT *b)
0120 {
0121 
0122     if (b->fts_statp->st_atime > a->fts_statp->st_atime)
0123         return (1);
0124     else if (b->fts_statp->st_atime < a->fts_statp->st_atime)
0125         return (-1);
0126     else if (ATIMENSEC_CMP(b->fts_statp, >, a->fts_statp))
0127         return (1);
0128     else if (ATIMENSEC_CMP(b->fts_statp, <, a->fts_statp))
0129         return (-1);
0130     else
0131         return (namecmp(a, b));
0132 }
0133 
0134 int
0135 revacccmp(const FTSENT *a, const FTSENT *b)
0136 {
0137 
0138     if (b->fts_statp->st_atime > a->fts_statp->st_atime)
0139         return (-1);
0140     else if (b->fts_statp->st_atime < a->fts_statp->st_atime)
0141         return (1);
0142     else if (ATIMENSEC_CMP(b->fts_statp, >, a->fts_statp))
0143         return (-1);
0144     else if (ATIMENSEC_CMP(b->fts_statp, <, a->fts_statp))
0145         return (1);
0146     else
0147         return (revnamecmp(a, b));
0148 }
0149 
0150 int
0151 statcmp(const FTSENT *a, const FTSENT *b)
0152 {
0153 
0154     if (b->fts_statp->st_ctime > a->fts_statp->st_ctime)
0155         return (1);
0156     else if (b->fts_statp->st_ctime < a->fts_statp->st_ctime)
0157         return (-1);
0158     else if (CTIMENSEC_CMP(b->fts_statp, >, a->fts_statp))
0159         return (1);
0160     else if (CTIMENSEC_CMP(b->fts_statp, <, a->fts_statp))
0161         return (-1);
0162     else
0163         return (namecmp(a, b));
0164 }
0165 
0166 int
0167 revstatcmp(const FTSENT *a, const FTSENT *b)
0168 {
0169 
0170     if (b->fts_statp->st_ctime > a->fts_statp->st_ctime)
0171         return (-1);
0172     else if (b->fts_statp->st_ctime < a->fts_statp->st_ctime)
0173         return (1);
0174     else if (CTIMENSEC_CMP(b->fts_statp, >, a->fts_statp))
0175         return (-1);
0176     else if (CTIMENSEC_CMP(b->fts_statp, <, a->fts_statp))
0177         return (1);
0178     else
0179         return (revnamecmp(a, b));
0180 }
0181 
0182 int
0183 sizecmp(const FTSENT *a, const FTSENT *b)
0184 {
0185 
0186     if (b->fts_statp->st_size > a->fts_statp->st_size)
0187         return (1);
0188     if (b->fts_statp->st_size < a->fts_statp->st_size)
0189         return (-1);
0190     else
0191         return (namecmp(a, b));
0192 }
0193 
0194 int
0195 revsizecmp(const FTSENT *a, const FTSENT *b)
0196 {
0197 
0198     if (b->fts_statp->st_size > a->fts_statp->st_size)
0199         return (-1);
0200     if (b->fts_statp->st_size < a->fts_statp->st_size)
0201         return (1);
0202     else
0203         return (revnamecmp(a, b));
0204 }