xref: /freebsd/bin/ls/cmp.c (revision 8a16b7a18f5d0b031f09832fd7752fba717e2a97)
19ddb49cbSWarner Losh /*-
2*8a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*8a16b7a1SPedro 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
6846251ddeSWarner Losh modcmp(const FTSENT *a, const FTSENT *b)
694b88c807SRodney W. Grimes {
70f137c9c1SDavid Malone 
7199742a23SEd Schouten 	if (b->fts_statp->st_mtim.tv_sec >
7299742a23SEd Schouten 	    a->fts_statp->st_mtim.tv_sec)
73f137c9c1SDavid Malone 		return (1);
7499742a23SEd Schouten 	if (b->fts_statp->st_mtim.tv_sec <
7599742a23SEd Schouten 	    a->fts_statp->st_mtim.tv_sec)
76f137c9c1SDavid Malone 		return (-1);
7799742a23SEd Schouten 	if (b->fts_statp->st_mtim.tv_nsec >
7899742a23SEd Schouten 	    a->fts_statp->st_mtim.tv_nsec)
79f137c9c1SDavid Malone 		return (1);
8099742a23SEd Schouten 	if (b->fts_statp->st_mtim.tv_nsec <
8199742a23SEd Schouten 	    a->fts_statp->st_mtim.tv_nsec)
82f137c9c1SDavid Malone 		return (-1);
839aa68a3fSGreg Lehey 	if (f_samesort)
849aa68a3fSGreg Lehey 		return (strcoll(b->fts_name, a->fts_name));
859aa68a3fSGreg Lehey 	else
8660e52383SDavid Malone 		return (strcoll(a->fts_name, b->fts_name));
874b88c807SRodney W. Grimes }
884b88c807SRodney W. Grimes 
894b88c807SRodney W. Grimes int
9046251ddeSWarner Losh revmodcmp(const FTSENT *a, const FTSENT *b)
914b88c807SRodney W. Grimes {
92f137c9c1SDavid Malone 
9360e52383SDavid Malone 	return (modcmp(b, a));
944b88c807SRodney W. Grimes }
954b88c807SRodney W. Grimes 
964b88c807SRodney W. Grimes int
9746251ddeSWarner Losh acccmp(const FTSENT *a, const FTSENT *b)
984b88c807SRodney W. Grimes {
99f137c9c1SDavid Malone 
10099742a23SEd Schouten 	if (b->fts_statp->st_atim.tv_sec >
10199742a23SEd Schouten 	    a->fts_statp->st_atim.tv_sec)
102f137c9c1SDavid Malone 		return (1);
10399742a23SEd Schouten 	if (b->fts_statp->st_atim.tv_sec <
10499742a23SEd Schouten 	    a->fts_statp->st_atim.tv_sec)
105f137c9c1SDavid Malone 		return (-1);
10699742a23SEd Schouten 	if (b->fts_statp->st_atim.tv_nsec >
10799742a23SEd Schouten 	    a->fts_statp->st_atim.tv_nsec)
108f137c9c1SDavid Malone 		return (1);
10999742a23SEd Schouten 	if (b->fts_statp->st_atim.tv_nsec <
11099742a23SEd Schouten 	    a->fts_statp->st_atim.tv_nsec)
111f137c9c1SDavid Malone 		return (-1);
1129aa68a3fSGreg Lehey 	if (f_samesort)
1139aa68a3fSGreg Lehey 		return (strcoll(b->fts_name, a->fts_name));
1149aa68a3fSGreg Lehey 	else
11560e52383SDavid Malone 		return (strcoll(a->fts_name, b->fts_name));
1164b88c807SRodney W. Grimes }
1174b88c807SRodney W. Grimes 
1184b88c807SRodney W. Grimes int
11946251ddeSWarner Losh revacccmp(const FTSENT *a, const FTSENT *b)
1204b88c807SRodney W. Grimes {
121f137c9c1SDavid Malone 
12260e52383SDavid Malone 	return (acccmp(b, a));
1234b88c807SRodney W. Grimes }
1244b88c807SRodney W. Grimes 
1254b88c807SRodney W. Grimes int
126fe79420eSJohn Baldwin birthcmp(const FTSENT *a, const FTSENT *b)
127fe79420eSJohn Baldwin {
128fe79420eSJohn Baldwin 
12999742a23SEd Schouten 	if (b->fts_statp->st_birthtim.tv_sec >
13099742a23SEd Schouten 	    a->fts_statp->st_birthtim.tv_sec)
131fe79420eSJohn Baldwin 		return (1);
13299742a23SEd Schouten 	if (b->fts_statp->st_birthtim.tv_sec <
13399742a23SEd Schouten 	    a->fts_statp->st_birthtim.tv_sec)
134fe79420eSJohn Baldwin 		return (-1);
13599742a23SEd Schouten 	if (b->fts_statp->st_birthtim.tv_nsec >
13699742a23SEd Schouten 	    a->fts_statp->st_birthtim.tv_nsec)
137fe79420eSJohn Baldwin 		return (1);
13899742a23SEd Schouten 	if (b->fts_statp->st_birthtim.tv_nsec <
13999742a23SEd Schouten 	    a->fts_statp->st_birthtim.tv_nsec)
140fe79420eSJohn Baldwin 		return (-1);
1419aa68a3fSGreg Lehey 	if (f_samesort)
1429aa68a3fSGreg Lehey 		return (strcoll(b->fts_name, a->fts_name));
1439aa68a3fSGreg Lehey 	else
144fe79420eSJohn Baldwin 		return (strcoll(a->fts_name, b->fts_name));
145fe79420eSJohn Baldwin }
146fe79420eSJohn Baldwin 
147fe79420eSJohn Baldwin int
148fe79420eSJohn Baldwin revbirthcmp(const FTSENT *a, const FTSENT *b)
149fe79420eSJohn Baldwin {
150fe79420eSJohn Baldwin 
151fe79420eSJohn Baldwin 	return (birthcmp(b, a));
152fe79420eSJohn Baldwin }
153fe79420eSJohn Baldwin 
154fe79420eSJohn Baldwin int
15546251ddeSWarner Losh statcmp(const FTSENT *a, const FTSENT *b)
1564b88c807SRodney W. Grimes {
157f137c9c1SDavid Malone 
15899742a23SEd Schouten 	if (b->fts_statp->st_ctim.tv_sec >
15999742a23SEd Schouten 	    a->fts_statp->st_ctim.tv_sec)
160f137c9c1SDavid Malone 		return (1);
16199742a23SEd Schouten 	if (b->fts_statp->st_ctim.tv_sec <
16299742a23SEd Schouten 	    a->fts_statp->st_ctim.tv_sec)
163f137c9c1SDavid Malone 		return (-1);
16499742a23SEd Schouten 	if (b->fts_statp->st_ctim.tv_nsec >
16599742a23SEd Schouten 	    a->fts_statp->st_ctim.tv_nsec)
166f137c9c1SDavid Malone 		return (1);
16799742a23SEd Schouten 	if (b->fts_statp->st_ctim.tv_nsec <
16899742a23SEd Schouten 	    a->fts_statp->st_ctim.tv_nsec)
169f137c9c1SDavid Malone 		return (-1);
1709aa68a3fSGreg Lehey 	if (f_samesort)
1719aa68a3fSGreg Lehey 		return (strcoll(b->fts_name, a->fts_name));
1729aa68a3fSGreg Lehey 	else
17360e52383SDavid Malone 		return (strcoll(a->fts_name, b->fts_name));
1744b88c807SRodney W. Grimes }
1754b88c807SRodney W. Grimes 
1764b88c807SRodney W. Grimes int
17746251ddeSWarner Losh revstatcmp(const FTSENT *a, const FTSENT *b)
1784b88c807SRodney W. Grimes {
179f137c9c1SDavid Malone 
18060e52383SDavid Malone 	return (statcmp(b, a));
1814b88c807SRodney W. Grimes }
18271b8b748SDima Dorfman 
18371b8b748SDima Dorfman int
18471b8b748SDima Dorfman sizecmp(const FTSENT *a, const FTSENT *b)
18571b8b748SDima Dorfman {
18671b8b748SDima Dorfman 
18771b8b748SDima Dorfman 	if (b->fts_statp->st_size > a->fts_statp->st_size)
18871b8b748SDima Dorfman 		return (1);
18971b8b748SDima Dorfman 	if (b->fts_statp->st_size < a->fts_statp->st_size)
19071b8b748SDima Dorfman 		return (-1);
19171b8b748SDima Dorfman 	return (strcoll(a->fts_name, b->fts_name));
19271b8b748SDima Dorfman }
19371b8b748SDima Dorfman 
19471b8b748SDima Dorfman int
19571b8b748SDima Dorfman revsizecmp(const FTSENT *a, const FTSENT *b)
19671b8b748SDima Dorfman {
19771b8b748SDima Dorfman 
19871b8b748SDima Dorfman 	return (sizecmp(b, a));
19971b8b748SDima Dorfman }
200