1/*- 2 * Copyright (c) 1989, 1993, 1994 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 * 4. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33#ifndef lint 34static const char copyright[] = 35"@(#) Copyright (c) 1989, 1993, 1994\n\ 36 The Regents of the University of California. All rights reserved.\n"; 37#endif /* not lint */ 38 39#if 0 40#ifndef lint 41static char sccsid[] = "@(#)ls.c 8.5 (Berkeley) 4/2/94"; 42#endif /* not lint */ 43#endif 44#include <sys/cdefs.h>
| 1/*- 2 * Copyright (c) 1989, 1993, 1994 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 * 4. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33#ifndef lint 34static const char copyright[] = 35"@(#) Copyright (c) 1989, 1993, 1994\n\ 36 The Regents of the University of California. All rights reserved.\n"; 37#endif /* not lint */ 38 39#if 0 40#ifndef lint 41static char sccsid[] = "@(#)ls.c 8.5 (Berkeley) 4/2/94"; 42#endif /* not lint */ 43#endif 44#include <sys/cdefs.h>
|
46 47#include <sys/types.h> 48#include <sys/stat.h> 49#include <sys/ioctl.h> 50#include <sys/mac.h> 51 52#include <dirent.h> 53#include <err.h> 54#include <errno.h> 55#include <fts.h> 56#include <grp.h> 57#include <inttypes.h> 58#include <limits.h> 59#include <locale.h> 60#include <pwd.h> 61#include <stdio.h> 62#include <stdlib.h> 63#include <string.h> 64#include <unistd.h> 65#ifdef COLORLS 66#include <termcap.h> 67#include <signal.h> 68#endif 69 70#include "ls.h" 71#include "extern.h" 72 73/* 74 * Upward approximation of the maximum number of characters needed to 75 * represent a value of integral type t as a string, excluding the 76 * NUL terminator, with provision for a sign. 77 */ 78#define STRBUF_SIZEOF(t) (1 + CHAR_BIT * sizeof(t) / 3 + 1) 79 80/* 81 * MAKENINES(n) turns n into (10**n)-1. This is useful for converting a width 82 * into a number that wide in decimal. 83 * XXX: Overflows are not considered. 84 */ 85#define MAKENINES(n) \ 86 do { \ 87 intmax_t i; \ 88 \ 89 /* Use a loop as all values of n are small. */ \ 90 for (i = 1; n > 0; i *= 10) \ 91 n--; \ 92 n = i - 1; \ 93 } while(0) 94 95static void display(const FTSENT *, FTSENT *, int); 96static int mastercmp(const FTSENT * const *, const FTSENT * const *); 97static void traverse(int, char **, int); 98 99static void (*printfcn)(const DISPLAY *); 100static int (*sortfcn)(const FTSENT *, const FTSENT *); 101 102long blocksize; /* block size units */ 103int termwidth = 80; /* default terminal width */ 104 105/* flags */ 106 int f_accesstime; /* use time of last access */ 107 int f_birthtime; /* use time of birth */ 108 int f_flags; /* show flags associated with a file */ 109 int f_humanval; /* show human-readable file sizes */ 110 int f_inode; /* print inode */ 111static int f_kblocks; /* print size in kilobytes */ 112static int f_listdir; /* list actual directory, not contents */ 113static int f_listdot; /* list files beginning with . */ 114static int f_noautodot; /* do not automatically enable -A for root */ 115 int f_longform; /* long listing format */ 116 int f_nonprint; /* show unprintables as ? */ 117static int f_nosort; /* don't sort output */ 118 int f_notabs; /* don't use tab-separated multi-col output */ 119static int f_numericonly; /* don't convert uid/gid to name */ 120 int f_octal; /* show unprintables as \xxx */ 121 int f_octal_escape; /* like f_octal but use C escapes if possible */ 122static int f_recursive; /* ls subdirectories also */ 123static int f_reversesort; /* reverse whatever sort is used */ 124 int f_sectime; /* print the real time for all files */ 125static int f_singlecol; /* use single column output */ 126 int f_size; /* list size in short listing */ 127 int f_slash; /* similar to f_type, but only for dirs */ 128 int f_sortacross; /* sort across rows, not down columns */ 129 int f_statustime; /* use time of last mode change */ 130static int f_stream; /* stream the output, separate with commas */ 131static int f_timesort; /* sort by time vice name */ 132static int f_sizesort; 133 int f_type; /* add type character for non-regular files */ 134static int f_whiteout; /* show whiteout entries */ 135 int f_label; /* show MAC label */ 136#ifdef COLORLS 137 int f_color; /* add type in color for non-regular files */ 138 139char *ansi_bgcol; /* ANSI sequence to set background colour */ 140char *ansi_fgcol; /* ANSI sequence to set foreground colour */ 141char *ansi_coloff; /* ANSI sequence to reset colours */ 142char *attrs_off; /* ANSI sequence to turn off attributes */ 143char *enter_bold; /* ANSI sequence to set color to bold mode */ 144#endif 145 146static int rval; 147 148int 149main(int argc, char *argv[]) 150{ 151 static char dot[] = ".", *dotav[] = {dot, NULL}; 152 struct winsize win; 153 int ch, fts_options, notused; 154 char *p; 155#ifdef COLORLS 156 char termcapbuf[1024]; /* termcap definition buffer */ 157 char tcapbuf[512]; /* capability buffer */ 158 char *bp = tcapbuf; 159#endif 160 161 (void)setlocale(LC_ALL, ""); 162 163 /* Terminal defaults to -Cq, non-terminal defaults to -1. */ 164 if (isatty(STDOUT_FILENO)) { 165 termwidth = 80; 166 if ((p = getenv("COLUMNS")) != NULL && *p != '\0') 167 termwidth = atoi(p); 168 else if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) != -1 && 169 win.ws_col > 0) 170 termwidth = win.ws_col; 171 f_nonprint = 1; 172 } else { 173 f_singlecol = 1; 174 /* retrieve environment variable, in case of explicit -C */ 175 p = getenv("COLUMNS"); 176 if (p) 177 termwidth = atoi(p); 178 } 179 180 fts_options = FTS_PHYSICAL; 181 while ((ch = getopt(argc, argv, 182 "1ABCFGHILPRSTUWZabcdfghiklmnopqrstuwx")) != -1) { 183 switch (ch) { 184 /* 185 * The -1, -C, -x and -l options all override each other so 186 * shell aliasing works right. 187 */ 188 case '1': 189 f_singlecol = 1; 190 f_longform = 0; 191 f_stream = 0; 192 break; 193 case 'B': 194 f_nonprint = 0; 195 f_octal = 1; 196 f_octal_escape = 0; 197 break; 198 case 'C': 199 f_sortacross = f_longform = f_singlecol = 0; 200 break; 201 case 'l': 202 f_longform = 1; 203 f_singlecol = 0; 204 f_stream = 0; 205 break; 206 case 'x': 207 f_sortacross = 1; 208 f_longform = 0; 209 f_singlecol = 0; 210 break; 211 /* The -c, -u, and -U options override each other. */ 212 case 'c': 213 f_statustime = 1; 214 f_accesstime = 0; 215 f_birthtime = 0; 216 break; 217 case 'u': 218 f_accesstime = 1; 219 f_statustime = 0; 220 f_birthtime = 0; 221 break; 222 case 'U': 223 f_birthtime = 1; 224 f_accesstime = 0; 225 f_statustime = 0; 226 break; 227 case 'F': 228 f_type = 1; 229 f_slash = 0; 230 break; 231 case 'H': 232 fts_options |= FTS_COMFOLLOW; 233 break; 234 case 'G': 235 setenv("CLICOLOR", "", 1); 236 break; 237 case 'L': 238 fts_options &= ~FTS_PHYSICAL; 239 fts_options |= FTS_LOGICAL; 240 break; 241 case 'P': 242 fts_options &= ~FTS_COMFOLLOW; 243 fts_options &= ~FTS_LOGICAL; 244 fts_options |= FTS_PHYSICAL; 245 break; 246 case 'R': 247 f_recursive = 1; 248 break; 249 case 'a': 250 fts_options |= FTS_SEEDOT; 251 /* FALLTHROUGH */ 252 case 'A': 253 f_listdot = 1; 254 break; 255 case 'I': 256 f_noautodot = 1; 257 break; 258 /* The -d option turns off the -R option. */ 259 case 'd': 260 f_listdir = 1; 261 f_recursive = 0; 262 break; 263 case 'f': 264 f_nosort = 1; 265 break; 266 case 'g': /* Compatibility with 4.3BSD. */ 267 break; 268 case 'h': 269 f_humanval = 1; 270 break; 271 case 'i': 272 f_inode = 1; 273 break; 274 case 'k': 275 f_humanval = 0; 276 f_kblocks = 1; 277 break; 278 case 'm': 279 f_stream = 1; 280 f_singlecol = 0; 281 f_longform = 0; 282 break; 283 case 'n': 284 f_numericonly = 1; 285 break; 286 case 'o': 287 f_flags = 1; 288 break; 289 case 'p': 290 f_slash = 1; 291 f_type = 1; 292 break; 293 case 'q': 294 f_nonprint = 1; 295 f_octal = 0; 296 f_octal_escape = 0; 297 break; 298 case 'r': 299 f_reversesort = 1; 300 break; 301 case 's': 302 f_size = 1; 303 break; 304 case 'T': 305 f_sectime = 1; 306 break; 307 case 't': 308 f_timesort = 1; 309 break; 310 case 'S': 311 f_sizesort = 1; 312 break; 313 case 'W': 314 f_whiteout = 1; 315 break; 316 case 'b': 317 f_nonprint = 0; 318 f_octal = 0; 319 f_octal_escape = 1; 320 break; 321 case 'w': 322 f_nonprint = 0; 323 f_octal = 0; 324 f_octal_escape = 0; 325 break; 326 case 'Z': 327 f_label = 1; 328 break; 329 default: 330 case '?': 331 usage(); 332 } 333 } 334 argc -= optind; 335 argv += optind; 336 337 /* Root is -A automatically unless -I. */ 338 if (!f_listdot && getuid() == (uid_t)0 && !f_noautodot) 339 f_listdot = 1; 340 341 /* Enabling of colours is conditional on the environment. */ 342 if (getenv("CLICOLOR") && 343 (isatty(STDOUT_FILENO) || getenv("CLICOLOR_FORCE"))) 344#ifdef COLORLS 345 if (tgetent(termcapbuf, getenv("TERM")) == 1) { 346 ansi_fgcol = tgetstr("AF", &bp); 347 ansi_bgcol = tgetstr("AB", &bp); 348 attrs_off = tgetstr("me", &bp); 349 enter_bold = tgetstr("md", &bp); 350 351 /* To switch colours off use 'op' if 352 * available, otherwise use 'oc', or 353 * don't do colours at all. */ 354 ansi_coloff = tgetstr("op", &bp); 355 if (!ansi_coloff) 356 ansi_coloff = tgetstr("oc", &bp); 357 if (ansi_fgcol && ansi_bgcol && ansi_coloff) 358 f_color = 1; 359 } 360#else 361 warnx("color support not compiled in"); 362#endif /*COLORLS*/ 363 364#ifdef COLORLS 365 if (f_color) { 366 /* 367 * We can't put tabs and color sequences together: 368 * column number will be incremented incorrectly 369 * for "stty oxtabs" mode. 370 */ 371 f_notabs = 1; 372 (void)signal(SIGINT, colorquit); 373 (void)signal(SIGQUIT, colorquit); 374 parsecolors(getenv("LSCOLORS")); 375 } 376#endif 377 378 /* 379 * If not -F, -i, -l, -s, -S or -t options, don't require stat 380 * information, unless in color mode in which case we do 381 * need this to determine which colors to display. 382 */ 383 if (!f_inode && !f_longform && !f_size && !f_timesort && 384 !f_sizesort && !f_type 385#ifdef COLORLS 386 && !f_color 387#endif 388 ) 389 fts_options |= FTS_NOSTAT; 390 391 /* 392 * If not -F, -d or -l options, follow any symbolic links listed on 393 * the command line. 394 */ 395 if (!f_longform && !f_listdir && !f_type) 396 fts_options |= FTS_COMFOLLOW; 397 398 /* 399 * If -W, show whiteout entries 400 */ 401#ifdef FTS_WHITEOUT 402 if (f_whiteout) 403 fts_options |= FTS_WHITEOUT; 404#endif 405 406 /* If -l or -s, figure out block size. */ 407 if (f_longform || f_size) { 408 if (f_kblocks) 409 blocksize = 2; 410 else { 411 (void)getbsize(¬used, &blocksize); 412 blocksize /= 512; 413 } 414 } 415 /* Select a sort function. */ 416 if (f_reversesort) { 417 if (!f_timesort && !f_sizesort) 418 sortfcn = revnamecmp;
| 46 47#include <sys/types.h> 48#include <sys/stat.h> 49#include <sys/ioctl.h> 50#include <sys/mac.h> 51 52#include <dirent.h> 53#include <err.h> 54#include <errno.h> 55#include <fts.h> 56#include <grp.h> 57#include <inttypes.h> 58#include <limits.h> 59#include <locale.h> 60#include <pwd.h> 61#include <stdio.h> 62#include <stdlib.h> 63#include <string.h> 64#include <unistd.h> 65#ifdef COLORLS 66#include <termcap.h> 67#include <signal.h> 68#endif 69 70#include "ls.h" 71#include "extern.h" 72 73/* 74 * Upward approximation of the maximum number of characters needed to 75 * represent a value of integral type t as a string, excluding the 76 * NUL terminator, with provision for a sign. 77 */ 78#define STRBUF_SIZEOF(t) (1 + CHAR_BIT * sizeof(t) / 3 + 1) 79 80/* 81 * MAKENINES(n) turns n into (10**n)-1. This is useful for converting a width 82 * into a number that wide in decimal. 83 * XXX: Overflows are not considered. 84 */ 85#define MAKENINES(n) \ 86 do { \ 87 intmax_t i; \ 88 \ 89 /* Use a loop as all values of n are small. */ \ 90 for (i = 1; n > 0; i *= 10) \ 91 n--; \ 92 n = i - 1; \ 93 } while(0) 94 95static void display(const FTSENT *, FTSENT *, int); 96static int mastercmp(const FTSENT * const *, const FTSENT * const *); 97static void traverse(int, char **, int); 98 99static void (*printfcn)(const DISPLAY *); 100static int (*sortfcn)(const FTSENT *, const FTSENT *); 101 102long blocksize; /* block size units */ 103int termwidth = 80; /* default terminal width */ 104 105/* flags */ 106 int f_accesstime; /* use time of last access */ 107 int f_birthtime; /* use time of birth */ 108 int f_flags; /* show flags associated with a file */ 109 int f_humanval; /* show human-readable file sizes */ 110 int f_inode; /* print inode */ 111static int f_kblocks; /* print size in kilobytes */ 112static int f_listdir; /* list actual directory, not contents */ 113static int f_listdot; /* list files beginning with . */ 114static int f_noautodot; /* do not automatically enable -A for root */ 115 int f_longform; /* long listing format */ 116 int f_nonprint; /* show unprintables as ? */ 117static int f_nosort; /* don't sort output */ 118 int f_notabs; /* don't use tab-separated multi-col output */ 119static int f_numericonly; /* don't convert uid/gid to name */ 120 int f_octal; /* show unprintables as \xxx */ 121 int f_octal_escape; /* like f_octal but use C escapes if possible */ 122static int f_recursive; /* ls subdirectories also */ 123static int f_reversesort; /* reverse whatever sort is used */ 124 int f_sectime; /* print the real time for all files */ 125static int f_singlecol; /* use single column output */ 126 int f_size; /* list size in short listing */ 127 int f_slash; /* similar to f_type, but only for dirs */ 128 int f_sortacross; /* sort across rows, not down columns */ 129 int f_statustime; /* use time of last mode change */ 130static int f_stream; /* stream the output, separate with commas */ 131static int f_timesort; /* sort by time vice name */ 132static int f_sizesort; 133 int f_type; /* add type character for non-regular files */ 134static int f_whiteout; /* show whiteout entries */ 135 int f_label; /* show MAC label */ 136#ifdef COLORLS 137 int f_color; /* add type in color for non-regular files */ 138 139char *ansi_bgcol; /* ANSI sequence to set background colour */ 140char *ansi_fgcol; /* ANSI sequence to set foreground colour */ 141char *ansi_coloff; /* ANSI sequence to reset colours */ 142char *attrs_off; /* ANSI sequence to turn off attributes */ 143char *enter_bold; /* ANSI sequence to set color to bold mode */ 144#endif 145 146static int rval; 147 148int 149main(int argc, char *argv[]) 150{ 151 static char dot[] = ".", *dotav[] = {dot, NULL}; 152 struct winsize win; 153 int ch, fts_options, notused; 154 char *p; 155#ifdef COLORLS 156 char termcapbuf[1024]; /* termcap definition buffer */ 157 char tcapbuf[512]; /* capability buffer */ 158 char *bp = tcapbuf; 159#endif 160 161 (void)setlocale(LC_ALL, ""); 162 163 /* Terminal defaults to -Cq, non-terminal defaults to -1. */ 164 if (isatty(STDOUT_FILENO)) { 165 termwidth = 80; 166 if ((p = getenv("COLUMNS")) != NULL && *p != '\0') 167 termwidth = atoi(p); 168 else if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) != -1 && 169 win.ws_col > 0) 170 termwidth = win.ws_col; 171 f_nonprint = 1; 172 } else { 173 f_singlecol = 1; 174 /* retrieve environment variable, in case of explicit -C */ 175 p = getenv("COLUMNS"); 176 if (p) 177 termwidth = atoi(p); 178 } 179 180 fts_options = FTS_PHYSICAL; 181 while ((ch = getopt(argc, argv, 182 "1ABCFGHILPRSTUWZabcdfghiklmnopqrstuwx")) != -1) { 183 switch (ch) { 184 /* 185 * The -1, -C, -x and -l options all override each other so 186 * shell aliasing works right. 187 */ 188 case '1': 189 f_singlecol = 1; 190 f_longform = 0; 191 f_stream = 0; 192 break; 193 case 'B': 194 f_nonprint = 0; 195 f_octal = 1; 196 f_octal_escape = 0; 197 break; 198 case 'C': 199 f_sortacross = f_longform = f_singlecol = 0; 200 break; 201 case 'l': 202 f_longform = 1; 203 f_singlecol = 0; 204 f_stream = 0; 205 break; 206 case 'x': 207 f_sortacross = 1; 208 f_longform = 0; 209 f_singlecol = 0; 210 break; 211 /* The -c, -u, and -U options override each other. */ 212 case 'c': 213 f_statustime = 1; 214 f_accesstime = 0; 215 f_birthtime = 0; 216 break; 217 case 'u': 218 f_accesstime = 1; 219 f_statustime = 0; 220 f_birthtime = 0; 221 break; 222 case 'U': 223 f_birthtime = 1; 224 f_accesstime = 0; 225 f_statustime = 0; 226 break; 227 case 'F': 228 f_type = 1; 229 f_slash = 0; 230 break; 231 case 'H': 232 fts_options |= FTS_COMFOLLOW; 233 break; 234 case 'G': 235 setenv("CLICOLOR", "", 1); 236 break; 237 case 'L': 238 fts_options &= ~FTS_PHYSICAL; 239 fts_options |= FTS_LOGICAL; 240 break; 241 case 'P': 242 fts_options &= ~FTS_COMFOLLOW; 243 fts_options &= ~FTS_LOGICAL; 244 fts_options |= FTS_PHYSICAL; 245 break; 246 case 'R': 247 f_recursive = 1; 248 break; 249 case 'a': 250 fts_options |= FTS_SEEDOT; 251 /* FALLTHROUGH */ 252 case 'A': 253 f_listdot = 1; 254 break; 255 case 'I': 256 f_noautodot = 1; 257 break; 258 /* The -d option turns off the -R option. */ 259 case 'd': 260 f_listdir = 1; 261 f_recursive = 0; 262 break; 263 case 'f': 264 f_nosort = 1; 265 break; 266 case 'g': /* Compatibility with 4.3BSD. */ 267 break; 268 case 'h': 269 f_humanval = 1; 270 break; 271 case 'i': 272 f_inode = 1; 273 break; 274 case 'k': 275 f_humanval = 0; 276 f_kblocks = 1; 277 break; 278 case 'm': 279 f_stream = 1; 280 f_singlecol = 0; 281 f_longform = 0; 282 break; 283 case 'n': 284 f_numericonly = 1; 285 break; 286 case 'o': 287 f_flags = 1; 288 break; 289 case 'p': 290 f_slash = 1; 291 f_type = 1; 292 break; 293 case 'q': 294 f_nonprint = 1; 295 f_octal = 0; 296 f_octal_escape = 0; 297 break; 298 case 'r': 299 f_reversesort = 1; 300 break; 301 case 's': 302 f_size = 1; 303 break; 304 case 'T': 305 f_sectime = 1; 306 break; 307 case 't': 308 f_timesort = 1; 309 break; 310 case 'S': 311 f_sizesort = 1; 312 break; 313 case 'W': 314 f_whiteout = 1; 315 break; 316 case 'b': 317 f_nonprint = 0; 318 f_octal = 0; 319 f_octal_escape = 1; 320 break; 321 case 'w': 322 f_nonprint = 0; 323 f_octal = 0; 324 f_octal_escape = 0; 325 break; 326 case 'Z': 327 f_label = 1; 328 break; 329 default: 330 case '?': 331 usage(); 332 } 333 } 334 argc -= optind; 335 argv += optind; 336 337 /* Root is -A automatically unless -I. */ 338 if (!f_listdot && getuid() == (uid_t)0 && !f_noautodot) 339 f_listdot = 1; 340 341 /* Enabling of colours is conditional on the environment. */ 342 if (getenv("CLICOLOR") && 343 (isatty(STDOUT_FILENO) || getenv("CLICOLOR_FORCE"))) 344#ifdef COLORLS 345 if (tgetent(termcapbuf, getenv("TERM")) == 1) { 346 ansi_fgcol = tgetstr("AF", &bp); 347 ansi_bgcol = tgetstr("AB", &bp); 348 attrs_off = tgetstr("me", &bp); 349 enter_bold = tgetstr("md", &bp); 350 351 /* To switch colours off use 'op' if 352 * available, otherwise use 'oc', or 353 * don't do colours at all. */ 354 ansi_coloff = tgetstr("op", &bp); 355 if (!ansi_coloff) 356 ansi_coloff = tgetstr("oc", &bp); 357 if (ansi_fgcol && ansi_bgcol && ansi_coloff) 358 f_color = 1; 359 } 360#else 361 warnx("color support not compiled in"); 362#endif /*COLORLS*/ 363 364#ifdef COLORLS 365 if (f_color) { 366 /* 367 * We can't put tabs and color sequences together: 368 * column number will be incremented incorrectly 369 * for "stty oxtabs" mode. 370 */ 371 f_notabs = 1; 372 (void)signal(SIGINT, colorquit); 373 (void)signal(SIGQUIT, colorquit); 374 parsecolors(getenv("LSCOLORS")); 375 } 376#endif 377 378 /* 379 * If not -F, -i, -l, -s, -S or -t options, don't require stat 380 * information, unless in color mode in which case we do 381 * need this to determine which colors to display. 382 */ 383 if (!f_inode && !f_longform && !f_size && !f_timesort && 384 !f_sizesort && !f_type 385#ifdef COLORLS 386 && !f_color 387#endif 388 ) 389 fts_options |= FTS_NOSTAT; 390 391 /* 392 * If not -F, -d or -l options, follow any symbolic links listed on 393 * the command line. 394 */ 395 if (!f_longform && !f_listdir && !f_type) 396 fts_options |= FTS_COMFOLLOW; 397 398 /* 399 * If -W, show whiteout entries 400 */ 401#ifdef FTS_WHITEOUT 402 if (f_whiteout) 403 fts_options |= FTS_WHITEOUT; 404#endif 405 406 /* If -l or -s, figure out block size. */ 407 if (f_longform || f_size) { 408 if (f_kblocks) 409 blocksize = 2; 410 else { 411 (void)getbsize(¬used, &blocksize); 412 blocksize /= 512; 413 } 414 } 415 /* Select a sort function. */ 416 if (f_reversesort) { 417 if (!f_timesort && !f_sizesort) 418 sortfcn = revnamecmp;
|
440 else /* Use modification time. */ 441 sortfcn = modcmp; 442 } 443 444 /* Select a print function. */ 445 if (f_singlecol) 446 printfcn = printscol; 447 else if (f_longform) 448 printfcn = printlong; 449 else if (f_stream) 450 printfcn = printstream; 451 else 452 printfcn = printcol; 453 454 if (argc) 455 traverse(argc, argv, fts_options); 456 else 457 traverse(1, dotav, fts_options); 458 exit(rval); 459} 460 461static int output; /* If anything output. */ 462 463/* 464 * Traverse() walks the logical directory structure specified by the argv list 465 * in the order specified by the mastercmp() comparison function. During the 466 * traversal it passes linked lists of structures to display() which represent 467 * a superset (may be exact set) of the files to be displayed. 468 */ 469static void 470traverse(int argc, char *argv[], int options) 471{ 472 FTS *ftsp; 473 FTSENT *p, *chp; 474 int ch_options; 475 476 if ((ftsp = 477 fts_open(argv, options, f_nosort ? NULL : mastercmp)) == NULL) 478 err(1, "fts_open"); 479 480 /* 481 * We ignore errors from fts_children here since they will be 482 * replicated and signalled on the next call to fts_read() below. 483 */ 484 chp = fts_children(ftsp, 0); 485 if (chp != NULL) 486 display(NULL, chp, options); 487 if (f_listdir) 488 return; 489 490 /* 491 * If not recursing down this tree and don't need stat info, just get 492 * the names. 493 */ 494 ch_options = !f_recursive && !f_label && 495 options & FTS_NOSTAT ? FTS_NAMEONLY : 0; 496 497 while ((p = fts_read(ftsp)) != NULL) 498 switch (p->fts_info) { 499 case FTS_DC: 500 warnx("%s: directory causes a cycle", p->fts_name); 501 break; 502 case FTS_DNR: 503 case FTS_ERR: 504 warnx("%s: %s", p->fts_name, strerror(p->fts_errno)); 505 rval = 1; 506 break; 507 case FTS_D: 508 if (p->fts_level != FTS_ROOTLEVEL && 509 p->fts_name[0] == '.' && !f_listdot) 510 break; 511 512 /* 513 * If already output something, put out a newline as 514 * a separator. If multiple arguments, precede each 515 * directory with its name. 516 */ 517 if (output) { 518 putchar('\n'); 519 (void)printname(p->fts_path); 520 puts(":"); 521 } else if (argc > 1) { 522 (void)printname(p->fts_path); 523 puts(":"); 524 output = 1; 525 } 526 chp = fts_children(ftsp, ch_options); 527 display(p, chp, options); 528 529 if (!f_recursive && chp != NULL) 530 (void)fts_set(ftsp, p, FTS_SKIP); 531 break; 532 default: 533 break; 534 } 535 if (errno) 536 err(1, "fts_read"); 537} 538 539/* 540 * Display() takes a linked list of FTSENT structures and passes the list 541 * along with any other necessary information to the print function. P 542 * points to the parent directory of the display list. 543 */ 544static void 545display(const FTSENT *p, FTSENT *list, int options) 546{ 547 struct stat *sp; 548 DISPLAY d; 549 FTSENT *cur; 550 NAMES *np; 551 off_t maxsize; 552 long maxblock; 553 u_long btotal, labelstrlen, maxinode, maxlen, maxnlink; 554 u_long maxlabelstr; 555 int bcfile, maxflags; 556 gid_t maxgroup; 557 uid_t maxuser; 558 size_t flen, ulen, glen; 559 char *initmax; 560 int entries, needstats; 561 const char *user, *group; 562 char *flags, *labelstr = NULL; 563 char buf[STRBUF_SIZEOF(u_quad_t) + 1]; 564 char ngroup[STRBUF_SIZEOF(uid_t) + 1]; 565 char nuser[STRBUF_SIZEOF(gid_t) + 1]; 566 567 needstats = f_inode || f_longform || f_size; 568 flen = 0; 569 btotal = 0; 570 initmax = getenv("LS_COLWIDTHS"); 571 /* Fields match -lios order. New ones should be added at the end. */ 572 maxlabelstr = maxblock = maxinode = maxlen = maxnlink = 573 maxuser = maxgroup = maxflags = maxsize = 0; 574 if (initmax != NULL && *initmax != '\0') { 575 char *initmax2, *jinitmax; 576 int ninitmax; 577 578 /* Fill-in "::" as "0:0:0" for the sake of scanf. */ 579 jinitmax = malloc(strlen(initmax) * 2 + 2); 580 if (jinitmax == NULL) 581 err(1, "malloc"); 582 initmax2 = jinitmax; 583 if (*initmax == ':') 584 strcpy(initmax2, "0:"), initmax2 += 2; 585 else 586 *initmax2++ = *initmax, *initmax2 = '\0'; 587 for (initmax++; *initmax != '\0'; initmax++) { 588 if (initmax[-1] == ':' && initmax[0] == ':') { 589 *initmax2++ = '0'; 590 *initmax2++ = initmax[0]; 591 initmax2[1] = '\0'; 592 } else { 593 *initmax2++ = initmax[0]; 594 initmax2[1] = '\0'; 595 } 596 } 597 if (initmax2[-1] == ':') 598 strcpy(initmax2, "0"); 599 600 ninitmax = sscanf(jinitmax, 601 " %lu : %ld : %lu : %u : %u : %i : %jd : %lu : %lu ", 602 &maxinode, &maxblock, &maxnlink, &maxuser, 603 &maxgroup, &maxflags, &maxsize, &maxlen, &maxlabelstr); 604 f_notabs = 1; 605 switch (ninitmax) { 606 case 0: 607 maxinode = 0; 608 /* FALLTHROUGH */ 609 case 1: 610 maxblock = 0; 611 /* FALLTHROUGH */ 612 case 2: 613 maxnlink = 0; 614 /* FALLTHROUGH */ 615 case 3: 616 maxuser = 0; 617 /* FALLTHROUGH */ 618 case 4: 619 maxgroup = 0; 620 /* FALLTHROUGH */ 621 case 5: 622 maxflags = 0; 623 /* FALLTHROUGH */ 624 case 6: 625 maxsize = 0; 626 /* FALLTHROUGH */ 627 case 7: 628 maxlen = 0; 629 /* FALLTHROUGH */ 630 case 8: 631 maxlabelstr = 0; 632 /* FALLTHROUGH */ 633#ifdef COLORLS 634 if (!f_color) 635#endif 636 f_notabs = 0; 637 /* FALLTHROUGH */ 638 default: 639 break; 640 } 641 MAKENINES(maxinode); 642 MAKENINES(maxblock); 643 MAKENINES(maxnlink); 644 MAKENINES(maxsize); 645 free(jinitmax); 646 } 647 bcfile = 0; 648 flags = NULL; 649 for (cur = list, entries = 0; cur; cur = cur->fts_link) { 650 if (cur->fts_info == FTS_ERR || cur->fts_info == FTS_NS) { 651 warnx("%s: %s", 652 cur->fts_name, strerror(cur->fts_errno)); 653 cur->fts_number = NO_PRINT; 654 rval = 1; 655 continue; 656 } 657 /* 658 * P is NULL if list is the argv list, to which different rules 659 * apply. 660 */ 661 if (p == NULL) { 662 /* Directories will be displayed later. */ 663 if (cur->fts_info == FTS_D && !f_listdir) { 664 cur->fts_number = NO_PRINT; 665 continue; 666 } 667 } else { 668 /* Only display dot file if -a/-A set. */ 669 if (cur->fts_name[0] == '.' && !f_listdot) { 670 cur->fts_number = NO_PRINT; 671 continue; 672 } 673 } 674 if (cur->fts_namelen > maxlen) 675 maxlen = cur->fts_namelen; 676 if (f_octal || f_octal_escape) { 677 u_long t = len_octal(cur->fts_name, cur->fts_namelen); 678 679 if (t > maxlen) 680 maxlen = t; 681 } 682 if (needstats) { 683 sp = cur->fts_statp; 684 if (sp->st_blocks > maxblock) 685 maxblock = sp->st_blocks; 686 if (sp->st_ino > maxinode) 687 maxinode = sp->st_ino; 688 if (sp->st_nlink > maxnlink) 689 maxnlink = sp->st_nlink; 690 if (sp->st_size > maxsize) 691 maxsize = sp->st_size; 692 693 btotal += sp->st_blocks; 694 if (f_longform) { 695 if (f_numericonly) { 696 (void)snprintf(nuser, sizeof(nuser), 697 "%u", sp->st_uid); 698 (void)snprintf(ngroup, sizeof(ngroup), 699 "%u", sp->st_gid); 700 user = nuser; 701 group = ngroup; 702 } else { 703 user = user_from_uid(sp->st_uid, 0); 704 group = group_from_gid(sp->st_gid, 0); 705 } 706 if ((ulen = strlen(user)) > maxuser) 707 maxuser = ulen; 708 if ((glen = strlen(group)) > maxgroup) 709 maxgroup = glen; 710 if (f_flags) { 711 flags = fflagstostr(sp->st_flags); 712 if (flags != NULL && *flags == '\0') { 713 free(flags); 714 flags = strdup("-"); 715 } 716 if (flags == NULL) 717 err(1, "fflagstostr"); 718 flen = strlen(flags); 719 if (flen > (size_t)maxflags) 720 maxflags = flen; 721 } else 722 flen = 0; 723 labelstr = NULL; 724 if (f_label) { 725 char name[PATH_MAX + 1]; 726 mac_t label; 727 int error; 728 729 error = mac_prepare_file_label(&label); 730 if (error == -1) { 731 warn("MAC label for %s/%s", 732 cur->fts_parent->fts_path, 733 cur->fts_name); 734 goto label_out; 735 } 736 737 if (cur->fts_level == FTS_ROOTLEVEL) 738 snprintf(name, sizeof(name), 739 "%s", cur->fts_name); 740 else 741 snprintf(name, sizeof(name), 742 "%s/%s", cur->fts_parent-> 743 fts_accpath, cur->fts_name); 744 745 if (options & FTS_LOGICAL) 746 error = mac_get_file(name, 747 label); 748 else 749 error = mac_get_link(name, 750 label); 751 if (error == -1) { 752 warn("MAC label for %s/%s", 753 cur->fts_parent->fts_path, 754 cur->fts_name); 755 mac_free(label); 756 goto label_out; 757 } 758 759 error = mac_to_text(label, 760 &labelstr); 761 if (error == -1) { 762 warn("MAC label for %s/%s", 763 cur->fts_parent->fts_path, 764 cur->fts_name); 765 mac_free(label); 766 goto label_out; 767 } 768 mac_free(label); 769label_out: 770 if (labelstr == NULL) 771 labelstr = strdup("-"); 772 labelstrlen = strlen(labelstr); 773 if (labelstrlen > maxlabelstr) 774 maxlabelstr = labelstrlen; 775 } else 776 labelstrlen = 0; 777 778 if ((np = malloc(sizeof(NAMES) + labelstrlen + 779 ulen + glen + flen + 4)) == NULL) 780 err(1, "malloc"); 781 782 np->user = &np->data[0]; 783 (void)strcpy(np->user, user); 784 np->group = &np->data[ulen + 1]; 785 (void)strcpy(np->group, group); 786 787 if (S_ISCHR(sp->st_mode) || 788 S_ISBLK(sp->st_mode)) 789 bcfile = 1; 790 791 if (f_flags) { 792 np->flags = &np->data[ulen + glen + 2]; 793 (void)strcpy(np->flags, flags); 794 free(flags); 795 } 796 if (f_label) { 797 np->label = &np->data[ulen + glen + 2 798 + (f_flags ? flen + 1 : 0)]; 799 (void)strcpy(np->label, labelstr); 800 free(labelstr); 801 } 802 cur->fts_pointer = np; 803 } 804 } 805 ++entries; 806 } 807 808 /* 809 * If there are no entries to display, we normally stop right 810 * here. However, we must continue if we have to display the 811 * total block count. In this case, we display the total only 812 * on the second (p != NULL) pass. 813 */ 814 if (!entries && (!(f_longform || f_size) || p == NULL)) 815 return; 816 817 d.list = list; 818 d.entries = entries; 819 d.maxlen = maxlen; 820 if (needstats) { 821 d.bcfile = bcfile; 822 d.btotal = btotal; 823 (void)snprintf(buf, sizeof(buf), "%lu", maxblock); 824 d.s_block = strlen(buf); 825 d.s_flags = maxflags; 826 d.s_label = maxlabelstr; 827 d.s_group = maxgroup; 828 (void)snprintf(buf, sizeof(buf), "%lu", maxinode); 829 d.s_inode = strlen(buf); 830 (void)snprintf(buf, sizeof(buf), "%lu", maxnlink); 831 d.s_nlink = strlen(buf); 832 (void)snprintf(buf, sizeof(buf), "%ju", maxsize); 833 d.s_size = strlen(buf); 834 d.s_user = maxuser; 835 } 836 printfcn(&d); 837 output = 1; 838 839 if (f_longform) 840 for (cur = list; cur; cur = cur->fts_link) 841 free(cur->fts_pointer); 842} 843 844/* 845 * Ordering for mastercmp: 846 * If ordering the argv (fts_level = FTS_ROOTLEVEL) return non-directories 847 * as larger than directories. Within either group, use the sort function. 848 * All other levels use the sort function. Error entries remain unsorted. 849 */ 850static int 851mastercmp(const FTSENT * const *a, const FTSENT * const *b) 852{ 853 int a_info, b_info; 854 855 a_info = (*a)->fts_info; 856 if (a_info == FTS_ERR) 857 return (0); 858 b_info = (*b)->fts_info; 859 if (b_info == FTS_ERR) 860 return (0); 861 862 if (a_info == FTS_NS || b_info == FTS_NS) 863 return (namecmp(*a, *b)); 864 865 if (a_info != b_info && 866 (*a)->fts_level == FTS_ROOTLEVEL && !f_listdir) { 867 if (a_info == FTS_D) 868 return (1); 869 if (b_info == FTS_D) 870 return (-1); 871 } 872 return (sortfcn(*a, *b)); 873}
| 440 else /* Use modification time. */ 441 sortfcn = modcmp; 442 } 443 444 /* Select a print function. */ 445 if (f_singlecol) 446 printfcn = printscol; 447 else if (f_longform) 448 printfcn = printlong; 449 else if (f_stream) 450 printfcn = printstream; 451 else 452 printfcn = printcol; 453 454 if (argc) 455 traverse(argc, argv, fts_options); 456 else 457 traverse(1, dotav, fts_options); 458 exit(rval); 459} 460 461static int output; /* If anything output. */ 462 463/* 464 * Traverse() walks the logical directory structure specified by the argv list 465 * in the order specified by the mastercmp() comparison function. During the 466 * traversal it passes linked lists of structures to display() which represent 467 * a superset (may be exact set) of the files to be displayed. 468 */ 469static void 470traverse(int argc, char *argv[], int options) 471{ 472 FTS *ftsp; 473 FTSENT *p, *chp; 474 int ch_options; 475 476 if ((ftsp = 477 fts_open(argv, options, f_nosort ? NULL : mastercmp)) == NULL) 478 err(1, "fts_open"); 479 480 /* 481 * We ignore errors from fts_children here since they will be 482 * replicated and signalled on the next call to fts_read() below. 483 */ 484 chp = fts_children(ftsp, 0); 485 if (chp != NULL) 486 display(NULL, chp, options); 487 if (f_listdir) 488 return; 489 490 /* 491 * If not recursing down this tree and don't need stat info, just get 492 * the names. 493 */ 494 ch_options = !f_recursive && !f_label && 495 options & FTS_NOSTAT ? FTS_NAMEONLY : 0; 496 497 while ((p = fts_read(ftsp)) != NULL) 498 switch (p->fts_info) { 499 case FTS_DC: 500 warnx("%s: directory causes a cycle", p->fts_name); 501 break; 502 case FTS_DNR: 503 case FTS_ERR: 504 warnx("%s: %s", p->fts_name, strerror(p->fts_errno)); 505 rval = 1; 506 break; 507 case FTS_D: 508 if (p->fts_level != FTS_ROOTLEVEL && 509 p->fts_name[0] == '.' && !f_listdot) 510 break; 511 512 /* 513 * If already output something, put out a newline as 514 * a separator. If multiple arguments, precede each 515 * directory with its name. 516 */ 517 if (output) { 518 putchar('\n'); 519 (void)printname(p->fts_path); 520 puts(":"); 521 } else if (argc > 1) { 522 (void)printname(p->fts_path); 523 puts(":"); 524 output = 1; 525 } 526 chp = fts_children(ftsp, ch_options); 527 display(p, chp, options); 528 529 if (!f_recursive && chp != NULL) 530 (void)fts_set(ftsp, p, FTS_SKIP); 531 break; 532 default: 533 break; 534 } 535 if (errno) 536 err(1, "fts_read"); 537} 538 539/* 540 * Display() takes a linked list of FTSENT structures and passes the list 541 * along with any other necessary information to the print function. P 542 * points to the parent directory of the display list. 543 */ 544static void 545display(const FTSENT *p, FTSENT *list, int options) 546{ 547 struct stat *sp; 548 DISPLAY d; 549 FTSENT *cur; 550 NAMES *np; 551 off_t maxsize; 552 long maxblock; 553 u_long btotal, labelstrlen, maxinode, maxlen, maxnlink; 554 u_long maxlabelstr; 555 int bcfile, maxflags; 556 gid_t maxgroup; 557 uid_t maxuser; 558 size_t flen, ulen, glen; 559 char *initmax; 560 int entries, needstats; 561 const char *user, *group; 562 char *flags, *labelstr = NULL; 563 char buf[STRBUF_SIZEOF(u_quad_t) + 1]; 564 char ngroup[STRBUF_SIZEOF(uid_t) + 1]; 565 char nuser[STRBUF_SIZEOF(gid_t) + 1]; 566 567 needstats = f_inode || f_longform || f_size; 568 flen = 0; 569 btotal = 0; 570 initmax = getenv("LS_COLWIDTHS"); 571 /* Fields match -lios order. New ones should be added at the end. */ 572 maxlabelstr = maxblock = maxinode = maxlen = maxnlink = 573 maxuser = maxgroup = maxflags = maxsize = 0; 574 if (initmax != NULL && *initmax != '\0') { 575 char *initmax2, *jinitmax; 576 int ninitmax; 577 578 /* Fill-in "::" as "0:0:0" for the sake of scanf. */ 579 jinitmax = malloc(strlen(initmax) * 2 + 2); 580 if (jinitmax == NULL) 581 err(1, "malloc"); 582 initmax2 = jinitmax; 583 if (*initmax == ':') 584 strcpy(initmax2, "0:"), initmax2 += 2; 585 else 586 *initmax2++ = *initmax, *initmax2 = '\0'; 587 for (initmax++; *initmax != '\0'; initmax++) { 588 if (initmax[-1] == ':' && initmax[0] == ':') { 589 *initmax2++ = '0'; 590 *initmax2++ = initmax[0]; 591 initmax2[1] = '\0'; 592 } else { 593 *initmax2++ = initmax[0]; 594 initmax2[1] = '\0'; 595 } 596 } 597 if (initmax2[-1] == ':') 598 strcpy(initmax2, "0"); 599 600 ninitmax = sscanf(jinitmax, 601 " %lu : %ld : %lu : %u : %u : %i : %jd : %lu : %lu ", 602 &maxinode, &maxblock, &maxnlink, &maxuser, 603 &maxgroup, &maxflags, &maxsize, &maxlen, &maxlabelstr); 604 f_notabs = 1; 605 switch (ninitmax) { 606 case 0: 607 maxinode = 0; 608 /* FALLTHROUGH */ 609 case 1: 610 maxblock = 0; 611 /* FALLTHROUGH */ 612 case 2: 613 maxnlink = 0; 614 /* FALLTHROUGH */ 615 case 3: 616 maxuser = 0; 617 /* FALLTHROUGH */ 618 case 4: 619 maxgroup = 0; 620 /* FALLTHROUGH */ 621 case 5: 622 maxflags = 0; 623 /* FALLTHROUGH */ 624 case 6: 625 maxsize = 0; 626 /* FALLTHROUGH */ 627 case 7: 628 maxlen = 0; 629 /* FALLTHROUGH */ 630 case 8: 631 maxlabelstr = 0; 632 /* FALLTHROUGH */ 633#ifdef COLORLS 634 if (!f_color) 635#endif 636 f_notabs = 0; 637 /* FALLTHROUGH */ 638 default: 639 break; 640 } 641 MAKENINES(maxinode); 642 MAKENINES(maxblock); 643 MAKENINES(maxnlink); 644 MAKENINES(maxsize); 645 free(jinitmax); 646 } 647 bcfile = 0; 648 flags = NULL; 649 for (cur = list, entries = 0; cur; cur = cur->fts_link) { 650 if (cur->fts_info == FTS_ERR || cur->fts_info == FTS_NS) { 651 warnx("%s: %s", 652 cur->fts_name, strerror(cur->fts_errno)); 653 cur->fts_number = NO_PRINT; 654 rval = 1; 655 continue; 656 } 657 /* 658 * P is NULL if list is the argv list, to which different rules 659 * apply. 660 */ 661 if (p == NULL) { 662 /* Directories will be displayed later. */ 663 if (cur->fts_info == FTS_D && !f_listdir) { 664 cur->fts_number = NO_PRINT; 665 continue; 666 } 667 } else { 668 /* Only display dot file if -a/-A set. */ 669 if (cur->fts_name[0] == '.' && !f_listdot) { 670 cur->fts_number = NO_PRINT; 671 continue; 672 } 673 } 674 if (cur->fts_namelen > maxlen) 675 maxlen = cur->fts_namelen; 676 if (f_octal || f_octal_escape) { 677 u_long t = len_octal(cur->fts_name, cur->fts_namelen); 678 679 if (t > maxlen) 680 maxlen = t; 681 } 682 if (needstats) { 683 sp = cur->fts_statp; 684 if (sp->st_blocks > maxblock) 685 maxblock = sp->st_blocks; 686 if (sp->st_ino > maxinode) 687 maxinode = sp->st_ino; 688 if (sp->st_nlink > maxnlink) 689 maxnlink = sp->st_nlink; 690 if (sp->st_size > maxsize) 691 maxsize = sp->st_size; 692 693 btotal += sp->st_blocks; 694 if (f_longform) { 695 if (f_numericonly) { 696 (void)snprintf(nuser, sizeof(nuser), 697 "%u", sp->st_uid); 698 (void)snprintf(ngroup, sizeof(ngroup), 699 "%u", sp->st_gid); 700 user = nuser; 701 group = ngroup; 702 } else { 703 user = user_from_uid(sp->st_uid, 0); 704 group = group_from_gid(sp->st_gid, 0); 705 } 706 if ((ulen = strlen(user)) > maxuser) 707 maxuser = ulen; 708 if ((glen = strlen(group)) > maxgroup) 709 maxgroup = glen; 710 if (f_flags) { 711 flags = fflagstostr(sp->st_flags); 712 if (flags != NULL && *flags == '\0') { 713 free(flags); 714 flags = strdup("-"); 715 } 716 if (flags == NULL) 717 err(1, "fflagstostr"); 718 flen = strlen(flags); 719 if (flen > (size_t)maxflags) 720 maxflags = flen; 721 } else 722 flen = 0; 723 labelstr = NULL; 724 if (f_label) { 725 char name[PATH_MAX + 1]; 726 mac_t label; 727 int error; 728 729 error = mac_prepare_file_label(&label); 730 if (error == -1) { 731 warn("MAC label for %s/%s", 732 cur->fts_parent->fts_path, 733 cur->fts_name); 734 goto label_out; 735 } 736 737 if (cur->fts_level == FTS_ROOTLEVEL) 738 snprintf(name, sizeof(name), 739 "%s", cur->fts_name); 740 else 741 snprintf(name, sizeof(name), 742 "%s/%s", cur->fts_parent-> 743 fts_accpath, cur->fts_name); 744 745 if (options & FTS_LOGICAL) 746 error = mac_get_file(name, 747 label); 748 else 749 error = mac_get_link(name, 750 label); 751 if (error == -1) { 752 warn("MAC label for %s/%s", 753 cur->fts_parent->fts_path, 754 cur->fts_name); 755 mac_free(label); 756 goto label_out; 757 } 758 759 error = mac_to_text(label, 760 &labelstr); 761 if (error == -1) { 762 warn("MAC label for %s/%s", 763 cur->fts_parent->fts_path, 764 cur->fts_name); 765 mac_free(label); 766 goto label_out; 767 } 768 mac_free(label); 769label_out: 770 if (labelstr == NULL) 771 labelstr = strdup("-"); 772 labelstrlen = strlen(labelstr); 773 if (labelstrlen > maxlabelstr) 774 maxlabelstr = labelstrlen; 775 } else 776 labelstrlen = 0; 777 778 if ((np = malloc(sizeof(NAMES) + labelstrlen + 779 ulen + glen + flen + 4)) == NULL) 780 err(1, "malloc"); 781 782 np->user = &np->data[0]; 783 (void)strcpy(np->user, user); 784 np->group = &np->data[ulen + 1]; 785 (void)strcpy(np->group, group); 786 787 if (S_ISCHR(sp->st_mode) || 788 S_ISBLK(sp->st_mode)) 789 bcfile = 1; 790 791 if (f_flags) { 792 np->flags = &np->data[ulen + glen + 2]; 793 (void)strcpy(np->flags, flags); 794 free(flags); 795 } 796 if (f_label) { 797 np->label = &np->data[ulen + glen + 2 798 + (f_flags ? flen + 1 : 0)]; 799 (void)strcpy(np->label, labelstr); 800 free(labelstr); 801 } 802 cur->fts_pointer = np; 803 } 804 } 805 ++entries; 806 } 807 808 /* 809 * If there are no entries to display, we normally stop right 810 * here. However, we must continue if we have to display the 811 * total block count. In this case, we display the total only 812 * on the second (p != NULL) pass. 813 */ 814 if (!entries && (!(f_longform || f_size) || p == NULL)) 815 return; 816 817 d.list = list; 818 d.entries = entries; 819 d.maxlen = maxlen; 820 if (needstats) { 821 d.bcfile = bcfile; 822 d.btotal = btotal; 823 (void)snprintf(buf, sizeof(buf), "%lu", maxblock); 824 d.s_block = strlen(buf); 825 d.s_flags = maxflags; 826 d.s_label = maxlabelstr; 827 d.s_group = maxgroup; 828 (void)snprintf(buf, sizeof(buf), "%lu", maxinode); 829 d.s_inode = strlen(buf); 830 (void)snprintf(buf, sizeof(buf), "%lu", maxnlink); 831 d.s_nlink = strlen(buf); 832 (void)snprintf(buf, sizeof(buf), "%ju", maxsize); 833 d.s_size = strlen(buf); 834 d.s_user = maxuser; 835 } 836 printfcn(&d); 837 output = 1; 838 839 if (f_longform) 840 for (cur = list; cur; cur = cur->fts_link) 841 free(cur->fts_pointer); 842} 843 844/* 845 * Ordering for mastercmp: 846 * If ordering the argv (fts_level = FTS_ROOTLEVEL) return non-directories 847 * as larger than directories. Within either group, use the sort function. 848 * All other levels use the sort function. Error entries remain unsorted. 849 */ 850static int 851mastercmp(const FTSENT * const *a, const FTSENT * const *b) 852{ 853 int a_info, b_info; 854 855 a_info = (*a)->fts_info; 856 if (a_info == FTS_ERR) 857 return (0); 858 b_info = (*b)->fts_info; 859 if (b_info == FTS_ERR) 860 return (0); 861 862 if (a_info == FTS_NS || b_info == FTS_NS) 863 return (namecmp(*a, *b)); 864 865 if (a_info != b_info && 866 (*a)->fts_level == FTS_ROOTLEVEL && !f_listdir) { 867 if (a_info == FTS_D) 868 return (1); 869 if (b_info == FTS_D) 870 return (-1); 871 } 872 return (sortfcn(*a, *b)); 873}
|