xref: /freebsd/bin/ls/cmp.c (revision e2662256cdbc6ca9be49a66837f9725081cfa9d3)
19ddb49cbSWarner Losh /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
44b88c807SRodney W. Grimes  * Copyright (c) 1989, 1993
54b88c807SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
64b88c807SRodney W. Grimes  *
74b88c807SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
84b88c807SRodney W. Grimes  * Michael Fischbein.
94b88c807SRodney W. Grimes  *
104b88c807SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
114b88c807SRodney W. Grimes  * modification, are permitted provided that the following conditions
124b88c807SRodney W. Grimes  * are met:
134b88c807SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
144b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
154b88c807SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
164b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
174b88c807SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
18fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
194b88c807SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
204b88c807SRodney W. Grimes  *    without specific prior written permission.
214b88c807SRodney W. Grimes  *
224b88c807SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
234b88c807SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
244b88c807SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
254b88c807SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
264b88c807SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
274b88c807SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
284b88c807SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
294b88c807SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
304b88c807SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
314b88c807SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
324b88c807SRodney W. Grimes  * SUCH DAMAGE.
334b88c807SRodney W. Grimes  */
344b88c807SRodney W. Grimes 
35febad2fcSSteve Price #if 0
36c73d77ceSMark Murray #ifndef lint
37febad2fcSSteve Price static char sccsid[] = "@(#)cmp.c	8.1 (Berkeley) 5/31/93";
384b88c807SRodney W. Grimes #endif /* not lint */
39c73d77ceSMark Murray #endif
405eb43ac2SDavid E. O'Brien #include <sys/cdefs.h>
415eb43ac2SDavid E. O'Brien __FBSDID("$FreeBSD$");
425eb43ac2SDavid E. O'Brien 
434b88c807SRodney W. Grimes 
444b88c807SRodney W. Grimes #include <sys/types.h>
454b88c807SRodney W. Grimes #include <sys/stat.h>
464b88c807SRodney W. Grimes 
474b88c807SRodney W. Grimes #include <fts.h>
484b88c807SRodney W. Grimes #include <string.h>
494b88c807SRodney W. Grimes 
504b88c807SRodney W. Grimes #include "ls.h"
514b88c807SRodney W. Grimes #include "extern.h"
524b88c807SRodney W. Grimes 
534b88c807SRodney W. Grimes int
5446251ddeSWarner Losh namecmp(const FTSENT *a, const FTSENT *b)
554b88c807SRodney W. Grimes {
56f137c9c1SDavid Malone 
57942c84aaSAndrey A. Chernov 	return (strcoll(a->fts_name, b->fts_name));
584b88c807SRodney W. Grimes }
594b88c807SRodney W. Grimes 
604b88c807SRodney W. Grimes int
6146251ddeSWarner Losh revnamecmp(const FTSENT *a, const FTSENT *b)
624b88c807SRodney W. Grimes {
63f137c9c1SDavid Malone 
64942c84aaSAndrey A. Chernov 	return (strcoll(b->fts_name, a->fts_name));
654b88c807SRodney W. Grimes }
664b88c807SRodney W. Grimes 
674b88c807SRodney W. Grimes int
68*e2662256SAymeric Wibo verscmp(const FTSENT *a, const FTSENT *b)
69*e2662256SAymeric Wibo {
70*e2662256SAymeric Wibo 
71*e2662256SAymeric Wibo 	return (strverscmp(a->fts_name, b->fts_name));
72*e2662256SAymeric Wibo }
73*e2662256SAymeric Wibo 
74*e2662256SAymeric Wibo int
75*e2662256SAymeric Wibo revverscmp(const FTSENT *a, const FTSENT *b)
76*e2662256SAymeric Wibo {
77*e2662256SAymeric Wibo 
78*e2662256SAymeric Wibo 	return (strverscmp(b->fts_name, a->fts_name));
79*e2662256SAymeric Wibo }
80*e2662256SAymeric Wibo 
81*e2662256SAymeric Wibo int
8246251ddeSWarner Losh modcmp(const FTSENT *a, const FTSENT *b)
834b88c807SRodney W. Grimes {
84f137c9c1SDavid Malone 
8599742a23SEd Schouten 	if (b->fts_statp->st_mtim.tv_sec >
8699742a23SEd Schouten 	    a->fts_statp->st_mtim.tv_sec)
87f137c9c1SDavid Malone 		return (1);
8899742a23SEd Schouten 	if (b->fts_statp->st_mtim.tv_sec <
8999742a23SEd Schouten 	    a->fts_statp->st_mtim.tv_sec)
90f137c9c1SDavid Malone 		return (-1);
9199742a23SEd Schouten 	if (b->fts_statp->st_mtim.tv_nsec >
9299742a23SEd Schouten 	    a->fts_statp->st_mtim.tv_nsec)
93f137c9c1SDavid Malone 		return (1);
9499742a23SEd Schouten 	if (b->fts_statp->st_mtim.tv_nsec <
9599742a23SEd Schouten 	    a->fts_statp->st_mtim.tv_nsec)
96f137c9c1SDavid Malone 		return (-1);
979aa68a3fSGreg Lehey 	if (f_samesort)
989aa68a3fSGreg Lehey 		return (strcoll(b->fts_name, a->fts_name));
999aa68a3fSGreg Lehey 	else
10060e52383SDavid Malone 		return (strcoll(a->fts_name, b->fts_name));
1014b88c807SRodney W. Grimes }
1024b88c807SRodney W. Grimes 
1034b88c807SRodney W. Grimes int
10446251ddeSWarner Losh revmodcmp(const FTSENT *a, const FTSENT *b)
1054b88c807SRodney W. Grimes {
106f137c9c1SDavid Malone 
10760e52383SDavid Malone 	return (modcmp(b, a));
1084b88c807SRodney W. Grimes }
1094b88c807SRodney W. Grimes 
1104b88c807SRodney W. Grimes int
11146251ddeSWarner Losh acccmp(const FTSENT *a, const FTSENT *b)
1124b88c807SRodney W. Grimes {
113f137c9c1SDavid Malone 
11499742a23SEd Schouten 	if (b->fts_statp->st_atim.tv_sec >
11599742a23SEd Schouten 	    a->fts_statp->st_atim.tv_sec)
116f137c9c1SDavid Malone 		return (1);
11799742a23SEd Schouten 	if (b->fts_statp->st_atim.tv_sec <
11899742a23SEd Schouten 	    a->fts_statp->st_atim.tv_sec)
119f137c9c1SDavid Malone 		return (-1);
12099742a23SEd Schouten 	if (b->fts_statp->st_atim.tv_nsec >
12199742a23SEd Schouten 	    a->fts_statp->st_atim.tv_nsec)
122f137c9c1SDavid Malone 		return (1);
12399742a23SEd Schouten 	if (b->fts_statp->st_atim.tv_nsec <
12499742a23SEd Schouten 	    a->fts_statp->st_atim.tv_nsec)
125f137c9c1SDavid Malone 		return (-1);
1269aa68a3fSGreg Lehey 	if (f_samesort)
1279aa68a3fSGreg Lehey 		return (strcoll(b->fts_name, a->fts_name));
1289aa68a3fSGreg Lehey 	else
12960e52383SDavid Malone 		return (strcoll(a->fts_name, b->fts_name));
1304b88c807SRodney W. Grimes }
1314b88c807SRodney W. Grimes 
1324b88c807SRodney W. Grimes int
13346251ddeSWarner Losh revacccmp(const FTSENT *a, const FTSENT *b)
1344b88c807SRodney W. Grimes {
135f137c9c1SDavid Malone 
13660e52383SDavid Malone 	return (acccmp(b, a));
1374b88c807SRodney W. Grimes }
1384b88c807SRodney W. Grimes 
1394b88c807SRodney W. Grimes int
140fe79420eSJohn Baldwin birthcmp(const FTSENT *a, const FTSENT *b)
141fe79420eSJohn Baldwin {
142fe79420eSJohn Baldwin 
14399742a23SEd Schouten 	if (b->fts_statp->st_birthtim.tv_sec >
14499742a23SEd Schouten 	    a->fts_statp->st_birthtim.tv_sec)
145fe79420eSJohn Baldwin 		return (1);
14699742a23SEd Schouten 	if (b->fts_statp->st_birthtim.tv_sec <
14799742a23SEd Schouten 	    a->fts_statp->st_birthtim.tv_sec)
148fe79420eSJohn Baldwin 		return (-1);
14999742a23SEd Schouten 	if (b->fts_statp->st_birthtim.tv_nsec >
15099742a23SEd Schouten 	    a->fts_statp->st_birthtim.tv_nsec)
151fe79420eSJohn Baldwin 		return (1);
15299742a23SEd Schouten 	if (b->fts_statp->st_birthtim.tv_nsec <
15399742a23SEd Schouten 	    a->fts_statp->st_birthtim.tv_nsec)
154fe79420eSJohn Baldwin 		return (-1);
1559aa68a3fSGreg Lehey 	if (f_samesort)
1569aa68a3fSGreg Lehey 		return (strcoll(b->fts_name, a->fts_name));
1579aa68a3fSGreg Lehey 	else
158fe79420eSJohn Baldwin 		return (strcoll(a->fts_name, b->fts_name));
159fe79420eSJohn Baldwin }
160fe79420eSJohn Baldwin 
161fe79420eSJohn Baldwin int
162fe79420eSJohn Baldwin revbirthcmp(const FTSENT *a, const FTSENT *b)
163fe79420eSJohn Baldwin {
164fe79420eSJohn Baldwin 
165fe79420eSJohn Baldwin 	return (birthcmp(b, a));
166fe79420eSJohn Baldwin }
167fe79420eSJohn Baldwin 
168fe79420eSJohn Baldwin int
16946251ddeSWarner Losh statcmp(const FTSENT *a, const FTSENT *b)
1704b88c807SRodney W. Grimes {
171f137c9c1SDavid Malone 
17299742a23SEd Schouten 	if (b->fts_statp->st_ctim.tv_sec >
17399742a23SEd Schouten 	    a->fts_statp->st_ctim.tv_sec)
174f137c9c1SDavid Malone 		return (1);
17599742a23SEd Schouten 	if (b->fts_statp->st_ctim.tv_sec <
17699742a23SEd Schouten 	    a->fts_statp->st_ctim.tv_sec)
177f137c9c1SDavid Malone 		return (-1);
17899742a23SEd Schouten 	if (b->fts_statp->st_ctim.tv_nsec >
17999742a23SEd Schouten 	    a->fts_statp->st_ctim.tv_nsec)
180f137c9c1SDavid Malone 		return (1);
18199742a23SEd Schouten 	if (b->fts_statp->st_ctim.tv_nsec <
18299742a23SEd Schouten 	    a->fts_statp->st_ctim.tv_nsec)
183f137c9c1SDavid Malone 		return (-1);
1849aa68a3fSGreg Lehey 	if (f_samesort)
1859aa68a3fSGreg Lehey 		return (strcoll(b->fts_name, a->fts_name));
1869aa68a3fSGreg Lehey 	else
18760e52383SDavid Malone 		return (strcoll(a->fts_name, b->fts_name));
1884b88c807SRodney W. Grimes }
1894b88c807SRodney W. Grimes 
1904b88c807SRodney W. Grimes int
19146251ddeSWarner Losh revstatcmp(const FTSENT *a, const FTSENT *b)
1924b88c807SRodney W. Grimes {
193f137c9c1SDavid Malone 
19460e52383SDavid Malone 	return (statcmp(b, a));
1954b88c807SRodney W. Grimes }
19671b8b748SDima Dorfman 
19771b8b748SDima Dorfman int
19871b8b748SDima Dorfman sizecmp(const FTSENT *a, const FTSENT *b)
19971b8b748SDima Dorfman {
20071b8b748SDima Dorfman 
20171b8b748SDima Dorfman 	if (b->fts_statp->st_size > a->fts_statp->st_size)
20271b8b748SDima Dorfman 		return (1);
20371b8b748SDima Dorfman 	if (b->fts_statp->st_size < a->fts_statp->st_size)
20471b8b748SDima Dorfman 		return (-1);
20571b8b748SDima Dorfman 	return (strcoll(a->fts_name, b->fts_name));
20671b8b748SDima Dorfman }
20771b8b748SDima Dorfman 
20871b8b748SDima Dorfman int
20971b8b748SDima Dorfman revsizecmp(const FTSENT *a, const FTSENT *b)
21071b8b748SDima Dorfman {
21171b8b748SDima Dorfman 
21271b8b748SDima Dorfman 	return (sizecmp(b, a));
21371b8b748SDima Dorfman }
214