Debian Codegraph @

libls/cmp.c

raw
1 /*
2 Copyright (C) 2000-2026 Free Software Foundation, Inc.
3
4 This file is part of GNU Inetutils.
5
6 GNU Inetutils is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or (at
9 your option) any later version.
10
11 GNU Inetutils is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see `http://www.gnu.org/licenses/'. */
18
19 /*
20 * Copyright (c) 1989, 1993
21 * The Regents of the University of California. All rights reserved.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the above copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. Neither the name of the University nor the names of its contributors
32 * may be used to endorse or promote products derived from this software
33 * without specific prior written permission.
34 *
35 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
36 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
38 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
39 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
40 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
41 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
43 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
44 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45 * SUCH DAMAGE.
46 */
47
48 /* This code is derived from software contributed to Berkeley by
49 Michael Fischbein. */
50
51 #include <config.h>
52 #include <sys/types.h>
53 #include <sys/stat.h>
54
55 #include <fts_.h>
56 #include <string.h>
57
58 #include "ls.h"
59 #include "extern.h"
60
61 #include "stat-time.h"
62 #include "timespec.h"
63
64 int
65 namecmp (const FTSENT *a, const FTSENT *b)
66 {
67 return (strcmp (a->fts_name, b->fts_name));
68 }
69
70 int
71 revnamecmp (const FTSENT *a, const FTSENT *b)
72 {
73 return (strcmp (b->fts_name, a->fts_name));
74 }
75
76 int
77 modcmp (const FTSENT *a, const FTSENT *b)
78 {
79 int diff = timespec_cmp (get_stat_mtime (b->fts_statp),
80 get_stat_mtime (a->fts_statp));
81 return diff ? diff : namecmp (a, b);
82 }
83
84 int
85 revmodcmp (const FTSENT *a, const FTSENT *b)
86 {
87 return (-modcmp (a, b));
88 }
89
90 int
91 acccmp (const FTSENT *a, const FTSENT *b)
92 {
93 int diff = timespec_cmp (get_stat_atime (b->fts_statp),
94 get_stat_atime (a->fts_statp));
95 return diff ? diff : namecmp (a, b);
96 }
97
98 int
99 revacccmp (const FTSENT *a, const FTSENT *b)
100 {
101 return (-acccmp (a, b));
102 }
103
104 int
105 statcmp (const FTSENT *a, const FTSENT *b)
106 {
107 int diff = timespec_cmp (get_stat_ctime (b->fts_statp),
108 get_stat_ctime (a->fts_statp));
109 return diff ? diff : namecmp (a, b);
110 }
111
112 int
113 revstatcmp (const FTSENT *a, const FTSENT *b)
114 {
115 return (-statcmp (a, b));
116 }
117
118 static int
119 off_cmp (off_t a, off_t b)
120 {
121 return (a > b) - (a < b);
122 }
123
124 int
125 sizecmp (const FTSENT *a, const FTSENT *b)
126 {
127 int diff = off_cmp (b->fts_statp->st_size, a->fts_statp->st_size);
128 return diff ? diff : namecmp (a, b);
129 }
130
131 int
132 revsizecmp (const FTSENT *a, const FTSENT *b)
133 {
134 return (-sizecmp (a, b));
135 }