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 | #ifdef HAVE_CONFIG_H |
---|
36 | #include "config.h" |
---|
37 | #endif |
---|
38 | |
---|
39 | #if 0 |
---|
40 | #include <sys/cdefs.h> |
---|
41 | #ifndef lint |
---|
42 | #if 0 |
---|
43 | static char sccsid[] = "@(#)cmp.c 8.1 (Berkeley) 5/31/93"; |
---|
44 | #else |
---|
45 | __RCSID("$NetBSD: cmp.c,v 1.17 2003/08/07 09:05:14 agc Exp $"); |
---|
46 | #endif |
---|
47 | #endif /* not lint */ |
---|
48 | #endif |
---|
49 | |
---|
50 | #include <sys/types.h> |
---|
51 | #include <sys/stat.h> |
---|
52 | |
---|
53 | #include "fts.h" |
---|
54 | #include <string.h> |
---|
55 | |
---|
56 | #include "extern-ls.h" |
---|
57 | |
---|
58 | #if defined(__rtems__) || defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) || \ |
---|
59 | defined(_XOPEN_SOURCE) || defined(__NetBSD__) |
---|
60 | #define ATIMENSEC_CMP(x, op, y) ((x)->st_atime op (y)->st_atime) |
---|
61 | #define CTIMENSEC_CMP(x, op, y) ((x)->st_ctime op (y)->st_ctime) |
---|
62 | #define MTIMENSEC_CMP(x, op, y) ((x)->st_mtime op (y)->st_mtime) |
---|
63 | #else |
---|
64 | #define ATIMENSEC_CMP(x, op, y) \ |
---|
65 | ((x)->st_atime.tv_nsec op (y)->st_atime.tv_nsec) |
---|
66 | #define CTIMENSEC_CMP(x, op, y) \ |
---|
67 | ((x)->st_ctime.tv_nsec op (y)->st_ctime.tv_nsec) |
---|
68 | #define MTIMENSEC_CMP(x, op, y) \ |
---|
69 | ((x)->st_mtime.tv_nsec op (y)->st_mtime.tv_nsec) |
---|
70 | #endif |
---|
71 | |
---|
72 | int |
---|
73 | namecmp(const FTSENT *a, const FTSENT *b) |
---|
74 | { |
---|
75 | |
---|
76 | return (strcmp(a->fts_name, b->fts_name)); |
---|
77 | } |
---|
78 | |
---|
79 | int |
---|
80 | revnamecmp(const FTSENT *a, const FTSENT *b) |
---|
81 | { |
---|
82 | |
---|
83 | return (strcmp(b->fts_name, a->fts_name)); |
---|
84 | } |
---|
85 | |
---|
86 | int |
---|
87 | modcmp(const FTSENT *a, const FTSENT *b) |
---|
88 | { |
---|
89 | |
---|
90 | if (b->fts_statp->st_mtime > a->fts_statp->st_mtime) |
---|
91 | return (1); |
---|
92 | else if (b->fts_statp->st_mtime < a->fts_statp->st_mtime) |
---|
93 | return (-1); |
---|
94 | else if (MTIMENSEC_CMP(b->fts_statp, >, a->fts_statp)) |
---|
95 | return (1); |
---|
96 | else if (MTIMENSEC_CMP(b->fts_statp, <, a->fts_statp)) |
---|
97 | return (-1); |
---|
98 | else |
---|
99 | return (namecmp(a, b)); |
---|
100 | } |
---|
101 | |
---|
102 | int |
---|
103 | revmodcmp(const FTSENT *a, const FTSENT *b) |
---|
104 | { |
---|
105 | |
---|
106 | if (b->fts_statp->st_mtime > a->fts_statp->st_mtime) |
---|
107 | return (-1); |
---|
108 | else if (b->fts_statp->st_mtime < a->fts_statp->st_mtime) |
---|
109 | return (1); |
---|
110 | else if (MTIMENSEC_CMP(b->fts_statp, >, a->fts_statp)) |
---|
111 | return (-1); |
---|
112 | else if (MTIMENSEC_CMP(b->fts_statp, <, a->fts_statp)) |
---|
113 | return (1); |
---|
114 | else |
---|
115 | return (revnamecmp(a, b)); |
---|
116 | } |
---|
117 | |
---|
118 | int |
---|
119 | acccmp(const FTSENT *a, const FTSENT *b) |
---|
120 | { |
---|
121 | |
---|
122 | if (b->fts_statp->st_atime > a->fts_statp->st_atime) |
---|
123 | return (1); |
---|
124 | else if (b->fts_statp->st_atime < a->fts_statp->st_atime) |
---|
125 | return (-1); |
---|
126 | else if (ATIMENSEC_CMP(b->fts_statp, >, a->fts_statp)) |
---|
127 | return (1); |
---|
128 | else if (ATIMENSEC_CMP(b->fts_statp, <, a->fts_statp)) |
---|
129 | return (-1); |
---|
130 | else |
---|
131 | return (namecmp(a, b)); |
---|
132 | } |
---|
133 | |
---|
134 | int |
---|
135 | revacccmp(const FTSENT *a, const FTSENT *b) |
---|
136 | { |
---|
137 | |
---|
138 | if (b->fts_statp->st_atime > a->fts_statp->st_atime) |
---|
139 | return (-1); |
---|
140 | else if (b->fts_statp->st_atime < a->fts_statp->st_atime) |
---|
141 | return (1); |
---|
142 | else if (ATIMENSEC_CMP(b->fts_statp, >, a->fts_statp)) |
---|
143 | return (-1); |
---|
144 | else if (ATIMENSEC_CMP(b->fts_statp, <, a->fts_statp)) |
---|
145 | return (1); |
---|
146 | else |
---|
147 | return (revnamecmp(a, b)); |
---|
148 | } |
---|
149 | |
---|
150 | int |
---|
151 | statcmp(const FTSENT *a, const FTSENT *b) |
---|
152 | { |
---|
153 | |
---|
154 | if (b->fts_statp->st_ctime > a->fts_statp->st_ctime) |
---|
155 | return (1); |
---|
156 | else if (b->fts_statp->st_ctime < a->fts_statp->st_ctime) |
---|
157 | return (-1); |
---|
158 | else if (CTIMENSEC_CMP(b->fts_statp, >, a->fts_statp)) |
---|
159 | return (1); |
---|
160 | else if (CTIMENSEC_CMP(b->fts_statp, <, a->fts_statp)) |
---|
161 | return (-1); |
---|
162 | else |
---|
163 | return (namecmp(a, b)); |
---|
164 | } |
---|
165 | |
---|
166 | int |
---|
167 | revstatcmp(const FTSENT *a, const FTSENT *b) |
---|
168 | { |
---|
169 | |
---|
170 | if (b->fts_statp->st_ctime > a->fts_statp->st_ctime) |
---|
171 | return (-1); |
---|
172 | else if (b->fts_statp->st_ctime < a->fts_statp->st_ctime) |
---|
173 | return (1); |
---|
174 | else if (CTIMENSEC_CMP(b->fts_statp, >, a->fts_statp)) |
---|
175 | return (-1); |
---|
176 | else if (CTIMENSEC_CMP(b->fts_statp, <, a->fts_statp)) |
---|
177 | return (1); |
---|
178 | else |
---|
179 | return (revnamecmp(a, b)); |
---|
180 | } |
---|
181 | |
---|
182 | int |
---|
183 | sizecmp(const FTSENT *a, const FTSENT *b) |
---|
184 | { |
---|
185 | |
---|
186 | if (b->fts_statp->st_size > a->fts_statp->st_size) |
---|
187 | return (1); |
---|
188 | if (b->fts_statp->st_size < a->fts_statp->st_size) |
---|
189 | return (-1); |
---|
190 | else |
---|
191 | return (namecmp(a, b)); |
---|
192 | } |
---|
193 | |
---|
194 | int |
---|
195 | revsizecmp(const FTSENT *a, const FTSENT *b) |
---|
196 | { |
---|
197 | |
---|
198 | if (b->fts_statp->st_size > a->fts_statp->st_size) |
---|
199 | return (-1); |
---|
200 | if (b->fts_statp->st_size < a->fts_statp->st_size) |
---|
201 | return (1); |
---|
202 | else |
---|
203 | return (revnamecmp(a, b)); |
---|
204 | } |
---|