source: rtems/cpukit/libmisc/shell/cmp-ls.c @ 6cdaa85

5
Last change on this file since 6cdaa85 was 6cdaa85, checked in by Sebastian Huber <sebastian.huber@…>, on Oct 4, 2018 at 6:16:45 PM

shell: Use #include "..." for local header files

Update #3375.

  • Property mode set to 100644
File size: 5.4 KB
Line 
1/*      $NetBSD: cmp.c,v 1.17 2003/08/07 09:05:14 agc Exp $     */
2
3/*
4 * Copyright (c) 1989, 1993
5 *      The Regents of the University of California.  All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Michael Fischbein.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 *    may be used to endorse or promote products derived from this software
20 *    without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35#ifdef HAVE_CONFIG_H
36#include "config.h"
37#endif
38
39#if 0
40#include <sys/cdefs.h>
41#ifndef lint
42#if 0
43static char sccsid[] = "@(#)cmp.c       8.1 (Berkeley) 5/31/93";
44#else
45__RCSID("$NetBSD: cmp.c,v 1.17 2003/08/07 09:05:14 agc Exp $");
46#endif
47#endif /* not lint */
48#endif
49
50#include <sys/types.h>
51#include <sys/stat.h>
52
53#include "fts.h"
54#include <string.h>
55
56#include "extern-ls.h"
57
58#if defined(__rtems__) || defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) || \
59    defined(_XOPEN_SOURCE) || defined(__NetBSD__)
60#define ATIMENSEC_CMP(x, op, y) ((x)->st_atime op (y)->st_atime)
61#define CTIMENSEC_CMP(x, op, y) ((x)->st_ctime op (y)->st_ctime)
62#define MTIMENSEC_CMP(x, op, y) ((x)->st_mtime op (y)->st_mtime)
63#else
64#define ATIMENSEC_CMP(x, op, y) \
65        ((x)->st_atime.tv_nsec op (y)->st_atime.tv_nsec)
66#define CTIMENSEC_CMP(x, op, y) \
67        ((x)->st_ctime.tv_nsec op (y)->st_ctime.tv_nsec)
68#define MTIMENSEC_CMP(x, op, y) \
69        ((x)->st_mtime.tv_nsec op (y)->st_mtime.tv_nsec)
70#endif
71
72int
73namecmp(const FTSENT *a, const FTSENT *b)
74{
75
76        return (strcmp(a->fts_name, b->fts_name));
77}
78
79int
80revnamecmp(const FTSENT *a, const FTSENT *b)
81{
82
83        return (strcmp(b->fts_name, a->fts_name));
84}
85
86int
87modcmp(const FTSENT *a, const FTSENT *b)
88{
89
90        if (b->fts_statp->st_mtime > a->fts_statp->st_mtime)
91                return (1);
92        else if (b->fts_statp->st_mtime < a->fts_statp->st_mtime)
93                return (-1);
94        else if (MTIMENSEC_CMP(b->fts_statp, >, a->fts_statp))
95                return (1);
96        else if (MTIMENSEC_CMP(b->fts_statp, <, a->fts_statp))
97                return (-1);
98        else
99                return (namecmp(a, b));
100}
101
102int
103revmodcmp(const FTSENT *a, const FTSENT *b)
104{
105
106        if (b->fts_statp->st_mtime > a->fts_statp->st_mtime)
107                return (-1);
108        else if (b->fts_statp->st_mtime < a->fts_statp->st_mtime)
109                return (1);
110        else if (MTIMENSEC_CMP(b->fts_statp, >, a->fts_statp))
111                return (-1);
112        else if (MTIMENSEC_CMP(b->fts_statp, <, a->fts_statp))
113                return (1);
114        else
115                return (revnamecmp(a, b));
116}
117
118int
119acccmp(const FTSENT *a, const FTSENT *b)
120{
121
122        if (b->fts_statp->st_atime > a->fts_statp->st_atime)
123                return (1);
124        else if (b->fts_statp->st_atime < a->fts_statp->st_atime)
125                return (-1);
126        else if (ATIMENSEC_CMP(b->fts_statp, >, a->fts_statp))
127                return (1);
128        else if (ATIMENSEC_CMP(b->fts_statp, <, a->fts_statp))
129                return (-1);
130        else
131                return (namecmp(a, b));
132}
133
134int
135revacccmp(const FTSENT *a, const FTSENT *b)
136{
137
138        if (b->fts_statp->st_atime > a->fts_statp->st_atime)
139                return (-1);
140        else if (b->fts_statp->st_atime < a->fts_statp->st_atime)
141                return (1);
142        else if (ATIMENSEC_CMP(b->fts_statp, >, a->fts_statp))
143                return (-1);
144        else if (ATIMENSEC_CMP(b->fts_statp, <, a->fts_statp))
145                return (1);
146        else
147                return (revnamecmp(a, b));
148}
149
150int
151statcmp(const FTSENT *a, const FTSENT *b)
152{
153
154        if (b->fts_statp->st_ctime > a->fts_statp->st_ctime)
155                return (1);
156        else if (b->fts_statp->st_ctime < a->fts_statp->st_ctime)
157                return (-1);
158        else if (CTIMENSEC_CMP(b->fts_statp, >, a->fts_statp))
159                return (1);
160        else if (CTIMENSEC_CMP(b->fts_statp, <, a->fts_statp))
161                return (-1);
162        else
163                return (namecmp(a, b));
164}
165
166int
167revstatcmp(const FTSENT *a, const FTSENT *b)
168{
169
170        if (b->fts_statp->st_ctime > a->fts_statp->st_ctime)
171                return (-1);
172        else if (b->fts_statp->st_ctime < a->fts_statp->st_ctime)
173                return (1);
174        else if (CTIMENSEC_CMP(b->fts_statp, >, a->fts_statp))
175                return (-1);
176        else if (CTIMENSEC_CMP(b->fts_statp, <, a->fts_statp))
177                return (1);
178        else
179                return (revnamecmp(a, b));
180}
181
182int
183sizecmp(const FTSENT *a, const FTSENT *b)
184{
185
186        if (b->fts_statp->st_size > a->fts_statp->st_size)
187                return (1);
188        if (b->fts_statp->st_size < a->fts_statp->st_size)
189                return (-1);
190        else
191                return (namecmp(a, b));
192}
193
194int
195revsizecmp(const FTSENT *a, const FTSENT *b)
196{
197
198        if (b->fts_statp->st_size > a->fts_statp->st_size)
199                return (-1);
200        if (b->fts_statp->st_size < a->fts_statp->st_size)
201                return (1);
202        else
203                return (revnamecmp(a, b));
204}
Note: See TracBrowser for help on using the repository browser.