xref: /freebsd/bin/ls/cmp.c (revision 9aa68a3ffae06d619ec56178d1397b59fd7751bf)
19ddb49cbSWarner Losh /*-
24b88c807SRodney W. Grimes  * Copyright (c) 1989, 1993
34b88c807SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
44b88c807SRodney W. Grimes  *
54b88c807SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
64b88c807SRodney W. Grimes  * Michael Fischbein.
74b88c807SRodney W. Grimes  *
84b88c807SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
94b88c807SRodney W. Grimes  * modification, are permitted provided that the following conditions
104b88c807SRodney W. Grimes  * are met:
114b88c807SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
124b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
134b88c807SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
144b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
154b88c807SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
164b88c807SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
174b88c807SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
184b88c807SRodney W. Grimes  *    without specific prior written permission.
194b88c807SRodney W. Grimes  *
204b88c807SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
214b88c807SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
224b88c807SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
234b88c807SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
244b88c807SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
254b88c807SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
264b88c807SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
274b88c807SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
284b88c807SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
294b88c807SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
304b88c807SRodney W. Grimes  * SUCH DAMAGE.
314b88c807SRodney W. Grimes  */
324b88c807SRodney W. Grimes 
33febad2fcSSteve Price #if 0
34c73d77ceSMark Murray #ifndef lint
35febad2fcSSteve Price static char sccsid[] = "@(#)cmp.c	8.1 (Berkeley) 5/31/93";
364b88c807SRodney W. Grimes #endif /* not lint */
37c73d77ceSMark Murray #endif
385eb43ac2SDavid E. O'Brien #include <sys/cdefs.h>
395eb43ac2SDavid E. O'Brien __FBSDID("$FreeBSD$");
405eb43ac2SDavid E. O'Brien 
414b88c807SRodney W. Grimes 
424b88c807SRodney W. Grimes #include <sys/types.h>
434b88c807SRodney W. Grimes #include <sys/stat.h>
444b88c807SRodney W. Grimes 
454b88c807SRodney W. Grimes #include <fts.h>
464b88c807SRodney W. Grimes #include <string.h>
474b88c807SRodney W. Grimes 
484b88c807SRodney W. Grimes #include "ls.h"
494b88c807SRodney W. Grimes #include "extern.h"
504b88c807SRodney W. Grimes 
514b88c807SRodney W. Grimes int
5246251ddeSWarner Losh namecmp(const FTSENT *a, const FTSENT *b)
534b88c807SRodney W. Grimes {
54f137c9c1SDavid Malone 
55942c84aaSAndrey A. Chernov 	return (strcoll(a->fts_name, b->fts_name));
564b88c807SRodney W. Grimes }
574b88c807SRodney W. Grimes 
584b88c807SRodney W. Grimes int
5946251ddeSWarner Losh revnamecmp(const FTSENT *a, const FTSENT *b)
604b88c807SRodney W. Grimes {
61f137c9c1SDavid Malone 
62942c84aaSAndrey A. Chernov 	return (strcoll(b->fts_name, a->fts_name));
634b88c807SRodney W. Grimes }
644b88c807SRodney W. Grimes 
654b88c807SRodney W. Grimes int
6646251ddeSWarner Losh modcmp(const FTSENT *a, const FTSENT *b)
674b88c807SRodney W. Grimes {
68f137c9c1SDavid Malone 
6999742a23SEd Schouten 	if (b->fts_statp->st_mtim.tv_sec >
7099742a23SEd Schouten 	    a->fts_statp->st_mtim.tv_sec)
71f137c9c1SDavid Malone 		return (1);
7299742a23SEd Schouten 	if (b->fts_statp->st_mtim.tv_sec <
7399742a23SEd Schouten 	    a->fts_statp->st_mtim.tv_sec)
74f137c9c1SDavid Malone 		return (-1);
7599742a23SEd Schouten 	if (b->fts_statp->st_mtim.tv_nsec >
7699742a23SEd Schouten 	    a->fts_statp->st_mtim.tv_nsec)
77f137c9c1SDavid Malone 		return (1);
7899742a23SEd Schouten 	if (b->fts_statp->st_mtim.tv_nsec <
7999742a23SEd Schouten 	    a->fts_statp->st_mtim.tv_nsec)
80f137c9c1SDavid Malone 		return (-1);
81*9aa68a3fSGreg Lehey         if (f_samesort)
82*9aa68a3fSGreg Lehey 		return (strcoll(b->fts_name, a->fts_name));
83*9aa68a3fSGreg Lehey         else
8460e52383SDavid Malone 		return (strcoll(a->fts_name, b->fts_name));
854b88c807SRodney W. Grimes }
864b88c807SRodney W. Grimes 
874b88c807SRodney W. Grimes int
8846251ddeSWarner Losh revmodcmp(const FTSENT *a, const FTSENT *b)
894b88c807SRodney W. Grimes {
90f137c9c1SDavid Malone 
9160e52383SDavid Malone 	return (modcmp(b, a));
924b88c807SRodney W. Grimes }
934b88c807SRodney W. Grimes 
944b88c807SRodney W. Grimes int
9546251ddeSWarner Losh acccmp(const FTSENT *a, const FTSENT *b)
964b88c807SRodney W. Grimes {
97f137c9c1SDavid Malone 
9899742a23SEd Schouten 	if (b->fts_statp->st_atim.tv_sec >
9999742a23SEd Schouten 	    a->fts_statp->st_atim.tv_sec)
100f137c9c1SDavid Malone 		return (1);
10199742a23SEd Schouten 	if (b->fts_statp->st_atim.tv_sec <
10299742a23SEd Schouten 	    a->fts_statp->st_atim.tv_sec)
103f137c9c1SDavid Malone 		return (-1);
10499742a23SEd Schouten 	if (b->fts_statp->st_atim.tv_nsec >
10599742a23SEd Schouten 	    a->fts_statp->st_atim.tv_nsec)
106f137c9c1SDavid Malone 		return (1);
10799742a23SEd Schouten 	if (b->fts_statp->st_atim.tv_nsec <
10899742a23SEd Schouten 	    a->fts_statp->st_atim.tv_nsec)
109f137c9c1SDavid Malone 		return (-1);
110*9aa68a3fSGreg Lehey         if (f_samesort)
111*9aa68a3fSGreg Lehey 		return (strcoll(b->fts_name, a->fts_name));
112*9aa68a3fSGreg Lehey         else
11360e52383SDavid Malone 		return (strcoll(a->fts_name, b->fts_name));
1144b88c807SRodney W. Grimes }
1154b88c807SRodney W. Grimes 
1164b88c807SRodney W. Grimes int
11746251ddeSWarner Losh revacccmp(const FTSENT *a, const FTSENT *b)
1184b88c807SRodney W. Grimes {
119f137c9c1SDavid Malone 
12060e52383SDavid Malone 	return (acccmp(b, a));
1214b88c807SRodney W. Grimes }
1224b88c807SRodney W. Grimes 
1234b88c807SRodney W. Grimes int
124fe79420eSJohn Baldwin birthcmp(const FTSENT *a, const FTSENT *b)
125fe79420eSJohn Baldwin {
126fe79420eSJohn Baldwin 
12799742a23SEd Schouten 	if (b->fts_statp->st_birthtim.tv_sec >
12899742a23SEd Schouten 	    a->fts_statp->st_birthtim.tv_sec)
129fe79420eSJohn Baldwin 		return (1);
13099742a23SEd Schouten 	if (b->fts_statp->st_birthtim.tv_sec <
13199742a23SEd Schouten 	    a->fts_statp->st_birthtim.tv_sec)
132fe79420eSJohn Baldwin 		return (-1);
13399742a23SEd Schouten 	if (b->fts_statp->st_birthtim.tv_nsec >
13499742a23SEd Schouten 	    a->fts_statp->st_birthtim.tv_nsec)
135fe79420eSJohn Baldwin 		return (1);
13699742a23SEd Schouten 	if (b->fts_statp->st_birthtim.tv_nsec <
13799742a23SEd Schouten 	    a->fts_statp->st_birthtim.tv_nsec)
138fe79420eSJohn Baldwin 		return (-1);
139*9aa68a3fSGreg Lehey         if (f_samesort)
140*9aa68a3fSGreg Lehey 		return (strcoll(b->fts_name, a->fts_name));
141*9aa68a3fSGreg Lehey         else
142fe79420eSJohn Baldwin 		return (strcoll(a->fts_name, b->fts_name));
143fe79420eSJohn Baldwin }
144fe79420eSJohn Baldwin 
145fe79420eSJohn Baldwin int
146fe79420eSJohn Baldwin revbirthcmp(const FTSENT *a, const FTSENT *b)
147fe79420eSJohn Baldwin {
148fe79420eSJohn Baldwin 
149fe79420eSJohn Baldwin 	return (birthcmp(b, a));
150fe79420eSJohn Baldwin }
151fe79420eSJohn Baldwin 
152fe79420eSJohn Baldwin int
15346251ddeSWarner Losh statcmp(const FTSENT *a, const FTSENT *b)
1544b88c807SRodney W. Grimes {
155f137c9c1SDavid Malone 
15699742a23SEd Schouten 	if (b->fts_statp->st_ctim.tv_sec >
15799742a23SEd Schouten 	    a->fts_statp->st_ctim.tv_sec)
158f137c9c1SDavid Malone 		return (1);
15999742a23SEd Schouten 	if (b->fts_statp->st_ctim.tv_sec <
16099742a23SEd Schouten 	    a->fts_statp->st_ctim.tv_sec)
161f137c9c1SDavid Malone 		return (-1);
16299742a23SEd Schouten 	if (b->fts_statp->st_ctim.tv_nsec >
16399742a23SEd Schouten 	    a->fts_statp->st_ctim.tv_nsec)
164f137c9c1SDavid Malone 		return (1);
16599742a23SEd Schouten 	if (b->fts_statp->st_ctim.tv_nsec <
16699742a23SEd Schouten 	    a->fts_statp->st_ctim.tv_nsec)
167f137c9c1SDavid Malone 		return (-1);
168*9aa68a3fSGreg Lehey         if (f_samesort)
169*9aa68a3fSGreg Lehey 		return (strcoll(b->fts_name, a->fts_name));
170*9aa68a3fSGreg Lehey         else
17160e52383SDavid Malone 		return (strcoll(a->fts_name, b->fts_name));
1724b88c807SRodney W. Grimes }
1734b88c807SRodney W. Grimes 
1744b88c807SRodney W. Grimes int
17546251ddeSWarner Losh revstatcmp(const FTSENT *a, const FTSENT *b)
1764b88c807SRodney W. Grimes {
177f137c9c1SDavid Malone 
17860e52383SDavid Malone 	return (statcmp(b, a));
1794b88c807SRodney W. Grimes }
18071b8b748SDima Dorfman 
18171b8b748SDima Dorfman int
18271b8b748SDima Dorfman sizecmp(const FTSENT *a, const FTSENT *b)
18371b8b748SDima Dorfman {
18471b8b748SDima Dorfman 
18571b8b748SDima Dorfman 	if (b->fts_statp->st_size > a->fts_statp->st_size)
18671b8b748SDima Dorfman 		return (1);
18771b8b748SDima Dorfman 	if (b->fts_statp->st_size < a->fts_statp->st_size)
18871b8b748SDima Dorfman 		return (-1);
18971b8b748SDima Dorfman 	return (strcoll(a->fts_name, b->fts_name));
19071b8b748SDima Dorfman }
19171b8b748SDima Dorfman 
19271b8b748SDima Dorfman int
19371b8b748SDima Dorfman revsizecmp(const FTSENT *a, const FTSENT *b)
19471b8b748SDima Dorfman {
19571b8b748SDima Dorfman 
19671b8b748SDima Dorfman 	return (sizecmp(b, a));
19771b8b748SDima Dorfman }
198