xref: /freebsd/bin/ls/cmp.c (revision 60e52383e720dd163ea4af154c9d3686cbd930aa)
14b88c807SRodney W. Grimes /*
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 {
54942c84aaSAndrey A. Chernov 	return (strcoll(a->fts_name, b->fts_name));
554b88c807SRodney W. Grimes }
564b88c807SRodney W. Grimes 
574b88c807SRodney W. Grimes int
5846251ddeSWarner Losh revnamecmp(const FTSENT *a, const FTSENT *b)
594b88c807SRodney W. Grimes {
60942c84aaSAndrey A. Chernov 	return (strcoll(b->fts_name, a->fts_name));
614b88c807SRodney W. Grimes }
624b88c807SRodney W. Grimes 
634b88c807SRodney W. Grimes int
6446251ddeSWarner Losh modcmp(const FTSENT *a, const FTSENT *b)
654b88c807SRodney W. Grimes {
6660e52383SDavid Malone 	if (b->fts_statp->st_mtimespec.tv_sec >
6760e52383SDavid Malone 	    a->fts_statp->st_mtimespec.tv_sec)
6860e52383SDavid Malone 		return 1;
6960e52383SDavid Malone 	if (b->fts_statp->st_mtimespec.tv_sec <
7060e52383SDavid Malone 	    a->fts_statp->st_mtimespec.tv_sec)
7160e52383SDavid Malone 		return -1;
7260e52383SDavid Malone 	if (b->fts_statp->st_mtimespec.tv_nsec >
7360e52383SDavid Malone 	    a->fts_statp->st_mtimespec.tv_nsec)
7460e52383SDavid Malone 		return 1;
7560e52383SDavid Malone 	if (b->fts_statp->st_mtimespec.tv_nsec <
7660e52383SDavid Malone 	    a->fts_statp->st_mtimespec.tv_nsec)
7760e52383SDavid Malone 		return -1;
7860e52383SDavid Malone 	return (strcoll(a->fts_name, b->fts_name));
794b88c807SRodney W. Grimes }
804b88c807SRodney W. Grimes 
814b88c807SRodney W. Grimes int
8246251ddeSWarner Losh revmodcmp(const FTSENT *a, const FTSENT *b)
834b88c807SRodney W. Grimes {
8460e52383SDavid Malone 	return (modcmp(b,a));
854b88c807SRodney W. Grimes }
864b88c807SRodney W. Grimes 
874b88c807SRodney W. Grimes int
8846251ddeSWarner Losh acccmp(const FTSENT *a, const FTSENT *b)
894b88c807SRodney W. Grimes {
9060e52383SDavid Malone 	if (b->fts_statp->st_atimespec.tv_sec >
9160e52383SDavid Malone 	    a->fts_statp->st_atimespec.tv_sec)
9260e52383SDavid Malone 		return 1;
9360e52383SDavid Malone 	if (b->fts_statp->st_atimespec.tv_sec <
9460e52383SDavid Malone 	    a->fts_statp->st_atimespec.tv_sec)
9560e52383SDavid Malone 		return -1;
9660e52383SDavid Malone 	if (b->fts_statp->st_atimespec.tv_nsec >
9760e52383SDavid Malone 	    a->fts_statp->st_atimespec.tv_nsec)
9860e52383SDavid Malone 		return 1;
9960e52383SDavid Malone 	if (b->fts_statp->st_atimespec.tv_nsec <
10060e52383SDavid Malone 	    a->fts_statp->st_atimespec.tv_nsec)
10160e52383SDavid Malone 		return -1;
10260e52383SDavid Malone 	return (strcoll(a->fts_name, b->fts_name));
1034b88c807SRodney W. Grimes }
1044b88c807SRodney W. Grimes 
1054b88c807SRodney W. Grimes int
10646251ddeSWarner Losh revacccmp(const FTSENT *a, const FTSENT *b)
1074b88c807SRodney W. Grimes {
10860e52383SDavid Malone 	return (acccmp(b,a));
1094b88c807SRodney W. Grimes }
1104b88c807SRodney W. Grimes 
1114b88c807SRodney W. Grimes int
11246251ddeSWarner Losh statcmp(const FTSENT *a, const FTSENT *b)
1134b88c807SRodney W. Grimes {
11460e52383SDavid Malone 	if (b->fts_statp->st_ctimespec.tv_sec >
11560e52383SDavid Malone 	    a->fts_statp->st_ctimespec.tv_sec)
11660e52383SDavid Malone 		return 1;
11760e52383SDavid Malone 	if (b->fts_statp->st_ctimespec.tv_sec <
11860e52383SDavid Malone 	    a->fts_statp->st_ctimespec.tv_sec)
11960e52383SDavid Malone 		return -1;
12060e52383SDavid Malone 	if (b->fts_statp->st_ctimespec.tv_nsec >
12160e52383SDavid Malone 	    a->fts_statp->st_ctimespec.tv_nsec)
12260e52383SDavid Malone 		return 1;
12360e52383SDavid Malone 	if (b->fts_statp->st_ctimespec.tv_nsec <
12460e52383SDavid Malone 	    a->fts_statp->st_ctimespec.tv_nsec)
12560e52383SDavid Malone 		return -1;
12660e52383SDavid Malone 	return (strcoll(a->fts_name, b->fts_name));
1274b88c807SRodney W. Grimes }
1284b88c807SRodney W. Grimes 
1294b88c807SRodney W. Grimes int
13046251ddeSWarner Losh revstatcmp(const FTSENT *a, const FTSENT *b)
1314b88c807SRodney W. Grimes {
13260e52383SDavid Malone 	return (statcmp(b,a));
1334b88c807SRodney W. Grimes }
134