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