1 /* $OpenBSD: print.c,v 1.25 2007/05/07 18:39:28 millert Exp $ */ 2 /* $NetBSD: print.c,v 1.15 1996/12/11 03:25:39 thorpej Exp $ */ 3 4 /* 5 * Copyright (c) 1989, 1993, 1994 6 * The Regents of the University of California. All rights reserved. 7 * 8 * This code is derived from software contributed to Berkeley by 9 * Michael Fischbein. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #ifndef lint 37 #if 0 38 static char sccsid[] = "@(#)print.c 8.5 (Berkeley) 7/28/94"; 39 #else 40 static char rcsid[] = "$OpenBSD: print.c,v 1.25 2007/05/07 18:39:28 millert 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 static int printaname(FTSENT *, u_long, u_long); 64 static void printlink(FTSENT *); 65 static void printsize(size_t, off_t); 66 static void printtime(time_t); 67 static int printtype(u_int); 68 static int compute_columns(DISPLAY *, int *); 69 70 #define IS_NOPRINT(p) ((p)->fts_number == NO_PRINT) 71 72 void 73 printscol(DISPLAY *dp) 74 { 75 FTSENT *p; 76 77 for (p = dp->list; p; p = p->fts_link) { 78 if (IS_NOPRINT(p)) 79 continue; 80 (void)printaname(p, dp->s_inode, dp->s_block); 81 (void)putchar('\n'); 82 } 83 } 84 85 void 86 printlong(DISPLAY *dp) 87 { 88 struct stat *sp; 89 FTSENT *p; 90 NAMES *np; 91 char buf[20]; 92 93 if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size)) 94 (void)printf("total %lu\n", howmany(dp->btotal, blocksize)); 95 96 for (p = dp->list; p; p = p->fts_link) { 97 if (IS_NOPRINT(p)) 98 continue; 99 sp = p->fts_statp; 100 if (f_inode) 101 (void)printf("%*u ", dp->s_inode, sp->st_ino); 102 if (f_size) 103 (void)printf("%*qd ", 104 dp->s_block, howmany(sp->st_blocks, blocksize)); 105 (void)strmode(sp->st_mode, buf); 106 np = p->fts_pointer; 107 (void)printf("%s %*u ", buf, dp->s_nlink, sp->st_nlink); 108 if (!f_grouponly) 109 (void)printf("%-*s ", dp->s_user, np->user); 110 (void)printf("%-*s ", dp->s_group, np->group); 111 if (f_flags) 112 (void)printf("%-*s ", dp->s_flags, np->flags); 113 if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode)) 114 (void)printf("%3d, %3d ", 115 major(sp->st_rdev), minor(sp->st_rdev)); 116 else if (dp->bcfile) 117 (void)printf("%*s%*qd ", 118 8 - dp->s_size, "", dp->s_size, sp->st_size); 119 else 120 printsize(dp->s_size, sp->st_size); 121 if (f_accesstime) 122 printtime(sp->st_atime); 123 else if (f_statustime) 124 printtime(sp->st_ctime); 125 else 126 printtime(sp->st_mtime); 127 (void)putname(p->fts_name); 128 if (f_type || (f_typedir && S_ISDIR(sp->st_mode))) 129 (void)printtype(sp->st_mode); 130 if (S_ISLNK(sp->st_mode)) 131 printlink(p); 132 (void)putchar('\n'); 133 } 134 } 135 136 static int 137 compute_columns(DISPLAY *dp, int *pnum) 138 { 139 int colwidth; 140 extern int termwidth; 141 int mywidth; 142 143 colwidth = dp->maxlen; 144 if (f_inode) 145 colwidth += dp->s_inode + 1; 146 if (f_size) 147 colwidth += dp->s_block + 1; 148 if (f_type || f_typedir) 149 colwidth += 1; 150 151 colwidth += 1; 152 mywidth = termwidth + 1; /* no extra space for last column */ 153 154 if (mywidth < 2 * colwidth) { 155 printscol(dp); 156 return (0); 157 } 158 159 *pnum = mywidth / colwidth; 160 return (mywidth / *pnum); /* spread out if possible */ 161 } 162 163 void 164 printcol(DISPLAY *dp) 165 { 166 static FTSENT **array; 167 static int lastentries = -1; 168 FTSENT *p; 169 int base, chcnt, col, colwidth, num; 170 int numcols, numrows, row; 171 172 if ((colwidth = compute_columns(dp, &numcols)) == 0) 173 return; 174 /* 175 * Have to do random access in the linked list -- build a table 176 * of pointers. 177 */ 178 if (dp->entries > lastentries) { 179 FTSENT **a; 180 181 if ((a = realloc(array, dp->entries * sizeof(FTSENT *))) == 182 NULL) { 183 free(array); 184 array = NULL; 185 dp->entries = 0; 186 lastentries = -1; 187 warn(NULL); 188 printscol(dp); 189 return; 190 } 191 lastentries = dp->entries; 192 array = a; 193 } 194 for (p = dp->list, num = 0; p; p = p->fts_link) 195 if (p->fts_number != NO_PRINT) 196 array[num++] = p; 197 198 numrows = num / numcols; 199 if (num % numcols) 200 ++numrows; 201 202 if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size)) 203 (void)printf("total %lu\n", howmany(dp->btotal, blocksize)); 204 for (row = 0; row < numrows; ++row) { 205 for (base = row, col = 0;;) { 206 chcnt = printaname(array[base], dp->s_inode, dp->s_block); 207 if ((base += numrows) >= num) 208 break; 209 if (++col == numcols) 210 break; 211 while (chcnt++ < colwidth) 212 putchar(' '); 213 } 214 (void)putchar('\n'); 215 } 216 } 217 218 /* 219 * print [inode] [size] name 220 * return # of characters printed, no trailing characters. 221 */ 222 static int 223 printaname(FTSENT *p, u_long inodefield, u_long sizefield) 224 { 225 struct stat *sp; 226 int chcnt; 227 228 sp = p->fts_statp; 229 chcnt = 0; 230 if (f_inode) 231 chcnt += printf("%*u ", (int)inodefield, sp->st_ino); 232 if (f_size) 233 chcnt += printf("%*qd ", 234 (int)sizefield, howmany(sp->st_blocks, blocksize)); 235 chcnt += putname(p->fts_name); 236 if (f_type || (f_typedir && S_ISDIR(sp->st_mode))) 237 chcnt += printtype(sp->st_mode); 238 return (chcnt); 239 } 240 241 static void 242 printtime(time_t ftime) 243 { 244 int i; 245 char *longstring; 246 247 longstring = ctime(&ftime); 248 for (i = 4; i < 11; ++i) 249 (void)putchar(longstring[i]); 250 251 #define SIXMONTHS ((DAYSPERNYEAR / 2) * SECSPERDAY) 252 if (f_sectime) 253 for (i = 11; i < 24; i++) 254 (void)putchar(longstring[i]); 255 else if (ftime + SIXMONTHS > time(NULL)) 256 for (i = 11; i < 16; ++i) 257 (void)putchar(longstring[i]); 258 else { 259 (void)putchar(' '); 260 for (i = 20; i < 24; ++i) 261 (void)putchar(longstring[i]); 262 } 263 (void)putchar(' '); 264 } 265 266 void 267 printacol(DISPLAY *dp) 268 { 269 FTSENT *p; 270 int chcnt, col, colwidth; 271 int numcols; 272 273 if ( (colwidth = compute_columns(dp, &numcols)) == 0) 274 return; 275 276 if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size)) 277 (void)printf("total %lu\n", howmany(dp->btotal, blocksize)); 278 col = 0; 279 for (p = dp->list; p; p = p->fts_link) { 280 if (IS_NOPRINT(p)) 281 continue; 282 if (col >= numcols) { 283 col = 0; 284 (void)putchar('\n'); 285 } 286 chcnt = printaname(p, dp->s_inode, dp->s_block); 287 col++; 288 if (col < numcols) 289 while (chcnt++ < colwidth) 290 (void)putchar(' '); 291 } 292 (void)putchar('\n'); 293 } 294 295 void 296 printstream(DISPLAY *dp) 297 { 298 extern int termwidth; 299 FTSENT *p; 300 int col; 301 int extwidth; 302 303 extwidth = 0; 304 if (f_inode) 305 extwidth += dp->s_inode + 1; 306 if (f_size) 307 extwidth += dp->s_block + 1; 308 if (f_type) 309 extwidth += 1; 310 311 for (col = 0, p = dp->list; p != NULL; p = p->fts_link) { 312 if (IS_NOPRINT(p)) 313 continue; 314 if (col > 0) { 315 (void)putchar(','), col++; 316 if (col + 1 + extwidth + p->fts_namelen >= termwidth) 317 (void)putchar('\n'), col = 0; 318 else 319 (void)putchar(' '), col++; 320 } 321 col += printaname(p, dp->s_inode, dp->s_block); 322 } 323 (void)putchar('\n'); 324 } 325 326 static int 327 printtype(u_int mode) 328 { 329 switch (mode & S_IFMT) { 330 case S_IFDIR: 331 (void)putchar('/'); 332 return (1); 333 case S_IFIFO: 334 (void)putchar('|'); 335 return (1); 336 case S_IFLNK: 337 (void)putchar('@'); 338 return (1); 339 case S_IFSOCK: 340 (void)putchar('='); 341 return (1); 342 } 343 if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) { 344 (void)putchar('*'); 345 return (1); 346 } 347 return (0); 348 } 349 350 static void 351 printlink(FTSENT *p) 352 { 353 int lnklen; 354 char name[MAXPATHLEN], path[MAXPATHLEN]; 355 356 if (p->fts_level == FTS_ROOTLEVEL) 357 (void)snprintf(name, sizeof(name), "%s", p->fts_name); 358 else 359 (void)snprintf(name, sizeof(name), 360 "%s/%s", p->fts_parent->fts_accpath, p->fts_name); 361 if ((lnklen = readlink(name, path, sizeof(path) - 1)) == -1) { 362 (void)fprintf(stderr, "\nls: %s: %s\n", name, strerror(errno)); 363 return; 364 } 365 path[lnklen] = '\0'; 366 (void)printf(" -> "); 367 (void)putname(path); 368 } 369 370 static void 371 printsize(size_t width, off_t bytes) 372 { 373 char ret[FMT_SCALED_STRSIZE]; 374 375 if ((f_humanval) && (fmt_scaled(bytes, ret) != -1)) { 376 (void)printf("%*s ", (u_int)width, ret); 377 return; 378 } 379 (void)printf("%*qd ", (u_int)width, bytes); 380 } 381