xref: /freebsd/bin/ls/cmp.c (revision 6ef644f5889afbd0f681b08ed1a2f369524af83e)
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 #include <sys/types.h>
42 #include <sys/stat.h>
43 
44 #include <fts.h>
45 #include <string.h>
46 
47 #include "ls.h"
48 #include "extern.h"
49 
50 int
51 namecmp(const FTSENT *a, const FTSENT *b)
52 {
53 
54 	return (strcoll(a->fts_name, b->fts_name));
55 }
56 
57 int
58 revnamecmp(const FTSENT *a, const FTSENT *b)
59 {
60 
61 	return (strcoll(b->fts_name, a->fts_name));
62 }
63 
64 int
65 verscmp(const FTSENT *a, const FTSENT *b)
66 {
67 
68 	return (strverscmp(a->fts_name, b->fts_name));
69 }
70 
71 int
72 revverscmp(const FTSENT *a, const FTSENT *b)
73 {
74 
75 	return (strverscmp(b->fts_name, a->fts_name));
76 }
77 
78 int
79 modcmp(const FTSENT *a, const FTSENT *b)
80 {
81 
82 	if (b->fts_statp->st_mtim.tv_sec >
83 	    a->fts_statp->st_mtim.tv_sec)
84 		return (1);
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_nsec >
89 	    a->fts_statp->st_mtim.tv_nsec)
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 (f_samesort)
95 		return (strcoll(b->fts_name, a->fts_name));
96 	else
97 		return (strcoll(a->fts_name, b->fts_name));
98 }
99 
100 int
101 revmodcmp(const FTSENT *a, const FTSENT *b)
102 {
103 
104 	return (modcmp(b, a));
105 }
106 
107 int
108 acccmp(const FTSENT *a, const FTSENT *b)
109 {
110 
111 	if (b->fts_statp->st_atim.tv_sec >
112 	    a->fts_statp->st_atim.tv_sec)
113 		return (1);
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_nsec >
118 	    a->fts_statp->st_atim.tv_nsec)
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 (f_samesort)
124 		return (strcoll(b->fts_name, a->fts_name));
125 	else
126 		return (strcoll(a->fts_name, b->fts_name));
127 }
128 
129 int
130 revacccmp(const FTSENT *a, const FTSENT *b)
131 {
132 
133 	return (acccmp(b, a));
134 }
135 
136 int
137 birthcmp(const FTSENT *a, const FTSENT *b)
138 {
139 
140 	if (b->fts_statp->st_birthtim.tv_sec >
141 	    a->fts_statp->st_birthtim.tv_sec)
142 		return (1);
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_nsec >
147 	    a->fts_statp->st_birthtim.tv_nsec)
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 (f_samesort)
153 		return (strcoll(b->fts_name, a->fts_name));
154 	else
155 		return (strcoll(a->fts_name, b->fts_name));
156 }
157 
158 int
159 revbirthcmp(const FTSENT *a, const FTSENT *b)
160 {
161 
162 	return (birthcmp(b, a));
163 }
164 
165 int
166 statcmp(const FTSENT *a, const FTSENT *b)
167 {
168 
169 	if (b->fts_statp->st_ctim.tv_sec >
170 	    a->fts_statp->st_ctim.tv_sec)
171 		return (1);
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_nsec >
176 	    a->fts_statp->st_ctim.tv_nsec)
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 (f_samesort)
182 		return (strcoll(b->fts_name, a->fts_name));
183 	else
184 		return (strcoll(a->fts_name, b->fts_name));
185 }
186 
187 int
188 revstatcmp(const FTSENT *a, const FTSENT *b)
189 {
190 
191 	return (statcmp(b, a));
192 }
193 
194 int
195 sizecmp(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 	return (strcoll(a->fts_name, b->fts_name));
203 }
204 
205 int
206 revsizecmp(const FTSENT *a, const FTSENT *b)
207 {
208 
209 	return (sizecmp(b, a));
210 }
211