xref: /freebsd/bin/ls/cmp.c (revision b032f27c365b992e9d8e42214183b39acfb8c6ac)
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  * 4. 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_mtimespec.tv_sec >
70 	    a->fts_statp->st_mtimespec.tv_sec)
71 		return (1);
72 	if (b->fts_statp->st_mtimespec.tv_sec <
73 	    a->fts_statp->st_mtimespec.tv_sec)
74 		return (-1);
75 	if (b->fts_statp->st_mtimespec.tv_nsec >
76 	    a->fts_statp->st_mtimespec.tv_nsec)
77 		return (1);
78 	if (b->fts_statp->st_mtimespec.tv_nsec <
79 	    a->fts_statp->st_mtimespec.tv_nsec)
80 		return (-1);
81 	return (strcoll(a->fts_name, b->fts_name));
82 }
83 
84 int
85 revmodcmp(const FTSENT *a, const FTSENT *b)
86 {
87 
88 	return (modcmp(b, a));
89 }
90 
91 int
92 acccmp(const FTSENT *a, const FTSENT *b)
93 {
94 
95 	if (b->fts_statp->st_atimespec.tv_sec >
96 	    a->fts_statp->st_atimespec.tv_sec)
97 		return (1);
98 	if (b->fts_statp->st_atimespec.tv_sec <
99 	    a->fts_statp->st_atimespec.tv_sec)
100 		return (-1);
101 	if (b->fts_statp->st_atimespec.tv_nsec >
102 	    a->fts_statp->st_atimespec.tv_nsec)
103 		return (1);
104 	if (b->fts_statp->st_atimespec.tv_nsec <
105 	    a->fts_statp->st_atimespec.tv_nsec)
106 		return (-1);
107 	return (strcoll(a->fts_name, b->fts_name));
108 }
109 
110 int
111 revacccmp(const FTSENT *a, const FTSENT *b)
112 {
113 
114 	return (acccmp(b, a));
115 }
116 
117 int
118 birthcmp(const FTSENT *a, const FTSENT *b)
119 {
120 
121 	if (b->fts_statp->st_birthtimespec.tv_sec >
122 	    a->fts_statp->st_birthtimespec.tv_sec)
123 		return (1);
124 	if (b->fts_statp->st_birthtimespec.tv_sec <
125 	    a->fts_statp->st_birthtimespec.tv_sec)
126 		return (-1);
127 	if (b->fts_statp->st_birthtimespec.tv_nsec >
128 	    a->fts_statp->st_birthtimespec.tv_nsec)
129 		return (1);
130 	if (b->fts_statp->st_birthtimespec.tv_nsec <
131 	    a->fts_statp->st_birthtimespec.tv_nsec)
132 		return (-1);
133 	return (strcoll(a->fts_name, b->fts_name));
134 }
135 
136 int
137 revbirthcmp(const FTSENT *a, const FTSENT *b)
138 {
139 
140 	return (birthcmp(b, a));
141 }
142 
143 int
144 statcmp(const FTSENT *a, const FTSENT *b)
145 {
146 
147 	if (b->fts_statp->st_ctimespec.tv_sec >
148 	    a->fts_statp->st_ctimespec.tv_sec)
149 		return (1);
150 	if (b->fts_statp->st_ctimespec.tv_sec <
151 	    a->fts_statp->st_ctimespec.tv_sec)
152 		return (-1);
153 	if (b->fts_statp->st_ctimespec.tv_nsec >
154 	    a->fts_statp->st_ctimespec.tv_nsec)
155 		return (1);
156 	if (b->fts_statp->st_ctimespec.tv_nsec <
157 	    a->fts_statp->st_ctimespec.tv_nsec)
158 		return (-1);
159 	return (strcoll(a->fts_name, b->fts_name));
160 }
161 
162 int
163 revstatcmp(const FTSENT *a, const FTSENT *b)
164 {
165 
166 	return (statcmp(b, a));
167 }
168 
169 int
170 sizecmp(const FTSENT *a, const FTSENT *b)
171 {
172 
173 	if (b->fts_statp->st_size > a->fts_statp->st_size)
174 		return (1);
175 	if (b->fts_statp->st_size < a->fts_statp->st_size)
176 		return (-1);
177 	return (strcoll(a->fts_name, b->fts_name));
178 }
179 
180 int
181 revsizecmp(const FTSENT *a, const FTSENT *b)
182 {
183 
184 	return (sizecmp(b, a));
185 }
186