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