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