xref: /freebsd/bin/ls/cmp.c (revision 99742a231f9bc662d03fb37ee620500cc1b4a8a7)
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);
8160e52383SDavid Malone 	return (strcoll(a->fts_name, b->fts_name));
824b88c807SRodney W. Grimes }
834b88c807SRodney W. Grimes 
844b88c807SRodney W. Grimes int
8546251ddeSWarner Losh revmodcmp(const FTSENT *a, const FTSENT *b)
864b88c807SRodney W. Grimes {
87f137c9c1SDavid Malone 
8860e52383SDavid Malone 	return (modcmp(b, a));
894b88c807SRodney W. Grimes }
904b88c807SRodney W. Grimes 
914b88c807SRodney W. Grimes int
9246251ddeSWarner Losh acccmp(const FTSENT *a, const FTSENT *b)
934b88c807SRodney W. Grimes {
94f137c9c1SDavid Malone 
9599742a23SEd Schouten 	if (b->fts_statp->st_atim.tv_sec >
9699742a23SEd Schouten 	    a->fts_statp->st_atim.tv_sec)
97f137c9c1SDavid Malone 		return (1);
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_nsec >
10299742a23SEd Schouten 	    a->fts_statp->st_atim.tv_nsec)
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);
10760e52383SDavid Malone 	return (strcoll(a->fts_name, b->fts_name));
1084b88c807SRodney W. Grimes }
1094b88c807SRodney W. Grimes 
1104b88c807SRodney W. Grimes int
11146251ddeSWarner Losh revacccmp(const FTSENT *a, const FTSENT *b)
1124b88c807SRodney W. Grimes {
113f137c9c1SDavid Malone 
11460e52383SDavid Malone 	return (acccmp(b, a));
1154b88c807SRodney W. Grimes }
1164b88c807SRodney W. Grimes 
1174b88c807SRodney W. Grimes int
118fe79420eSJohn Baldwin birthcmp(const FTSENT *a, const FTSENT *b)
119fe79420eSJohn Baldwin {
120fe79420eSJohn Baldwin 
12199742a23SEd Schouten 	if (b->fts_statp->st_birthtim.tv_sec >
12299742a23SEd Schouten 	    a->fts_statp->st_birthtim.tv_sec)
123fe79420eSJohn Baldwin 		return (1);
12499742a23SEd Schouten 	if (b->fts_statp->st_birthtim.tv_sec <
12599742a23SEd Schouten 	    a->fts_statp->st_birthtim.tv_sec)
126fe79420eSJohn Baldwin 		return (-1);
12799742a23SEd Schouten 	if (b->fts_statp->st_birthtim.tv_nsec >
12899742a23SEd Schouten 	    a->fts_statp->st_birthtim.tv_nsec)
129fe79420eSJohn Baldwin 		return (1);
13099742a23SEd Schouten 	if (b->fts_statp->st_birthtim.tv_nsec <
13199742a23SEd Schouten 	    a->fts_statp->st_birthtim.tv_nsec)
132fe79420eSJohn Baldwin 		return (-1);
133fe79420eSJohn Baldwin 	return (strcoll(a->fts_name, b->fts_name));
134fe79420eSJohn Baldwin }
135fe79420eSJohn Baldwin 
136fe79420eSJohn Baldwin int
137fe79420eSJohn Baldwin revbirthcmp(const FTSENT *a, const FTSENT *b)
138fe79420eSJohn Baldwin {
139fe79420eSJohn Baldwin 
140fe79420eSJohn Baldwin 	return (birthcmp(b, a));
141fe79420eSJohn Baldwin }
142fe79420eSJohn Baldwin 
143fe79420eSJohn Baldwin int
14446251ddeSWarner Losh statcmp(const FTSENT *a, const FTSENT *b)
1454b88c807SRodney W. Grimes {
146f137c9c1SDavid Malone 
14799742a23SEd Schouten 	if (b->fts_statp->st_ctim.tv_sec >
14899742a23SEd Schouten 	    a->fts_statp->st_ctim.tv_sec)
149f137c9c1SDavid Malone 		return (1);
15099742a23SEd Schouten 	if (b->fts_statp->st_ctim.tv_sec <
15199742a23SEd Schouten 	    a->fts_statp->st_ctim.tv_sec)
152f137c9c1SDavid Malone 		return (-1);
15399742a23SEd Schouten 	if (b->fts_statp->st_ctim.tv_nsec >
15499742a23SEd Schouten 	    a->fts_statp->st_ctim.tv_nsec)
155f137c9c1SDavid Malone 		return (1);
15699742a23SEd Schouten 	if (b->fts_statp->st_ctim.tv_nsec <
15799742a23SEd Schouten 	    a->fts_statp->st_ctim.tv_nsec)
158f137c9c1SDavid Malone 		return (-1);
15960e52383SDavid Malone 	return (strcoll(a->fts_name, b->fts_name));
1604b88c807SRodney W. Grimes }
1614b88c807SRodney W. Grimes 
1624b88c807SRodney W. Grimes int
16346251ddeSWarner Losh revstatcmp(const FTSENT *a, const FTSENT *b)
1644b88c807SRodney W. Grimes {
165f137c9c1SDavid Malone 
16660e52383SDavid Malone 	return (statcmp(b, a));
1674b88c807SRodney W. Grimes }
16871b8b748SDima Dorfman 
16971b8b748SDima Dorfman int
17071b8b748SDima Dorfman sizecmp(const FTSENT *a, const FTSENT *b)
17171b8b748SDima Dorfman {
17271b8b748SDima Dorfman 
17371b8b748SDima Dorfman 	if (b->fts_statp->st_size > a->fts_statp->st_size)
17471b8b748SDima Dorfman 		return (1);
17571b8b748SDima Dorfman 	if (b->fts_statp->st_size < a->fts_statp->st_size)
17671b8b748SDima Dorfman 		return (-1);
17771b8b748SDima Dorfman 	return (strcoll(a->fts_name, b->fts_name));
17871b8b748SDima Dorfman }
17971b8b748SDima Dorfman 
18071b8b748SDima Dorfman int
18171b8b748SDima Dorfman revsizecmp(const FTSENT *a, const FTSENT *b)
18271b8b748SDima Dorfman {
18371b8b748SDima Dorfman 
18471b8b748SDima Dorfman 	return (sizecmp(b, a));
18571b8b748SDima Dorfman }
186