19ddb49cbSWarner Losh /*-
28a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro 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
354b88c807SRodney W. Grimes #include <sys/types.h>
364b88c807SRodney W. Grimes #include <sys/stat.h>
374b88c807SRodney W. Grimes
384b88c807SRodney W. Grimes #include <fts.h>
394b88c807SRodney W. Grimes #include <string.h>
404b88c807SRodney W. Grimes
414b88c807SRodney W. Grimes #include "ls.h"
424b88c807SRodney W. Grimes #include "extern.h"
434b88c807SRodney W. Grimes
444b88c807SRodney W. Grimes int
namecmp(const FTSENT * a,const FTSENT * b)4546251ddeSWarner Losh namecmp(const FTSENT *a, const FTSENT *b)
464b88c807SRodney W. Grimes {
47f137c9c1SDavid Malone
48942c84aaSAndrey A. Chernov return (strcoll(a->fts_name, b->fts_name));
494b88c807SRodney W. Grimes }
504b88c807SRodney W. Grimes
514b88c807SRodney W. Grimes int
revnamecmp(const FTSENT * a,const FTSENT * b)5246251ddeSWarner Losh revnamecmp(const FTSENT *a, const FTSENT *b)
534b88c807SRodney W. Grimes {
54f137c9c1SDavid Malone
55942c84aaSAndrey A. Chernov return (strcoll(b->fts_name, a->fts_name));
564b88c807SRodney W. Grimes }
574b88c807SRodney W. Grimes
584b88c807SRodney W. Grimes int
verscmp(const FTSENT * a,const FTSENT * b)59*e2662256SAymeric Wibo verscmp(const FTSENT *a, const FTSENT *b)
60*e2662256SAymeric Wibo {
61*e2662256SAymeric Wibo
62*e2662256SAymeric Wibo return (strverscmp(a->fts_name, b->fts_name));
63*e2662256SAymeric Wibo }
64*e2662256SAymeric Wibo
65*e2662256SAymeric Wibo int
revverscmp(const FTSENT * a,const FTSENT * b)66*e2662256SAymeric Wibo revverscmp(const FTSENT *a, const FTSENT *b)
67*e2662256SAymeric Wibo {
68*e2662256SAymeric Wibo
69*e2662256SAymeric Wibo return (strverscmp(b->fts_name, a->fts_name));
70*e2662256SAymeric Wibo }
71*e2662256SAymeric Wibo
72*e2662256SAymeric Wibo int
modcmp(const FTSENT * a,const FTSENT * b)7346251ddeSWarner Losh modcmp(const FTSENT *a, const FTSENT *b)
744b88c807SRodney W. Grimes {
75f137c9c1SDavid Malone
7699742a23SEd Schouten if (b->fts_statp->st_mtim.tv_sec >
7799742a23SEd Schouten a->fts_statp->st_mtim.tv_sec)
78f137c9c1SDavid Malone return (1);
7999742a23SEd Schouten if (b->fts_statp->st_mtim.tv_sec <
8099742a23SEd Schouten a->fts_statp->st_mtim.tv_sec)
81f137c9c1SDavid Malone return (-1);
8299742a23SEd Schouten if (b->fts_statp->st_mtim.tv_nsec >
8399742a23SEd Schouten a->fts_statp->st_mtim.tv_nsec)
84f137c9c1SDavid Malone return (1);
8599742a23SEd Schouten if (b->fts_statp->st_mtim.tv_nsec <
8699742a23SEd Schouten a->fts_statp->st_mtim.tv_nsec)
87f137c9c1SDavid Malone return (-1);
889aa68a3fSGreg Lehey if (f_samesort)
899aa68a3fSGreg Lehey return (strcoll(b->fts_name, a->fts_name));
909aa68a3fSGreg Lehey else
9160e52383SDavid Malone return (strcoll(a->fts_name, b->fts_name));
924b88c807SRodney W. Grimes }
934b88c807SRodney W. Grimes
944b88c807SRodney W. Grimes int
revmodcmp(const FTSENT * a,const FTSENT * b)9546251ddeSWarner Losh revmodcmp(const FTSENT *a, const FTSENT *b)
964b88c807SRodney W. Grimes {
97f137c9c1SDavid Malone
9860e52383SDavid Malone return (modcmp(b, a));
994b88c807SRodney W. Grimes }
1004b88c807SRodney W. Grimes
1014b88c807SRodney W. Grimes int
acccmp(const FTSENT * a,const FTSENT * b)10246251ddeSWarner Losh acccmp(const FTSENT *a, const FTSENT *b)
1034b88c807SRodney W. Grimes {
104f137c9c1SDavid Malone
10599742a23SEd Schouten if (b->fts_statp->st_atim.tv_sec >
10699742a23SEd Schouten a->fts_statp->st_atim.tv_sec)
107f137c9c1SDavid Malone return (1);
10899742a23SEd Schouten if (b->fts_statp->st_atim.tv_sec <
10999742a23SEd Schouten a->fts_statp->st_atim.tv_sec)
110f137c9c1SDavid Malone return (-1);
11199742a23SEd Schouten if (b->fts_statp->st_atim.tv_nsec >
11299742a23SEd Schouten a->fts_statp->st_atim.tv_nsec)
113f137c9c1SDavid Malone return (1);
11499742a23SEd Schouten if (b->fts_statp->st_atim.tv_nsec <
11599742a23SEd Schouten a->fts_statp->st_atim.tv_nsec)
116f137c9c1SDavid Malone return (-1);
1179aa68a3fSGreg Lehey if (f_samesort)
1189aa68a3fSGreg Lehey return (strcoll(b->fts_name, a->fts_name));
1199aa68a3fSGreg Lehey else
12060e52383SDavid Malone return (strcoll(a->fts_name, b->fts_name));
1214b88c807SRodney W. Grimes }
1224b88c807SRodney W. Grimes
1234b88c807SRodney W. Grimes int
revacccmp(const FTSENT * a,const FTSENT * b)12446251ddeSWarner Losh revacccmp(const FTSENT *a, const FTSENT *b)
1254b88c807SRodney W. Grimes {
126f137c9c1SDavid Malone
12760e52383SDavid Malone return (acccmp(b, a));
1284b88c807SRodney W. Grimes }
1294b88c807SRodney W. Grimes
1304b88c807SRodney W. Grimes int
birthcmp(const FTSENT * a,const FTSENT * b)131fe79420eSJohn Baldwin birthcmp(const FTSENT *a, const FTSENT *b)
132fe79420eSJohn Baldwin {
133fe79420eSJohn Baldwin
13499742a23SEd Schouten if (b->fts_statp->st_birthtim.tv_sec >
13599742a23SEd Schouten a->fts_statp->st_birthtim.tv_sec)
136fe79420eSJohn Baldwin return (1);
13799742a23SEd Schouten if (b->fts_statp->st_birthtim.tv_sec <
13899742a23SEd Schouten a->fts_statp->st_birthtim.tv_sec)
139fe79420eSJohn Baldwin return (-1);
14099742a23SEd Schouten if (b->fts_statp->st_birthtim.tv_nsec >
14199742a23SEd Schouten a->fts_statp->st_birthtim.tv_nsec)
142fe79420eSJohn Baldwin return (1);
14399742a23SEd Schouten if (b->fts_statp->st_birthtim.tv_nsec <
14499742a23SEd Schouten a->fts_statp->st_birthtim.tv_nsec)
145fe79420eSJohn Baldwin return (-1);
1469aa68a3fSGreg Lehey if (f_samesort)
1479aa68a3fSGreg Lehey return (strcoll(b->fts_name, a->fts_name));
1489aa68a3fSGreg Lehey else
149fe79420eSJohn Baldwin return (strcoll(a->fts_name, b->fts_name));
150fe79420eSJohn Baldwin }
151fe79420eSJohn Baldwin
152fe79420eSJohn Baldwin int
revbirthcmp(const FTSENT * a,const FTSENT * b)153fe79420eSJohn Baldwin revbirthcmp(const FTSENT *a, const FTSENT *b)
154fe79420eSJohn Baldwin {
155fe79420eSJohn Baldwin
156fe79420eSJohn Baldwin return (birthcmp(b, a));
157fe79420eSJohn Baldwin }
158fe79420eSJohn Baldwin
159fe79420eSJohn Baldwin int
statcmp(const FTSENT * a,const FTSENT * b)16046251ddeSWarner Losh statcmp(const FTSENT *a, const FTSENT *b)
1614b88c807SRodney W. Grimes {
162f137c9c1SDavid Malone
16399742a23SEd Schouten if (b->fts_statp->st_ctim.tv_sec >
16499742a23SEd Schouten a->fts_statp->st_ctim.tv_sec)
165f137c9c1SDavid Malone return (1);
16699742a23SEd Schouten if (b->fts_statp->st_ctim.tv_sec <
16799742a23SEd Schouten a->fts_statp->st_ctim.tv_sec)
168f137c9c1SDavid Malone return (-1);
16999742a23SEd Schouten if (b->fts_statp->st_ctim.tv_nsec >
17099742a23SEd Schouten a->fts_statp->st_ctim.tv_nsec)
171f137c9c1SDavid Malone return (1);
17299742a23SEd Schouten if (b->fts_statp->st_ctim.tv_nsec <
17399742a23SEd Schouten a->fts_statp->st_ctim.tv_nsec)
174f137c9c1SDavid Malone return (-1);
1759aa68a3fSGreg Lehey if (f_samesort)
1769aa68a3fSGreg Lehey return (strcoll(b->fts_name, a->fts_name));
1779aa68a3fSGreg Lehey else
17860e52383SDavid Malone return (strcoll(a->fts_name, b->fts_name));
1794b88c807SRodney W. Grimes }
1804b88c807SRodney W. Grimes
1814b88c807SRodney W. Grimes int
revstatcmp(const FTSENT * a,const FTSENT * b)18246251ddeSWarner Losh revstatcmp(const FTSENT *a, const FTSENT *b)
1834b88c807SRodney W. Grimes {
184f137c9c1SDavid Malone
18560e52383SDavid Malone return (statcmp(b, a));
1864b88c807SRodney W. Grimes }
18771b8b748SDima Dorfman
18871b8b748SDima Dorfman int
sizecmp(const FTSENT * a,const FTSENT * b)18971b8b748SDima Dorfman sizecmp(const FTSENT *a, const FTSENT *b)
19071b8b748SDima Dorfman {
19171b8b748SDima Dorfman
19271b8b748SDima Dorfman if (b->fts_statp->st_size > a->fts_statp->st_size)
19371b8b748SDima Dorfman return (1);
19471b8b748SDima Dorfman if (b->fts_statp->st_size < a->fts_statp->st_size)
19571b8b748SDima Dorfman return (-1);
19671b8b748SDima Dorfman return (strcoll(a->fts_name, b->fts_name));
19771b8b748SDima Dorfman }
19871b8b748SDima Dorfman
19971b8b748SDima Dorfman int
revsizecmp(const FTSENT * a,const FTSENT * b)20071b8b748SDima Dorfman revsizecmp(const FTSENT *a, const FTSENT *b)
20171b8b748SDima Dorfman {
20271b8b748SDima Dorfman
20371b8b748SDima Dorfman return (sizecmp(b, a));
20471b8b748SDima Dorfman }
205