xref: /freebsd/bin/ls/cmp.c (revision 4d846d26)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
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 #if 0
36 #ifndef lint
37 static char sccsid[] = "@(#)cmp.c	8.1 (Berkeley) 5/31/93";
38 #endif /* not lint */
39 #endif
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
42 
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 int
54 namecmp(const FTSENT *a, const FTSENT *b)
55 {
56 
57 	return (strcoll(a->fts_name, b->fts_name));
58 }
59 
60 int
61 revnamecmp(const FTSENT *a, const FTSENT *b)
62 {
63 
64 	return (strcoll(b->fts_name, a->fts_name));
65 }
66 
67 int
68 verscmp(const FTSENT *a, const FTSENT *b)
69 {
70 
71 	return (strverscmp(a->fts_name, b->fts_name));
72 }
73 
74 int
75 revverscmp(const FTSENT *a, const FTSENT *b)
76 {
77 
78 	return (strverscmp(b->fts_name, a->fts_name));
79 }
80 
81 int
82 modcmp(const FTSENT *a, const FTSENT *b)
83 {
84 
85 	if (b->fts_statp->st_mtim.tv_sec >
86 	    a->fts_statp->st_mtim.tv_sec)
87 		return (1);
88 	if (b->fts_statp->st_mtim.tv_sec <
89 	    a->fts_statp->st_mtim.tv_sec)
90 		return (-1);
91 	if (b->fts_statp->st_mtim.tv_nsec >
92 	    a->fts_statp->st_mtim.tv_nsec)
93 		return (1);
94 	if (b->fts_statp->st_mtim.tv_nsec <
95 	    a->fts_statp->st_mtim.tv_nsec)
96 		return (-1);
97 	if (f_samesort)
98 		return (strcoll(b->fts_name, a->fts_name));
99 	else
100 		return (strcoll(a->fts_name, b->fts_name));
101 }
102 
103 int
104 revmodcmp(const FTSENT *a, const FTSENT *b)
105 {
106 
107 	return (modcmp(b, a));
108 }
109 
110 int
111 acccmp(const FTSENT *a, const FTSENT *b)
112 {
113 
114 	if (b->fts_statp->st_atim.tv_sec >
115 	    a->fts_statp->st_atim.tv_sec)
116 		return (1);
117 	if (b->fts_statp->st_atim.tv_sec <
118 	    a->fts_statp->st_atim.tv_sec)
119 		return (-1);
120 	if (b->fts_statp->st_atim.tv_nsec >
121 	    a->fts_statp->st_atim.tv_nsec)
122 		return (1);
123 	if (b->fts_statp->st_atim.tv_nsec <
124 	    a->fts_statp->st_atim.tv_nsec)
125 		return (-1);
126 	if (f_samesort)
127 		return (strcoll(b->fts_name, a->fts_name));
128 	else
129 		return (strcoll(a->fts_name, b->fts_name));
130 }
131 
132 int
133 revacccmp(const FTSENT *a, const FTSENT *b)
134 {
135 
136 	return (acccmp(b, a));
137 }
138 
139 int
140 birthcmp(const FTSENT *a, const FTSENT *b)
141 {
142 
143 	if (b->fts_statp->st_birthtim.tv_sec >
144 	    a->fts_statp->st_birthtim.tv_sec)
145 		return (1);
146 	if (b->fts_statp->st_birthtim.tv_sec <
147 	    a->fts_statp->st_birthtim.tv_sec)
148 		return (-1);
149 	if (b->fts_statp->st_birthtim.tv_nsec >
150 	    a->fts_statp->st_birthtim.tv_nsec)
151 		return (1);
152 	if (b->fts_statp->st_birthtim.tv_nsec <
153 	    a->fts_statp->st_birthtim.tv_nsec)
154 		return (-1);
155 	if (f_samesort)
156 		return (strcoll(b->fts_name, a->fts_name));
157 	else
158 		return (strcoll(a->fts_name, b->fts_name));
159 }
160 
161 int
162 revbirthcmp(const FTSENT *a, const FTSENT *b)
163 {
164 
165 	return (birthcmp(b, a));
166 }
167 
168 int
169 statcmp(const FTSENT *a, const FTSENT *b)
170 {
171 
172 	if (b->fts_statp->st_ctim.tv_sec >
173 	    a->fts_statp->st_ctim.tv_sec)
174 		return (1);
175 	if (b->fts_statp->st_ctim.tv_sec <
176 	    a->fts_statp->st_ctim.tv_sec)
177 		return (-1);
178 	if (b->fts_statp->st_ctim.tv_nsec >
179 	    a->fts_statp->st_ctim.tv_nsec)
180 		return (1);
181 	if (b->fts_statp->st_ctim.tv_nsec <
182 	    a->fts_statp->st_ctim.tv_nsec)
183 		return (-1);
184 	if (f_samesort)
185 		return (strcoll(b->fts_name, a->fts_name));
186 	else
187 		return (strcoll(a->fts_name, b->fts_name));
188 }
189 
190 int
191 revstatcmp(const FTSENT *a, const FTSENT *b)
192 {
193 
194 	return (statcmp(b, a));
195 }
196 
197 int
198 sizecmp(const FTSENT *a, const FTSENT *b)
199 {
200 
201 	if (b->fts_statp->st_size > a->fts_statp->st_size)
202 		return (1);
203 	if (b->fts_statp->st_size < a->fts_statp->st_size)
204 		return (-1);
205 	return (strcoll(a->fts_name, b->fts_name));
206 }
207 
208 int
209 revsizecmp(const FTSENT *a, const FTSENT *b)
210 {
211 
212 	return (sizecmp(b, a));
213 }
214