1 /*	$OpenBSD: cmp.c,v 1.5 2003/06/11 23:42:12 deraadt Exp $	*/
2 /*	$NetBSD: cmp.c,v 1.10 1996/07/08 10:32:01 mycroft Exp $	*/
3 
4 /*
5  * Copyright (c) 1989, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Michael Fischbein.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include <sys/cdefs.h>
37 __SCCSID("@(#)cmp.c	8.1 (Berkeley) 5/31/93");
38 __RCSID("$OpenBSD: cmp.c,v 1.5 2003/06/11 23:42:12 deraadt Exp $");
39 
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 
43 #include <fts.h>
44 #include <string.h>
45 
46 #include "ls.h"
47 #include "extern.h"
48 
49 int
namecmp(const FTSENT * a,const FTSENT * b)50 namecmp(const FTSENT *a, const FTSENT *b)
51 {
52 	return (strcmp(a->fts_name, b->fts_name));
53 }
54 
55 int
revnamecmp(const FTSENT * a,const FTSENT * b)56 revnamecmp(const FTSENT *a, const FTSENT *b)
57 {
58 	return (strcmp(b->fts_name, a->fts_name));
59 }
60 
61 int
modcmp(const FTSENT * a,const FTSENT * b)62 modcmp(const FTSENT *a, const FTSENT *b)
63 {
64 	if (b->fts_statp->st_mtime > a->fts_statp->st_mtime)
65 		return (1);
66 	else if (b->fts_statp->st_mtime < a->fts_statp->st_mtime)
67 		return (-1);
68 	else if (b->fts_statp->st_mtimensec > a->fts_statp->st_mtimensec)
69 		return (1);
70 	else if (b->fts_statp->st_mtimensec < a->fts_statp->st_mtimensec)
71 		return (-1);
72 	else
73 		return (namecmp(a, b));
74 }
75 
76 int
revmodcmp(const FTSENT * a,const FTSENT * b)77 revmodcmp(const FTSENT *a, const FTSENT *b)
78 {
79 	if (b->fts_statp->st_mtime > a->fts_statp->st_mtime)
80 		return (-1);
81 	else if (b->fts_statp->st_mtime < a->fts_statp->st_mtime)
82 		return (1);
83 	else if (b->fts_statp->st_mtimensec > a->fts_statp->st_mtimensec)
84 		return (-1);
85 	else if (b->fts_statp->st_mtimensec < a->fts_statp->st_mtimensec)
86 		return (1);
87 	else
88 		return (revnamecmp(a, b));
89 }
90 
91 int
acccmp(const FTSENT * a,const FTSENT * b)92 acccmp(const FTSENT *a, const FTSENT *b)
93 {
94 	if (b->fts_statp->st_atime > a->fts_statp->st_atime)
95 		return (1);
96 	else if (b->fts_statp->st_atime < a->fts_statp->st_atime)
97 		return (-1);
98 	else if (b->fts_statp->st_atimensec > a->fts_statp->st_atimensec)
99 		return (1);
100 	else if (b->fts_statp->st_atimensec < a->fts_statp->st_atimensec)
101 		return (-1);
102 	else
103 		return (namecmp(a, b));
104 }
105 
106 int
revacccmp(const FTSENT * a,const FTSENT * b)107 revacccmp(const FTSENT *a, const FTSENT *b)
108 {
109 	if (b->fts_statp->st_atime > a->fts_statp->st_atime)
110 		return (-1);
111 	else if (b->fts_statp->st_atime < a->fts_statp->st_atime)
112 		return (1);
113 	else if (b->fts_statp->st_atimensec > a->fts_statp->st_atimensec)
114 		return (-1);
115 	else if (b->fts_statp->st_atimensec < a->fts_statp->st_atimensec)
116 		return (1);
117 	else
118 		return (revnamecmp(a, b));
119 }
120 
121 int
statcmp(const FTSENT * a,const FTSENT * b)122 statcmp(const FTSENT *a, const FTSENT *b)
123 {
124 	if (b->fts_statp->st_ctime > a->fts_statp->st_ctime)
125 		return (1);
126 	else if (b->fts_statp->st_ctime < a->fts_statp->st_ctime)
127 		return (-1);
128 	else if (b->fts_statp->st_ctimensec > a->fts_statp->st_ctimensec)
129 		return (1);
130 	else if (b->fts_statp->st_ctimensec < a->fts_statp->st_ctimensec)
131 		return (-1);
132 	else
133 		return (namecmp(a, b));
134 }
135 
136 int
revstatcmp(const FTSENT * a,const FTSENT * b)137 revstatcmp(const FTSENT *a, const FTSENT *b)
138 {
139 	if (b->fts_statp->st_ctime > a->fts_statp->st_ctime)
140 		return (-1);
141 	else if (b->fts_statp->st_ctime < a->fts_statp->st_ctime)
142 		return (1);
143 	else if (b->fts_statp->st_ctimensec > a->fts_statp->st_ctimensec)
144 		return (-1);
145 	else if (b->fts_statp->st_ctimensec < a->fts_statp->st_ctimensec)
146 		return (1);
147 	else
148 		return (revnamecmp(a, b));
149 }
150 
151 int
sizecmp(const FTSENT * a,const FTSENT * b)152 sizecmp(const FTSENT *a, const FTSENT *b)
153 {
154 	if (b->fts_statp->st_size > a->fts_statp->st_size)
155 		return (1);
156 	if (b->fts_statp->st_size < a->fts_statp->st_size)
157 		return (-1);
158 	else
159 		return (namecmp(a, b));
160 }
161 
162 int
revsizecmp(const FTSENT * a,const FTSENT * b)163 revsizecmp(const FTSENT *a, const FTSENT *b)
164 {
165 	if (b->fts_statp->st_size > a->fts_statp->st_size)
166 		return (-1);
167 	if (b->fts_statp->st_size < a->fts_statp->st_size)
168 		return (1);
169 	else
170 		return (revnamecmp(a, b));
171 }
172