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