1*b7ef8cfbSLionel Sambuc /* $NetBSD: cmp.c,v 1.17 2003/08/07 09:05:14 agc Exp $ */
2*b7ef8cfbSLionel Sambuc
3*b7ef8cfbSLionel Sambuc /*
4*b7ef8cfbSLionel Sambuc * Copyright (c) 1989, 1993
5*b7ef8cfbSLionel Sambuc * The Regents of the University of California. All rights reserved.
6*b7ef8cfbSLionel Sambuc *
7*b7ef8cfbSLionel Sambuc * This code is derived from software contributed to Berkeley by
8*b7ef8cfbSLionel Sambuc * Michael Fischbein.
9*b7ef8cfbSLionel Sambuc *
10*b7ef8cfbSLionel Sambuc * Redistribution and use in source and binary forms, with or without
11*b7ef8cfbSLionel Sambuc * modification, are permitted provided that the following conditions
12*b7ef8cfbSLionel Sambuc * are met:
13*b7ef8cfbSLionel Sambuc * 1. Redistributions of source code must retain the above copyright
14*b7ef8cfbSLionel Sambuc * notice, this list of conditions and the following disclaimer.
15*b7ef8cfbSLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
16*b7ef8cfbSLionel Sambuc * notice, this list of conditions and the following disclaimer in the
17*b7ef8cfbSLionel Sambuc * documentation and/or other materials provided with the distribution.
18*b7ef8cfbSLionel Sambuc * 3. Neither the name of the University nor the names of its contributors
19*b7ef8cfbSLionel Sambuc * may be used to endorse or promote products derived from this software
20*b7ef8cfbSLionel Sambuc * without specific prior written permission.
21*b7ef8cfbSLionel Sambuc *
22*b7ef8cfbSLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23*b7ef8cfbSLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24*b7ef8cfbSLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25*b7ef8cfbSLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26*b7ef8cfbSLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27*b7ef8cfbSLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28*b7ef8cfbSLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29*b7ef8cfbSLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30*b7ef8cfbSLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31*b7ef8cfbSLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32*b7ef8cfbSLionel Sambuc * SUCH DAMAGE.
33*b7ef8cfbSLionel Sambuc */
34*b7ef8cfbSLionel Sambuc
35*b7ef8cfbSLionel Sambuc #include <sys/cdefs.h>
36*b7ef8cfbSLionel Sambuc #ifndef lint
37*b7ef8cfbSLionel Sambuc #if 0
38*b7ef8cfbSLionel Sambuc static char sccsid[] = "@(#)cmp.c 8.1 (Berkeley) 5/31/93";
39*b7ef8cfbSLionel Sambuc #else
40*b7ef8cfbSLionel Sambuc __RCSID("$NetBSD: cmp.c,v 1.17 2003/08/07 09:05:14 agc Exp $");
41*b7ef8cfbSLionel Sambuc #endif
42*b7ef8cfbSLionel Sambuc #endif /* not lint */
43*b7ef8cfbSLionel Sambuc
44*b7ef8cfbSLionel Sambuc #include <sys/types.h>
45*b7ef8cfbSLionel Sambuc #include <sys/stat.h>
46*b7ef8cfbSLionel Sambuc
47*b7ef8cfbSLionel Sambuc #include <fts.h>
48*b7ef8cfbSLionel Sambuc #include <string.h>
49*b7ef8cfbSLionel Sambuc
50*b7ef8cfbSLionel Sambuc #include "ls.h"
51*b7ef8cfbSLionel Sambuc #include "extern.h"
52*b7ef8cfbSLionel Sambuc
53*b7ef8cfbSLionel Sambuc #if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) || \
54*b7ef8cfbSLionel Sambuc defined(_XOPEN_SOURCE) || defined(__NetBSD__)
55*b7ef8cfbSLionel Sambuc #define ATIMENSEC_CMP(x, op, y) ((x)->st_atimensec op (y)->st_atimensec)
56*b7ef8cfbSLionel Sambuc #define CTIMENSEC_CMP(x, op, y) ((x)->st_ctimensec op (y)->st_ctimensec)
57*b7ef8cfbSLionel Sambuc #define MTIMENSEC_CMP(x, op, y) ((x)->st_mtimensec op (y)->st_mtimensec)
58*b7ef8cfbSLionel Sambuc #else
59*b7ef8cfbSLionel Sambuc #define ATIMENSEC_CMP(x, op, y) \
60*b7ef8cfbSLionel Sambuc ((x)->st_atimespec.tv_nsec op (y)->st_atimespec.tv_nsec)
61*b7ef8cfbSLionel Sambuc #define CTIMENSEC_CMP(x, op, y) \
62*b7ef8cfbSLionel Sambuc ((x)->st_ctimespec.tv_nsec op (y)->st_ctimespec.tv_nsec)
63*b7ef8cfbSLionel Sambuc #define MTIMENSEC_CMP(x, op, y) \
64*b7ef8cfbSLionel Sambuc ((x)->st_mtimespec.tv_nsec op (y)->st_mtimespec.tv_nsec)
65*b7ef8cfbSLionel Sambuc #endif
66*b7ef8cfbSLionel Sambuc
67*b7ef8cfbSLionel Sambuc int
namecmp(const FTSENT * a,const FTSENT * b)68*b7ef8cfbSLionel Sambuc namecmp(const FTSENT *a, const FTSENT *b)
69*b7ef8cfbSLionel Sambuc {
70*b7ef8cfbSLionel Sambuc
71*b7ef8cfbSLionel Sambuc return (strcmp(a->fts_name, b->fts_name));
72*b7ef8cfbSLionel Sambuc }
73*b7ef8cfbSLionel Sambuc
74*b7ef8cfbSLionel Sambuc int
revnamecmp(const FTSENT * a,const FTSENT * b)75*b7ef8cfbSLionel Sambuc revnamecmp(const FTSENT *a, const FTSENT *b)
76*b7ef8cfbSLionel Sambuc {
77*b7ef8cfbSLionel Sambuc
78*b7ef8cfbSLionel Sambuc return (strcmp(b->fts_name, a->fts_name));
79*b7ef8cfbSLionel Sambuc }
80*b7ef8cfbSLionel Sambuc
81*b7ef8cfbSLionel Sambuc int
modcmp(const FTSENT * a,const FTSENT * b)82*b7ef8cfbSLionel Sambuc modcmp(const FTSENT *a, const FTSENT *b)
83*b7ef8cfbSLionel Sambuc {
84*b7ef8cfbSLionel Sambuc
85*b7ef8cfbSLionel Sambuc if (b->fts_statp->st_mtime > a->fts_statp->st_mtime)
86*b7ef8cfbSLionel Sambuc return (1);
87*b7ef8cfbSLionel Sambuc else if (b->fts_statp->st_mtime < a->fts_statp->st_mtime)
88*b7ef8cfbSLionel Sambuc return (-1);
89*b7ef8cfbSLionel Sambuc else if (MTIMENSEC_CMP(b->fts_statp, >, a->fts_statp))
90*b7ef8cfbSLionel Sambuc return (1);
91*b7ef8cfbSLionel Sambuc else if (MTIMENSEC_CMP(b->fts_statp, <, a->fts_statp))
92*b7ef8cfbSLionel Sambuc return (-1);
93*b7ef8cfbSLionel Sambuc else
94*b7ef8cfbSLionel Sambuc return (namecmp(a, b));
95*b7ef8cfbSLionel Sambuc }
96*b7ef8cfbSLionel Sambuc
97*b7ef8cfbSLionel Sambuc int
revmodcmp(const FTSENT * a,const FTSENT * b)98*b7ef8cfbSLionel Sambuc revmodcmp(const FTSENT *a, const FTSENT *b)
99*b7ef8cfbSLionel Sambuc {
100*b7ef8cfbSLionel Sambuc
101*b7ef8cfbSLionel Sambuc if (b->fts_statp->st_mtime > a->fts_statp->st_mtime)
102*b7ef8cfbSLionel Sambuc return (-1);
103*b7ef8cfbSLionel Sambuc else if (b->fts_statp->st_mtime < a->fts_statp->st_mtime)
104*b7ef8cfbSLionel Sambuc return (1);
105*b7ef8cfbSLionel Sambuc else if (MTIMENSEC_CMP(b->fts_statp, >, a->fts_statp))
106*b7ef8cfbSLionel Sambuc return (-1);
107*b7ef8cfbSLionel Sambuc else if (MTIMENSEC_CMP(b->fts_statp, <, a->fts_statp))
108*b7ef8cfbSLionel Sambuc return (1);
109*b7ef8cfbSLionel Sambuc else
110*b7ef8cfbSLionel Sambuc return (revnamecmp(a, b));
111*b7ef8cfbSLionel Sambuc }
112*b7ef8cfbSLionel Sambuc
113*b7ef8cfbSLionel Sambuc int
acccmp(const FTSENT * a,const FTSENT * b)114*b7ef8cfbSLionel Sambuc acccmp(const FTSENT *a, const FTSENT *b)
115*b7ef8cfbSLionel Sambuc {
116*b7ef8cfbSLionel Sambuc
117*b7ef8cfbSLionel Sambuc if (b->fts_statp->st_atime > a->fts_statp->st_atime)
118*b7ef8cfbSLionel Sambuc return (1);
119*b7ef8cfbSLionel Sambuc else if (b->fts_statp->st_atime < a->fts_statp->st_atime)
120*b7ef8cfbSLionel Sambuc return (-1);
121*b7ef8cfbSLionel Sambuc else if (ATIMENSEC_CMP(b->fts_statp, >, a->fts_statp))
122*b7ef8cfbSLionel Sambuc return (1);
123*b7ef8cfbSLionel Sambuc else if (ATIMENSEC_CMP(b->fts_statp, <, a->fts_statp))
124*b7ef8cfbSLionel Sambuc return (-1);
125*b7ef8cfbSLionel Sambuc else
126*b7ef8cfbSLionel Sambuc return (namecmp(a, b));
127*b7ef8cfbSLionel Sambuc }
128*b7ef8cfbSLionel Sambuc
129*b7ef8cfbSLionel Sambuc int
revacccmp(const FTSENT * a,const FTSENT * b)130*b7ef8cfbSLionel Sambuc revacccmp(const FTSENT *a, const FTSENT *b)
131*b7ef8cfbSLionel Sambuc {
132*b7ef8cfbSLionel Sambuc
133*b7ef8cfbSLionel Sambuc if (b->fts_statp->st_atime > a->fts_statp->st_atime)
134*b7ef8cfbSLionel Sambuc return (-1);
135*b7ef8cfbSLionel Sambuc else if (b->fts_statp->st_atime < a->fts_statp->st_atime)
136*b7ef8cfbSLionel Sambuc return (1);
137*b7ef8cfbSLionel Sambuc else if (ATIMENSEC_CMP(b->fts_statp, >, a->fts_statp))
138*b7ef8cfbSLionel Sambuc return (-1);
139*b7ef8cfbSLionel Sambuc else if (ATIMENSEC_CMP(b->fts_statp, <, a->fts_statp))
140*b7ef8cfbSLionel Sambuc return (1);
141*b7ef8cfbSLionel Sambuc else
142*b7ef8cfbSLionel Sambuc return (revnamecmp(a, b));
143*b7ef8cfbSLionel Sambuc }
144*b7ef8cfbSLionel Sambuc
145*b7ef8cfbSLionel Sambuc int
statcmp(const FTSENT * a,const FTSENT * b)146*b7ef8cfbSLionel Sambuc statcmp(const FTSENT *a, const FTSENT *b)
147*b7ef8cfbSLionel Sambuc {
148*b7ef8cfbSLionel Sambuc
149*b7ef8cfbSLionel Sambuc if (b->fts_statp->st_ctime > a->fts_statp->st_ctime)
150*b7ef8cfbSLionel Sambuc return (1);
151*b7ef8cfbSLionel Sambuc else if (b->fts_statp->st_ctime < a->fts_statp->st_ctime)
152*b7ef8cfbSLionel Sambuc return (-1);
153*b7ef8cfbSLionel Sambuc else if (CTIMENSEC_CMP(b->fts_statp, >, a->fts_statp))
154*b7ef8cfbSLionel Sambuc return (1);
155*b7ef8cfbSLionel Sambuc else if (CTIMENSEC_CMP(b->fts_statp, <, a->fts_statp))
156*b7ef8cfbSLionel Sambuc return (-1);
157*b7ef8cfbSLionel Sambuc else
158*b7ef8cfbSLionel Sambuc return (namecmp(a, b));
159*b7ef8cfbSLionel Sambuc }
160*b7ef8cfbSLionel Sambuc
161*b7ef8cfbSLionel Sambuc int
revstatcmp(const FTSENT * a,const FTSENT * b)162*b7ef8cfbSLionel Sambuc revstatcmp(const FTSENT *a, const FTSENT *b)
163*b7ef8cfbSLionel Sambuc {
164*b7ef8cfbSLionel Sambuc
165*b7ef8cfbSLionel Sambuc if (b->fts_statp->st_ctime > a->fts_statp->st_ctime)
166*b7ef8cfbSLionel Sambuc return (-1);
167*b7ef8cfbSLionel Sambuc else if (b->fts_statp->st_ctime < a->fts_statp->st_ctime)
168*b7ef8cfbSLionel Sambuc return (1);
169*b7ef8cfbSLionel Sambuc else if (CTIMENSEC_CMP(b->fts_statp, >, a->fts_statp))
170*b7ef8cfbSLionel Sambuc return (-1);
171*b7ef8cfbSLionel Sambuc else if (CTIMENSEC_CMP(b->fts_statp, <, a->fts_statp))
172*b7ef8cfbSLionel Sambuc return (1);
173*b7ef8cfbSLionel Sambuc else
174*b7ef8cfbSLionel Sambuc return (revnamecmp(a, b));
175*b7ef8cfbSLionel Sambuc }
176*b7ef8cfbSLionel Sambuc
177*b7ef8cfbSLionel Sambuc int
sizecmp(const FTSENT * a,const FTSENT * b)178*b7ef8cfbSLionel Sambuc sizecmp(const FTSENT *a, const FTSENT *b)
179*b7ef8cfbSLionel Sambuc {
180*b7ef8cfbSLionel Sambuc
181*b7ef8cfbSLionel Sambuc if (b->fts_statp->st_size > a->fts_statp->st_size)
182*b7ef8cfbSLionel Sambuc return (1);
183*b7ef8cfbSLionel Sambuc if (b->fts_statp->st_size < a->fts_statp->st_size)
184*b7ef8cfbSLionel Sambuc return (-1);
185*b7ef8cfbSLionel Sambuc else
186*b7ef8cfbSLionel Sambuc return (namecmp(a, b));
187*b7ef8cfbSLionel Sambuc }
188*b7ef8cfbSLionel Sambuc
189*b7ef8cfbSLionel Sambuc int
revsizecmp(const FTSENT * a,const FTSENT * b)190*b7ef8cfbSLionel Sambuc revsizecmp(const FTSENT *a, const FTSENT *b)
191*b7ef8cfbSLionel Sambuc {
192*b7ef8cfbSLionel Sambuc
193*b7ef8cfbSLionel Sambuc if (b->fts_statp->st_size > a->fts_statp->st_size)
194*b7ef8cfbSLionel Sambuc return (-1);
195*b7ef8cfbSLionel Sambuc if (b->fts_statp->st_size < a->fts_statp->st_size)
196*b7ef8cfbSLionel Sambuc return (1);
197*b7ef8cfbSLionel Sambuc else
198*b7ef8cfbSLionel Sambuc return (revnamecmp(a, b));
199*b7ef8cfbSLionel Sambuc }
200