]>
git.cameronkatri.com Git - apple_cmds.git/blob - file_cmds/ls/cmp.c
2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 static char sccsid
[] = "@(#)cmp.c 8.1 (Berkeley) 5/31/93";
42 #include <sys/cdefs.h>
43 __RCSID("$FreeBSD: src/bin/ls/cmp.c,v 1.12 2002/06/30 05:13:54 obrien Exp $");
46 #include <sys/types.h>
55 #if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) || \
56 defined(_XOPEN_SOURCE) || defined(__NetBSD__)
57 #define ATIMENSEC_CMP(x, op, y) ((x)->st_atimensec op (y)->st_atimensec)
58 #define CTIMENSEC_CMP(x, op, y) ((x)->st_ctimensec op (y)->st_ctimensec)
59 #define MTIMENSEC_CMP(x, op, y) ((x)->st_mtimensec op (y)->st_mtimensec)
60 #define BTIMENSEC_CMP(x, op, y) ((x)->st_birthtimensec op (y)->st_birthtimensec)
62 #define ATIMENSEC_CMP(x, op, y) \
63 ((x)->st_atimespec.tv_nsec op (y)->st_atimespec.tv_nsec)
64 #define CTIMENSEC_CMP(x, op, y) \
65 ((x)->st_ctimespec.tv_nsec op (y)->st_ctimespec.tv_nsec)
66 #define MTIMENSEC_CMP(x, op, y) \
67 ((x)->st_mtimespec.tv_nsec op (y)->st_mtimespec.tv_nsec)
68 #define BTIMENSEC_CMP(x, op, y) \
69 ((x)->st_birthtimespec.tv_nsec op (y)->st_birthtimespec.tv_nsec)
73 namecmp(const FTSENT
*a
, const FTSENT
*b
)
75 return (strcoll(a
->fts_name
, b
->fts_name
));
79 revnamecmp(const FTSENT
*a
, const FTSENT
*b
)
81 return (strcoll(b
->fts_name
, a
->fts_name
));
85 modcmp(const FTSENT
*a
, const FTSENT
*b
)
87 if (b
->fts_statp
->st_mtime
> a
->fts_statp
->st_mtime
)
89 else if (b
->fts_statp
->st_mtime
< a
->fts_statp
->st_mtime
)
91 else if (MTIMENSEC_CMP(b
->fts_statp
, >, a
->fts_statp
))
93 else if (MTIMENSEC_CMP(b
->fts_statp
, <, a
->fts_statp
))
96 return (namecmp(a
, b
));
100 revmodcmp(const FTSENT
*a
, const FTSENT
*b
)
102 if (b
->fts_statp
->st_mtime
> a
->fts_statp
->st_mtime
)
104 else if (b
->fts_statp
->st_mtime
< a
->fts_statp
->st_mtime
)
106 else if (MTIMENSEC_CMP(b
->fts_statp
, >, a
->fts_statp
))
108 else if (MTIMENSEC_CMP(b
->fts_statp
, <, a
->fts_statp
))
111 return (revnamecmp(a
, b
));
115 acccmp(const FTSENT
*a
, const FTSENT
*b
)
117 if (b
->fts_statp
->st_atime
> a
->fts_statp
->st_atime
)
119 else if (b
->fts_statp
->st_atime
< a
->fts_statp
->st_atime
)
121 else if (ATIMENSEC_CMP(b
->fts_statp
, >, a
->fts_statp
))
123 else if (ATIMENSEC_CMP(b
->fts_statp
, <, a
->fts_statp
))
126 return (namecmp(a
, b
));
130 revacccmp(const FTSENT
*a
, const FTSENT
*b
)
132 if (b
->fts_statp
->st_atime
> a
->fts_statp
->st_atime
)
134 else if (b
->fts_statp
->st_atime
< a
->fts_statp
->st_atime
)
136 else if (ATIMENSEC_CMP(b
->fts_statp
, >, a
->fts_statp
))
138 else if (ATIMENSEC_CMP(b
->fts_statp
, <, a
->fts_statp
))
141 return (revnamecmp(a
, b
));
145 statcmp(const FTSENT
*a
, const FTSENT
*b
)
147 if (b
->fts_statp
->st_ctime
> a
->fts_statp
->st_ctime
)
149 else if (b
->fts_statp
->st_ctime
< a
->fts_statp
->st_ctime
)
151 else if (CTIMENSEC_CMP(b
->fts_statp
, >, a
->fts_statp
))
153 else if (CTIMENSEC_CMP(b
->fts_statp
, <, a
->fts_statp
))
156 return (namecmp(a
, b
));
160 revstatcmp(const FTSENT
*a
, const FTSENT
*b
)
162 if (b
->fts_statp
->st_ctime
> a
->fts_statp
->st_ctime
)
164 else if (b
->fts_statp
->st_ctime
< a
->fts_statp
->st_ctime
)
166 else if (CTIMENSEC_CMP(b
->fts_statp
, >, a
->fts_statp
))
168 else if (CTIMENSEC_CMP(b
->fts_statp
, <, a
->fts_statp
))
171 return (revnamecmp(a
, b
));
178 if (b
->fts_statp
->st_size
> a
->fts_statp
->st_size
)
180 if (b
->fts_statp
->st_size
< a
->fts_statp
->st_size
)
183 return (namecmp(a
, b
));
190 if (b
->fts_statp
->st_size
> a
->fts_statp
->st_size
)
192 if (b
->fts_statp
->st_size
< a
->fts_statp
->st_size
)
195 return (revnamecmp(a
, b
));
199 birthcmp(const FTSENT
*a
, const FTSENT
*b
)
201 if (b
->fts_statp
->st_birthtime
> a
->fts_statp
->st_birthtime
)
203 else if (b
->fts_statp
->st_birthtime
< a
->fts_statp
->st_birthtime
)
205 else if (BTIMENSEC_CMP(b
->fts_statp
, >, a
->fts_statp
))
207 else if (BTIMENSEC_CMP(b
->fts_statp
, <, a
->fts_statp
))
210 return (namecmp(a
, b
));
214 revbirthcmp(const FTSENT
*a
, const FTSENT
*b
)
216 if (b
->fts_statp
->st_birthtime
> a
->fts_statp
->st_birthtime
)
218 else if (b
->fts_statp
->st_birthtime
< a
->fts_statp
->st_birthtime
)
220 else if (BTIMENSEC_CMP(b
->fts_statp
, >, a
->fts_statp
))
222 else if (BTIMENSEC_CMP(b
->fts_statp
, <, a
->fts_statp
))
225 return (revnamecmp(a
, b
));