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