xref: /openbsd-src/bin/ls/cmp.c (revision 2b0358df1d88d06ef4139321dd05bd5e05d91eaf)
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 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)cmp.c	8.1 (Berkeley) 5/31/93";
39 #else
40 static char rcsid[] = "$OpenBSD: cmp.c,v 1.5 2003/06/11 23:42:12 deraadt Exp $";
41 #endif
42 #endif /* not lint */
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 	return (strcmp(a->fts_name, b->fts_name));
57 }
58 
59 int
60 revnamecmp(const FTSENT *a, const FTSENT *b)
61 {
62 	return (strcmp(b->fts_name, a->fts_name));
63 }
64 
65 int
66 modcmp(const FTSENT *a, const FTSENT *b)
67 {
68 	if (b->fts_statp->st_mtime > a->fts_statp->st_mtime)
69 		return (1);
70 	else if (b->fts_statp->st_mtime < a->fts_statp->st_mtime)
71 		return (-1);
72 	else if (b->fts_statp->st_mtimensec > a->fts_statp->st_mtimensec)
73 		return (1);
74 	else if (b->fts_statp->st_mtimensec < a->fts_statp->st_mtimensec)
75 		return (-1);
76 	else
77 		return (namecmp(a, b));
78 }
79 
80 int
81 revmodcmp(const FTSENT *a, const FTSENT *b)
82 {
83 	if (b->fts_statp->st_mtime > a->fts_statp->st_mtime)
84 		return (-1);
85 	else if (b->fts_statp->st_mtime < a->fts_statp->st_mtime)
86 		return (1);
87 	else if (b->fts_statp->st_mtimensec > a->fts_statp->st_mtimensec)
88 		return (-1);
89 	else if (b->fts_statp->st_mtimensec < a->fts_statp->st_mtimensec)
90 		return (1);
91 	else
92 		return (revnamecmp(a, b));
93 }
94 
95 int
96 acccmp(const FTSENT *a, const FTSENT *b)
97 {
98 	if (b->fts_statp->st_atime > a->fts_statp->st_atime)
99 		return (1);
100 	else if (b->fts_statp->st_atime < a->fts_statp->st_atime)
101 		return (-1);
102 	else if (b->fts_statp->st_atimensec > a->fts_statp->st_atimensec)
103 		return (1);
104 	else if (b->fts_statp->st_atimensec < a->fts_statp->st_atimensec)
105 		return (-1);
106 	else
107 		return (namecmp(a, b));
108 }
109 
110 int
111 revacccmp(const FTSENT *a, const FTSENT *b)
112 {
113 	if (b->fts_statp->st_atime > a->fts_statp->st_atime)
114 		return (-1);
115 	else if (b->fts_statp->st_atime < a->fts_statp->st_atime)
116 		return (1);
117 	else if (b->fts_statp->st_atimensec > a->fts_statp->st_atimensec)
118 		return (-1);
119 	else if (b->fts_statp->st_atimensec < a->fts_statp->st_atimensec)
120 		return (1);
121 	else
122 		return (revnamecmp(a, b));
123 }
124 
125 int
126 statcmp(const FTSENT *a, const FTSENT *b)
127 {
128 	if (b->fts_statp->st_ctime > a->fts_statp->st_ctime)
129 		return (1);
130 	else if (b->fts_statp->st_ctime < a->fts_statp->st_ctime)
131 		return (-1);
132 	else if (b->fts_statp->st_ctimensec > a->fts_statp->st_ctimensec)
133 		return (1);
134 	else if (b->fts_statp->st_ctimensec < a->fts_statp->st_ctimensec)
135 		return (-1);
136 	else
137 		return (namecmp(a, b));
138 }
139 
140 int
141 revstatcmp(const FTSENT *a, const FTSENT *b)
142 {
143 	if (b->fts_statp->st_ctime > a->fts_statp->st_ctime)
144 		return (-1);
145 	else if (b->fts_statp->st_ctime < a->fts_statp->st_ctime)
146 		return (1);
147 	else if (b->fts_statp->st_ctimensec > a->fts_statp->st_ctimensec)
148 		return (-1);
149 	else if (b->fts_statp->st_ctimensec < a->fts_statp->st_ctimensec)
150 		return (1);
151 	else
152 		return (revnamecmp(a, b));
153 }
154 
155 int
156 sizecmp(const FTSENT *a, const FTSENT *b)
157 {
158 	if (b->fts_statp->st_size > a->fts_statp->st_size)
159 		return (1);
160 	if (b->fts_statp->st_size < a->fts_statp->st_size)
161 		return (-1);
162 	else
163 		return (namecmp(a, b));
164 }
165 
166 int
167 revsizecmp(const FTSENT *a, const FTSENT *b)
168 {
169 	if (b->fts_statp->st_size > a->fts_statp->st_size)
170 		return (-1);
171 	if (b->fts_statp->st_size < a->fts_statp->st_size)
172 		return (1);
173 	else
174 		return (revnamecmp(a, b));
175 }
176