1 /* $OpenBSD: print.c,v 1.28 2012/07/12 09:41:09 guenther 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 #include <sys/param.h> 37 #include <sys/stat.h> 38 39 #include <err.h> 40 #include <errno.h> 41 #include <fts.h> 42 #include <grp.h> 43 #include <pwd.h> 44 #include <stdio.h> 45 #include <stdlib.h> 46 #include <string.h> 47 #include <time.h> 48 #include <tzfile.h> 49 #include <unistd.h> 50 #include <util.h> 51 52 #include "ls.h" 53 #include "extern.h" 54 55 static int printaname(FTSENT *, u_long, u_long); 56 static void printlink(FTSENT *); 57 static void printsize(size_t, off_t); 58 static void printtime(time_t); 59 static int printtype(u_int); 60 static int compute_columns(DISPLAY *, int *); 61 62 #define IS_NOPRINT(p) ((p)->fts_number == NO_PRINT) 63 64 void 65 printscol(DISPLAY *dp) 66 { 67 FTSENT *p; 68 69 for (p = dp->list; p; p = p->fts_link) { 70 if (IS_NOPRINT(p)) 71 continue; 72 (void)printaname(p, dp->s_inode, dp->s_block); 73 (void)putchar('\n'); 74 } 75 } 76 77 void 78 printlong(DISPLAY *dp) 79 { 80 struct stat *sp; 81 FTSENT *p; 82 NAMES *np; 83 char buf[20]; 84 85 if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size)) 86 (void)printf("total %lu\n", howmany(dp->btotal, blocksize)); 87 88 for (p = dp->list; p; p = p->fts_link) { 89 if (IS_NOPRINT(p)) 90 continue; 91 sp = p->fts_statp; 92 if (f_inode) 93 (void)printf("%*u ", dp->s_inode, sp->st_ino); 94 if (f_size) 95 (void)printf("%*qd ", 96 dp->s_block, howmany(sp->st_blocks, blocksize)); 97 (void)strmode(sp->st_mode, buf); 98 np = p->fts_pointer; 99 (void)printf("%s %*u ", buf, dp->s_nlink, sp->st_nlink); 100 if (!f_grouponly) 101 (void)printf("%-*s ", dp->s_user, np->user); 102 (void)printf("%-*s ", dp->s_group, np->group); 103 if (f_flags) 104 (void)printf("%-*s ", dp->s_flags, np->flags); 105 if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode)) 106 (void)printf("%3d, %3d ", 107 major(sp->st_rdev), minor(sp->st_rdev)); 108 else if (dp->bcfile) 109 (void)printf("%*s%*qd ", 110 8 - dp->s_size, "", dp->s_size, sp->st_size); 111 else 112 printsize(dp->s_size, sp->st_size); 113 if (f_accesstime) 114 printtime(sp->st_atime); 115 else if (f_statustime) 116 printtime(sp->st_ctime); 117 else 118 printtime(sp->st_mtime); 119 (void)putname(p->fts_name); 120 if (f_type || (f_typedir && S_ISDIR(sp->st_mode))) 121 (void)printtype(sp->st_mode); 122 if (S_ISLNK(sp->st_mode)) 123 printlink(p); 124 (void)putchar('\n'); 125 } 126 } 127 128 static int 129 compute_columns(DISPLAY *dp, int *pnum) 130 { 131 int colwidth; 132 extern int termwidth; 133 int mywidth; 134 135 colwidth = dp->maxlen; 136 if (f_inode) 137 colwidth += dp->s_inode + 1; 138 if (f_size) 139 colwidth += dp->s_block + 1; 140 if (f_type || f_typedir) 141 colwidth += 1; 142 143 colwidth += 1; 144 mywidth = termwidth + 1; /* no extra space for last column */ 145 146 if (mywidth < 2 * colwidth) { 147 printscol(dp); 148 return (0); 149 } 150 151 *pnum = mywidth / colwidth; 152 return (mywidth / *pnum); /* spread out if possible */ 153 } 154 155 void 156 printcol(DISPLAY *dp) 157 { 158 static FTSENT **array; 159 static int lastentries = -1; 160 FTSENT *p; 161 int base, chcnt, col, colwidth, num; 162 int numcols, numrows, row; 163 164 if ((colwidth = compute_columns(dp, &numcols)) == 0) 165 return; 166 /* 167 * Have to do random access in the linked list -- build a table 168 * of pointers. 169 */ 170 if (dp->entries > lastentries) { 171 FTSENT **a; 172 173 if ((a = realloc(array, dp->entries * sizeof(FTSENT *))) == 174 NULL) { 175 free(array); 176 array = NULL; 177 dp->entries = 0; 178 lastentries = -1; 179 warn(NULL); 180 printscol(dp); 181 return; 182 } 183 lastentries = dp->entries; 184 array = a; 185 } 186 for (p = dp->list, num = 0; p; p = p->fts_link) 187 if (p->fts_number != NO_PRINT) 188 array[num++] = p; 189 190 numrows = num / numcols; 191 if (num % numcols) 192 ++numrows; 193 194 if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size)) 195 (void)printf("total %lu\n", howmany(dp->btotal, blocksize)); 196 for (row = 0; row < numrows; ++row) { 197 for (base = row, col = 0;;) { 198 chcnt = printaname(array[base], dp->s_inode, dp->s_block); 199 if ((base += numrows) >= num) 200 break; 201 if (++col == numcols) 202 break; 203 while (chcnt++ < colwidth) 204 putchar(' '); 205 } 206 (void)putchar('\n'); 207 } 208 } 209 210 /* 211 * print [inode] [size] name 212 * return # of characters printed, no trailing characters. 213 */ 214 static int 215 printaname(FTSENT *p, u_long inodefield, u_long sizefield) 216 { 217 struct stat *sp; 218 int chcnt; 219 220 sp = p->fts_statp; 221 chcnt = 0; 222 if (f_inode) 223 chcnt += printf("%*u ", (int)inodefield, sp->st_ino); 224 if (f_size) 225 chcnt += printf("%*qd ", 226 (int)sizefield, howmany(sp->st_blocks, blocksize)); 227 chcnt += putname(p->fts_name); 228 if (f_type || (f_typedir && S_ISDIR(sp->st_mode))) 229 chcnt += printtype(sp->st_mode); 230 return (chcnt); 231 } 232 233 static void 234 printtime(time_t ftime) 235 { 236 int i; 237 char *longstring; 238 static time_t six_months_ago; 239 static int sma_set = 0; 240 241 #define SIXMONTHS ((DAYSPERNYEAR / 2) * SECSPERDAY) 242 if (! sma_set) { 243 six_months_ago = time(NULL) - SIXMONTHS; 244 sma_set = 1; 245 } 246 longstring = ctime(&ftime); 247 for (i = 4; i < 11; ++i) 248 (void)putchar(longstring[i]); 249 250 if (f_sectime) 251 for (i = 11; i < 24; i++) 252 (void)putchar(longstring[i]); 253 else if (ftime > six_months_ago) 254 for (i = 11; i < 16; ++i) 255 (void)putchar(longstring[i]); 256 else { 257 (void)putchar(' '); 258 for (i = 20; i < 24; ++i) 259 (void)putchar(longstring[i]); 260 } 261 (void)putchar(' '); 262 } 263 264 void 265 printacol(DISPLAY *dp) 266 { 267 FTSENT *p; 268 int chcnt, col, colwidth; 269 int numcols; 270 271 if ( (colwidth = compute_columns(dp, &numcols)) == 0) 272 return; 273 274 if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size)) 275 (void)printf("total %lu\n", howmany(dp->btotal, blocksize)); 276 col = 0; 277 for (p = dp->list; p; p = p->fts_link) { 278 if (IS_NOPRINT(p)) 279 continue; 280 if (col >= numcols) { 281 col = 0; 282 (void)putchar('\n'); 283 } 284 chcnt = printaname(p, dp->s_inode, dp->s_block); 285 col++; 286 if (col < numcols) 287 while (chcnt++ < colwidth) 288 (void)putchar(' '); 289 } 290 (void)putchar('\n'); 291 } 292 293 void 294 printstream(DISPLAY *dp) 295 { 296 extern int termwidth; 297 FTSENT *p; 298 int col; 299 int extwidth; 300 301 extwidth = 0; 302 if (f_inode) 303 extwidth += dp->s_inode + 1; 304 if (f_size) 305 extwidth += dp->s_block + 1; 306 if (f_type) 307 extwidth += 1; 308 309 for (col = 0, p = dp->list; p != NULL; p = p->fts_link) { 310 if (IS_NOPRINT(p)) 311 continue; 312 if (col > 0) { 313 (void)putchar(','), col++; 314 if (col + 1 + extwidth + p->fts_namelen >= termwidth) 315 (void)putchar('\n'), col = 0; 316 else 317 (void)putchar(' '), col++; 318 } 319 col += printaname(p, dp->s_inode, dp->s_block); 320 } 321 (void)putchar('\n'); 322 } 323 324 static int 325 printtype(u_int mode) 326 { 327 switch (mode & S_IFMT) { 328 case S_IFDIR: 329 (void)putchar('/'); 330 return (1); 331 case S_IFIFO: 332 (void)putchar('|'); 333 return (1); 334 case S_IFLNK: 335 (void)putchar('@'); 336 return (1); 337 case S_IFSOCK: 338 (void)putchar('='); 339 return (1); 340 } 341 if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) { 342 (void)putchar('*'); 343 return (1); 344 } 345 return (0); 346 } 347 348 static void 349 printlink(FTSENT *p) 350 { 351 int lnklen; 352 char name[MAXPATHLEN], path[MAXPATHLEN]; 353 354 if (p->fts_level == FTS_ROOTLEVEL) 355 (void)snprintf(name, sizeof(name), "%s", p->fts_name); 356 else 357 (void)snprintf(name, sizeof(name), 358 "%s/%s", p->fts_parent->fts_accpath, p->fts_name); 359 if ((lnklen = readlink(name, path, sizeof(path) - 1)) == -1) { 360 (void)fprintf(stderr, "\nls: %s: %s\n", name, strerror(errno)); 361 return; 362 } 363 path[lnklen] = '\0'; 364 (void)printf(" -> "); 365 (void)putname(path); 366 } 367 368 static void 369 printsize(size_t width, off_t bytes) 370 { 371 char ret[FMT_SCALED_STRSIZE]; 372 373 if ((f_humanval) && (fmt_scaled(bytes, ret) != -1)) { 374 (void)printf("%*s ", (u_int)width, ret); 375 return; 376 } 377 (void)printf("%*qd ", (u_int)width, bytes); 378 } 379