xref: /dragonfly/bin/ls/cmp.c (revision 9e5de6b5)
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  * @(#)cmp.c	8.1 (Berkeley) 5/31/93
33  * $FreeBSD: src/bin/ls/cmp.c,v 1.16 2005/01/10 08:39:23 imp Exp $
34  */
35 
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 
39 #include <fts.h>
40 #include <string.h>
41 
42 #include "ls.h"
43 #include "extern.h"
44 
45 int
namecmp(const FTSENT * a,const FTSENT * b)46 namecmp(const FTSENT *a, const FTSENT *b)
47 {
48 	return (strcoll(a->fts_name, b->fts_name));
49 }
50 
51 int
revnamecmp(const FTSENT * a,const FTSENT * b)52 revnamecmp(const FTSENT *a, const FTSENT *b)
53 {
54 	return (strcoll(b->fts_name, a->fts_name));
55 }
56 
57 int
modcmp(const FTSENT * a,const FTSENT * b)58 modcmp(const FTSENT *a, const FTSENT *b)
59 {
60 	if (b->fts_statp->st_mtimespec.tv_sec >
61 	    a->fts_statp->st_mtimespec.tv_sec)
62 		return (1);
63 	if (b->fts_statp->st_mtimespec.tv_sec <
64 	    a->fts_statp->st_mtimespec.tv_sec)
65 		return (-1);
66 	if (b->fts_statp->st_mtimespec.tv_nsec >
67 	    a->fts_statp->st_mtimespec.tv_nsec)
68 		return (1);
69 	if (b->fts_statp->st_mtimespec.tv_nsec <
70 	    a->fts_statp->st_mtimespec.tv_nsec)
71 		return (-1);
72 	return (strcoll(a->fts_name, b->fts_name));
73 }
74 
75 int
revmodcmp(const FTSENT * a,const FTSENT * b)76 revmodcmp(const FTSENT *a, const FTSENT *b)
77 {
78 	return (modcmp(b, a));
79 }
80 
81 int
acccmp(const FTSENT * a,const FTSENT * b)82 acccmp(const FTSENT *a, const FTSENT *b)
83 {
84 	if (b->fts_statp->st_atimespec.tv_sec >
85 	    a->fts_statp->st_atimespec.tv_sec)
86 		return (1);
87 	if (b->fts_statp->st_atimespec.tv_sec <
88 	    a->fts_statp->st_atimespec.tv_sec)
89 		return (-1);
90 	if (b->fts_statp->st_atimespec.tv_nsec >
91 	    a->fts_statp->st_atimespec.tv_nsec)
92 		return (1);
93 	if (b->fts_statp->st_atimespec.tv_nsec <
94 	    a->fts_statp->st_atimespec.tv_nsec)
95 		return (-1);
96 	return (strcoll(a->fts_name, b->fts_name));
97 }
98 
99 int
revacccmp(const FTSENT * a,const FTSENT * b)100 revacccmp(const FTSENT *a, const FTSENT *b)
101 {
102 	return (acccmp(b, a));
103 }
104 
105 int
sizecmp(const FTSENT * a,const FTSENT * b)106 sizecmp(const FTSENT *a, const FTSENT *b)
107 {
108 	if (b->fts_statp->st_size >
109 	    a->fts_statp->st_size)
110 		return (1);
111 	if (b->fts_statp->st_size <
112 	    a->fts_statp->st_size)
113 		return (-1);
114 	return (strcoll(a->fts_name, b->fts_name));
115 }
116 
117 int
revsizecmp(const FTSENT * a,const FTSENT * b)118 revsizecmp(const FTSENT *a, const FTSENT *b)
119 {
120 	return (sizecmp(b, a));
121 }
122 
123 int
statcmp(const FTSENT * a,const FTSENT * b)124 statcmp(const FTSENT *a, const FTSENT *b)
125 {
126 	if (b->fts_statp->st_ctimespec.tv_sec >
127 	    a->fts_statp->st_ctimespec.tv_sec)
128 		return (1);
129 	if (b->fts_statp->st_ctimespec.tv_sec <
130 	    a->fts_statp->st_ctimespec.tv_sec)
131 		return (-1);
132 	if (b->fts_statp->st_ctimespec.tv_nsec >
133 	    a->fts_statp->st_ctimespec.tv_nsec)
134 		return (1);
135 	if (b->fts_statp->st_ctimespec.tv_nsec <
136 	    a->fts_statp->st_ctimespec.tv_nsec)
137 		return (-1);
138 	return (strcoll(a->fts_name, b->fts_name));
139 }
140 
141 int
revstatcmp(const FTSENT * a,const FTSENT * b)142 revstatcmp(const FTSENT *a, const FTSENT *b)
143 {
144 	return (statcmp(b, a));
145 }
146