1/*	$OpenBSD: cmp.c,v 1.7 2023/08/08 04:45:44 guenther Exp $	*/
2/*	$NetBSD: cmp.c,v 1.10 1996/07/08 10:32:01 mycroft Exp $	*/
3
4/*
5 * Copyright (c) 1989, 1993
6 *	The Regents of the University of California.  All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * Michael Fischbein.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 *    may be used to endorse or promote products derived from this software
21 *    without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36#include <sys/types.h>
37#include <sys/stat.h>
38
39#include <fts.h>
40#include <string.h>
41
42#include "ls.h"
43#include "extern.h"
44
45int
46namecmp(const FTSENT *a, const FTSENT *b)
47{
48	return (strcmp(a->fts_name, b->fts_name));
49}
50
51int
52revnamecmp(const FTSENT *a, const FTSENT *b)
53{
54	return (strcmp(b->fts_name, a->fts_name));
55}
56
57int
58modcmp(const FTSENT *a, const FTSENT *b)
59{
60	if (b->fts_statp->st_mtime > a->fts_statp->st_mtime)
61		return (1);
62	else if (b->fts_statp->st_mtime < a->fts_statp->st_mtime)
63		return (-1);
64	else if (b->fts_statp->st_mtim.tv_nsec > a->fts_statp->st_mtim.tv_nsec)
65		return (1);
66	else if (b->fts_statp->st_mtim.tv_nsec < a->fts_statp->st_mtim.tv_nsec)
67		return (-1);
68	else
69		return (namecmp(a, b));
70}
71
72int
73revmodcmp(const FTSENT *a, const FTSENT *b)
74{
75	if (b->fts_statp->st_mtime > a->fts_statp->st_mtime)
76		return (-1);
77	else if (b->fts_statp->st_mtime < a->fts_statp->st_mtime)
78		return (1);
79	else if (b->fts_statp->st_mtim.tv_nsec > a->fts_statp->st_mtim.tv_nsec)
80		return (-1);
81	else if (b->fts_statp->st_mtim.tv_nsec < a->fts_statp->st_mtim.tv_nsec)
82		return (1);
83	else
84		return (revnamecmp(a, b));
85}
86
87int
88acccmp(const FTSENT *a, const FTSENT *b)
89{
90	if (b->fts_statp->st_atime > a->fts_statp->st_atime)
91		return (1);
92	else if (b->fts_statp->st_atime < a->fts_statp->st_atime)
93		return (-1);
94	else if (b->fts_statp->st_atim.tv_nsec > a->fts_statp->st_atim.tv_nsec)
95		return (1);
96	else if (b->fts_statp->st_atim.tv_nsec < a->fts_statp->st_atim.tv_nsec)
97		return (-1);
98	else
99		return (namecmp(a, b));
100}
101
102int
103revacccmp(const FTSENT *a, const FTSENT *b)
104{
105	if (b->fts_statp->st_atime > a->fts_statp->st_atime)
106		return (-1);
107	else if (b->fts_statp->st_atime < a->fts_statp->st_atime)
108		return (1);
109	else if (b->fts_statp->st_atim.tv_nsec > a->fts_statp->st_atim.tv_nsec)
110		return (-1);
111	else if (b->fts_statp->st_atim.tv_nsec < a->fts_statp->st_atim.tv_nsec)
112		return (1);
113	else
114		return (revnamecmp(a, b));
115}
116
117int
118statcmp(const FTSENT *a, const FTSENT *b)
119{
120	if (b->fts_statp->st_ctime > a->fts_statp->st_ctime)
121		return (1);
122	else if (b->fts_statp->st_ctime < a->fts_statp->st_ctime)
123		return (-1);
124	else if (b->fts_statp->st_ctim.tv_nsec > a->fts_statp->st_ctim.tv_nsec)
125		return (1);
126	else if (b->fts_statp->st_ctim.tv_nsec < a->fts_statp->st_ctim.tv_nsec)
127		return (-1);
128	else
129		return (namecmp(a, b));
130}
131
132int
133revstatcmp(const FTSENT *a, const FTSENT *b)
134{
135	if (b->fts_statp->st_ctime > a->fts_statp->st_ctime)
136		return (-1);
137	else if (b->fts_statp->st_ctime < a->fts_statp->st_ctime)
138		return (1);
139	else if (b->fts_statp->st_ctim.tv_nsec > a->fts_statp->st_ctim.tv_nsec)
140		return (-1);
141	else if (b->fts_statp->st_ctim.tv_nsec < a->fts_statp->st_ctim.tv_nsec)
142		return (1);
143	else
144		return (revnamecmp(a, b));
145}
146
147int
148sizecmp(const FTSENT *a, const FTSENT *b)
149{
150	if (b->fts_statp->st_size > a->fts_statp->st_size)
151		return (1);
152	if (b->fts_statp->st_size < a->fts_statp->st_size)
153		return (-1);
154	else
155		return (namecmp(a, b));
156}
157
158int
159revsizecmp(const FTSENT *a, const FTSENT *b)
160{
161	if (b->fts_statp->st_size > a->fts_statp->st_size)
162		return (-1);
163	if (b->fts_statp->st_size < a->fts_statp->st_size)
164		return (1);
165	else
166		return (revnamecmp(a, b));
167}
168