1 /* $NetBSD: print.c,v 1.52 2013/05/02 22:43:55 zafer Exp $ */ 2 3 /* 4 * Copyright (c) 1989, 1993, 1994 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 #include <sys/cdefs.h> 36 #ifndef lint 37 #if 0 38 static char sccsid[] = "@(#)print.c 8.5 (Berkeley) 7/28/94"; 39 #else 40 __RCSID("$NetBSD: print.c,v 1.52 2013/05/02 22:43:55 zafer Exp $"); 41 #endif 42 #endif /* not lint */ 43 44 #include <sys/param.h> 45 #include <sys/stat.h> 46 47 #include <err.h> 48 #include <errno.h> 49 #include <fts.h> 50 #include <grp.h> 51 #include <pwd.h> 52 #include <stdio.h> 53 #include <stdlib.h> 54 #include <string.h> 55 #include <time.h> 56 #include <tzfile.h> 57 #include <unistd.h> 58 #include <util.h> 59 60 #include "ls.h" 61 #include "extern.h" 62 63 extern int termwidth; 64 65 static int printaname(FTSENT *, int, int); 66 static void printlink(FTSENT *); 67 static void printtime(time_t); 68 static void printtotal(DISPLAY *dp); 69 static int printtype(u_int); 70 71 static time_t now; 72 73 #define IS_NOPRINT(p) ((p)->fts_number == NO_PRINT) 74 75 void 76 printscol(DISPLAY *dp) 77 { 78 FTSENT *p; 79 80 for (p = dp->list; p; p = p->fts_link) { 81 if (IS_NOPRINT(p)) 82 continue; 83 (void)printaname(p, dp->s_inode, dp->s_block); 84 (void)putchar('\n'); 85 } 86 } 87 88 void 89 printlong(DISPLAY *dp) 90 { 91 struct stat *sp; 92 FTSENT *p; 93 NAMES *np; 94 char buf[20], szbuf[5]; 95 96 now = time(NULL); 97 98 printtotal(dp); /* "total: %u\n" */ 99 100 for (p = dp->list; p; p = p->fts_link) { 101 if (IS_NOPRINT(p)) 102 continue; 103 sp = p->fts_statp; 104 if (f_inode) 105 (void)printf("%*lu ", dp->s_inode, 106 (unsigned long)sp->st_ino); 107 if (f_size) { 108 if (f_humanize) { 109 if ((humanize_number(szbuf, sizeof(szbuf), 110 sp->st_blocks * S_BLKSIZE, 111 "", HN_AUTOSCALE, 112 (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1) 113 err(1, "humanize_number"); 114 (void)printf("%*s ", dp->s_block, szbuf); 115 } else { 116 (void)printf(f_commas ? "%'*llu " : "%*llu ", 117 dp->s_block, 118 (unsigned long long)howmany(sp->st_blocks, 119 blocksize)); 120 } 121 } 122 (void)strmode(sp->st_mode, buf); 123 np = p->fts_pointer; 124 (void)printf("%s %*lu ", buf, dp->s_nlink, 125 (unsigned long)sp->st_nlink); 126 if (!f_grouponly) 127 (void)printf("%-*s ", dp->s_user, np->user); 128 (void)printf("%-*s ", dp->s_group, np->group); 129 if (f_flags) 130 (void)printf("%-*s ", dp->s_flags, np->flags); 131 if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode)) 132 (void)printf("%*lld, %*lld ", 133 dp->s_major, (long long)major(sp->st_rdev), 134 dp->s_minor, (long long)minor(sp->st_rdev)); 135 else 136 if (f_humanize) { 137 if ((humanize_number(szbuf, sizeof(szbuf), 138 sp->st_size, "", HN_AUTOSCALE, 139 (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1) 140 err(1, "humanize_number"); 141 (void)printf("%*s ", dp->s_size, szbuf); 142 } else { 143 (void)printf(f_commas ? "%'*llu " : "%*llu ", 144 dp->s_size, (unsigned long long) 145 sp->st_size); 146 } 147 if (f_accesstime) 148 printtime(sp->st_atime); 149 else if (f_statustime) 150 printtime(sp->st_ctime); 151 else 152 printtime(sp->st_mtime); 153 if (f_octal || f_octal_escape) 154 (void)safe_print(p->fts_name); 155 else if (f_nonprint) 156 (void)printescaped(p->fts_name); 157 else 158 (void)printf("%s", p->fts_name); 159 160 if (f_type || (f_typedir && S_ISDIR(sp->st_mode))) 161 (void)printtype(sp->st_mode); 162 if (S_ISLNK(sp->st_mode)) 163 printlink(p); 164 (void)putchar('\n'); 165 } 166 } 167 168 void 169 printcol(DISPLAY *dp) 170 { 171 static FTSENT **array; 172 static int lastentries = -1; 173 FTSENT *p; 174 int base, chcnt, col, colwidth, num; 175 int numcols, numrows, row; 176 177 colwidth = dp->maxlen; 178 if (f_inode) 179 colwidth += dp->s_inode + 1; 180 if (f_size) { 181 if (f_humanize) 182 colwidth += dp->s_size + 1; 183 else 184 colwidth += dp->s_block + 1; 185 } 186 if (f_type || f_typedir) 187 colwidth += 1; 188 189 colwidth += 1; 190 191 if (termwidth < 2 * colwidth) { 192 printscol(dp); 193 return; 194 } 195 196 /* 197 * Have to do random access in the linked list -- build a table 198 * of pointers. 199 */ 200 if (dp->entries > lastentries) { 201 FTSENT **newarray; 202 203 newarray = realloc(array, dp->entries * sizeof(FTSENT *)); 204 if (newarray == NULL) { 205 warn(NULL); 206 printscol(dp); 207 return; 208 } 209 lastentries = dp->entries; 210 array = newarray; 211 } 212 for (p = dp->list, num = 0; p; p = p->fts_link) 213 if (p->fts_number != NO_PRINT) 214 array[num++] = p; 215 216 numcols = termwidth / colwidth; 217 colwidth = termwidth / numcols; /* spread out if possible */ 218 numrows = num / numcols; 219 if (num % numcols) 220 ++numrows; 221 222 printtotal(dp); /* "total: %u\n" */ 223 224 for (row = 0; row < numrows; ++row) { 225 for (base = row, chcnt = col = 0; col < numcols; ++col) { 226 chcnt = printaname(array[base], dp->s_inode, 227 f_humanize ? dp->s_size : dp->s_block); 228 if ((base += numrows) >= num) 229 break; 230 while (chcnt++ < colwidth) 231 (void)putchar(' '); 232 } 233 (void)putchar('\n'); 234 } 235 } 236 237 void 238 printacol(DISPLAY *dp) 239 { 240 FTSENT *p; 241 int chcnt, col, colwidth; 242 int numcols; 243 244 colwidth = dp->maxlen; 245 if (f_inode) 246 colwidth += dp->s_inode + 1; 247 if (f_size) { 248 if (f_humanize) 249 colwidth += dp->s_size + 1; 250 else 251 colwidth += dp->s_block + 1; 252 } 253 if (f_type || f_typedir) 254 colwidth += 1; 255 256 colwidth += 1; 257 258 if (termwidth < 2 * colwidth) { 259 printscol(dp); 260 return; 261 } 262 263 numcols = termwidth / colwidth; 264 colwidth = termwidth / numcols; /* spread out if possible */ 265 266 printtotal(dp); /* "total: %u\n" */ 267 268 chcnt = col = 0; 269 for (p = dp->list; p; p = p->fts_link) { 270 if (IS_NOPRINT(p)) 271 continue; 272 if (col >= numcols) { 273 chcnt = col = 0; 274 (void)putchar('\n'); 275 } 276 chcnt = printaname(p, dp->s_inode, 277 f_humanize ? dp->s_size : dp->s_block); 278 while (chcnt++ < colwidth) 279 (void)putchar(' '); 280 col++; 281 } 282 (void)putchar('\n'); 283 } 284 285 void 286 printstream(DISPLAY *dp) 287 { 288 FTSENT *p; 289 int col; 290 int extwidth; 291 292 extwidth = 0; 293 if (f_inode) 294 extwidth += dp->s_inode + 1; 295 if (f_size) { 296 if (f_humanize) 297 extwidth += dp->s_size + 1; 298 else 299 extwidth += dp->s_block + 1; 300 } 301 if (f_type) 302 extwidth += 1; 303 304 for (col = 0, p = dp->list; p != NULL; p = p->fts_link) { 305 if (IS_NOPRINT(p)) 306 continue; 307 if (col > 0) { 308 (void)putchar(','), col++; 309 if (col + 1 + extwidth + (int)p->fts_namelen >= termwidth) 310 (void)putchar('\n'), col = 0; 311 else 312 (void)putchar(' '), col++; 313 } 314 col += printaname(p, dp->s_inode, 315 f_humanize ? dp->s_size : dp->s_block); 316 } 317 (void)putchar('\n'); 318 } 319 320 /* 321 * print [inode] [size] name 322 * return # of characters printed, no trailing characters. 323 */ 324 static int 325 printaname(FTSENT *p, int inodefield, int sizefield) 326 { 327 struct stat *sp; 328 int chcnt; 329 char szbuf[5]; 330 331 sp = p->fts_statp; 332 chcnt = 0; 333 if (f_inode) 334 chcnt += printf("%*lu ", inodefield, (unsigned long)sp->st_ino); 335 if (f_size) { 336 if (f_humanize) { 337 if ((humanize_number(szbuf, sizeof(szbuf), sp->st_size, 338 "", HN_AUTOSCALE, 339 (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1) 340 err(1, "humanize_number"); 341 chcnt += printf("%*s ", sizefield, szbuf); 342 } else { 343 chcnt += printf(f_commas ? "%'*llu " : "%*llu ", 344 sizefield, (unsigned long long) 345 howmany(sp->st_blocks, blocksize)); 346 } 347 } 348 if (f_octal || f_octal_escape) 349 chcnt += safe_print(p->fts_name); 350 else if (f_nonprint) 351 chcnt += printescaped(p->fts_name); 352 else 353 chcnt += printf("%s", p->fts_name); 354 if (f_type || (f_typedir && S_ISDIR(sp->st_mode))) 355 chcnt += printtype(sp->st_mode); 356 return (chcnt); 357 } 358 359 static void 360 printtime(time_t ftime) 361 { 362 int i; 363 const char *longstring; 364 365 if ((longstring = ctime(&ftime)) == NULL) { 366 /* 012345678901234567890123 */ 367 longstring = "????????????????????????"; 368 } 369 for (i = 4; i < 11; ++i) 370 (void)putchar(longstring[i]); 371 372 #define SIXMONTHS ((DAYSPERNYEAR / 2) * SECSPERDAY) 373 if (f_sectime) 374 for (i = 11; i < 24; i++) 375 (void)putchar(longstring[i]); 376 else if (ftime + SIXMONTHS > now && ftime - SIXMONTHS < now) 377 for (i = 11; i < 16; ++i) 378 (void)putchar(longstring[i]); 379 else { 380 (void)putchar(' '); 381 for (i = 20; i < 24; ++i) 382 (void)putchar(longstring[i]); 383 } 384 (void)putchar(' '); 385 } 386 387 /* 388 * Display total used disk space in the form "total: %u\n". 389 * Note: POSIX (IEEE Std 1003.1-2001) says this should be always in 512 blocks, 390 * but we humanise it with -h, or separate it with commas with -M, and use 1024 391 * with -k. 392 */ 393 static void 394 printtotal(DISPLAY *dp) 395 { 396 char szbuf[5]; 397 398 if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size)) { 399 if (f_humanize) { 400 if ((humanize_number(szbuf, sizeof(szbuf), (int64_t)dp->stotal, 401 "", HN_AUTOSCALE, 402 (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1) 403 err(1, "humanize_number"); 404 (void)printf("total %s\n", szbuf); 405 } else { 406 (void)printf(f_commas ? "total %'llu\n" : 407 "total %llu\n", (unsigned long long) 408 howmany(dp->btotal, blocksize)); 409 } 410 } 411 } 412 413 static int 414 printtype(u_int mode) 415 { 416 switch (mode & S_IFMT) { 417 case S_IFDIR: 418 (void)putchar('/'); 419 return (1); 420 case S_IFIFO: 421 (void)putchar('|'); 422 return (1); 423 case S_IFLNK: 424 (void)putchar('@'); 425 return (1); 426 case S_IFSOCK: 427 (void)putchar('='); 428 return (1); 429 case S_IFWHT: 430 (void)putchar('%'); 431 return (1); 432 } 433 if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) { 434 (void)putchar('*'); 435 return (1); 436 } 437 return (0); 438 } 439 440 static void 441 printlink(FTSENT *p) 442 { 443 int lnklen; 444 char name[MAXPATHLEN + 1], path[MAXPATHLEN + 1]; 445 446 if (p->fts_level == FTS_ROOTLEVEL) 447 (void)snprintf(name, sizeof(name), "%s", p->fts_name); 448 else 449 (void)snprintf(name, sizeof(name), 450 "%s/%s", p->fts_parent->fts_accpath, p->fts_name); 451 if ((lnklen = readlink(name, path, sizeof(path) - 1)) == -1) { 452 (void)fprintf(stderr, "\nls: %s: %s\n", name, strerror(errno)); 453 return; 454 } 455 path[lnklen] = '\0'; 456 (void)printf(" -> "); 457 if (f_octal || f_octal_escape) 458 (void)safe_print(path); 459 else if (f_nonprint) 460 (void)printescaped(path); 461 else 462 (void)printf("%s", path); 463 } 464